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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.dbcapp.entity.ConfigureDataCollect;
import com.nantian.cloud.dbcapp.entity.ConfigureGaugeRelevance;
import com.nantian.cloud.dbcapp.mapper.jly.ConfigureGaugeRelevanceMapper;
import com.nantian.cloud.dbcapp.service.ConfigureGaugeRelevanceService;
import com.nantian.cloud.dbcapp.vo.ConfigureGaugeVo;
import com.nantian.cloud.dbcapp.vo.MapVo;
import com.nantian.cloud.dbcapp.vo.RelevanceStateVo;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


@Service("ConfigureGaugeRelevanceService")
public class ConfigureGaugeRelevanceServiceImpl extends ServiceImpl<ConfigureGaugeRelevanceMapper, ConfigureGaugeRelevance> implements ConfigureGaugeRelevanceService {

    @Override
    public IPage<ConfigureGaugeVo> listState(Page page, String epc, String dataCode, String type) {
        return baseMapper.listState(page,epc,dataCode,type);
    }

    @Override
    public IPage<ConfigureGaugeRelevance> getList(Page page, String epc, String dataCode, String auditStatus) {
        return baseMapper.getList(page, epc,dataCode,auditStatus);
    }

    @Override
    public IPage<ConfigureGaugeVo> getGaugeByRelevance(Page page, String epc, String dataCode) {
        return baseMapper.getGaugeByRelevance(page, epc,dataCode);
    }

    @Override
    public IPage<ConfigureDataCollect> getConfigByRelevance(Page page, Long gaugeId) {
        return baseMapper.getConfigByRelevance(page, gaugeId);
    }

    @Override
    public boolean updateDelete(List<Long> ids) {
        return baseMapper.updateDelete(ids);
    }

    @Override
    public List<RelevanceStateVo> relevanceState(String epc) {
        return baseMapper.relevanceState(epc);
    }

    @Override
    public void updateDeleteByEpcAndDataCode(String epc, String dataCode) {
        baseMapper.updateDeleteByEpcAndDataCode(epc,dataCode);
    }

    @Override
    public IPage<ConfigureGaugeRelevance> listGroupByEpcAndDataCode(Page page, String epc, String dataCode, String auditStatus) {
        return baseMapper.listGroupByEpcAndDataCode(page,epc,dataCode,auditStatus);
    }

    @Override
    public boolean auditGroup(String epc, String dataCode, String auditStatus, String auditRemark, String username) {
        return baseMapper.auditGroup(epc,dataCode,auditStatus,auditRemark,username);
    }

    @Override
    public int autoMatchRelevance() {
        return baseMapper.autoMatchRelevance();
    }

    @Override
    public boolean updateMeterLevel(String enterpriseCode, String dataCode) {
        List<Long> gaugeIds = baseMapper.getGaugeIds(enterpriseCode, dataCode);
        try {
            //将计量器具基本信息中的器具等级进行更新
            baseMapper.updateMeterLevel(gaugeIds, dataCode, getMeterLevelByDataCode(dataCode));
            //根据关联取消状态复原计量器具基本信息
            baseMapper.recoverGauges(enterpriseCode, dataCode);
            List<MapVo> maps = baseMapper.getIdAndNewDataCodeMap(enterpriseCode, dataCode);
            for (MapVo map : maps) {
                List<String> dataCodes = Arrays.asList(map.getValue().split(","));
                map.setValue(getMeterLevelByDataCodes(dataCodes));
            }
            if (!maps.isEmpty()){
                baseMapper.recoverGaugesLevel(maps);
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public String getMeterLevelByDataCode(String dataCode){
        if (dataCode.startsWith("00000000")) {
            return "1";
        } else if (dataCode.startsWith("0000", 4)) {
            return "2";
        } else {
            return "3";
        }
    }
    
    public String getMeterLevelByDataCodes(List<String> dataCodes){
        StringBuilder meterLevel = new StringBuilder();
        for (String dataCode : dataCodes) {
            meterLevel.append(getMeterLevelByDataCode(dataCode));
        }
        String meterLevelStr = meterLevel.toString();
        if (meterLevelStr.contains("1") && meterLevelStr.contains("2") && meterLevelStr.contains("3")){
            return "123";
        }else if(meterLevelStr.contains("1") && meterLevelStr.contains("2")){
            return "12";
        }else if(meterLevelStr.contains("1") && meterLevelStr.contains("3")){
            return "13";
        }else if (meterLevelStr.contains("2") && meterLevelStr.contains("3")){
            return "23";
        }else if (meterLevelStr.contains("1")){
            return "1";
        }else if (meterLevelStr.contains("2")){
            return "2";
        }else if (meterLevelStr.contains("3")){
            return "3";
        }else {
            return null;
        }
    }
}
