package com.lz.modules.performance.controller;

import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.common.utils.StringUtil;
import com.lz.modules.app.dto.StaffSimpleDto;
import com.lz.modules.flow.dao.FlowStartMapper;
import com.lz.modules.flow.entity.FlowStart;
import com.lz.modules.performance.req.AssessChangeReq;
import com.lz.modules.performance.req.AssessListReq;
import com.lz.modules.performance.req.AssessDetailReq;
import com.lz.modules.performance.req.AssessToScoreReq;
import com.lz.modules.performance.res.AssessManagerDetailRes;
import com.lz.modules.performance.res.AssessManagerListRes;
import com.lz.modules.performance.res.ChartStatistical;
import com.lz.modules.performance.res.ChartStatisticalRes;
import com.lz.modules.performance.service.AssessManagerService;
import com.lz.modules.performance.service.ChartResultService;
import com.lz.modules.sys.controller.AbstractController;
import com.lz.modules.sys.dao.app.ResultRecordMapper;
import com.lz.modules.sys.service.app.ResultRecordService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: djc
 * @Desc:
 * @Date: 2020/10/20 17:33
 */
@RestController
@RequestMapping("/performance")
@Slf4j
@Api(value="考核管理接口", tags={"考核管理"})
public class AssessManagerController extends AbstractController{

    @Autowired
    private FlowStartMapper flowStartMapper;
    @Autowired
    private AssessManagerService assessManagerService;
    @Autowired
    private ResultRecordMapper resultRecordMapper;
    @Autowired
    private ChartResultService chartResultService;


    @PostMapping("assess/manager/list")
    @ApiOperation("获取考核列表")
    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = AssessManagerListRes.class)})
    public R assessList(@RequestBody AssessListReq req){
        try {
            Long userId = getUserId();
            req.setLoginUserId(userId);
            PageUtils pageUtils = assessManagerService.assessList(req);
            return R.ok().put("data",pageUtils);
        } catch (Exception e) {
            log.error("获取考核列表异常，",e);
            return R.error(e.getMessage());
        }
    }

    @PostMapping("assess/manager/detail")
    @ApiOperation("获取考核详情列表")
    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = AssessManagerDetailRes.class)})
    public R assessDetail(@RequestBody AssessDetailReq req){
        PageUtils pageUtils;
        try {
            Long userId = getUserId();
            req.setLoginUserId(userId);
            pageUtils = assessManagerService.assessDetail(req);
        } catch (Exception e) {
            log.error("获取考核详情列表异常" ,e);
            return R.error(e.getMessage());
        }
        return R.ok().put("data",pageUtils);

    }



    @PostMapping("assess/manager/chart")
    @ApiOperation("获取考核详情列表人数统计")
    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = ChartStatistical.class)})
    public R assessChart(@RequestBody AssessDetailReq req){
        List<ChartStatistical> process;
        try {
            Long userId = getUserId();
            req.setLoginUserId(userId);
            process = chartResultService.countAssessNumByFlowProcess(req);
        } catch (Exception e) {
            log.error("获取详情顶部人数统计异常" ,e);
            return R.error(e.getMessage());
        }
        return R.ok().put("data",process);

    }


    @PostMapping("assess/manager/change")
    @ApiOperation("考核管理组管理变更")
    @ApiResponses({@ApiResponse(code = 200,message = "成功"),@ApiResponse(code = 503,message = "去设置",response = StaffSimpleDto.class)})
    public R assessChange(@RequestBody AssessChangeReq req){
        try {
            return assessManagerService.assessChange(req);
        } catch (Exception e) {
            log.error("考核管理变更异常" ,e);
            return R.error(e.getMessage());
        }
    }





    @GetMapping("assess/manager/delete")
    @ApiOperation("删除考核任务")
    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = ChartStatisticalRes.class)})
    @ApiImplicitParam(name = "assessId",value = "发起考核id", required = true, dataType = "Long",paramType = "query")
    public R assessDelete(@RequestParam Long assessId){
        FlowStart flowStart = flowStartMapper.selectFlowStartById(assessId);
        if(flowStart == null){
            return R.error("没有此条记录");
        }
        try {
            assessManagerService.accessDelete(flowStart);
        } catch (Exception e) {
            log.error("删除考核任务异常, id:" + assessId ,e);
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    @PostMapping("assess/manager/toScore")
    @ApiOperation("开始评分")
    @ApiResponses({@ApiResponse(code = 200,message = "成功")})
    public R assessToScore(@RequestBody @ApiParam AssessToScoreReq req){
        try {
            log.info("开始评分，当前登陆用户，userId：" + getUserId());
            assessManagerService.toScore(req);
        } catch (Exception e) {
            log.error("开始评分异常" ,e);
            return R.error(e.getMessage());
        }
        return R.ok();
    }
}
