package com.nantian.cloud.jly.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.InspectionData;
import com.nantian.cloud.jly.service.InspectionDataService;
import com.nantian.cloud.jly.vo.InspectionAuditVo;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * 企业自查18项资料
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@Api(value = "InspectionData", tags = "企业自查18项资料")
@RequestMapping("/InspectionData")
public class InspectionDataController {
    private final InspectionDataService inspectionDataService;
    private final MinioTemplate minioTemplate;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param inspectioData 请假审批单
     * @return
     */
    @SysLog("企业自查18项资料-分页查询")
    @GetMapping("/page")
    @ApiOperation(value = "查询企业自查18项资料", notes = "条件查询企业自查18项资料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "InspectioData", value = "企业自查18项资料", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path")
    })

    public R getcheckAbnormalReportPage(Page page, InspectionData inspectioData) {
        inspectioData.setDelFlag("1"); //查询未删除数据
        return R.ok(inspectionDataService.page(page, Wrappers.query(inspectioData)));
    }

    /**
     * 信息
     *
     * @param inspectioDataId
     * @return R
     */
    @SysLog("企业自查18项资料-通过ID查询企业自查18项资料")
    @GetMapping("/{inspectioDataId}")
    @ApiOperation(value = "通过ID查询企业自查18项资料", notes = "通过ID条件企业自查18项资料")
    @ApiImplicitParam(name = "inspectioDataId", value = "不符合报告记录ID", required = false, dataType = "int", paramType = "path")
    public R getById(@PathVariable("inspectioDataId") Integer inspectioDataId) {
        return R.ok(inspectionDataService.getById(inspectioDataId));
    }

    /**
     * 保存
     */
    @SysLog("企业自查18项资料-保存")
    //@PostMapping   中间表不提供新增接口
    //@ApiOperation(value = "新增企业自查18项资料", notes = "新增企业自查18项资料")
    //@ApiImplicitParam(name = "InspectionData", value = "企业自查18项资料", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody InspectionData inspectioData) {
        return R.ok(inspectionDataService.save(inspectioData));
    }

    /**
     * 修改
     *
     * @param inspectionData
     * @return R
     */
    @SysLog("企业自查18项资料-修改")
    @PutMapping
    @ApiOperation(value = "修改企业自查18项资料", notes = "修改企业自查18项资料")
    @ApiImplicitParam(name = "InspectionData", value = "企业自查18项资料", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody InspectionData inspectionData) {
        return R.ok(inspectionDataService.updateById(inspectionData));
    }

    /**
     * 删除
     */
    @SysLog("企业自查18项资料-删除")
    @DeleteMapping("/{inspectioDataId}")
    @ApiOperation(value = "删除企业自查18项资料", notes = "删除企业自查18项资料")
    @ApiImplicitParam(name = "inspectioDataId", value = "企业自查18项资料id", required = true, dataType = "int", paramType = "path")
    public R removeById(@PathVariable("inspectioDataId") Long inspectioDataid) {
        return R.ok(inspectionDataService.removeById(inspectioDataid));
    }

    /**
     * 根据计划ID、企业信用代码查询企业18项资料数据
     *
     * @return R
     */
    @SysLog("企业自查18项资料-根据计划ID、企业信用代码查询企业18项资料数据")
    @GetMapping("/getByPlanIdAndEpc")
    @ApiOperation(value = "根据计划ID、企业信用代码查询企业18项资料数据", notes = "{'checkPlanId':审查计划ID,'enterpriseCode':企业社会行用代码}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "审查计划ID", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "epc", value = "企业社会行用代码", required = true, dataType = "String", paramType = "path")
    })
    public R getInpectionDataBycheckPlanIdAndEnterpriseCode(BigInteger planId, String epc) {
        return R.ok(inspectionDataService.getByPlanIdAndEpc(planId, epc));
    }

    /**
     * 上传文件材料
     */
    @SysLog("企业自查18项资料-上传文件材料")
    @SneakyThrows
    @PostMapping("/upload")
    @ApiOperation(value = "企业上传文件材料", notes = "企业上传文件材料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid", value = "uuid", required = false, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "文件类型（1：本单位基本情况和组织机构设置框图，2：能源计量工作自查报告，" +
                    "3：能源计量管理制度，4：相关审查报告，5：能源流向图，6：能源计量器具配备及计量采集点网络图，" +
                    "7：能源计量器具自行检定装置量值传递/溯源框图，8：能源计量器具量值传递/溯源框图 ）", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "File", paramType = "path"),
            @ApiImplicitParam(name = "checkPlanId", value = "审查计划ID", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "enterpriseCode", value = "企业社会行用代码", required = true, dataType = "string", paramType = "path")
    })
    public R upload(String uuid, String type, @RequestParam("file") MultipartFile file, BigInteger checkPlanId, String enterpriseCode) {
        if (uuid == null || uuid == "") {
            uuid = UUID.randomUUID().toString();
        }

        //上传文件
        InputStream inputStream = file.getInputStream(); //得到文件流
        String fileName = file.getOriginalFilename(); //文件名
        //minioTemplate.putObject("jly", uuid, inputStream,size, fileName);
        //minioTemplate.putObject("jly", uuid, inputStream);
        minioTemplate.putObject("jly", uuid, inputStream, inputStream.available(), "application/octet-stream");

        //fileName + "<" + uuid   文件名、uuid的拼接
        boolean flag = inspectionDataService.upload(fileName + "<" + uuid, type, checkPlanId, enterpriseCode);

        //Map map = new HashMap<String, String>();
        //map.put("uuid", uuid);
        //map.put("fileName", fileName);
        return flag ? R.ok(fileName + "<" + uuid) : R.failed("文件材料上传失败！");
    }


    /**
     * 上传多张网络图
     */
    @SysLog("企业自查18项资料-上传多张网络图")
    @SneakyThrows
    @PostMapping("/uploadImgs")
    @ApiOperation(value = "企业上传文件材料", notes = "企业上传文件材料")
    public R uploadImgs(String uuid, @RequestParam("file") MultipartFile file) {
        if (uuid == null || uuid == "") {
            uuid = UUID.randomUUID().toString();
        }
        //上传文件
        InputStream inputStream = file.getInputStream(); //得到文件流
        String fileName = file.getOriginalFilename(); //文件名
        minioTemplate.putObject("jly", uuid, inputStream, inputStream.available(), "application/octet-stream");
        return R.ok(fileName + "<" + uuid);
    }


    /**
     * 政府端审查企业18项资料
     */
    @SysLog("企业自查18项资料-政府端审查企业18项资料")
    @PostMapping("/audit")
    public R audit(@RequestBody InspectionAuditVo inspectionAuditVo) {
        return inspectionDataService.audit(inspectionAuditVo.getId(), inspectionAuditVo.getState(), inspectionAuditVo.getStatus(), inspectionAuditVo.getInfo(),inspectionAuditVo.getMendFile());
    }
//    public R audit(@RequestParam("id")BigInteger id, String state, String status, String info,String mendFile) {
//        return inspectionDataService.audit(id, state, status, info,mendFile);
//    }

    /**
     * 重置状态
     */
    @SysLog("企业自查18项资料-重置状态")
    @PostMapping("/resetState")
    public R resetState(@RequestBody InspectionAuditVo inspectionAuditVo) {
        return inspectionDataService.resetState(inspectionAuditVo.getId(), inspectionAuditVo.getState(), inspectionAuditVo.getStatus(), inspectionAuditVo.getInfo(),inspectionAuditVo.getMendFile());
    }

    /**
     * 提交时保存文件列表
     */
    @SysLog("企业自查18项资料-提交时保存文件列表")
    @PostMapping("/uploadFiles")
    public R uploadFiles(String type,BigInteger checkPlanId, String enterpriseCode,String flieList,String mend) {
        return inspectionDataService.uploadFiles(type,checkPlanId,enterpriseCode,flieList,mend);
    }
}
