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

import com.nantian.cloud.dbcapp.bean.SysUserSub;
import com.nantian.cloud.dbcapp.bean.XYBo;
import com.nantian.cloud.dbcapp.mapper.record.EnterpriseDataInfoMapper;
import com.nantian.cloud.dbcapp.service.EnergyCalculateService;
import com.nantian.sc.cloud.common.core.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service("EnergyCalculateService")
@Transactional
public class EnergyCalculateServiceImpl implements EnergyCalculateService {

    @Autowired
    EnterpriseDataInfoMapper enterpriseDataInfoMapper;

    @Override
    public Integer isHasOpenId(String openId) {
        return enterpriseDataInfoMapper.isHasOpenId(openId);
    }

    @Override
    public Integer saveOpenId(String openId, String userId) {
        return enterpriseDataInfoMapper.savaOpenId(openId, userId);
    }

    @Override
    public List<SysUserSub> getUserInfo(String openId) {
        return enterpriseDataInfoMapper.getUserInfo(openId);
    }


    @Override
    public R updateDateCodeState(String enterpriseCode, String dataCode, Integer state) {
        Integer states = enterpriseDataInfoMapper.updateDataCodeState(enterpriseCode, dataCode, state);
        enterpriseDataInfoMapper.updateDataCodeStateNew(enterpriseCode, dataCode, state);
        // 修改该企业能源消费品种
        //enterpriseConfigureService.insertEnterpriseEnergyKindsInfo(enterpriseCode); //不能影响停用的点以前数据结算
        if (states != 0) {
            return R.ok("success", "修改成功");
        } else {
            return R.ok("fail", "修改失败");
        }
    }

    @Override
    public void updateElectricityData(String enterpriseCode,String time) {
        // 获取企业列表
        List<String> enterpriseInfo = enterpriseDataInfoMapper.selectEnterpriseInfoByEpc(enterpriseCode);
        // 读取电网公司月数据 若电网公司月数据不为空 则替换 在线采集数据 0000000002330011
        List<XYBo> list = enterpriseDataInfoMapper.selectElectricityDataInfo(enterpriseInfo,time);
        ArrayList<String> strings = new ArrayList<>();
        Collections.addAll(strings,"0000000002330011","0000000002330021","0000000002330020","0000000002330010");
        if(list!=null){
            a: for (int i = 0; i < list.size(); i++) {
                XYBo xyBo = list.get(i);
                if(xyBo.getY()==null||Float.valueOf(xyBo.getY())==0.0f){
                    continue a;
                }else {
                    for (int j = 0; j < strings.size(); j++) {
                        String dataCode = strings.get(j);
                        Float value = enterpriseDataInfoMapper.selectCollectDataInfo(xyBo.getX(),time,dataCode);
                        if(value!=null&&value!=0){
                            // 替换 并且修改状态位
                            Integer result = enterpriseDataInfoMapper.updateCollectDataInfoByElectricity(xyBo.getX(),xyBo.getY(),time,dataCode);
                            if(result!=0){
                                continue a;
                            }
                        }else {

                        }
                    }
                }
            }
        }
    }
}



