package com.nantian.cloud.jly.service.impl;

import com.alibaba.nacos.api.config.filter.IFilterConfig;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jly.entity.CheckEnterprice;
import com.nantian.cloud.jly.entity.InspectionData;
import com.nantian.cloud.jly.mapper.CheckEnterpriceMapper;
import com.nantian.cloud.jly.mapper.InspectionDataMapper;
import com.nantian.cloud.jly.service.CheckEnterpriceService;
import com.nantian.cloud.jly.service.InspectionDataService;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @date 2018-09-27
 */
@Service("CheckEnterpriceService")
public class CheckEnterpriceServiceImpl extends ServiceImpl<CheckEnterpriceMapper, CheckEnterprice> implements CheckEnterpriceService {


    @Resource
    private CheckEnterpriceMapper checkEnterpriceMapper;

    @Autowired
    private InspectionDataService inspectionDataService;

    public List<CheckEnterprice> getCheckEnterpriceByCheckPlanId(String checkPlanId) {
        return checkEnterpriceMapper.getCheckEnterpriceByCheckPlanId(checkPlanId);
    }

//    public IPage getCheckEnterpricePageDataByCheckPlanId(Page page, String checkPlanId, String cmp_name, String epc,String checkStatus) {
//        IPage byPage = checkEnterpriceMapper.getCheckEnterpricePageDataByCheckPlanId(page, checkPlanId, cmp_name, epc,new DataScope(),checkStatus);
//        List<CheckEnterprice> records = byPage.getRecords();
//        byPage.setRecords(records);
//        greturn byPage;
//    }

    public IPage getCheckEnterpricePageDataByCheckPlanId(Page page, String checkPlanId, String cmp_name, String epc,String checkStatus) {
        IPage byPage = checkEnterpriceMapper.getCheckEnterpricePageDataByCheckPlanId(page, checkPlanId, cmp_name, epc,new DataScope(),checkStatus);
        List<CheckEnterprice> records = byPage.getRecords();
        Iterator<CheckEnterprice> iterator = records.iterator();
        while (iterator.hasNext()) {
            CheckEnterprice checkEnterprice = iterator.next();

            //保存初始状态
            String state = checkEnterprice.getCheckStatus();

            //保存文件数量
            int count = 0;

            InspectionData inspectionData = inspectionDataService.getByPlanIdAndEpc(checkEnterprice.getCheckPlanId(), checkEnterprice.getEpc());

            //计算文件数量
            if (inspectionData == null) {
                checkEnterprice.setCount(count);
            }else {
                if (!inspectionData.getBasicInformation().equals("D")||!inspectionData.getBasicInformationState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getEnergyMeteringReport().equals("D")||!inspectionData.getEnergyMeteringReportState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getMeasurementManagementSystem().equals("D")||!inspectionData.getMeasurementManagementSystemState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getEnergyStatisticsStatement().equals("D")||!inspectionData.getEnergyStatisticsStatementState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getEnergyMeteringPersonnel().equals("D")||!inspectionData.getEnergyMeteringPersonnelState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getEnergyusingEquipment().equals("D")||!inspectionData.getEnergyusingEquipmentState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getEnergyMeasuringInstruments().equals("D")||!inspectionData.getEnergyMeasuringInstrumentsState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getMeasuringInstruments().equals("D")||!inspectionData.getMeasuringInstrumentsState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getSecondaryMeasuringInstrument().equals("D")||!inspectionData.getSecondaryMeasuringInstrumentState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getMainMeasuringInstrument().equals("D")||!inspectionData.getMainMeasuringInstrumentState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getOtherMeasuringInstruments().equals("D")||!inspectionData.getOtherMeasuringInstrumentsState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getEquippedSituation().equals("D")||!inspectionData.getEquippedSituationState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getAccuracyGrade().equals("D")||!inspectionData.getAccuracyGradeState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getInventoryStatus().equals("D")||!inspectionData.getInventoryStatusState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getEnergyFlow().equals("D")||!inspectionData.getEnergyFlowState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getCollectionPointNetwork().equals("D")||!inspectionData.getCollectionPointNetworkState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getValuesTransfer().equals("D")||!inspectionData.getValuesTransferState().equals("D")) {
                    count++;
                }
                if (!inspectionData.getRootsDiagram().equals("D")||!inspectionData.getRootsDiagramState().equals("D")) {
                    count++;
                }
                checkEnterprice.setCount(count);
            }

            //更新状态
            if (checkEnterprice.getCount() < 18) {
                checkEnterprice.setCheckStatus("1");
            } else {
                if (checkEnterprice.getCheckStatus().equals("1")) {
                    checkEnterprice.setCheckStatus("2");
                }
            }

            if (inspectionData != null
                    && !checkEnterprice.getCheckStatus().equals("1")
                    && !checkEnterprice.getCheckStatus().equals("4")) {
                if (inspectionData.getBasicInformationState().equals("N")
                        || inspectionData.getEnergyMeteringReportState().equals("N")
                        || inspectionData.getMeasurementManagementSystemState().equals("N")
                        || inspectionData.getEnergyStatisticsStatementState().equals("N")
                        || inspectionData.getEnergyMeteringPersonnelState().equals("N")
                        || inspectionData.getEnergyusingEquipmentState().equals("N")
                        || inspectionData.getEnergyMeasuringInstrumentsState().equals("N")
                        || inspectionData.getMeasuringInstrumentsState().equals("N")
                        || inspectionData.getSecondaryMeasuringInstrumentState().equals("N")
                        || inspectionData.getMainMeasuringInstrumentState().equals("N")
                        || inspectionData.getOtherMeasuringInstrumentsState().equals("N")
                        || inspectionData.getEquippedSituationState().equals("N")
                        || inspectionData.getAccuracyGradeState().equals("N")
                        || inspectionData.getInventoryStatusState().equals("N")
                        || inspectionData.getEnergyFlowState().equals("N")
                        || inspectionData.getCollectionPointNetworkState().equals("N")
                        || inspectionData.getValuesTransferState().equals("N")
                        || inspectionData.getRootsDiagramState().equals("N")) {
                    checkEnterprice.setCheckStatus("4");
                }
            }


            if (inspectionData != null
                    && !checkEnterprice.getCheckStatus().equals("1")
                    && !checkEnterprice.getCheckStatus().equals("5")) {
                if (inspectionData.getBasicInformationState().equals("Z")
                        || inspectionData.getEnergyMeteringReportState().equals("Z")
                        || inspectionData.getMeasurementManagementSystemState().equals("Z")
                        || inspectionData.getEnergyStatisticsStatementState().equals("Z")
                        || inspectionData.getEnergyMeteringPersonnelState().equals("Z")
                        || inspectionData.getEnergyusingEquipmentState().equals("Z")
                        || inspectionData.getEnergyMeasuringInstrumentsState().equals("Z")
                        || inspectionData.getMeasuringInstrumentsState().equals("Z")
                        || inspectionData.getSecondaryMeasuringInstrumentState().equals("Z")
                        || inspectionData.getMainMeasuringInstrumentState().equals("Z")
                        || inspectionData.getOtherMeasuringInstrumentsState().equals("Z")
                        || inspectionData.getEquippedSituationState().equals("Z")
                        || inspectionData.getAccuracyGradeState().equals("Z")
                        || inspectionData.getInventoryStatusState().equals("Z")
                        || inspectionData.getEnergyFlowState().equals("Z")
                        || inspectionData.getCollectionPointNetworkState().equals("Z")
                        || inspectionData.getValuesTransferState().equals("Z")
                        || inspectionData.getRootsDiagramState().equals("Z")) {
                    checkEnterprice.setCheckStatus("5");
                }
            }

            if (inspectionData != null
                    && !checkEnterprice.getCheckStatus().equals("3")) {
                if (inspectionData.getBasicInformationState().equals("Y")
                        && inspectionData.getEnergyMeteringReportState().equals("Y")
                        && inspectionData.getMeasurementManagementSystemState().equals("Y")
                        && inspectionData.getEnergyStatisticsStatementState().equals("Y")
                        && inspectionData.getEnergyMeteringPersonnelState().equals("Y")
                        && inspectionData.getEnergyusingEquipmentState().equals("Y")
                        && inspectionData.getEnergyMeasuringInstrumentsState().equals("Y")
                        && inspectionData.getMeasuringInstrumentsState().equals("Y")
                        && inspectionData.getSecondaryMeasuringInstrumentState().equals("Y")
                        && inspectionData.getMainMeasuringInstrumentState().equals("Y")
                        && inspectionData.getOtherMeasuringInstrumentsState().equals("Y")
                        && inspectionData.getEquippedSituationState().equals("Y")
                        && inspectionData.getAccuracyGradeState().equals("Y")
                        && inspectionData.getInventoryStatusState().equals("Y")
                        && inspectionData.getEnergyFlowState().equals("Y")
                        && inspectionData.getCollectionPointNetworkState().equals("Y")
                        && inspectionData.getValuesTransferState().equals("Y")
                        && inspectionData.getRootsDiagramState().equals("Y")) {
                    checkEnterprice.setCheckStatus("3");
                }
            }

            //更新后状态与初始状态不一致时，将更新后状态存入数据库
            if (!checkEnterprice.getCheckStatus().equals(state)) {
                checkEnterpriceMapper.updatecCheckStatus(checkEnterprice.getCheckPlanId(),checkEnterprice.getEpc(),checkEnterprice.getCheckStatus());
            }

        }

        byPage.setRecords(records);
        return byPage;
    }


