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

import com.chinaitop.depot.intelligent.pushs.service.VentilationPushsService;
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.ventilation.model.TVentilation;
import com.chinaitop.depot.intelligent.ventilation.service.AerationRecordService;
import com.google.common.collect.Lists;
import com.unissoft.model.ResponseEntity;
import com.unissoft.model.TypeEnum;
import org.apache.commons.lang.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 VentilationPushsServiceImpl implements VentilationPushsService {
    final static Logger logger  =  LoggerFactory.getLogger(VentilationPushsServiceImpl.class );
    @Resource
    private AerationRecordService aerationRecordService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseEntity uploadVentilationData(String dataSource,String ventilationData) {
        if (!VerificationSourceUtils.verificationSource(dataSource)){
            return ResponseEntity.failed("验证失败！");
        }
        //数据来源
        String dataSource2 = VerificationSourceUtils.sourceMap().get(dataSource);
        logger.info(ventilationData);
        try {
            if (StringUtils.isNotBlank(ventilationData)) {
                ArrayList<TVentilation> tVentilations = Lists.newArrayList();
                ArrayList<Object> ts = JsonToObjectUtils.jsonToObjects(ventilationData, TVentilation.class);
                ts.forEach(obj -> {
                    TVentilation tVentilation = (TVentilation) obj;
                    tVentilation.setId(UuidUtils.getCode());
                    tVentilation.setDataSource(dataSource2);
                    tVentilations.add(tVentilation);
                });
                List<String> cDcsCodes = tVentilations.stream().map(TVentilation::getvCfCode).collect(Collectors.toList());
                List<Date> cTimes = tVentilations.stream().map(TVentilation::getvUpdateTime).collect(Collectors.toList());
                //校验重复数据
                List<TVentilation> byCodeAndTimes = aerationRecordService.getByCodeAndTimes(cDcsCodes, cTimes);
                if (byCodeAndTimes != null && byCodeAndTimes.size() > 0) {
                    return ResponseEntity.failed("上传数据重复！");
                }
                aerationRecordService.insertBatch(tVentilations);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(e.getMessage());
            throw new RuntimeException(TypeEnum.ED.getCode() +"  "+ TypeEnum.ED.getDesc());
        }
        return ResponseEntity.ok();
    }

}
