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.util.ArrayList;
import java.util.List;
import java.util.Date;

import org.springframework.web.bind.annotation.*;
import com.zcxk.evaluation.entity.EvaluationCycleEntity;
import com.zcxk.evaluation.service.EvaluationCycleService;

/**
 * 考评周期页面控制器
 *
 * @author WYY
 * @date 2021-05-08 09:09
 */
@RestController
@RequestMapping("/evaluation/cycle")
@Api(tags = "考评周期")
public class EvaluationCycleController {

    @Autowired
    private EvaluationCycleService evaluationCycleService;

    @ApiOperation(value = "分页查询考评周期")
    @RequestMapping(value = "/selectPage", method = RequestMethod.GET)
    public AjaxMessage<Pagination<EvaluationCycleEntity>> list(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @ApiParam(value = "周期类型")@RequestParam(required = false) String type,
            @RequestParam(required = false) Long companyOrgId) {
        IPage<EvaluationCycleEntity> iPage = new Page<>(pageNum, pageSize);
        EvaluationCycleEntity entity = new EvaluationCycleEntity();
        if (type != null && type.trim() != "") {
            entity.setType(Integer.valueOf(type));
        }
        if (companyOrgId != null) {
            entity.setCompanyOrgId(companyOrgId.intValue());
        }
        iPage = evaluationCycleService.findPage(iPage, entity);
        Pagination<EvaluationCycleEntity> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }
    /**
     * 新增
     */
    @ApiOperation(value = "新增考评周期")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxMessage<Integer> addCycle(@ApiParam(value = "周期信息", required = true) @RequestBody EvaluationCycleEntity evaluationCycle) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        boolean isOk = true;
        List<EvaluationCycleEntity> evaluationCycleEntities = new ArrayList<>();
        for (Integer companyOrgId : evaluationCycle.getCompanyOrgIds()) {
            EvaluationCycleEntity item= new EvaluationCycleEntity();
            BeanUtils.copyProperties(evaluationCycle,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);
            evaluationCycleEntities.add(item);
            List<EvaluationCycleEntity> list = evaluationCycleService.findList(item);
            if (list.size() > 0) {
                isOk = false;
                break;
            }
        }
        if (isOk) {
            evaluationCycleService.addBatchEvaluationCycle(evaluationCycleEntities);
        } else {
            return new AjaxMessage<>(ResultStatus.CYCLE_ALREADY_EXISTS, 0);
        }
        return new AjaxMessage<>(ResultStatus.OK, 1);
    }


    @ApiOperation(value = "按ID查询考评周期")
    @GetMapping(value = "/findCycleById")
    public  AjaxMessage<EvaluationCycleEntity> findCycleById(@ApiParam(value = "周期ID", required = true) @RequestParam  Long id){
        EvaluationCycleEntity evaluationCycle = evaluationCycleService.findEvaluationCycleById(id);
        return new AjaxMessage<>(ResultStatus.OK, evaluationCycle);
    }

    /**
     * 修改保存考评等级
     */
    @ApiOperation(value = "修改保存考评周期")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public  AjaxMessage<Integer> editEvaluationCycle(@ApiParam(value = "考评周期", required = true) @RequestBody EvaluationCycleEntity evaluationCycle){
        LoginUser currentUser = UserUtil.getCurrentUser();
        if (currentUser != null) {
            evaluationCycle.setUpdateBy(currentUser.getUsername());
            evaluationCycle.setDateUpdate(new Date());
        }
        EvaluationCycleEntity itemCycle = evaluationCycleService.findEvaluationCycleById(evaluationCycle.getId());
        EvaluationCycleEntity evaluationCycleEntity = new EvaluationCycleEntity();
        evaluationCycleEntity.setType(evaluationCycle.getType());
        evaluationCycleEntity.setCompanyOrgId(itemCycle.getCompanyOrgId());
        evaluationCycleEntity.setTenantId(currentUser.getTenantId());
        evaluationCycleEntity.setId(evaluationCycle.getId());
        List<EvaluationCycleEntity> list = evaluationCycleService.findList(evaluationCycleEntity);
        if (list.size() > 0) {
            return new AjaxMessage<>(ResultStatus.CYCLE_ALREADY_EXISTS, 0);
        }
        int result= evaluationCycleService.updateEvaluationCycleById(evaluationCycle)? 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 = evaluationCycleService.delEvaluationCycleByIds(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);
        EvaluationCycleEntity evaluationCycle = evaluationCycleService.findEvaluationCycleById(id);
        if (evaluationCycle == null) {
            return new AjaxMessage<>(ResultStatus.EXSIT_IS_PARENT_ERROR, 0);
        } else {
            int result = evaluationCycleService.delEvaluationCycleById(id) ? 1 : 0;
            return new AjaxMessage<>(ResultStatus.OK, result);
        }
    }
}
