package com.zcxk.evaluation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.LoginUser;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import org.springframework.web.bind.annotation.*;
import com.zcxk.evaluation.entity.EvaluationGradeEntity;
import com.zcxk.evaluation.service.EvaluationGradeService;

/**
 * 考评等级页面控制器
 * @author WYY
 * @date 2021-05-08 09:23
 */
@RestController
@RequestMapping("/evaluation/grade")
@Api(tags = "考评等级")
public class EvaluationGradeController {

    @Autowired
    private EvaluationGradeService evaluationGradeService;

    @ApiOperation(value = "分页查询等级信息")
    @RequestMapping(value = "/selectPage", method = RequestMethod.GET)
    public AjaxMessage<Pagination<EvaluationGradeEntity>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @ApiParam(value = "绩效等级", required = false)@RequestParam(required = false) String grade,
            @RequestParam(required = false) Long companyOrgId
            ) {
        IPage<EvaluationGradeEntity> iPage = new Page<>(pageNum, pageSize);
        EvaluationGradeEntity entity = new EvaluationGradeEntity();
        if (grade != null && grade.trim() != "") {
            entity.setEvaluationGrade(Long.valueOf(grade));
        }
        if (companyOrgId != null) {
            entity.setCompanyOrgId(companyOrgId.intValue());
        }
        iPage = evaluationGradeService.findPage(iPage, entity);
        Pagination<EvaluationGradeEntity> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增等级信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxMessage<Integer> addGrade(@ApiParam(value = "等级信息", required = true) @RequestBody EvaluationGradeEntity evaluationGrade) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        List<EvaluationGradeEntity> evaluationGradeEntities = new ArrayList<>();
        boolean isOk = true;
        for (Integer companyOrgId : evaluationGrade.getCompanyOrgIds()) {

            EvaluationGradeEntity item = new EvaluationGradeEntity();
            BeanUtils.copyProperties(evaluationGrade, item);
            item.setCompanyOrgId(companyOrgId);
            if (currentUser != null) {
                item.setUpdateBy(currentUser.getUsername());
                item.setCreateBy(currentUser.getUsername());
                item.setDateCreate(new Date());
                item.setDateUpdate(new Date());
                item.setTenantId(currentUser.getTenantId());
            }
            item.setStatus(1);
            evaluationGradeEntities.add(item);
            List<EvaluationGradeEntity> list = evaluationGradeService.findList(item);
            if (list.size() > 0) {
                isOk = false;
                break;
            }
        }
        if (isOk) {
            evaluationGradeService.addBatchEvaluationGrade(evaluationGradeEntities);
        } else {
            return new AjaxMessage<>(ResultStatus.GRADE_ALREADY_EXISTS, 0);
        }
        return new AjaxMessage<>(ResultStatus.OK, 1);
    }


    @ApiOperation(value = "按ID查询绩效等级")
    @GetMapping(value = "/findGradeById")
    public  AjaxMessage<EvaluationGradeEntity> findGradeById(@ApiParam(value = "等级ID", required = true) @RequestParam  Long id){
        EvaluationGradeEntity evaluationGrade = evaluationGradeService.findEvaluationGradeById(id);
        return new AjaxMessage<>(ResultStatus.OK, evaluationGrade);
    }

    /**
     * 修改保存考评等级
     */
    @ApiOperation(value = "修改保存考评等级")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public  AjaxMessage<Integer> editEvaluationGrade(@ApiParam(value = "绩效等级", required = true) @RequestBody EvaluationGradeEntity evaluationGrade) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser != null) {
            evaluationGrade.setUpdateBy(currentUser.getUsername());
            evaluationGrade.setDateUpdate(new Date());
        }
        EvaluationGradeEntity orgGrade = evaluationGradeService.findEvaluationGradeById(evaluationGrade.getId());
        EvaluationGradeEntity evaluationGradeEntity = new EvaluationGradeEntity();
        evaluationGradeEntity.setEvaluationGrade(evaluationGrade.getEvaluationGrade());
        evaluationGradeEntity.setCompanyOrgId(orgGrade.getCompanyOrgId());
        evaluationGradeEntity.setTenantId(currentUser.getTenantId());
        evaluationGradeEntity.setId(evaluationGrade.getId());
        List<EvaluationGradeEntity> list = evaluationGradeService.findList(evaluationGradeEntity);
        if (list.size() > 0) {
            return new AjaxMessage<>(ResultStatus.GRADE_ALREADY_EXISTS, 0);
        }
        int result = evaluationGradeService.updateEvaluationGradeById(evaluationGrade) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 删除
     */
    @ApiOperation(value = "批量删除绩效等级")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    @ResponseBody
    public AjaxMessage<Integer> deleteByIds(@ApiParam(value = "等级IDS", required = true) @RequestBody Long[] ids) {
        if (ids == null || ids.length == 0) {
            return new AjaxMessage<>(ResultStatus.PARAM_ERROR);
        }
        int result = evaluationGradeService.delEvaluationGradeByIds(ids) ? 1 : 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 删除
     */
    @ApiOperation(value = "单个删除绩效等级")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public AjaxMessage<Integer> deleteById(@ApiParam(value = "绩效ID", required = true) @RequestParam Long id) {
        if (id == null) {
            return new AjaxMessage<>(ResultStatus.PARAM_ERROR);
        }
        List<Long> longIds = new ArrayList<>();
        longIds.add(id);
        EvaluationGradeEntity evaluationGrade = evaluationGradeService.findEvaluationGradeById(id);
        if (evaluationGrade == null) {
            return new AjaxMessage<>(ResultStatus.EXSIT_IS_PARENT_ERROR, 0);
        } else {
            int result = evaluationGradeService.delEvaluationGradeById(id) ? 1 : 0;
            return new AjaxMessage<>(ResultStatus.OK, result);
        }
    }

    @ApiOperation(value = "获取绩效等级")
    @RequestMapping(value = "/getScoreGrade", method = RequestMethod.POST)
    public AjaxMessage<String> getScoreGrade(@ApiParam(value = "租户ID", required = true) @RequestParam String tenantId,
                                         @ApiParam(value = "公司ID", required = true) @RequestParam Integer companyOrgId,
                                         @ApiParam(value = "分数", required = true) @RequestParam BigDecimal score) {
        return new AjaxMessage<>(ResultStatus.OK, evaluationGradeService.getScoreGrade(tenantId, companyOrgId, score, true));
    }
}
