package com.zcxk.evaluation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.evaluation.entity.EvaluationItemEntity;
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.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import org.springframework.web.bind.annotation.*;
import com.zcxk.evaluation.service.EvaluationItemService;

/**
 * 考评项页面控制器
 * @author WYY
 * @date 2021-05-08 09:25
 */
@RestController
@RequestMapping("/evaluation/item")
@Api(tags = "考评项")
public class EvaluationItemController{
    @Autowired
    private EvaluationItemService evaluationItemService;
    @ApiOperation(value = "分页查询考评项")
    @RequestMapping(value = "/selectPage", method = RequestMethod.GET)
    public AjaxMessage<Pagination<EvaluationItemEntity>> list(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long deptOrgId,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize) {
        IPage<EvaluationItemEntity> iPage = new Page<>(pageNum, pageSize);
        EvaluationItemEntity entity = new EvaluationItemEntity();
        entity.setName(name);
        if (deptOrgId != null) {
            entity.setDeptOrgId(deptOrgId.intValue());
        }
        iPage = evaluationItemService.findPage(iPage, entity);
        Pagination<EvaluationItemEntity> 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 EvaluationItemEntity evaluationItem){
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser != null) {
            evaluationItem.setUpdateBy(currentUser.getUsername());
            evaluationItem.setCreateBy(currentUser.getUsername());
            evaluationItem.setDateCreate(new Date());
            evaluationItem.setDateUpdate(new Date());
            evaluationItem.setTenantId(currentUser.getTenantId());
        }
        evaluationItem.setStatus(1);
        int result = evaluationItemService.addEvaluationItem(evaluationItem)? 1: 0;
        return new AjaxMessage<>(ResultStatus.OK, result);
    }


    @ApiOperation(value = "按ID查询考评项")
    @GetMapping(value = "/findGradeById")
    public  AjaxMessage<EvaluationItemEntity> findGradeById(@ApiParam(value = "考评项ID", required = true) @RequestParam  Long id){
        EvaluationItemEntity evaluationItem = evaluationItemService.findEvaluationItemById(id);
        return new AjaxMessage<>(ResultStatus.OK, evaluationItem);
    }

    /**
     * 修改保存考评等级
     */
    @ApiOperation(value = "修改保存考评项")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public  AjaxMessage<Integer> editEvaluationGrade(@ApiParam(value = "考评项", required = true) @RequestBody EvaluationItemEntity evaluationItem){
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser != null) {
            evaluationItem.setUpdateBy(currentUser.getUsername());
            evaluationItem.setDateUpdate(new Date());
        }
        int result= evaluationItemService.updateEvaluationItemById(evaluationItem)? 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 = evaluationItemService.delEvaluationItemByIds(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);
        EvaluationItemEntity evaluationGrade = evaluationItemService.findEvaluationItemById(id);
        if (evaluationGrade == null) {
            return new AjaxMessage<>(ResultStatus.EXSIT_IS_PARENT_ERROR, 0);
        } else {
            int result = evaluationItemService.delEvaluationItemById(id) ? 1 : 0;
            return new AjaxMessage<>(ResultStatus.OK, result);
        }
    }
}
