package com.nantian.cloud.jly.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nantian.cloud.jly.entity.CheckAudit;
import com.nantian.cloud.jly.entity.CheckBasis;
import com.nantian.cloud.jly.entity.EnterpriseHalt;
import com.nantian.cloud.jly.service.CheckBasisService;
import com.nantian.cloud.jly.service.EnterpriseHaltService;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * 企业停产表
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/enterpriseHalt")
@Api(value = "enterpriseHalt", tags = "企业停产表")
public class EnterpriseHaltController {

    private final EnterpriseHaltService enterpriseHaltService;
    private final MinioTemplate minioTemplate;

    /**
     * 企业停产表查询
     * @param epc
     * @return
     */
    @SysLog("企业停产表-企业停产表查询")
    @GetMapping("/getEnterpriseHalt")
    public R getEnterpriseHalt(String epc) {
        return R.ok(enterpriseHaltService.getEnterpriseHalt(epc));
    }

    /**
     * 保存
     *
     * @param enterpriseHalt
     * @return R
     */
    @SysLog("企业停产表-保存")
    @PostMapping("/save")
    public R save(@RequestBody EnterpriseHalt enterpriseHalt) {
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            enterpriseHalt.setUserid(u.getId());
        }
        if (u.getDeptId() != null) {
            enterpriseHalt.setDeptId(u.getDeptId());
        }
        enterpriseHalt.setDelFlag("1");
        enterpriseHalt.setUpdateTime(new Date());
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        enterpriseHalt.setOperatorTime(sdf.format(d));
        return R.ok(enterpriseHaltService.saveOrUpdate(enterpriseHalt));
    }

    /**
     * 恢复生产
     *
     * @param enterpriseHalt
     * @return R
     */
    @SysLog("企业停产表-恢复生产")
    @PostMapping("/renew")
    public R renew(@RequestBody EnterpriseHalt enterpriseHalt) {
        ScUser u = SecurityUtils.getUser();
//        Date date = new Date();
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        if (u.getId() != null) {
            enterpriseHalt.setUserid(u.getId());
        }
        if (u.getDeptId() != null) {
            enterpriseHalt.setDeptId(u.getDeptId());
        }
        enterpriseHalt.setDelFlag("1");
        enterpriseHalt.setUpdateTime(new Date());
//        enterpriseHalt.setRegainTime(new Date());
        return R.ok(enterpriseHaltService.updateById(enterpriseHalt));
    }

    @SysLog("企业停产表-获取某个时间停产的企业")
    @GetMapping("/getHaltCompany")
    @ApiOperation(value = "获取某个时间停产的企业（用于支持dbc和mv工程）", notes = "获取某个时间停产的企业（用于支持dbc和mv工程）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "statDate", value = "时间，如2021-09-01", required = true, dataType = "string", paramType = "string"),
            @ApiImplicitParam(name = "epcStr", value = "企业编码字符串", required = false, dataType = "string", paramType = "string")
    })
    public R getHaltCompany(String statDate, String epcStr) {
        return R.ok(enterpriseHaltService.getHaltCompany(statDate,epcStr));
    }

    /**
     * 查询待审批企业
     * @param state
     * @return
     */
    @SysLog("企业停产表-查询待审批企业")
    @GetMapping("/getHaltByState")
    public R getHaltByState(String state) {
        return R.ok(enterpriseHaltService.getHaltByState(state));
    }


    /**
     * 上传文件
     */
    @SysLog("企业停产表-上传文件")
    @SneakyThrows
    @PostMapping("/uploads")
    @ApiOperation(value = "企业上传文件材料", notes = "企业上传文件材料")
    public R uploads(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);
    }


    @GetMapping("/getHaltStateByEpc")
    @ApiOperation(value = "查询企业停产状态",notes = "查询企业停产状态")
    public R getHaltStateByEpc(String epc) {
        return R.ok(enterpriseHaltService.getHaltstateByEpc(epc));
    }


}
