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

import com.chinaitop.depot.intelligent.pushs.service.WeatherPushsService;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.chinaitop.depot.intelligent.utils.VerificationSourceUtils;
import com.chinaitop.depot.intelligent.weather.model.TWeatherhsitory;
import com.chinaitop.depot.intelligent.weather.model.TWertherinfo;
import com.chinaitop.depot.intelligent.weather.service.WertherinfoService;
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
@SuppressWarnings("all")
public class WeatherPushsServiceImpl implements WeatherPushsService {
    final static Logger logger  =  LoggerFactory.getLogger(WeatherPushsServiceImpl.class );

    @Resource
    private WertherinfoService wertherinfoService;

    @Resource
    private UuidUtils uuidUtils;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseEntity uploadWeatherData(String dataSource, String weatherData) {
        if (!VerificationSourceUtils.verificationSource(dataSource)){
            return ResponseEntity.failed("验证失败！");
        }
        //数据来源
        String dataSource2 = VerificationSourceUtils.sourceMap().get(dataSource);
        logger.info(weatherData);
        try {
            if (StringUtils.isNotBlank(weatherData)) {
                weatherData = weatherData.replaceAll("updatetime","updateTime");
                boolean temp = false;
                ArrayList<TWeatherhsitory> wertherinfos = Lists.newArrayList();
                ArrayList<Object> ts = JsonToObjectUtils.jsonToObjects(weatherData, TWeatherhsitory.class);
                for(Object obj: ts){
                    TWeatherhsitory wertherinfo = (TWeatherhsitory) obj;
                    String orgId = wertherinfoService.getByCode(wertherinfo.getVqxzcode()).get(0).getOrgId();
                    if (StringUtils.isBlank(orgId)){
                        temp = true;
                        break;
                    }
                    wertherinfo.setId(uuidUtils.getCodeId(orgId,"t_wertherinfo"));
                    wertherinfo.setOrgId(orgId);
                    wertherinfos.add(wertherinfo);
                }
                if (temp){
                    return ResponseEntity.failed("没有该设备！");
                }
                List<String> cDcsCodes = wertherinfos.stream().map(TWeatherhsitory::getVqxzcode).collect(Collectors.toList());
                List<Date> cTimes = wertherinfos.stream().map(TWeatherhsitory::getUpdateTime).collect(Collectors.toList());
                //校验重复数据
                List<TWeatherhsitory> byCodeAndTimes = wertherinfoService.getByCodeAndTimes(cDcsCodes, cTimes);
                if (byCodeAndTimes != null && byCodeAndTimes.size() > 0) {
                    return ResponseEntity.failed("上传数据重复！");
                }
                wertherinfoService.insertBatch(wertherinfos);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(e.getMessage());
            throw new RuntimeException(TypeEnum.ED.getCode() +"  "+ TypeEnum.ED.getDesc());
        }
        return ResponseEntity.ok();
    }
}
