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.CheckLevelDegreeReport;
import com.nantian.cloud.jly.entity.InspectionData;
import com.nantian.cloud.jly.service.CheckLevelDegreeReportService;
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;

@RestController
@AllArgsConstructor
@Api(value = "checkLevelDegreeReport",tags = "能源计量器具准确度等级统计汇总表B9")
@RequestMapping("/checkLevelDegreeReport")
public class CheckLevelDegreeReportController {
    @Autowired
    private CheckLevelDegreeReportService checkLevelDegreeReportService;

    @Autowired
    private InspectionDataService inspectionDataService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param checkLevelDegreeReport 能源计量器具准确度等级统计汇总表B9
     * @return
     */
    @SysLog("能源计量器具准确度等级统计汇总表B9-分页查询")
    @ApiOperation(value = "查询能源计量器具准确度等级统计汇总表B9",notes = "条件查询能源计量器具准确度等级统计汇总表B9")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checkGaugesReport", value = "能源计量器具准确度等级统计汇总表B9", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "Page", value = "分页对象", required = false, dataType = "json", paramType = "path")
    })
    @GetMapping("/page")
    public R getCheckGaugesReportService(Page page, CheckLevelDegreeReport checkLevelDegreeReport) {
        checkLevelDegreeReport.setDelFlag("1"); //查询未删除数据
        return R.ok(checkLevelDegreeReportService.getPageScope(page,checkLevelDegreeReport));
        //return R.ok(checkGaugesService.page(page, Wrappers.query(checkGauges)));
    }




    /**
     * 信息
     *
     * @param checkLevelDegreeReportId
     * @return R
     */
    @SysLog("能源计量器具准确度等级统计汇总表B9-查询")
    @ApiOperation(value = "通过ID查询能源计量器具准确度等级统计汇总表B9",notes = "通过ID查询能源计量器具准确度等级统计汇总表B9")
    @ApiImplicitParam(name ="checkGaugesReportId",value = "能源计量器具准确度等级统计汇总表B9id",required = true,paramType = "path",dataType = "int")
    @GetMapping("/{checkGaugesReportId}")
    public R getById(@PathVariable("checkGaugesReportId") Integer checkLevelDegreeReportId) {
        return R.ok(checkLevelDegreeReportService.getById(checkLevelDegreeReportId));
    }

    /**
     * 保存
     *
     * @param checkLevelDegreeReport
     * @return R
     */
    @SysLog("能源计量器具准确度等级统计汇总表B9-保存")
    @ApiOperation(value = "保存能源计量器具准确度等级统计汇总表B9",notes = "新增能源计量器具准确度等级统计汇总表B9")
    @ApiImplicitParam(name ="checkLevelDegreeReport",value = "能源计量器具准确度等级统计汇总表B9",required = true,dataType = "json",paramType = "path")
    @PostMapping("/save")
    public R save(@RequestBody CheckLevelDegreeReport checkLevelDegreeReport) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkLevelDegreeReport.setUserid(u.getId());
        }
        checkLevelDegreeReport.setDeptId(u.getDeptId());
        checkLevelDegreeReport.setDelFlag("1");
        checkLevelDegreeReport.setUpdateTime(new Date());
        return R.ok(checkLevelDegreeReportService.save(checkLevelDegreeReport));
    }

    /**
     * 批量保存
     *
     * @param checkLevelDegreeReportList
     * @return R
     */
    @SysLog("能源计量器具准确度等级统计汇总表B9-批量保存")
    @ApiOperation(value = "保存能源计量器具准确度等级统计汇总表B9",notes = "新增能源计量器具准确度等级统计汇总表B9")
    @ApiImplicitParam(name ="CheckGauges",value = "能源计量器具准确度等级统计汇总表B9",required = true,dataType = "json",paramType = "path")
    @PostMapping("/saveList")
    public R saveList(@RequestBody List<CheckLevelDegreeReport> checkLevelDegreeReportList) {
        ScUser u= SecurityUtils.getUser();
        List<CheckLevelDegreeReport> newList = new ArrayList<>();
        for (CheckLevelDegreeReport checkLevelDegreeReport : checkLevelDegreeReportList) {
            if(u.getId()!=null){
                checkLevelDegreeReport.setUserid(u.getId());
            }
            checkLevelDegreeReport.setDelFlag("1");
            checkLevelDegreeReport.setUpdateTime(new Date());
            checkLevelDegreeReport.setDeptId(u.getDeptId());
            newList.add(checkLevelDegreeReport);
        }
        return R.ok(checkLevelDegreeReportService.saveBatch(newList));
    }

    /**
     * 批量修改
     *
     * @param checkLevelDegreeReportList
     * @return R
     */
    @SysLog("能源计量器具准确度等级统计汇总表B9-批量修改")
    @ApiOperation(value = "保存能源计量器具准确度等级统计汇总表B9",notes = "新增能源计量器具准确度等级统计汇总表B9")
    @ApiImplicitParam(name ="CheckGauges",value = "能源计量器具准确度等级统计汇总表B9",required = true,dataType = "json",paramType = "path")
    @PostMapping("/updataList")
    public synchronized R updataList(@RequestBody List<CheckLevelDegreeReport> checkLevelDegreeReportList) {
        List<CheckLevelDegreeReport> newList = new ArrayList<>();
        for (CheckLevelDegreeReport checkLevelDegreeReport : checkLevelDegreeReportList) {
            CheckLevelDegreeReport byId = checkLevelDegreeReportService.getById(checkLevelDegreeReport.getId());
            if (checkLevelDegreeReport.getInoutDegree() != null && checkLevelDegreeReport.getInoutDegree() != ""){
                byId.setInoutDegree(checkLevelDegreeReport.getInoutDegree());
                byId.setInoutLevel(checkLevelDegreeReport.getInoutLevel());
                newList.add(byId);
            }
            if (checkLevelDegreeReport.getSecondaryDegree() != null && checkLevelDegreeReport.getSecondaryDegree() != ""){
                byId.setSecondaryLevel(checkLevelDegreeReport.getSecondaryLevel());
                byId.setSecondaryDegree(checkLevelDegreeReport.getSecondaryDegree());
                newList.add(byId);
            }
            if (checkLevelDegreeReport.getMainDegree() != null && checkLevelDegreeReport.getMainDegree() != ""){
                byId.setMainLevel(checkLevelDegreeReport.getMainLevel());
                byId.setMainDegree(checkLevelDegreeReport.getMainDegree());
                newList.add(byId);
            }
        }
        return R.ok(checkLevelDegreeReportService.updateBatchById(newList));
    }


    /**
     * 导入excel
     */
    @SysLog("能源计量器具准确度等级统计汇总表B9-导入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.getAccuracyGradeState())) {  //已审核
                return R.failed("能源计量器具准确度等级统计汇总表已审核，不能进行增加、修改、删除操作！");
            }
        }else{
            return R.failed("没有查询到自查18项资料记录");
        }
        return checkLevelDegreeReportService.excelImport(file,checkPlanId,epc,inspectionData);
    }

    /**
     * 批量新增
     */
    @SysLog("能源计量器具准确度等级统计汇总表B9-批量新增")
    @PostMapping("/saveBatch")
    @ApiOperation(value = "批量新增能源计量器具准确度等级统计汇总表B9", notes = "批量新增能源计量器具准确度等级统计汇总表B9")
    @ApiImplicitParam(name = "checkGaugesReports", value = "能源计量器具准确度等级统计汇总表B9集合", required = true, dataType = "json", paramType = "path")
    public R saveBatch(@RequestBody String s) {
        JSONObject json = JSONObject.parseObject(s);
        String ss = json.getString("checkLevelDegreeReports");
        String mend = json.getString("mend");
        List<CheckLevelDegreeReport> checkLevelDegreeReportList = (List<CheckLevelDegreeReport>) JSONArray.parseArray(ss, CheckLevelDegreeReport.class);
        //ist<CheckGauges> checkGaugesLis
        ScUser u= SecurityUtils.getUser();
        List<CheckLevelDegreeReport> newList = new ArrayList<>();
        Long checkPlanId =null;
        String enterpriseCode=null;
        for (CheckLevelDegreeReport checkGaugesReport : checkLevelDegreeReportList) {
            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){
            inspectionData.setAccuracyGrade("Y");
            inspectionData.setAccuracyGradeState("D");
            inspectionDataService.save(inspectionData);
//            return R.failed("没有查询到自查18项资料记录");
        }else{
            inspectionData.setId(oldData.getId());
            inspectionData.setAccuracyGrade("Y");
            inspectionData.setAccuracyGradeState("D");
            if ("Z".equals(mend)){
                inspectionData.setAccuracyGradeState("Z");
            }
            inspectionDataService.updateById(inspectionData);
        }
        return R.ok(checkLevelDegreeReportService.saveOrUpdateBatch(newList));
    }

    /**
     * 修改
     *
     * @param checkLevelDegreeReport
     * @return R
     */
    @SysLog("能源计量器具准确度等级统计汇总表B9-修改")
    @ApiOperation(value = "修改能源计量器具准确度等级统计汇总表B9",notes = "修改能源计量器具准确度等级统计汇总表B9")
    @ApiImplicitParam(name ="checkGaugesReport",value = "能源计量器具准确度等级统计汇总表B9",required = true,dataType = "json",paramType = "path")
    @PutMapping
    public R updateById(@RequestBody CheckLevelDegreeReport checkLevelDegreeReport) {
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkLevelDegreeReport.setUserid(u.getId());
        }
        checkLevelDegreeReport.setUpdateTime(new Date());
        return R.ok(checkLevelDegreeReportService.updateById(checkLevelDegreeReport));
    }

    /**
     * 删除
     *
     * @param checkLevelDegreeReportId
     * @return R
     */
    @SysLog("能源计量器具准确度等级统计汇总表B9-删除")
    @ApiOperation(value = "删除能源计量器具准确度等级统计汇总表B9",notes = "删除能源计量器具准确度等级统计汇总表B9")
    @ApiImplicitParam(name ="checkAbnormalReportId",value = "能源计量器具准确度等级统计汇总表B9ID",required = true,dataType = "int",paramType = "path")
    @DeleteMapping("/{checkGaugesId}")
    public R removeById(@PathVariable("checkGaugesId") Long checkLevelDegreeReportId) {
        CheckLevelDegreeReport checkLevelDegreeReport = checkLevelDegreeReportService.getById(checkLevelDegreeReportId);
        ScUser u= SecurityUtils.getUser();
        if(u.getId()!=null){
            checkLevelDegreeReport.setUserid(u.getId());
        }
        checkLevelDegreeReport.setDelFlag("4");
        checkLevelDegreeReport.setUpdateTime(new Date());
        return R.ok(checkLevelDegreeReportService.updateById(checkLevelDegreeReport));
    }

}
