package com.nantian.cloud.jly.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.CheckBasisFile;
import com.nantian.cloud.jly.entity.CheckEnterprice;
import com.nantian.cloud.jly.service.CheckEnterpriceService;
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.List;
import java.util.UUID;


/**
 * 审查企业列表
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@Api(value = "checkEnterprice", tags = "审查企业列表")
@RequestMapping("/checkEnterprice")
public class CheckEnterpriceController {
    private final CheckEnterpriceService checkEnterpriceService;
    private final MinioTemplate minioTemplate;


    /**
     * 分页查询
     *
     * @param page            分页对象
     * @param checkEnterprice 审查企业
     * @return
     */
    @SysLog("查询审查企业列表")
    @GetMapping("/page")
    @ApiOperation(value = "查询审查企业列表", notes = "条件查询审查企业列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "CheckEnterprice", value = "审查企业列表", required = false, dataType = "json", paramType = "path")
    })
    public R getcheckEnterpricePage(Page page, CheckEnterprice checkEnterprice) {
        checkEnterprice.setDelFlag("1"); //查询未删除数据
        return R.ok(checkEnterpriceService.getPageScope(page, checkEnterprice));
    }

    /**
     * 分页查询企业端用
     *
     * @param page            分页对象
     * @param checkEnterprice 审查企业
     * @return
     */
    @SysLog("查询审查企业列表企业端用")
    @GetMapping("/queryListByCmp")
    @ApiOperation(value = "查询审查企业列表企业端用", notes = "条件查询审查企业列表企业端用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "CheckEnterprice", value = "审查企业列表", required = false, dataType = "json", paramType = "path")
    })
    public R queryListByCmp(Page page, CheckEnterprice checkEnterprice) {
        //checkEnterprice.setDelFlag("1"); //查询未删除数据
        ScUser u = SecurityUtils.getUser();
        return R.ok(checkEnterpriceService.queryListByCmp(page, checkEnterprice));
    }

    @SysLog("查询审查企业列表")
    @GetMapping("/queryCheckVerdict")
    public R queryCheckVerdict(BigInteger checkPlanId, String epc) {
        QueryWrapper<CheckEnterprice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("check_plan_id",checkPlanId);
        queryWrapper.eq("epc",epc);
        List<CheckEnterprice> list = checkEnterpriceService.list(queryWrapper);
        return R.ok(list);
    }


    /**
     * 信息
     *
     * @param checkEnterpriceId
     * @return R
     */
    @SysLog("通过id查询审查企业列表")
    @GetMapping("/{checkEnterpriceId}")
    @ApiOperation(value = "通过id查询审查企业列表", notes = "通过id查询审查企业列表")
    @ApiImplicitParam(name = "checkEnterpriceId", value = "审查企业列表id", required = true, paramType = "path", dataType = "int")
    public R getById(@PathVariable("checkEnterpriceId") Integer checkEnterpriceId) {
        return R.ok(checkEnterpriceService.getById(checkEnterpriceId));
    }

    /**
     * 保存
     *
     * @param checkEnterprice
     * @return R
     */
    @SysLog("保存审查企业列表")
    @PostMapping
    @ApiOperation(value = "保存审查企业列表", notes = "新增审查企业列表")
    @ApiImplicitParam(name = "CheckEnterprice", value = "审查企业列表", required = false, dataType = "json", paramType = "path")
    public R save(@RequestBody CheckEnterprice checkEnterprice) {
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkEnterprice.setUserid(u.getId());
        }
        if (u.getDeptId() != null) {
            checkEnterprice.setDeptId(u.getDeptId());
        }
        checkEnterprice.setDelFlag("1");
        checkEnterprice.setUpdateTime(new Date());
        return R.ok(checkEnterpriceService.save(checkEnterprice));
    }

    /**
     * 修改
     *
     * @param checkEnterprice
     * @return R
     */
    @SysLog("修改审查企业列表")
    @PutMapping
    @ApiOperation(value = "修改审查企业列表", notes = "修改审查企业列表")
    @ApiImplicitParam(name = "CheckEnterprice", value = "审查企业列表", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody CheckEnterprice checkEnterprice) {
        System.out.println(checkEnterprice.toString());
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkEnterprice.setUserid(u.getId());
        }
        checkEnterprice.setUpdateTime(new Date());
        boolean returnData = checkEnterpriceService.updateById(checkEnterprice);

        CheckEnterprice c = checkEnterpriceService.getById(checkEnterprice.getId());
        if ("1".equals(c.getPart1()) && "1".equals(c.getPart2()) && "1".equals(c.getPart3()) && "1".equals(c.getPart4()) && "1".equals(c.getPart5())) {
            checkEnterprice.setCheckStatus("3");
            checkEnterpriceService.updateById(checkEnterprice);
        }
        return R.ok(returnData);
    }

    /**
     * 删除
     *
     * @param checkEnterpriceId
     * @return R
     */
    @SysLog("删除审查企业列表")
    @DeleteMapping("/{checkEnterpriceId}")
    @ApiOperation(value = "删除审查企业列表", notes = "{checkEnterpriceId:审查企业ID}")
    @ApiImplicitParam(name = "checkEnterpriceId", value = "审查企业列表", required = true, dataType = "int", paramType = "path")
    public R removeById(@PathVariable("checkEnterpriceId") Long checkEnterpriceId) {
        CheckEnterprice checkEnterprice = checkEnterpriceService.getById(checkEnterpriceId);
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkEnterprice.setUserid(u.getId());
        }
        checkEnterprice.setDelFlag("4");
        checkEnterprice.setUpdateTime(new Date());
        return R.ok(checkEnterpriceService.updateById(checkEnterprice));
    }

    /**
     * 根据审查计划ID查询企业列表
     *
     * @param checkPlanId 审查计划ID
     * @return R
     */
    @SysLog("根据计划ID查查询计划下所有审查企业")
    @GetMapping("/getCheckEnterpriceByCheckPlanId")
    @ApiOperation(value = "根据计划ID查查询计划下所有审查企业", notes = "根据计划ID查查询计划下所有审查企业")
    @ApiImplicitParam(name = "checkPlanId", value = "审查计划ID", required = true, dataType = "int", paramType = "path")
    public R getCheckEnterpriceByCheckPlanId(@RequestParam String checkPlanId) {
        return R.ok(checkEnterpriceService.getCheckEnterpriceByCheckPlanId(checkPlanId));
    }

    /**
     * 根据审查计划ID查询企业分页列表
     *
     * @param checkPlanId 审查计划ID
     * @return R
     */
    @SysLog("根据审查计划ID查询企业分页列表")
    @GetMapping("/getCheckEnterpricePageDataByCheckPlanId")
    @ApiOperation(value = "根据审查计划ID查询企业分页列表", notes = "{'checkPlanId':审查计划ID}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkPlanId", value = "审查计划ID", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "Page", value = "分页", required = true, dataType = "json", paramType = "path")
    })
    public R getCheckEnterpricePageDataByCheckPlanId(Page page, String checkPlanId, String cmp_name, String epc,String checkStatus) {
        return R.ok(checkEnterpriceService.getCheckEnterpricePageDataByCheckPlanId(page, checkPlanId, cmp_name, epc,checkStatus));
    }

    @SysLog("根据审查计划ID查询企业分页列表")
    @GetMapping("/byCheckPlanId")
    @ApiOperation(value = "根据审查计划ID查询企业分页列表", notes = "{'checkPlanId':审查计划ID}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkPlanId", value = "审查计划ID", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "current", value = "页码", required = true, paramType = "path"),
            @ApiImplicitParam(name = "size", value = "每页显示", required = true, paramType = "path")
    })
    public R byCheckPlanId(BigInteger checkPlanId, long current, long size) {
        Page page = new Page(current, size);
        CheckEnterprice t = new CheckEnterprice();
        t.setCheckPlanId(checkPlanId);
        return R.ok(checkEnterpriceService.page(page, Wrappers.query(t)));
    }

    /**
     * 上传文件材料
     * type为1 时 上传审查报告   type 为2时 上传不符合项文件
     */
    @SysLog("根据审查计划上传文件材料")
    @SneakyThrows
    @PostMapping("/upload")
    public R upload(String type,String uuid, @RequestParam("file") MultipartFile file, CheckEnterprice checkEnterprice) {
        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 = checkEnterpriceService.upload(fileName + "<" + uuid, checkEnterprice,type);

        return flag ? R.ok(fileName + "<" + uuid) : R.failed("文件材料上传失败！");
    }
    /**
     * 上传文件材料
     */
    @SysLog("根据审查计划上传文件材料")
    @SneakyThrows
    @PostMapping("/upload1")
    public R upload1(String uuid, @RequestParam("file") MultipartFile file, CheckEnterprice checkEnterprice) {
        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 = checkEnterpriceService.upload1(fileName + "<" + uuid, checkEnterprice);

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

    @SysLog("修改审查计划企业")
    @PostMapping("/updataCheckEnterprice")
    public R updataCheckEnterprice(@RequestBody CheckEnterprice checkEnterprice){
        return R.ok(checkEnterpriceService.updateById(checkEnterprice));
    }

    @SysLog("修改审查计划企业")
    @PostMapping("/updateEnterprice")
    public R updateEnterprice(@RequestBody List<CheckEnterprice> checkEnterpriceList){
        return R.ok(checkEnterpriceService.updateBatchById(checkEnterpriceList));
    }

    /**
     * 修改审查企业为现场审查
     * @param checkEnterpriceList
     * @return
     */
    @SysLog("修改审查企业为现场审查")
    @PostMapping("/updataCheckType")
    public R updataCheckType(@RequestBody List<CheckEnterprice> checkEnterpriceList){
//        checkEnterpriceList.forEach(r->{
//            r.setCheckType("1");
//        });
        return R.ok(checkEnterpriceService.updateBatchById(checkEnterpriceList));
    }
}
