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

import com.chinaitop.depot.intelligent.fumigation.model.TFumigationProcess;
import com.chinaitop.depot.intelligent.fumigation.service.FumigationPlanService;
import com.chinaitop.depot.intelligent.pushs.service.FumigationPushsService;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
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.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
public class FumigationPushsServiceImpl implements FumigationPushsService {

    final static Logger logger  =  LoggerFactory.getLogger(FumigationPushsServiceImpl.class );
    @Resource
    private FumigationPlanService fumigationPlanService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseEntity uploadFumigationData(String dataSource, String fumigationData) {
        if (!VerificationSourceUtils.verificationSource(dataSource)) {
            return ResponseEntity.failed("验证失败！");
        }
        //数据来源
        String dataSource2 = VerificationSourceUtils.sourceMap().get(dataSource);
        logger.info(fumigationData);
        try {
            if (StringUtils.isNotBlank(fumigationData)) {
                ArrayList<TFumigationProcess> tFumigationProcesses = Lists.newArrayList();
                ArrayList<Object> ts = JsonToObjectUtils.jsonToObjects(fumigationData, TFumigationProcess.class);
                ts.forEach(obj -> {
                    TFumigationProcess tFumigationProcess = (TFumigationProcess) obj;
                    tFumigationProcess.setDataSource(dataSource2);
                    tFumigationProcesses.add(tFumigationProcess);
                });
                List<String> cDcsCodes = tFumigationProcesses.stream().map(TFumigationProcess::getFumigationId).collect(Collectors.toList());
                List<Date> cTimes = tFumigationProcesses.stream().map(TFumigationProcess::getCreateTime).collect(Collectors.toList());
                //校验重复数据
                List<TFumigationProcess> byCodeAndTimes = fumigationPlanService.getByCodeAndTimes(cDcsCodes, cTimes);
                if (byCodeAndTimes != null && byCodeAndTimes.size() > 0) {
                    return ResponseEntity.failed("上传数据重复！");
                }
                fumigationPlanService.insertBatch(tFumigationProcesses);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(e.getMessage());
            throw new RuntimeException(TypeEnum.ED.getCode() +"  "+ TypeEnum.ED.getDesc());
        }
        return ResponseEntity.ok();
    }
}
