package com.nantian.cloud.jly.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.CheckStore;
import com.nantian.cloud.jly.entity.InspectionData;
import com.nantian.cloud.jly.service.CheckStoreService;
import com.nantian.cloud.jly.service.InspectionDataService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
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 org.apache.xmlbeans.impl.piccolo.io.FileFormatException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 年度重点用能单位能源购进、消费与库存情况表
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@RestController
@AllArgsConstructor
@RequestMapping("/checkStore")
@Api(value = "checkStore", tags = "年度重点用能单位能源购进、消费与库存情况表")
public class CheckStoreController {
    private final CheckStoreService checkStoreService;
    private final InspectionDataService inspectionDataService;


    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param checkStore 年度重点用能单位能源购进、消费与库存情况表
     * @return
     */
    @SysLog("年度重点用能单位能源购进、消费与库存情况表-分页查询")
    @ApiOperation(value = "查询年度重点用能单位能源购进、消费与库存情况表", notes = "条件查询年度重点用能单位能源购进、消费与库存情况表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "CheckStore", value = "年度重点用能单位能源购进、消费与库存情况表", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path")
    })
    @GetMapping("/page")
    public R getcheckStorePage(Page page, CheckStore checkStore) {
        checkStore.setDelFlag("1"); //查询未删除数据
        return R.ok(checkStoreService.page(page, Wrappers.query(checkStore)));
    }


    /**
     * 信息
     *
     * @param checkStoreId
     * @return R
     */
    @SysLog("年度重点用能单位能源购进、消费与库存情况表-查询")
    @ApiOperation(value = "查询年度重点用能单位能源购进、消费与库存情况表", notes = "条件查询年度重点用能单位能源购进、消费与库存情况表")
    @ApiImplicitParam(name = "checkStoreId", value = "年度重点用能单位能源购进、消费与库存情况表id", required = true, dataType = "int", paramType = "path")
    @GetMapping("/{checkStoreId}")
    public R getById(@PathVariable("checkStoreId") Integer checkStoreId) {
        return R.ok(checkStoreService.getById(checkStoreId));
    }

    /**
     * 保存
     *
     * @param checkStore
     * @return R
     */
    @SysLog("年度重点用能单位能源购进、消费与库存情况表-保存")
    @ApiOperation(value = "保存年度重点用能单位能源购进、消费与库存情况表", notes = "新增年度重点用能单位能源购进、消费与库存情况表")
    @ApiImplicitParam(name = "CheckStore", value = "年度重点用能单位能源购进、消费与库存情况表", required = true, dataType = "JSON", paramType = "path")
    @PostMapping
    public R save(@RequestBody CheckStore checkStore) {
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkStore.setUserid(u.getId());
        }
        if (u.getDeptId() != null) {
            checkStore.setDeptId(u.getDeptId());
        }
        checkStore.setDelFlag("1");
        checkStore.setUpdateTime(new Date());
        return R.ok(checkStoreService.save(checkStore));
    }

    /**
     * 导入excel
     */
    @SysLog("年度重点用能单位能源购进、消费与库存情况表-导入excel")
    @PostMapping("excelImport")
    @ApiOperation(value = "导入excel", notes = "导入excel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file", paramType = "path"),
            @ApiImplicitParam(name = "checkPlanId", value = "审查计划id", required = true, dataType = "string", paramType = "path"),
            @ApiImplicitParam(name = "epc", value = "企业社会信用编码", required = true, dataType = "string", paramType = "path")
    })
    public R excelImport(MultipartFile file, BigInteger checkPlanId, String epc) throws FileFormatException {
        if (epc == null || "".equals(epc)) {
            return R.failed("企业社会信用编码不能为空！");
        }
        if (checkPlanId == null) {
            return R.failed("审查计划id不能为空！");
        }
        InspectionData inspectionData = inspectionDataService.getByPlanIdAndEpc(checkPlanId, epc);
        if (inspectionData != null) {
            if ("Y".equals(inspectionData.getInventoryStatusState())) {  //已审核
                return R.failed("年度重点用能单位能源购进、消费与库存表已审核，不能进行增加、修改、删除操作！");
            }
        } else {
            return R.failed("没有查询到自查18项资料记录");
        }
        return checkStoreService.excelImport(file, checkPlanId, epc, inspectionData);
    }

    /**
     * 批量新增
     */
    @SysLog("年度重点用能单位能源购进、消费与库存情况表-批量新增")
    @PostMapping("/saveBatch")
    @ApiOperation(value = "年度重点用能单位能源购进、消费与库存", notes = "年度重点用能单位能源购进、消费与库存")
    @ApiImplicitParam(name = "checkStores", value = "年度重点用能单位能源购进、消费与库存对象集合", required = true, dataType = "json", paramType = "path")
    public R saveBatch(@RequestBody String s) {
        JSONObject json = JSONObject.parseObject(s);
        String ss = json.getString("checkStores");
        String mend = json.getString("mend");
        List<CheckStore> checkGaugesList = (List<CheckStore>) JSONArray.parseArray(ss, CheckStore.class);
        //ist<CheckGauges> checkGaugesLis
        ScUser u = SecurityUtils.getUser();
        List<CheckStore> newList = new ArrayList<>();
        BigInteger checkPlanId = null;
        String enterpriseCode = null;
        for (CheckStore checkStore : checkGaugesList) {
            if (u.getId() != null) {
                checkStore.setUserid(u.getId());
            }
            checkPlanId = checkStore.getCheckPlanId();
            enterpriseCode = checkStore.getEpc();
            checkStore.setDelFlag("1");
            checkStore.setUpdateTime(new Date());
            checkStore.setDeptId(u.getDeptId());
            if (checkStore.getUseTotal() != null && checkStore.getModulus() != null) {
                checkStore.setMop(String.valueOf(Float.parseFloat(checkStore.getUseTotal()) * Float.parseFloat(checkStore.getModulus())));
            }
//            if(checkStore.getUseProduct() != null && checkStore.getUseProduct() != ""){
                newList.add(checkStore);
//            }
        }

        InspectionData inspectionData = new InspectionData();
        inspectionData.setCheckPlanId(checkPlanId);
        inspectionData.setEnterpriseCode(enterpriseCode);
        inspectionData.setDelFlag("1");
        InspectionData oldData = inspectionDataService.getOne(Wrappers.query(inspectionData));
        inspectionData.setInventoryStatus("Y");
        if (oldData == null) {
            //return R.failed("没有查询到自查18项资料记录");
            if (u.getId() != null) {
                inspectionData.setUserid(u.getId());
            }
            inspectionDataService.save(inspectionData);
        } else {
            inspectionData.setId(oldData.getId());
            if ("Z".equals(mend)){
                inspectionData.setInventoryStatusState("Z");
            }
            inspectionDataService.updateById(inspectionData);
        }
        return R.ok(checkStoreService.saveOrUpdateBatch(newList));
    }

    /**
     * 修改
     *
     * @param checkStore
     * @return R
     */
    @SysLog("年度重点用能单位能源购进、消费与库存情况表-修改")
    @ApiOperation(value = "修改年度重点用能单位能源购进、消费与库存情况表", notes = "修改年度重点用能单位能源购进、消费与库存情况表")
    @ApiImplicitParam(name = "CheckStore", value = "年度重点用能单位能源购进、消费与库存情况表", required = true, dataType = "JSON", paramType = "path")
    @PutMapping
    public R updateById(@RequestBody CheckStore checkStore) {
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkStore.setUserid(u.getId());
        }
        checkStore.setUpdateTime(new Date());
        return R.ok(checkStoreService.updateById(checkStore));
    }

    /**
     * 删除
     *
     * @param checkStoreId
     * @return R
     */
    @SysLog("年度重点用能单位能源购进、消费与库存情况表-删除")
    @ApiOperation(value = "删除年度重点用能单位能源购进、消费与库存情况表", notes = "删除年度重点用能单位能源购进、消费与库存情况表")
    @ApiImplicitParam(name = "checkStoreId", value = "年度重点用能单位能源购进、消费与库存情况表id", required = true, dataType = "int", paramType = "path")
    @DeleteMapping("/{checkStoreId}")
    public R removeById(@PathVariable("checkStoreId") Long checkStoreId) {
        CheckStore checkStore = checkStoreService.getById(checkStoreId);
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkStore.setUserid(u.getId());
        }
        checkStore.setDelFlag("4");
        checkStore.setUpdateTime(new Date());
        return R.ok(checkStoreService.updateById(checkStore));
    }

}
