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.CheckGaugesReport;
import com.nantian.cloud.jly.entity.InspectionData;
import com.nantian.cloud.jly.service.CheckGaugesReportService;
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.beans.factory.annotation.Autowired;
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;


/**
 * 能源计量器具配备情况统计汇总表B.8
 * @date 2020-09-14 12:01:57
 */
@RestController
@AllArgsConstructor
@Api(value = "checkGaugesReport",tags = "能源计量器具配备情况统计汇总表B.8")
@RequestMapping("/checkGaugesReport")
public class CheckGaugesReportController {
    @Autowired
    private CheckGaugesReportService checkGaugesReportService;
    @Autowired
    private final InspectionDataService inspectionDataService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param checkGaugesReport 能源计量器具配备情况统计汇总表B.8
     * @return
     */
    @SysLog("查询能源计量器具配备情况统计汇总表B.8")
    @ApiOperation(value = "查询能源计量器具配备情况统计汇总表B.8",notes = "条件查询能源计量器具配备情况统计汇总表B.8")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkGaugesReport", value = "能源计量器具配备情况统计汇总表B.8", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path")
    })
    @GetMapping("/page")
    public R getCheckGaugesReportService(Page page, CheckGaugesReport checkGaugesReport) {
        checkGaugesReport.setDelFlag("1"); //查询未删除数据
        return R.ok(checkGaugesReportService.getPageScope(page,checkGaugesReport));
        //return R.ok(checkGaugesReportService.page(page, Wrappers.query(checkGaugesReport)));
    }




    /**
     * 信息
     *
     * @param checkGaugesReportId
     * @return R
     */
    @SysLog("通过ID查询能源计量器具配备情况统计汇总表B.8")
    @ApiOperation(value = "通过ID查询能源计量器具配备情况统计汇总表B.8",notes = "通过ID查询能源计量器具配备情况统计汇总表B.8")
    @ApiImplicitParam(name ="checkGaugesReportId",value = "能源计量器具配备情况统计汇总表B.8id",required = true,paramType = "path",dataType = "int")
    @GetMapping("/{checkGaugesReportId}")
    public R getById(@PathVariable("checkGaugesReportId") Integer checkGaugesReportId) {
        return R.ok(checkGaugesReportService.getById(checkGaugesReportId));
    }

    /**
     * 保存
     *
     * @param checkGaugesReport
     * @return R
     */
    @SysLog("保存能源计量器具配备情况统计汇总表B.8")
    @ApiOperation(value = "保存能源计量器具配备情况统计汇总表B.8",notes = "新增能源计量器具配备情况统计汇总表B.8")
    @ApiImplicitParam(name ="checkGaugesReport",value = "能源计量器具配备情况统计汇总表B.8",required = true,dataType = "json",paramType = "path")
    @PostMapping("/save")
    public R save(@RequestBody CheckGaugesReport checkGaugesReport) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkGaugesReport.setUserid(u.getId());
        }
        checkGaugesReport.setDeptId(u.getDeptId());
        checkGaugesReport.setDelFlag("1");
        checkGaugesReport.setUpdateTime(new Date());
        return R.ok(checkGaugesReportService.save(checkGaugesReport));
    }

    /**
     * 批量保存
     *
     * @param checkGaugesReportList
     * @return R
     */
    @SysLog("保存能源计量器具配备情况统计汇总表B.8")
    @ApiOperation(value = "保存能源计量器具配备情况统计汇总表B.8",notes = "新增能源计量器具配备情况统计汇总表B.8")
    @ApiImplicitParam(name ="CheckGauges",value = "能源计量器具配备情况统计汇总表B.8",required = true,dataType = "json",paramType = "path")
    @PostMapping("/saveList")
    public R saveList(@RequestBody List<CheckGaugesReport> checkGaugesReportList) {
        ScUser u= SecurityUtils.getUser();
        List<CheckGaugesReport> newList = new ArrayList<>();
        for (CheckGaugesReport checkGaugesReport : checkGaugesReportList) {
            if(u.getId()!=null){
                checkGaugesReport.setUserid(u.getId());
            }
            checkGaugesReport.setDelFlag("1");
            checkGaugesReport.setUpdateTime(new Date());
            checkGaugesReport.setDeptId(u.getDeptId());
            newList.add(checkGaugesReport);
        }
        return R.ok(checkGaugesReportService.saveBatch(newList));
    }

    /**
     * 导入excel
     */
    @SysLog("能源计量器具配备情况统计汇总表B.8导入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(checkPlanId==null){
            return R.failed("审查计划id不能为空！");
        }
        if(epc==null||"".equals(epc)){
            return R.failed("企业社会信用编码不能为空！");
        }
        InspectionData inspectionData = inspectionDataService.getByPlanIdAndEpc(checkPlanId, epc);
        if (inspectionData == null) {
            return R.failed("没有查询到自查18项资料记录");
        }else{
            if ("Y".equals(inspectionData.getEquippedSituationState())) {  //已审核
                return R.failed("能源计量器具配备情况统计汇总表已审核，不能进行增加、修改、删除操作！");
            }
        }
        return checkGaugesReportService.excelImport(file,checkPlanId,epc,inspectionData);
    }

    /**
     * 批量新增
     */
    @SysLog("能源计量器具配备情况统计汇总表B.8批量新增")
    @PostMapping("/saveBatch")
    @ApiOperation(value = "批量新增能源计量器具配备情况统计汇总表B.8", notes = "批量新增能源计量器具配备情况统计汇总表B.8")
    @ApiImplicitParam(name = "checkGaugesReports", value = "能源计量器具配备情况统计汇总表B.8集合", required = true, dataType = "json", paramType = "path")
    public R saveBatch(@RequestBody String s) {
        JSONObject json = JSONObject.parseObject(s);
        String ss = json.getString("checkGaugesReport");
        String mend = json.getString("mend");
        List<CheckGaugesReport> checkGaugesReportList = (List<CheckGaugesReport>) JSONArray.parseArray(ss, CheckGaugesReport.class);
        //ist<CheckGauges> checkGaugesLis
        ScUser u= SecurityUtils.getUser();
        List<CheckGaugesReport> newList = new ArrayList<>();
        Long checkPlanId =null;
        String enterpriseCode=null;
        for (CheckGaugesReport checkGaugesReport : checkGaugesReportList) {
            if(u.getId()!=null){
                checkGaugesReport.setUserid(u.getId());
            }
            checkGaugesReport.setDelFlag("1");
            checkGaugesReport.setUpdateTime(new Date());
            checkGaugesReport.setDeptId(u.getDeptId());
            newList.add(checkGaugesReport);
            checkPlanId=checkGaugesReport.getCheckPlanId();
            enterpriseCode=checkGaugesReport.getEpc();
        }
        InspectionData inspectionData = new InspectionData();
        inspectionData.setCheckPlanId(BigInteger.valueOf(checkPlanId));
        inspectionData.setEnterpriseCode(enterpriseCode);
        inspectionData.setDelFlag("1");
        InspectionData oldData = inspectionDataService.getOne(Wrappers.query(inspectionData));
        if(oldData==null){
            return R.failed("没有查询到自查18项资料记录");
        }else{
            inspectionData.setId(oldData.getId());
            inspectionData.setEquippedSituation("Y");
            if ("Z".equals(mend)){
                inspectionData.setEquippedSituationState("Z");
            }
            inspectionDataService.updateById(inspectionData);
        }
        return R.ok(checkGaugesReportService.saveOrUpdateBatch(newList));
    }

    /**
     * 修改
     *
     * @param checkGaugesReport
     * @return R
     */
    @SysLog("修改能源计量器具配备情况统计汇总表B.8")
    @ApiOperation(value = "修改能源计量器具配备情况统计汇总表B.8",notes = "修改能源计量器具配备情况统计汇总表B.8")
    @ApiImplicitParam(name ="checkGaugesReport",value = "能源计量器具配备情况统计汇总表B.8",required = true,dataType = "json",paramType = "path")
    @PutMapping
    public R updateById(@RequestBody CheckGaugesReport checkGaugesReport) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkGaugesReport.setUserid(u.getId());
        }
        checkGaugesReport.setUpdateTime(new Date());
        return R.ok(checkGaugesReportService.updateById(checkGaugesReport));
    }

    /**
     * 删除
     *
     * @param checkGaugesReportId
     * @return R
     */
    @SysLog("删除能源计量器具配备情况统计汇总表B.8")
    @ApiOperation(value = "删除能源计量器具配备情况统计汇总表B.8",notes = "删除能源计量器具配备情况统计汇总表B.8")
    @ApiImplicitParam(name ="checkAbnormalReportId",value = "能源计量器具配备情况统计汇总表B.8ID",required = true,dataType = "int",paramType = "path")
    @DeleteMapping("/{checkGaugesReportId}")
    public R removeById(@PathVariable("checkGaugesReportId") Long checkGaugesReportId) {
        CheckGaugesReport checkGaugesReport = checkGaugesReportService.getById(checkGaugesReportId);
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkGaugesReport.setUserid(u.getId());
        }
        checkGaugesReport.setDelFlag("4");
        checkGaugesReport.setUpdateTime(new Date());
        return R.ok(checkGaugesReportService.updateById(checkGaugesReport));
    }
}
