package com.changhu.module.assessmentCriteria.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.changhu.common.annotation.CheckUserType;
import com.changhu.common.annotation.JsonBody;
import com.changhu.common.db.enums.EnterprisesUnitType;
import com.changhu.common.db.enums.UserType;
import com.changhu.common.exception.MessageException;
import com.changhu.common.pojo.vo.SelectNodeVo;
import com.changhu.module.assessmentCriteria.pojo.entity.CkGroup;
import com.changhu.module.assessmentCriteria.pojo.entity.CkItem;
import com.changhu.module.assessmentCriteria.pojo.entity.CkStandard;
import com.changhu.module.assessmentCriteria.pojo.params.*;
import com.changhu.module.assessmentCriteria.pojo.queryParams.CkProjectPagerQueryParams;
import com.changhu.module.assessmentCriteria.pojo.vo.AssessmentCriteriaRuleVo;
import com.changhu.module.assessmentCriteria.pojo.vo.CkProjectDetailTableVo;
import com.changhu.module.assessmentCriteria.pojo.vo.CkProjectPagerVo;
import com.changhu.module.assessmentCriteria.service.AssessmentCriteriaService;
import com.changhu.support.mybatisplus.pojo.params.PageParams;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 20252
 * @createTime 2024/11/6 上午10:02
 * @desc AssessmentCriteriaController...
 */
@Tag(name = "考核标准")
@JsonBody
@RequestMapping("/assessmentCriteria")
@CheckUserType(userTypes = UserType.MANAGEMENT_SUPER)
public class AssessmentCriteriaController {

    @Autowired
    private AssessmentCriteriaService assessmentCriteriaService;

    @Operation(summary = "根据类型获取考核项目列表")
    @GetMapping("/ckProjectListByType")
    @CheckUserType(value = false)
    public List<SelectNodeVo<Long>> ckProjectListByType(@RequestParam EnterprisesUnitType type) {
        return assessmentCriteriaService.assessmentCriteriaListByType(type);
    }

    @Operation(summary = "根据考核项目获取考核规则")
    @GetMapping("/assessmentCriteriaRulesByCkProjectId")
    @CheckUserType(value = false)
    public List<AssessmentCriteriaRuleVo> assessmentCriteriaRulesByCkProjectId(@RequestParam Long ckProjectId) {
        return assessmentCriteriaService.assessmentCriteriaRulesByCkProjectId(ckProjectId);
    }

    @Operation(summary = "提交考核记录")
    @PostMapping("/submitAssessmentRecord")
    @CheckUserType(value = false)
    public void submitAssessmentRecord(@Validated @RequestBody AssessmentRecordParams params) {
        assessmentCriteriaService.submitAssessmentRecord(params);
    }

    @Operation(summary = "考核项目分页查询")
    @PostMapping("/ckProjectPagerVoPager")
    public Page<CkProjectPagerVo> ckProjectPagerVoPager(@RequestBody PageParams<CkProjectPagerQueryParams, CkProjectPagerVo> queryParams) {
        return assessmentCriteriaService.ckProjectPagerVoPager(queryParams);
    }

    @Operation(summary = "新增或修改考核项目")
    @PostMapping("/saveOrUpdateCkProject")
    public void saveOrUpdateCkProject(@Valid @RequestBody CkProjectSaveOrUpdateParams params) {
        assessmentCriteriaService.saveOrUpdateCkProject(params);
    }

    @Operation(summary = "删除考核项目")
    @DeleteMapping("/deleteCkProjectById")
    public void deleteCkProjectById(@RequestParam Long ckProjectId) {
        assessmentCriteriaService.deleteCkProjectById(ckProjectId);
    }

    @Operation(summary = "考核项目详情")
    @GetMapping("/ckProjectDetail")
    public List<CkProjectDetailTableVo> ckProjectDetail(@RequestParam Long ckProjectId) {
        return assessmentCriteriaService.ckProjectDetail(ckProjectId);
    }

    @Operation(summary = "新增或修改考核分组")
    @PostMapping("/saveOrUpdateCkGroup")
    public void saveOrUpdateCkGroup(@Valid @RequestBody CkGroupSaveOrUpdateParams params) {
        assessmentCriteriaService.saveOrUpdateCkGroup(params);
    }

    @Operation(summary = "新增或修改考核项")
    @PostMapping("/saveOrUpdateCkItem")
    public void saveOrUpdateCkItem(@Valid @RequestBody CkItemSaveOrUpdateParams params) {
        assessmentCriteriaService.saveOrUpdateCkItem(params);
    }

    @Operation(summary = "新增或修改考核标准")
    @PostMapping("/saveOrUpdateCkStandard")
    public void saveOrUpdateCkStandard(@Valid @RequestBody CkStandardSaveOrUpdateParams params) {
        assessmentCriteriaService.saveOrUpdateCkStandard(params);
    }

    @Operation(summary = "删除考核分组")
    @DeleteMapping("/deleteCkGroupById")
    public void deleteCkGroupById(@RequestParam Long ckGroupId) {
        boolean b = Db.removeById(ckGroupId, CkGroup.class);
        if (!b) {
            throw new MessageException();
        }
    }

    @Operation(summary = "删除考核项")
    @DeleteMapping("/deleteCkItemById")
    public void deleteCkItemById(@RequestParam Long ckItemId) {
        boolean b = Db.removeById(ckItemId, CkItem.class);
        if (!b) {
            throw new MessageException();
        }
    }

    @Operation(summary = "删除考核标准")
    @DeleteMapping("/deleteCkStandardById")
    public void deleteCkStandardById(@RequestParam Long ckStandardId) {
        boolean b = Db.removeById(ckStandardId, CkStandard.class);
        if (!b) {
            throw new MessageException();
        }
    }

}
