package com.yiboshi.science.rest.v1;

import com.yiboshi.arch.base.ResponseDataModel;
import com.yiboshi.science.base.Pagination;
import com.yiboshi.science.config.annotation.Logs;
import com.yiboshi.science.config.security.SecurityUserHolder;
import com.yiboshi.science.entity.ComProjectAudit;
import com.yiboshi.science.entity.ComProjectTask;
import com.yiboshi.science.entity.SelectListItem;
import com.yiboshi.science.enumeration.CommonEnum;
import com.yiboshi.science.param.dto.ComDownloadDTO;
import com.yiboshi.science.param.dto.ComFileDTO;
import com.yiboshi.science.param.dto.ComProjectTaskDTO;
import com.yiboshi.science.param.dto.DataStatisticsDTO;
import com.yiboshi.science.param.query.ComProjectTaskQueryVO;
import com.yiboshi.science.rest.BaseController;
import com.yiboshi.science.service.ComProjectTaskService;
import com.yiboshi.science.service.SystemParameterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能：任务书表 接口
 *
 * @author lkl
 * @version 2021-08-26
 */
@Api(tags = "com-project-test", description = "任务书表")
@RestController
@RequestMapping("/v1/science-admin/com-project-test")
public class ComProjectTaskController extends BaseController<ComProjectTaskService, ComProjectTaskQueryVO, ComProjectTaskDTO, ComProjectTask> {

    @Autowired
    private ComProjectTaskService ComProjectTaskService;

    @Autowired
    private SystemParameterService systemParameterService;

    /**
     * 功能:分页查询
     *
     * @param vo 查询条件
     */
    @ApiOperation(value = "1.02 分页查询", httpMethod = "GET", notes = "1.02 根据参数获取列表")
    @GetMapping
    @RequestMapping("/getListByPage")
    public ResponseDataModel<Pagination<ComProjectTaskDTO>> getListByPage(@Validated ComProjectTaskQueryVO vo, BindingResult bindingResult) {
        vo.setAppPersonId(SecurityUserHolder.getPersonId());
        vo.setProjState(CommonEnum.projState.pass.getCode());
        Pagination<ComProjectTaskDTO> page = ComProjectTaskService.getListByPage(vo);
        page.getDataList().forEach((e) -> {

        });
        return ResponseDataModel.ok(page);
    }

    /**
     * 获取统计数据
     */
    @ApiOperation(value = "获取统计数据", httpMethod = "GET", notes = "获取统计数据")
    @GetMapping
    @RequestMapping("/getCount")
    public ResponseDataModel<DataStatisticsDTO> getCount(ComProjectTask e) {
        ComProjectTaskQueryVO vo=new ComProjectTaskQueryVO();
        vo.setAppPersonId(SecurityUserHolder.getPersonId());
        vo.setReportYear(e.getReportYear());
        return ResponseDataModel.ok(ComProjectTaskService.getCount(vo));
    }

    /**
     * 保存任务书信息
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "保存任务书信息", httpMethod = "POST", notes = "保存任务书信息")
    @PostMapping
    @RequestMapping("/save")
    //@SystemLogs(value = CommonEnum.LogType.TaskBookSave)
    public ResponseDataModel<String> save(@RequestBody ComProjectTaskDTO dto) {
        return ResponseDataModel.ok(ComProjectTaskService.save(dto));
    }

    /**
     * 根据id获取任务书信息
     *
     * @param projId 查询条件
     */
    @ApiOperation(value = "根据id获取项目信息", httpMethod = "GET", notes = "根据id获取项目信息")
    @GetMapping
    @RequestMapping("/getTestInfoByProjId")
    public ResponseDataModel<ComProjectTaskDTO> getTestInfoByProjId(String projId) {
        return ResponseDataModel.ok(ComProjectTaskService.getTestInfoById(projId));
    }

    /**
     * 项目/任务书/中期考核表上报
     */
    @ApiOperation(value = "项目/任务书/中期考核表上报", httpMethod = "POST", notes = "项目/任务书/中期考核表上报")
    @PostMapping
    @RequestMapping("/report")
    @Logs(value = CommonEnum.logType.taskBookReport)
    public ResponseDataModel<String> report(@RequestBody ComProjectAudit comProjectAudit) {
        ComProjectTaskService.report(comProjectAudit, SecurityUserHolder.getUnitId(), SecurityUserHolder.getUnitCode());
        return ResponseDataModel.ok("上报成功");
    }

    /**
     * 项目/任务书/中期考核表审核
     */
    @ApiOperation(value = "项目/任务书/中期考核表审核", httpMethod = "POST", notes = "项目/任务书/中期考核表审核")
    @PostMapping
    @RequestMapping("/audit")
    @Logs(value = CommonEnum.logType.taskBookAudit)
    public ResponseDataModel<String> audit(@RequestBody ComProjectAudit comProjectAudit) {
        ComProjectTaskService.audit(comProjectAudit, SecurityUserHolder.getUnitId(), SecurityUserHolder.getUnitCode());
        return ResponseDataModel.ok("审核成功");
    }

    @ApiOperation(value = "获取任务书状态", httpMethod = "GET", notes = "获取任务书状态")
    @GetMapping
    @RequestMapping("/getTestStateList")
    public ResponseDataModel<List<SelectListItem>> getTestStateList() {
        List<SelectListItem> list = new ArrayList<>(CommonEnum.taskState.values().length);
        for (CommonEnum.taskState type : CommonEnum.taskState.values()) {
            SelectListItem item = new SelectListItem();
            item.setKey(type.getCode().toString());
            item.setTitle(type.getDescription());
            list.add(item);
        }
        return ResponseDataModel.ok(list);
    }

    @ApiOperation(value = "根据项目id获取任务书附件列表", httpMethod = "GET", notes = "根据项目id获取任务书附件列表")
    @GetMapping
    @RequestMapping("/getTaskFileList")
    public ResponseDataModel<List<ComFileDTO>> getTaskFileList(String projId) {
        return ResponseDataModel.ok(ComProjectTaskService.getTaskFileList(projId));
    }

    @ApiOperation(value = "任务书文件上传", httpMethod = "POST", notes = "文件上传")
    @PostMapping("/asyncUploadTaskFile")
    public ResponseDataModel<ComDownloadDTO> asyncUploadTaskFile(@RequestParam("file") MultipartFile file, @RequestParam @NotBlank String projId) {
        return ResponseDataModel.ok(ComProjectTaskService.asyncUploadTaskFile(file, projId));
    }

    @ApiOperation(value = "删除任务书文件", httpMethod = "DELETE", notes = "删除任务书文件")
    @DeleteMapping(value = "deleteTaskFile/{id}")
    @Logs(value = CommonEnum.logType.fileDelete)
    public ResponseDataModel<String> deleteTaskFile(@PathVariable String id) {
        return ResponseDataModel.ok(ComProjectTaskService.deleteTaskFile(id));
    }
}