package com.nantian.cloud.jly.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.CheckGaugesTypeReport;
import com.nantian.cloud.jly.entity.InspectionData;
import com.nantian.cloud.jly.service.CheckGaugesTypeReportService;
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.annotation.Inner;
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.*;
import java.util.stream.Collectors;


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

    /**
     * 分页查询
     *
     * @param page                  分页对象
     * @param checkGaugesTypeReport 能源计量器具配备情况统计汇总表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, CheckGaugesTypeReport checkGaugesTypeReport) {
        checkGaugesTypeReport.setDelFlag("1"); //查询未删除数据
        IPage pageScope = checkGaugesTypeReportService.getPageScope(page, checkGaugesTypeReport);
        return R.ok(pageScope);
        //return R.ok(checkGaugesService.page(page, Wrappers.query(checkGauges)));
    }

    /**
     * 表8续表排序、查询接口
     * @param checkGaugesTypeReport
     * @return
     */
//    @Inner(value = false)
    @SysLog("能源计量表8续表排序、查询接口")
    @GetMapping("/getInfoList")
    public R getInfoList(CheckGaugesTypeReport checkGaugesTypeReport){
        List<CheckGaugesTypeReport> list = checkGaugesTypeReportService.getInfoList(checkGaugesTypeReport);
        return R.ok(list);
    }

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

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

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

    /**
     * 批量更新
     *
     * @param checkGaugesTypeReportList
     * @return R
     */
    @SysLog("批量更新能源计量器具配备情况统计汇总表B.8续")
    @ApiOperation(value = "保存能源计量器具配备情况统计汇总表B.8续", notes = "新增能源计量器具配备情况统计汇总表B.8续")
    @ApiImplicitParam(name = "checkGaugesTypeReport", value = "能源计量器具配备情况统计汇总表B.8续", required = true, dataType = "json", paramType = "path")
    @PostMapping("/updataList")
    public synchronized R updataList(@RequestBody List<CheckGaugesTypeReport> checkGaugesTypeReportList) {
        ScUser u = SecurityUtils.getUser();
        List<CheckGaugesTypeReport> newList = new ArrayList<>();
        for (CheckGaugesTypeReport checkGaugesTypeReport : checkGaugesTypeReportList) {
            if (checkGaugesTypeReport.getId() != null) {
                CheckGaugesTypeReport byId = checkGaugesTypeReportService.getById(checkGaugesTypeReport.getId());
                if (checkGaugesTypeReport.getInoutShouldMatch() != null && checkGaugesTypeReport.getInoutRealMatch() != null) {
                    byId.setInoutShouldMatch(checkGaugesTypeReport.getInoutShouldMatch());
                    byId.setInoutRealMatch(checkGaugesTypeReport.getInoutRealMatch());
                    byId.setInoutRate(checkGaugesTypeReport.getInoutRate());
                }
                if (checkGaugesTypeReport.getSecondaryShouldMatch() != null && checkGaugesTypeReport.getSecondaryRealMatch() != null) {
                    byId.setSecondaryShouldMatch(checkGaugesTypeReport.getSecondaryShouldMatch());
                    byId.setSecondaryRealMatch(checkGaugesTypeReport.getSecondaryRealMatch());
                    byId.setSecondaryRate(checkGaugesTypeReport.getSecondaryRate());
                }
                if (checkGaugesTypeReport.getMainShouldMatch() != null && checkGaugesTypeReport.getMainRealMatch() != null) {
                    byId.setMainShouldMatch(checkGaugesTypeReport.getMainShouldMatch());
                    byId.setMainRealMatch(checkGaugesTypeReport.getMainRealMatch());
                    byId.setMainRate(checkGaugesTypeReport.getMainRate());
                }
                if (u.getId() != null) {
                    checkGaugesTypeReport.setUserid(u.getId());
                }
                checkGaugesTypeReport.setDelFlag("1");
                checkGaugesTypeReport.setUpdateTime(new Date());
                checkGaugesTypeReport.setDeptId(u.getDeptId());
                newList.add(byId);
            }
        }
        return R.ok(checkGaugesTypeReportService.updateBatchById(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 (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.getEquippedSituationState())) {  //已审核
                return R.failed("能源计量器具配备情况统计汇总表已审核，不能进行增加、修改、删除操作！");
            }
        } else {
            return R.failed("没有查询到自查18项资料记录");
        }
        return checkGaugesTypeReportService.excelImport(file, checkPlanId, epc, inspectionData);
    }

    /**
     * 批量新增
     */
    @SysLog("能源计量器具配备情况统计汇总表B.8续批量新增")
    @PostMapping("/saveBatch")
    @ApiOperation(value = "批量新增能源计量器具配备情况统计汇总表B.8续", notes = "批量新增能源计量器具配备情况统计汇总表B.8续")
    @ApiImplicitParam(name = "checkGaugesTypeReports", value = "能源计量器具配备情况统计汇总表B.8续集合", required = true, dataType = "json", paramType = "path")
    public R saveBatch(@RequestBody String s) {
        JSONObject json = JSONObject.parseObject(s);
        String ss = json.getString("checkGaugesTypeReports");
        String mend = json.getString("mend");
        List<CheckGaugesTypeReport> checkGaugesTypeReportList = (List<CheckGaugesTypeReport>) JSONArray.parseArray(ss, CheckGaugesTypeReport.class);
        //ist<CheckGauges> checkGaugesLis
        ScUser u = SecurityUtils.getUser();
        List<CheckGaugesTypeReport> newList = new ArrayList<>();
        Long checkPlanId = null;
        String enterpriseCode = null;
        for (CheckGaugesTypeReport checkGaugesTypeReport : checkGaugesTypeReportList) {
            if (u.getId() != null) {
                checkGaugesTypeReport.setUserid(u.getId());
            }
//            checkGaugesTypeReport.setItem(checkGaugesTypeReport.getName());
            checkGaugesTypeReport.setDelFlag("1");
            checkGaugesTypeReport.setUpdateTime(new Date());
            checkGaugesTypeReport.setDeptId(u.getDeptId());
            newList.add(checkGaugesTypeReport);
            checkPlanId = checkGaugesTypeReport.getCheckPlanId();
            enterpriseCode = checkGaugesTypeReport.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.setEquippedSituation("Y");
            inspectionData.setEquippedSituationState("D");
            inspectionDataService.save(inspectionData);
//            return R.failed("没有查询到自查18项资料记录");
        } else {
            inspectionData.setId(oldData.getId());
            inspectionData.setEquippedSituation("Y");
            inspectionData.setEquippedSituationState("D");
            if ("Z".equals(mend)){
                inspectionData.setEquippedSituationState("Z");
            }
            inspectionDataService.updateById(inspectionData);
        }
//        List<CheckGaugesTypeReport> selectList = checkGaugesTypeReportService.selectByCheckPlanidAndEpc(checkPlanId, enterpriseCode);
//        if (selectList.size() > 0) {
//            return R.ok(checkGaugesTypeReportService.updateBatchById(newList));
//        } else {
//            return R.ok(checkGaugesTypeReportService.saveOrUpdateBatch(newList));
//        }
        return R.ok(checkGaugesTypeReportService.saveOrUpdateBatch(newList));
    }

    /**
     * 修改
     *
     * @param checkGaugesTypeReport
     * @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 CheckGaugesTypeReport checkGaugesTypeReport) {
        ScUser u = SecurityUtils.getUser();
        if (u.getId() != null) {
            checkGaugesTypeReport.setUserid(u.getId());
        }
        checkGaugesTypeReport.setUpdateTime(new Date());
        return R.ok(checkGaugesTypeReportService.updateById(checkGaugesTypeReport));
    }

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

}