    @Override
    public IPage<CheckEnterprice> getPageScope(Page page, CheckEnterprice checkEnterprice) {
        return baseMapper.getPageScope(page, checkEnterprice, new DataScope());
    }

    @Override
    public IPage<Map<String, Object>> queryListByCmp(Page page, CheckEnterprice checkEnterprice) {
        return baseMapper.queryListByCmp(page, checkEnterprice);
    }

    @Override
    public boolean upload(String uuid, CheckEnterprice checkEnterprice,String type) {
        int i = 0;
        CheckEnterprice oldData = baseMapper.selectOne(Wrappers.query(checkEnterprice));
        if (oldData != null) { //修改
            oldData.setUpdateTime(new Date());
            if ("1".equals(type)){
                oldData.setCheckFile(uuid);
            }
            if ("2".equals(type)){
                oldData.setCheckNonFile(uuid);
            }
            i = baseMapper.updateById(oldData);
            System.err.println("oldDate：" + oldData);
        }
        return i == 0 ? false : true;
    }

    @Override
    public boolean upload1(String uuid, CheckEnterprice checkEnterprice) {
        int i = 0;
        CheckEnterprice oldData = baseMapper.selectOne(Wrappers.query(checkEnterprice));
        if (oldData != null) { //修改
            oldData.setUpdateTime(new Date());
            oldData.setCheckMendFile(uuid);
            oldData.setCheckNonStatus(1);
            oldData.setCheckNonRemain(" ");
            i = baseMapper.updateById(oldData);
            System.err.println("oldDate：" + oldData);
        }
        return i == 0 ? false : true;
    }

}

