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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.nantian.cloud.jljc.entity.*;
import com.nantian.cloud.jljc.mapper.EnergyInfoMapper;
import com.nantian.cloud.jljc.mapper.TReportEnterpriseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jljc.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 报表1-1企业信息 服务实现类
 *
 * @author stz
 * @since 2020-09-16
 */
@Service("tReportEnterpriseService")
public class TReportEnterpriseServiceImpl extends ServiceImpl<TReportEnterpriseMapper, TReportEnterprise> implements TReportEnterpriseService {

    @Autowired
    private DictRegionService dictRegionService;
    @Autowired
    private TradeKindService tradeKindService;
    @Autowired
    private DictEnterpriseTypeService dictEnterpriseTypeService;
    @Autowired
    private DictEnterpriseType1998Service dictEnterpriseType1998Service;
    @Autowired
    private CompanyTypeService companyTypeService;
    @Autowired
    private EnergyInfoMapper energyInfoMapper;

    @Override
    public TReportEnterprise getEnterpriseInfo(String enterpriseCode, String year) {
        return baseMapper.getEnterpriseInfo(enterpriseCode, year);
    }

    @Override
    public String getProductInfo(String enterpriseCode, String year) {
        return baseMapper.getProductInfo(enterpriseCode, year);
    }

    @Override
    public String getEnergyInfo(String enterpriseCode, String year) {
        String energyInfo = baseMapper.getEnergyInfo(enterpriseCode, year);
        if(StringUtils.isNotEmpty(energyInfo)){
            List<EnergyInfo> infoList = JSONArray.parseArray(energyInfo, EnergyInfo.class);
            List<EnergyInfo> newInfoList = energyInfoMapper.getEnergyInfoByReportSetting(infoList);
            String newEnergyInfo = JSON.toJSONString(newInfoList).replaceAll("\"false\"","false").replaceAll("\"true\"","true");
            return newEnergyInfo;
        }else{
            return energyInfo;
        }
    }

    @Override
    public String getEnergyIndex(String enterpriseCode, String year) {
        return baseMapper.getEnergyIndex(enterpriseCode, year);
    }

    @Override
    public TReportEnterprise getNameByEnterprise(TReportEnterprise enterprise) {
        if(enterprise!=null){
            TradeKind tradeKind = new TradeKind();
            DictRegion dictRegion = new DictRegion();
            DictEnterpriseType enterpriseType = new DictEnterpriseType();
            if (null != enterprise.getParea()) {
                //地区1
                dictRegion.setId(enterprise.getParea());
                DictRegion region = dictRegionService.getNameByCode(dictRegion);
                if (null != region) {
                    enterprise.setParea(region.getMeaning());
                }
            }
            if (null != enterprise.getArea()) {
                //地区2
                dictRegion.setId(enterprise.getArea());
                DictRegion region2 = dictRegionService.getNameByCode(dictRegion);
                if (null != region2) {
                    enterprise.setArea(region2.getMeaning());
                }
            }
            if (null != enterprise.getPtrade()) {
                //行业1
                tradeKind.setCode(enterprise.getPtrade());
                TradeKind trade = tradeKindService.getNameByCode(tradeKind);
                if (null != trade) {
                    enterprise.setPtrade(trade.getName());
                }
            }
            if (null != enterprise.getTrade()) {
                //行业2
                tradeKind.setCode(enterprise.getTrade());
                TradeKind trade2 = tradeKindService.getNameByCode(tradeKind);
                if (null != trade2) {
                    enterprise.setTrade(trade2.getName());
                }
            }
            if (null != enterprise.getEnterpriseType()) {
                //企业类型
                //DictEnterpriseType type = dictEnterpriseTypeService.getById(enterprise.getEnterpriseType());
                DictEnterpriseType1998 type = dictEnterpriseType1998Service.getById(enterprise.getEnterpriseType());
                if (null != type) {
                    //String codeName = type.getNameSamr();
                    String codeName = type.getName1998();
                    enterprise.setEnterpriseType(codeName);
                }
            }
            if (null != enterprise.getField()) {
                //企业领域
                if("100".equals(enterprise.getField())){
                    enterprise.setField("工业");
                }else if("200".equals(enterprise.getField())){
                    enterprise.setField("交通运输、仓储和邮政业");
                }else if("300".equals(enterprise.getField())){
                    enterprise.setField("住宿和餐饮业");
                }else if("400".equals(enterprise.getField())){
                    enterprise.setField("批发和零售业");
                }else if("500".equals(enterprise.getField())){
                    enterprise.setField("教育");
                }else {
                    enterprise.setField("其他");
                }
            }
            if (null != enterprise.getEpcType()) {
                enterprise.setEpcType(getTypeNameByStr(enterprise.getEpcType()));
            }
        }

        return enterprise;
    }

    @Override
    public String getTypeNameByStr(String str) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if(m.find()){ //包含中文，不进行转换
            return str;
        }else{
            //企业类别
            StringBuilder epcTypeStr = new StringBuilder();
            String[] splits = str.split(";");
            for (String split : splits) {
                /*switch (split) {
                    case "1":
                        epcTypeStr.append("国家重点监控耗能企业“万家”，");
                        break;
                    case "2":
                        epcTypeStr.append("省重点监控耗能企业，");
                        break;
                    case "3":
                        epcTypeStr.append("市重点监控耗能企业，");
                        break;
                    case "4":
                        epcTypeStr.append("区县重点监控耗能企业，");
                        break;
                    case "5":
                        epcTypeStr.append("云南省重点用能单位“千家”，");
                        break;
                    default:
                        epcTypeStr.append("云南省重点用能单位“万家”，");
                        break;
                }*/
                if(!"".equals(split)){
                    CompanyType type = companyTypeService.getById(Long.valueOf(split));
                    if(type!=null){
                        epcTypeStr.append(type.getTypeName());
                        epcTypeStr.append("，");
                    }
                }
            }

            if(epcTypeStr.length()>0 && epcTypeStr.charAt(epcTypeStr.length()-1) == ','){
                epcTypeStr.deleteCharAt(epcTypeStr.length() - 1);
            }

            return epcTypeStr.toString();
        }
    }

    @Override
    public TReportEnterprise getEnterpriseByReportId(Long reportId) {
        return baseMapper.getEnterpriseByReportId(reportId);
    }

    @Override
    public TReportEnterprise queryCustomEnterprise(String epc, String year) {
        return baseMapper.queryCustomEnterprise(epc,year);
    }

    @Override
    public int saveOrUpdateByUniqueKey(TReportEnterprise tReportEnterprise) {
        return  baseMapper.saveOrUpdateByUniqueKey(tReportEnterprise);
    }
}
