package com.lz.modules.performance.controller;

import com.lz.common.utils.BigDecimalUtil;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.R;
import com.lz.modules.performance.dao.ResultTaskMapper;
import com.lz.modules.performance.dto.ResultTaskDto;
import com.lz.modules.performance.dto.TaskProcessRecordDto;
import com.lz.modules.performance.entity.TaskProcessRecord;
import com.lz.modules.performance.req.ChangeTaskListReq;
import com.lz.modules.performance.req.ResultUpdateTaskReq;
import com.lz.modules.performance.res.ResultTaskDetailRes;
import com.lz.modules.performance.service.ResultTaskService;
import com.lz.modules.performance.service.TaskProcessRecordService;
import com.lz.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: djc
 * @Desc:
 * @Date: 2020/12/9 9:52
 */
@RestController
@RequestMapping("/performance")
@Slf4j
@Api(value="绩效任务接口", tags={"绩效任务"})
public class ResultTaskController extends AbstractController{

    @Autowired
    private ResultTaskService resultTaskService;

    @Autowired
    private TaskProcessRecordService taskProcessRecordService;
    @Autowired
    private ResultTaskMapper resultTaskMapper;

    @GetMapping("/result/taskDetail")
    @ApiOperation("获取绩效任务详情")
    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = ResultTaskDetailRes.class)})
    public R taskDetail(Long detailId){
        ResultTaskDetailRes resultTaskDetailRes = resultTaskService.taskDetail(detailId);
        return R.ok().put("data",resultTaskDetailRes);
    }


    @PostMapping("/result/changeTask")
    @ApiOperation("任务新增或修改")
    @ApiResponses({@ApiResponse(code = 200,message = "成功")})
    public R changeTask(@RequestBody ResultUpdateTaskReq req){
        Long userId = getUserId();
        return resultTaskService.saveOrUpdateResultTask(req,userId);
    }


    @PostMapping("/result/changeTaskList")
    @ApiOperation("任务变更记录")
    @ApiResponses({@ApiResponse(code = 200,message = "成功",response = TaskProcessRecordDto.class)})
    public R changeTaskList(@RequestBody ChangeTaskListReq req){
        if(req.getStaffId()==null){
            req.setStaffId(getUserId());
        }
        PageUtils pageUtils = taskProcessRecordService.selectTaskProcessRecordsByTaskId(req);
        return R.ok().put("data",pageUtils);
    }

    @GetMapping("/result/deleteTask")
    @ApiOperation("任务删除")
    @ApiResponses({@ApiResponse(code = 200,message = "成功")})
    public R deleteTask(Long taskId){
        Long userId = getUserId();
        return resultTaskService.deleteTask(taskId,userId);
    }

}
