package com.nantian.cloud.jljc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nantian.cloud.jljc.entity.TFaFixedAssets;
import com.nantian.cloud.jljc.entity.TFaFunctionalOpinion;
import com.nantian.cloud.jljc.entity.TFaProcessControl;
import com.nantian.cloud.jljc.service.TFaFixedAssetsService;
import com.nantian.cloud.jljc.service.TFaProcessControlService;
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.IOException;
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-15
 */
@CrossOrigin
@AllArgsConstructor
@RestController
@RequestMapping("/tFaProcessControl")
public class TFaProcessControlController {

    @Autowired
    MinioTemplate minioTemplate;

    @Autowired
    TFaProcessControlService tFaProcessControlService;

    @Autowired
    TFaFixedAssetsService tFaFixedAssetsService;

    @PostMapping("/fileUpload/{id}")
    @SysLog("根据id上传固定资产投资项目节能审查事中监管表")
    public R fileUpload(@PathVariable Integer id,@RequestParam("file") MultipartFile[] multipartFiles) throws Exception {
        ScUser u = SecurityUtils.getUser();
        if (multipartFiles.length > 0) {
            for (MultipartFile multipartFile : multipartFiles) {
                //获取文件名称
                String fileName = multipartFile.getOriginalFilename();
                //获取文件的后缀
                String extension = "." + FilenameUtils.getExtension(multipartFile.getOriginalFilename());
                //生成新的文件名称
                String newFileName = "SZCC" + 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");
                //文件信息保存到数据库
                TFaProcessControl tFaProcessControl = new TFaProcessControl();
                TFaProcessControl one = tFaProcessControlService.getOne(
                        new QueryWrapper<TFaProcessControl>()
                                .eq("del_flag", 1)
                                .eq("project_id", id));
                tFaProcessControl.setDelFlag(1);
                tFaProcessControl.setExtension(extension);
                tFaProcessControl.setFilePath("jljc/" + newFileName);
                tFaProcessControl.setFileSize(String.valueOf(size));
                tFaProcessControl.setFileType(contentType);
                tFaProcessControl.setNewFileName(newFileName);
                tFaProcessControl.setOldFileName(fileName);
                tFaProcessControl.setDeptId(u.getDeptId());
                tFaProcessControl.setUpdateTime(new Date());
                tFaProcessControl.setUserId(u.getId());
                tFaProcessControl.setUploadTime(new Date());
                tFaProcessControl.setId(one.getId());
                tFaProcessControlService.updateById(tFaProcessControl);
                return R.ok();
            }
            TFaFixedAssets faFixedAssets = tFaFixedAssetsService.getById(id);
            faFixedAssets.setDeptId(u.getDeptId());
            faFixedAssets.setUserId(u.getId());
            faFixedAssets.setStatusData(4);
            faFixedAssets.setUpdateTime(new Date());
            tFaFixedAssetsService.updateById(faFixedAssets);
        } else {
            return R.failed("上传文件为空！");
        }
        return R.ok();
    }

    @GetMapping("/fileDownload/{id}")
    @SysLog("根据id下载固定资产投资项目节能审查事中监管表")
    public R fileDownload(@PathVariable("id") String id) throws IOException {
        TFaProcessControl tFaProcessControl = tFaProcessControlService.getById(id);
        //获取文件名
        String newFileName = tFaProcessControl.getNewFileName();
        //文件下载
        minioTemplate.getObject("jljc",newFileName);
        return R.ok();
    }

    @DeleteMapping("/fileRemove/{id}")
    @SysLog("根据id删除固定资产投资项目节能审查事中监管表")
    public R fileRemove(@PathVariable("id") String id) throws Exception {
        ScUser u = SecurityUtils.getUser();
        TFaProcessControl tFaProcessControl = tFaProcessControlService.getById(id);
        //获取文件名
        String newFileName = tFaProcessControl.getNewFileName();
        tFaProcessControl.setUserId(u.getId());
        tFaProcessControl.setDelFlag(4);
        tFaProcessControl.setUpdateTime(new Date());
        tFaProcessControl.setDeptId(u.getDeptId());
        tFaProcessControlService.updateById(tFaProcessControl);
        //文件删除
        minioTemplate.removeObject("jljc",newFileName);
        return R.ok();
    }

    @GetMapping("/setPro/{id}")
    @SysLog("根据id更新固定资产投资项目节能审查事中监管表")
    public R setPro(@PathVariable Integer id){
        ScUser u = SecurityUtils.getUser();
        TFaProcessControl tFaProcessControl = new TFaProcessControl();
        tFaProcessControl.setDelFlag(1);
        tFaProcessControl.setDeptId(u.getDeptId());
        tFaProcessControl.setUserId(u.getId());
        tFaProcessControl.setUpdateTime(new Date());
        tFaProcessControl.setProjectId(id);
        tFaProcessControlService.save(tFaProcessControl);
        return R.ok();
    }

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

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

    @PutMapping("/update")
    @SysLog("更新固定资产投资项目节能审查事中监管表")
    public R update(@RequestBody TFaProcessControl tFaProcessControl){
        ScUser u = SecurityUtils.getUser();
        TFaProcessControl one = tFaProcessControlService.getOne(new QueryWrapper<TFaProcessControl>()
                .eq("del_flag", 1).eq("project_id", tFaProcessControl.getProjectId()));
        tFaProcessControl.setUserId(u.getId());
        tFaProcessControl.setId(one.getId());
        tFaProcessControl.setUpdateTime(new Date());
        tFaProcessControl.setDeptId(u.getId());
        tFaProcessControlService.updateById(tFaProcessControl);
        return R.ok();
    }

    @GetMapping("/getInfo/{id}")
    @SysLog("根据id获取固定资产投资项目节能审查事中监管表")
    public R getInfo(@PathVariable Integer id){

        TFaProcessControl tFaProcessControl = tFaProcessControlService.getOne(
                new QueryWrapper<TFaProcessControl>()
                .eq("del_flag", 1)
                .eq("project_id", id));
        return R.ok(tFaProcessControl
        );
    }
}
