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

import com.chinaitop.depot.intelligent.energyMonitor.model.TEnergyHistory;
import com.chinaitop.depot.intelligent.energyMonitor.model.TEnergyInfo;
import com.chinaitop.depot.intelligent.energyMonitor.service.EnergyDetectionService;
import com.chinaitop.depot.intelligent.pushs.service.EnergyPushsService;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.chinaitop.depot.intelligent.utils.VerificationSourceUtils;
import com.google.common.collect.Lists;
import com.unissoft.model.ResponseEntity;
import com.unissoft.model.TypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EnergyPushsServiceImpl implements EnergyPushsService {

    final static Logger logger = LoggerFactory.getLogger(EnergyPushsServiceImpl.class);

    @Resource
    private EnergyDetectionService energyDetectionService;

    @Resource
    private UuidUtils uuidUtils;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseEntity uploadEenergyData(String dataSource, String energyData) {
        if (!VerificationSourceUtils.verificationSource(dataSource)) {
            return ResponseEntity.failed("验证失败！");
        }
        //数据来源
        String dataSource2 = VerificationSourceUtils.sourceMap().get(dataSource);
        logger.info(energyData);
        try {
            if (StringUtils.isNotBlank(energyData)) {
                ArrayList<TEnergyHistory> energys = Lists.newArrayList();
                ArrayList<Object> ts = JsonToObjectUtils.jsonToObjects(energyData, TEnergyHistory.class);
                boolean temp = false;
                for (Object ener: ts){
                    TEnergyHistory energy = (TEnergyHistory) ener;
                    List<TEnergyInfo> energyInfo = energyDetectionService.getByNhCode(energy.getvNhCode());
                    String orgId = energyInfo.get(0).getOrgId().toString();
                    if (StringUtils.isBlank(orgId)){
                        temp = true;
                        break;
                    }
                    energy.setId(uuidUtils.getCodeId(orgId,"t_energy_history"));
                    energy.setOrgId(orgId);
                    energy.setDataSource(dataSource2);
                    energys.add(energy);
                }
                if (temp){
                    return ResponseEntity.failed("没有该设备！");
                }
                List<String> cDcsCodes = energys.stream().map(TEnergyHistory::getvNhCode).collect(Collectors.toList());

                List<Date> cTimes = energys.stream().map(TEnergyHistory::getvUpdateTime).collect(Collectors.toList());
                //校验重复数据
                List<TEnergyHistory> byCodeAndTimes = energyDetectionService.getByCodeAndTimes(cDcsCodes, cTimes);
                if (byCodeAndTimes != null && byCodeAndTimes.size() > 0) {
                    return ResponseEntity.failed("上传数据重复！");
                }

                energyDetectionService.insertBatch(energys);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(e.getMessage());
            throw new RuntimeException(TypeEnum.ED.getCode() + "  " + TypeEnum.ED.getDesc());
        }
        return ResponseEntity.ok();
    }

}
