package com.nantian.cloud.jly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.CheckBasisFile;
import com.nantian.cloud.jly.entity.CheckPlan;
import com.nantian.cloud.jly.service.CheckBasisFileService;
import com.nantian.cloud.jly.service.CheckPlanService;
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.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.math.BigInteger;
import java.util.Date;
import java.util.UUID;


/**
 * 审查计划表
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("/checkBasisFile")
@Api(value = "checkBasisFile", tags = "审查依据表")
public class CheckBasisFileController {

    private final CheckBasisFileService checkBasisFileService;

    private final MinioTemplate minioTemplate;


    /**
     * 分页查询
     *
     * @param page      分页对象
     * @return
     */
    @SysLog("查询审查依据表")
    @GetMapping("/page")
    @ApiOperation(value = "查询审查计划表", notes = "条件查询审查计划表")
    public R getcheckPlanPage(Page page, CheckBasisFile checkBasisFile) {
        checkBasisFile.setDelFlag("1"); //查询未删除数据
        return R.ok(checkBasisFileService.selectAll(page,checkBasisFile));
    }



    /**
     * 保存
     *
     * @param checkPlan
     * @return R
     */
    @SysLog("保存审查依据表")
    @PostMapping
    @ApiOperation(value = "保存审查计划表", notes = "新增审查计划表")
    @ApiImplicitParam(name = "CheckPlan", value = "审查计划表", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody CheckPlan checkPlan) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkPlan.setUserid(u.getId());
        }
        if (u.getDeptId() != null) {
            checkPlan.setDeptId(u.getDeptId());
        }
        checkPlan.setDelFlag("1");
        checkPlan.setUpdateTime(new Date());
        return R.ok();
//        return R.ok(checkPlanService.save(checkPlan));
    }


    /**
     * 修改
     *
     * @param checkPlan
     * @return R
     */
    @SysLog("修改审查依据表")
    @PutMapping
    @ApiOperation(value = "修改审查计划表", notes = "修改审查计划表")
    public R updateById(@RequestBody CheckPlan checkPlan) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkPlan.setUserid(u.getId());
        }
        checkPlan.setUpdateTime(new Date());
        return R.ok();
//        return R.ok(checkPlanService.updateById(checkPlan));
    }


    /**
     * 上传文件材料
     */
    @SysLog("审查依据表上传文件")
    @SneakyThrows
    @PostMapping("/upload")
//    @ApiOperation(value = "企业上传文件材料", notes = "企业上传文件材料")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "uuid", value = "uuid", required = false, 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"),
//    })
    public R upload(String uuid, @RequestParam("file") MultipartFile file, CheckBasisFile checkBasisFile) {
        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");

        //fileName + "<" + uuid   文件名、uuid的拼接
        boolean flag = checkBasisFileService.upload(fileName + "<" + uuid, checkBasisFile);

        return flag ? R.ok(fileName + "<" + uuid) : R.failed("文件材料上传失败！");
    }

    /**
     * 上传能源计量审查规范记录表文件材料
     */
    @SysLog("上传能源计量审查规范记录表文件材料")
    @SneakyThrows
    @PostMapping("/upload1")
    public R upload1(String uuid, @RequestParam("file") MultipartFile file, CheckBasisFile checkBasisFile) {
        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");

        //fileName + "<" + uuid   文件名、uuid的拼接
        boolean flag = checkBasisFileService.upload1(fileName + "<" + uuid, checkBasisFile);

        return flag ? R.ok(fileName + "<" + uuid) : R.failed("文件材料上传失败！");
    }

//    /**
//     * 删除
//     *
//     * @param checkPlanId
//     * @return R
//     */
//    @DeleteMapping("/{checkPlanId}")
//    @ApiOperation(value = "删除审查计划表", notes = "删除审查计划表")
//    @ApiImplicitParam(name = "checkPlanId", value = "审查计划表id", required = true, dataType = "int", paramType = "path")
//    public R removeById(@PathVariable("checkPlanId") Long checkPlanId) {
//        CheckPlan checkPlan = checkPlanService.getById(checkPlanId);
//        ScUser u= SecurityUtils.getUser();
//        if(u.getId()!=null){
//            checkPlan.setUserid(u.getId());
//        }
//        checkPlan.setDelFlag("4");
//        checkPlan.setUpdateTime(new Date());
//        return R.ok(checkPlanService.updateById(checkPlan));
//    }

}
