package jnpf.cultivate.controller.web.question;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jnpf.base.ActionResult;
import jnpf.cultivate.service.FtbCultivateAssessmentPointsService;
import jnpf.model.cultivate.po.FtbCultivateAssessmentPoints;
import jnpf.model.cultivate.req.exam.AssessmentPointsReq;
import jnpf.model.cultivate.req.exam.QueryAssessmentPointsReq;
import jnpf.model.cultivate.resp.AssessmentPointsVo;
import jnpf.model.enums.CourseEnums;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * web题目考核点模块
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/assessment/points")
public class FtbCultivateAssessmentPointsController {
    /**
     * 服务对象
     */
    @Resource
    private FtbCultivateAssessmentPointsService assessmentPointsService;


    /**
     * 查询考点列表
     *
     * @return
     */
    @PostMapping("/lists")
    public ActionResult<List<AssessmentPointsVo>> lists(@RequestBody QueryAssessmentPointsReq req) {
        QueryWrapper<FtbCultivateAssessmentPoints> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(StringUtils.isNotEmpty(req.getKeyword()), FtbCultivateAssessmentPoints::getName, req.getKeyword())
                .eq(FtbCultivateAssessmentPoints::getEnabledMark, CourseEnums.EnabledMarkType.VALID.getCode());
        List<FtbCultivateAssessmentPoints> assessmentPointsList = assessmentPointsService.list(wrapper);
        if (CollectionUtil.isEmpty(assessmentPointsList)) {
            return ActionResult.success(CollectionUtil.newArrayList());
        }
        return ActionResult.success(BeanUtil.copyToList(assessmentPointsList, AssessmentPointsVo.class));
    }


    /**
     * 添加考点
     *
     * @param req
     * @return
     */
    @PostMapping("/add")
    public ActionResult<Boolean> add(@RequestBody @Valid AssessmentPointsReq req) {
        //1、检测名称是否重复
        QueryWrapper<FtbCultivateAssessmentPoints> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(FtbCultivateAssessmentPoints::getName, req.getName())
                .eq(FtbCultivateAssessmentPoints::getEnabledMark, CourseEnums.EnabledMarkType.VALID.getCode());
        Long count = assessmentPointsService.count(wrapper);
        if (null != count && count > 0) {
            throw new RuntimeException("考点名称重复");
        }

        //2、保存数据
        FtbCultivateAssessmentPoints assessmentPoints = new FtbCultivateAssessmentPoints();
        assessmentPoints.setName(req.getName());
        assessmentPoints.setEnabledMark(CourseEnums.EnabledMarkType.VALID.getCode());
        assessmentPoints.setCreatorTime(new Date());
        assessmentPointsService.save(assessmentPoints);
        return ActionResult.success(true);
    }

    /**
     * 编辑考点
     *
     * @param req
     * @return
     */
    @PostMapping("/edit")
    public ActionResult<Boolean> edit(@RequestBody @Valid AssessmentPointsReq req) {

        //1、检测名称是否重复
        QueryWrapper<FtbCultivateAssessmentPoints> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(FtbCultivateAssessmentPoints::getName, req.getName())
                .ne(FtbCultivateAssessmentPoints::getId, req.getId())
                .eq(FtbCultivateAssessmentPoints::getEnabledMark, CourseEnums.EnabledMarkType.VALID.getCode());
        Long count = assessmentPointsService.count(wrapper);
        if (null != count && count > 0) {
            throw new RuntimeException("考点名称重复");
        }
        //2、写入数据
        FtbCultivateAssessmentPoints assessmentPoints = new FtbCultivateAssessmentPoints();
        assessmentPoints.setName(req.getName());
        assessmentPoints.setId(req.getId());
        assessmentPointsService.updateById(assessmentPoints);
        return ActionResult.success(true);
    }

    /**
     * 删除考点
     *
     * @param id 考点id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public ActionResult<Boolean> del(@PathVariable("id") String id) {

        //1、检测考点
        QueryWrapper<FtbCultivateAssessmentPoints> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(FtbCultivateAssessmentPoints::getId, id)
                .eq(FtbCultivateAssessmentPoints::getEnabledMark, CourseEnums.EnabledMarkType.VALID.getCode());
        FtbCultivateAssessmentPoints points = assessmentPointsService.getOne(wrapper);
        if (null == points) {
            throw new RuntimeException("考点不存在");
        }
        if (points.getEnabledMark().equals(CourseEnums.EnabledMarkType.INVALID.getCode())) {
            throw new RuntimeException("考点已经被删除");
        }
        //2、删除
        FtbCultivateAssessmentPoints assessmentPoints = new FtbCultivateAssessmentPoints();
        assessmentPoints.setId(id);
        assessmentPoints.setEnabledMark(CourseEnums.EnabledMarkType.INVALID.getCode());
        assessmentPointsService.updateById(assessmentPoints);
        return ActionResult.success(true);
    }


}
