package com.lz.modules.performance.controller;

import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.modules.app.resp.OwnResultResp;
import com.lz.modules.flow.dao.FlowStartMapper;
import com.lz.modules.performance.req.ChartResultReq;
import com.lz.modules.performance.req.ChartStartsReq;
import com.lz.modules.performance.req.OwnResultReq;
import com.lz.modules.performance.res.ChartStartsRes;
import com.lz.modules.performance.res.ChartStatisticalRes;
import com.lz.modules.performance.res.ResultRankListRes;
import com.lz.modules.performance.service.ChartResultService;
import com.lz.modules.sys.controller.AbstractController;
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/15 9:33
 */
@RestController
@RequestMapping("/performance")
@Api(value="报表接口", tags={"绩效报表"})
@Slf4j
public class ChartController extends AbstractController{
    @Autowired
    private ChartResultService chartResultService;
    @Autowired
    private FlowStartMapper flowStartMapper;
    @Autowired
    private ResultRecordService resultRecordService;

    @GetMapping("chart/result")
    @ApiOperation("获取绩效报表统计")
    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = ChartStatisticalRes.class)})
    public R chartResult(@RequestParam(required = false) @ApiParam(name = "startId",value = "考核周期标识id") Long startId){
        List<ChartStatisticalRes> chartStatisticalRes;
        try {
            Long userId = getUserId();
            chartStatisticalRes = chartResultService.chartReport(startId,userId);
        } catch (Exception e) {
            log.error("获取绩效报表统计异常" ,e);
            return R.error(e.getMessage());
        }
        return R.ok().put("data",chartStatisticalRes);
    }


    @PostMapping("chart/starts")
    @ApiOperation("获取考核类型列表")
    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = ChartStartsRes.class)})
    public R chartStarts(@RequestBody @ApiParam(name = "body",value = "body请求体",required = true)ChartStartsReq req){
        PageUtils pageUtils;
        try {
            pageUtils = chartResultService.chartStarts(req);
        } catch (Exception e) {
            log.error("获取考核类型列表异常" ,e);
            return R.error(e.getMessage());
        }
        return R.ok().put("data",pageUtils);
    }


    @PostMapping("chart/detail")
    @ApiOperation("获取报表等级详情")
    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = ResultRankListRes.class)})
    public R chartDetailList(@RequestBody @ApiParam(name = "body",value = "body请求体",required = true) ChartResultReq req){
        PageUtils pageUtils;
        try {
            Long userId = getUserId();
            req.setLoginUserId(userId);
            pageUtils = chartResultService.selectChartDetailList(req);
        } catch (Exception e) {
            log.error("获取报表等级详情异常" ,e);
            return R.error(e.getMessage());
        }
        return R.ok().put("data",pageUtils);
    }



    @PostMapping("/own/result")
    @ApiOperation("获取个人成长曲线")
    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = OwnResultResp.class)})
    public R ownResult(@RequestBody @ApiParam(name = "body",value = "body请求体",required = true) OwnResultReq req){
        if(req.getUserId() == null){
            //如果id为空，查询自己信息
            req.setUserId(getUserId());
        }
        List<OwnResultResp> ownResultResps = resultRecordService.ownResult(req);
        return R.ok().put("data",ownResultResps);
    }


}
