package com.chinaitop.depot.intelligent.energyMonitor.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.energyMonitor.mapper.TEnergyHistoryMapper;
import com.chinaitop.depot.intelligent.energyMonitor.mapper.TEnergyInfoMapper;
import com.chinaitop.depot.intelligent.energyMonitor.model.*;
import com.chinaitop.depot.intelligent.energyMonitor.service.EnergyDetectionService;
import com.chinaitop.depot.intelligent.utils.DateUtils;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.unissoft.model.BasicEntity;
import com.unissoft.model.SendEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author hf
 * @description: 能耗检测
 * @create 2018-12-11 11:00
 */
@Service
public class EnergyDetectionServiceImpl implements EnergyDetectionService {


    @Resource
    private TEnergyInfoMapper tEnergyInfoMapper;

    @Resource
    private TEnergyHistoryMapper tEnergyHistoryMapper;

    @Resource
    private UuidUtils uUidUtils;

    /**
     * 列表
     *
     * @param map
     * @return
     */
    @Override
    public List<TEnergyHistory> getList(Map<String, Object> map) {
        return tEnergyHistoryMapper.getList(map);
    }

    @Override
    public List<TEnergyInfo> queryMeter(Map<String, Object> map) {
        return tEnergyInfoMapper.queryMeter(map);
    }

    @Override
    public List<TEnergyInfo> queryDeviceList(Map<String, Object> map) {
        return tEnergyInfoMapper.queryDeviceList(map);
    }

    @Override
    public List<Map<String, Object>> getTrendThreePage(Map<String, Object> map) {
        return tEnergyHistoryMapper.getTrendThreePage(map);
    }

    @Override
    public List<Map<String, Object>> getTimePointThreePage(Map<String, Object> map) {
        return tEnergyHistoryMapper.getTimePointThreePage(map);
    }

    @Override
    public List<Map<String, Object>> getTimeThreePage(Map<String, Object> map) {
        return tEnergyHistoryMapper.getTimeThreePage(map);
    }

    @Override
    public List<TEnergyHistory> getDailyConsumptionList(Map<String, Object> map) {
        return tEnergyHistoryMapper.getDailyConsumptionList(map);
    }

    @Override
    public List<TEnergyHistory> getTaskConsumptionList(Map<String, Object> map) {
        return tEnergyHistoryMapper.getTaskConsumptionList(map);
    }

    @Override
    public List<TEnergyInfo> getAmmeterPageInfo(Map<String, Object> map) {
        return tEnergyInfoMapper.getAmmeterPageInfo(map);
    }

    @Override
    public int deleteById(String id, String ammeterJson) {
        TEnergyInfo tEnergyInfo = (TEnergyInfo) JsonToObjectUtils.jsonToObject(ammeterJson, TEnergyInfo.class);
        tEnergyInfo.setDelFlag(0); //删除
        int i = tEnergyInfoMapper.updateByPrimaryKeySelective(tEnergyInfo);
        //int i = tEnergyInfoMapper.deleteByPrimaryKey(id);
        // 推送基础数据
        if (i > 0) {
            SendEntity.cxfFactor().saveEnergyData(SendEntity.energyData(ammeterJson, "D", null, null));
        }
        return i;
    }

