package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TSuperviseTask;
import com.nantian.cloud.jljc.entity.TSupervisionTaskAudit;
import com.nantian.cloud.jljc.service.TSuperviseTaskService;
import com.nantian.cloud.jljc.service.TSupervisionTaskAuditService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.minio.service.MinioTemplate;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;


/**
 * 监察任务表-审批意见
 *
 * @date 2020-09-23
 */
@Slf4j
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tSupervisionTaskAudit")
@Api(value = "tSupervisionTaskAudit", tags = "节能节能监察任务表-审批意见")
public class TSupervisionTaskAuditController {
    @Autowired
    private TSupervisionTaskAuditService tSupervisionTaskAuditService;
    @Autowired
    private TSuperviseTaskService tSuperviseTaskService;
    @Autowired
    private MinioTemplate minioTemplate;

    /**
     * 分页获取监察任务表审批意见
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页获取监察任务表审批意见", notes = "分页获取监察任务表审批意见")
    @SysLog("分页获取监察任务表审批意见")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "tSupervisionTaskAudit", value = "审批意见对象", required = false, dataType = "json", paramType = "path")
    })
    public R getTSupervisionTaskAuditPage(Page page, TSupervisionTaskAudit tSupervisionTaskAudit) {
        tSupervisionTaskAudit.setDelFlag("1"); //筛选未删除项
        return R.ok(tSupervisionTaskAuditService.page(page, Wrappers.query(tSupervisionTaskAudit)));
    }

    /**
     * 根据id获取监察任务表审批意见
     */
    @GetMapping("/getById")
    @SysLog("根据id获取监察任务表审批意见")
    @ApiOperation(value = "根据id获取监察任务表审批意见", notes = "根据id获取监察任务表审批意见")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getById(Long id) {
        return R.ok(tSupervisionTaskAuditService.getById(id));
    }

    /**
     * 根据节能监察任务表id获取监察任务表审批意见
     */
    @GetMapping("/getByTaskId")
    @SysLog("根据节能监察任务表id获取监察任务表审批意见")
    @ApiOperation(value = "根据节能监察任务表id获取监察任务表审批意见", notes = "根据节能监察任务表id获取监察任务表审批意见")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R getByProjectId(Long id) {
        TSupervisionTaskAudit tSupervisionTaskAudit = new TSupervisionTaskAudit();
        tSupervisionTaskAudit.setProjectId(id);
        tSupervisionTaskAudit.setDelFlag("1");
        return R.ok(tSupervisionTaskAuditService.list(Wrappers.query(tSupervisionTaskAudit)));
    }

    /**
     * 更新监察任务表审批意见
     */
    @PostMapping("/audit")
    @SysLog("更新监察任务表审批意见")
    @ApiOperation(value = "更新监察任务表审批意见", notes = "更新监察任务表审批意见")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file1", value = "文件流（现场监察笔录）", required = false, dataType = "File", paramType = "path"),
            @ApiImplicitParam(name = "file2", value = "文件流（现场询问笔录）", required = false, dataType = "File", paramType = "path"),
            @ApiImplicitParam(name = "TSupervisionTaskAudit", value = "审批意见对象", required = true, dataType = "json", paramType = "path")
    })
    public R audit(TSupervisionTaskAudit tSupervisionTaskAudit, MultipartFile file1, MultipartFile file2) throws Exception {

        if (tSupervisionTaskAudit.getProjectId() == null) {
            return R.failed("节能监察任务表id为空，审查失败");
        }

        ScUser u = SecurityUtils.getUser();
        //获取监察任务
        TSuperviseTask tSuperviseTask = tSuperviseTaskService.getById(tSupervisionTaskAudit.getProjectId());
        if("3".equals(tSuperviseTask.getTaskState())){ //3成功  4不成功
            return R.failed("该节能监察任务已审核通过，不能再次审核！");
        }

        //审批意见
        if (tSupervisionTaskAudit.getId() == null) { //新增
            tSupervisionTaskAudit.setDelFlag("1");
            tSupervisionTaskAudit.setDeptId(u.getDeptId());
        }
        tSupervisionTaskAudit.setUpdateTime(new Date());
        tSupervisionTaskAudit.setUserid(u.getId());
        tSupervisionTaskAudit.setCheckTime(new Date());
        tSupervisionTaskAudit.setType(tSuperviseTask.getType());
        boolean b = tSupervisionTaskAuditService.saveOrUpdate(tSupervisionTaskAudit);

        //TSuperviseTask tSuperviseTask = new TSuperviseTask();
        //tSuperviseTask.setId(tSupervisionTaskAudit.getProjectId());

        tSuperviseTask.setUpdateTime(new Date());
        tSuperviseTask.setUserid(u.getId());
        if ("T".equals(tSupervisionTaskAudit.getState())) { //审批通过
            tSuperviseTask.setTaskState("3");
        } else {
            tSuperviseTask.setTaskState("4");
        }
        if (file1 != null) {
            //5：现场监察笔录(附件)
            tSuperviseTask.setInsiteSupervision(getFileUrl(file1));
        }
        if (file2 != null) {
            //6：现场询问笔录(附件)
            tSuperviseTask.setInterviewTranscripts(getFileUrl(file2));
        }
        boolean c = tSuperviseTaskService.updateById(tSuperviseTask);

        return R.ok(b && c);
    }

    public String getFileUrl(MultipartFile file) throws Exception {
        String uuid = UUID.randomUUID().toString();
        InputStream inputStream = file.getInputStream(); //得到文件流
        String fileName = file.getOriginalFilename();    //文件名（含后缀）
        //minioTemplate.putObject("jljc", uuid, inputStream);
        minioTemplate.putObject("jljc", uuid, inputStream,inputStream.available(),"application/octet-stream");
        return fileName + "<" + uuid;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @SysLog("删除监察任务表审批意见")
    @ApiOperation(value = "删除监察任务表审批意见", notes = "删除监察任务表审批意见")
    @ApiImplicitParam(name = "tSupervisionTaskAuditId", value = "id", required = true, dataType = "int", paramType = "path")
    public R removeById(Integer id) {
        TSupervisionTaskAudit tSupervisionTaskAudit = tSupervisionTaskAuditService.getById(id);
        if (tSupervisionTaskAudit == null) {
            return R.failed("审批意见不存在，删除失败");
        }
        ScUser u = SecurityUtils.getUser();
        tSupervisionTaskAudit.setUpdateTime(new Date());
        tSupervisionTaskAudit.setUserid(u.getId());
        tSupervisionTaskAudit.setDelFlag("4");
        return R.ok(tSupervisionTaskAuditService.updateById(tSupervisionTaskAudit));
    }

}
