package com.nantian.cloud.jljc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nantian.cloud.jljc.entity.TFaAccreditingBody;
import com.nantian.cloud.jljc.entity.TFaReviewReport;
import com.nantian.cloud.jljc.entity.TFaSpecialistOpinion;
import com.nantian.cloud.jljc.service.TFaAccreditingBodyService;
import com.nantian.cloud.jljc.service.TFaReviewReportService;
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 lombok.AllArgsConstructor;
import org.apache.commons.io.FilenameUtils;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 固定资产投资项目节能审查评审报告表 前端控制器
 * </p>
 *
 * @author fyf
 * @since 2022-02-23
 */
@RestController
@CrossOrigin
@AllArgsConstructor
@RequestMapping("/tFaReviewReport")
public class TFaReviewReportController {

    @Autowired
    MinioTemplate minioTemplate;

    @Autowired
    TFaReviewReportService tFaReviewReportService;

    @Autowired
    TFaAccreditingBodyService tFaAccreditingBodyService;


    @PostMapping("/fileUpload/{id}")
    @SysLog("根据id上传固定资产投资项目节能审查评审报告表")
    public R fileUpload(@PathVariable Integer id, @RequestParam("file") MultipartFile[] multipartFiles) throws Exception {
        TFaAccreditingBody tFaAccreditingBody = tFaAccreditingBodyService.getOne(new QueryWrapper<TFaAccreditingBody>().eq("project_id", id));
        ScUser u = SecurityUtils.getUser();
        if (multipartFiles.length > 0) {
            for (MultipartFile multipartFile : multipartFiles) {
                //获取文件名称
                String fileName = multipartFile.getOriginalFilename();
                //获取文件的后缀
                String extension = "." + FilenameUtils.getExtension(multipartFile.getOriginalFilename());
                //生成新的文件名称
                String newFileName = "PSBG" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
                        + UUID.randomUUID().toString()
                        .replace("-", "") + extension;
                //文件的大小
                long size = multipartFile.getSize();
                //文件的类型
                String contentType = multipartFile.getContentType();
                //上传文件
                InputStream inputStream = multipartFile.getInputStream();
                minioTemplate.putObject("jljc", newFileName, inputStream, inputStream.available(), "application/octet-stream");
                //文件信息保存到数据库
                TFaReviewReport tFaReviewReport = new TFaReviewReport();
                tFaReviewReport.setDelFlag(1);
                tFaReviewReport.setExtension(extension);
                tFaReviewReport.setFilePath("jljc/" + newFileName);
                tFaReviewReport.setFileSize(String.valueOf(size));
                tFaReviewReport.setFileType(contentType);
                tFaReviewReport.setNewFileName(newFileName);
                tFaReviewReport.setOldFileName(fileName);
                tFaReviewReport.setDeptId(u.getDeptId());
                tFaReviewReport.setUpdateTime(new Date());
                tFaReviewReport.setUserId(u.getId());
                tFaReviewReport.setUploadPerson(u.getUserRealName());
                tFaReviewReport.setProjectId(id);
                tFaReviewReport.setUploadTime(new Date());
                tFaReviewReport.setPersonId(tFaAccreditingBody.getId());
                tFaReviewReportService.save(tFaReviewReport);
                return R.ok();
            }
        } else {
            return R.failed("上传文件为空！");
        }
        return R.ok();
    }

    @GetMapping("/getFileList/{id}")
    @SysLog("根据id查询固定资产投资项目节能审查评审报告表")
    public R getFileList(@PathVariable Integer id){

        List<TFaReviewReport> list = tFaReviewReportService.list(
                new QueryWrapper<TFaReviewReport>()
                        .eq("project_id", id)
                        .eq("del_flag", 1));
        return R.ok(list);
    }

    @GetMapping("/fileDownload/{id}")
    @SysLog("根据id下载固定资产投资项目节能审查评审报告表")
    public R fileDownload(@PathVariable Integer id){
        TFaReviewReport tFaReviewReport = tFaReviewReportService.getById(id);
        String newFileName = tFaReviewReport.getNewFileName();
        return R.ok(minioTemplate.getObject("jljc",newFileName));
    }

    @DeleteMapping("/fileRemove/{id}")
    @SysLog("根据id删除固定资产投资项目节能审查评审报告表")
    public R fileRemove(@PathVariable Integer id) throws Exception {
        ScUser u = SecurityUtils.getUser();
        TFaReviewReport tFaReviewReport = tFaReviewReportService.getById(id);
        String newFileName = tFaReviewReport.getNewFileName();
        tFaReviewReport.setDelFlag(4);
        tFaReviewReport.setUserId(u.getId());
        tFaReviewReport.setUpdateTime(new Date());
        tFaReviewReport.setDeptId(u.getDeptId());
        tFaReviewReportService.updateById(tFaReviewReport);
        minioTemplate.removeObject("jljc",newFileName);
        return R.ok();
    }
}