    @Override
    public TEnergyInfo getById(String id) {
        return tEnergyInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public TEnergyInfo validCode(Map<String, Object> map) {
        return tEnergyInfoMapper.validCode(map);
    }

    @Override
    public void add(TEnergyInfo tEnergyInfo) {
        tEnergyInfo.setDelFlag(1); //未删除
        tEnergyInfo.setId(uUidUtils.getCodeId("tEnergyInfo.getOrgId()", "t_energy_info"));
        tEnergyInfoMapper.insert(tEnergyInfo);
    }

    @Override
    public void update(TEnergyInfo tEnergyInfo) {
        tEnergyInfoMapper.updateByPrimaryKey(tEnergyInfo);
    }

    @Override
    public void addHistoryData() {
        tEnergyHistoryMapper.addHistoryData();
    }

    @Override
    public TEnergyInfo getTEnergyInfo(Map<String, Object> map) {
        return tEnergyInfoMapper.getTEnergyInfo(map);
    }

    @Override
    public void addTEnergyHistory(TEnergyHistory tEnergyHistory) {
        tEnergyHistoryMapper.insert(tEnergyHistory);
    }

    @Transactional
    @Override
    public void insertBatch(List<TEnergyHistory> tEnergyInfos) {
        tEnergyHistoryMapper.insertBatch(tEnergyInfos);
    }

    @Override
    public List<TEnergyHistory> getByCodeAndTimes(List<String> cDcsCodes, List<Date> cTimes) {
        TEnergyHistoryExample example = new TEnergyHistoryExample();
        TEnergyHistoryExample.Criteria criteria = example.createCriteria();
        if ((cDcsCodes != null && cDcsCodes.size() > 0) && (cTimes != null && cTimes.size() > 0)) {
            criteria.andVNhCodeIn(cDcsCodes);
            criteria.andVUpdateTimeIn(cTimes);
            List<TEnergyHistory> tEnergyHistories = tEnergyHistoryMapper.selectByExample(example);
            return tEnergyHistories;
        }
        return null;
    }

    @Override
    public List<TEnergyInfo> getEnergyInfo(List<String> vNhCodes) {
        TEnergyInfoExample example = new TEnergyInfoExample();
        TEnergyInfoExample.Criteria criteria = example.createCriteria();
        if (vNhCodes != null && vNhCodes.size() > 0) {
            criteria.andVNhCodeIn(vNhCodes);
            List<TEnergyInfo> tEnergyInfos = tEnergyInfoMapper.selectByExample(example);
            return tEnergyInfos;
        }
        return null;
    }

    @Override
    public List<TEnergyInfo> getByNhCode(String vnhCode) {
        // TODO Auto-generated method stub
        TEnergyInfoExample example = new TEnergyInfoExample();
        TEnergyInfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(vnhCode)) {
            criteria.andVNhCodeEqualTo(vnhCode);
            List<TEnergyInfo> tEnergyInfos = tEnergyInfoMapper.selectByExample(example);
            return tEnergyInfos;
        }
        return null;
    }

    @Transactional
    @Override
    public String synchronizationAll(String orgId) {
        //获取所有基础数据getEnergyData
        String device = SendEntity.cxfFactor().getEnergyData();
        //转换对象
        BasicEntity basicEntity = (BasicEntity) JsonToObjectUtils.jsonToObject(device, BasicEntity.class);
        if (basicEntity.getState().equals("0")) {
            List<Object> data = basicEntity.getData();
            if (data.size() > 0) {
                String tt = JSONObject.toJSONString(data);
                ArrayList<Object> objects = JsonToObjectUtils.jsonToObjectsDiverse(tt, TEnergInfoLocal.class, "yyyy-MM-dd'T'HH:mm:ss");
                objects.forEach(t -> {
                    TEnergyInfo tc = new TEnergyInfo();
                    TEnergInfoLocal tcl = (TEnergInfoLocal) t;
                    tc.setvNhName(tcl.getVdevname());
                    tc.setvNhCode(tcl.getVdevcode());
                    tc.setiTdh(Integer.valueOf(tcl.getIdevtdh()));
                    tc.setvZdCode(tcl.getSitecode());
                    tc.setvCfCode(tcl.getVcfcode());
                    tc.setvHhtype(tcl.getVnhtype());
                    tc.setfTotalNh(new BigDecimal(tcl.getTotalpower() == null ? "0" : tcl.getTotalpower()));
                    tc.setfVoltage(new BigDecimal(tcl.getVoltage() == null ? "0" : tcl.getVoltage()));
                    tc.setfElectric(new BigDecimal(tcl.getElectric() == null ? "0" : tcl.getElectric()));
                    tc.setvRemark(tcl.getRemark());
                    if (tcl.getUpdatetime() != null){
                        tc.setvUpdateTime(DateUtils.stringToData(tcl.getUpdatetime(),"yyyy-MM-dd'T'HH:mm:ss"));
                    }
                    tc.setId(uUidUtils.getCodeId(orgId, "t_energy_info"));
                    tc.setOrgId(Integer.valueOf(orgId));
                    tc.setDelFlag(1); //未删除sitecode
                    String vdevcode = tc.getvNhCode();
                    TEnergyInfo info1 = getVinfo(vdevcode);
                    if (info1 != null) {
                        tc.setId(info1.getId());
                        tEnergyInfoMapper.updateByPrimaryKey(tc);
                    } else {
                        tEnergyInfoMapper.insert(tc);
                    }
                });
            }
        } else {
            return basicEntity.getMsg();
        }
        return basicEntity.getMsg();
    }

    public TEnergyInfo getVinfo(String vdevcode) {
        TEnergyInfoExample example = new TEnergyInfoExample();
        TEnergyInfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(vdevcode)) {
            criteria.andVNhCodeEqualTo(vdevcode);
        }
        List<TEnergyInfo> infos = tEnergyInfoMapper.selectByExample(example);
        if (infos.size() > 0) {
            return infos.get(0);
        }
        return null;
    }
}
