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

import com.chinaitop.depot.intelligent.basicdata.model.TJctable;
import com.chinaitop.depot.intelligent.basicdata.service.QuantityService;
import com.chinaitop.depot.intelligent.pushs.service.NumberPushsService;
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
@SuppressWarnings("all")
public class NumberPushsServiceImpl implements NumberPushsService {
    final static Logger logger = LoggerFactory.getLogger(NumberPushsServiceImpl.class);
    @Resource
    private QuantityService quantityService;

    @Resource
    private UuidUtils uuidUtils;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseEntity uploadNumberData(String dataSource, String numberData) {
        if (!VerificationSourceUtils.verificationSource(dataSource)) {
            return ResponseEntity.failed("验证失败！");
        }
        //数据来源
        String dataSource2 = VerificationSourceUtils.sourceMap().get(dataSource);
        logger.info(numberData);
        try {
            if (StringUtils.isNotBlank(numberData)) {
                ArrayList<TJctable> tJctables = Lists.newArrayList();
                ArrayList<Object> ts = JsonToObjectUtils.jsonToObjects(numberData, TJctable.class);
                for (Object t : ts) {
                    TJctable tJctable = (TJctable) t;
                    if (StringUtils.isBlank(tJctable.getOrgId())) {
                        return ResponseEntity.failed("库点code不能为空！");
                    }
                    tJctable.setId(uuidUtils.getCodeId(tJctable.getOrgId(), "t_jctable"));
                    tJctable.setDataSource(dataSource2);
                    tJctables.add(tJctable);
                }
                List<String> cDcsCodes = tJctables.stream().map(TJctable::getvCfCode).collect(Collectors.toList());
                List<Date> cTimes = tJctables.stream().map(TJctable::getUpdateTime).collect(Collectors.toList());
                //校验重复数据
                List<TJctable> byCodeAndTimes = quantityService.getByCodeAndTimes(cDcsCodes, cTimes);
                if (byCodeAndTimes != null && byCodeAndTimes.size() > 0) {
                    return ResponseEntity.failed("上传数据重复！");
                }
                quantityService.insertBatch(tJctables);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(e.getMessage());
            throw new RuntimeException(TypeEnum.ED.getCode() + "  " + TypeEnum.ED.getDesc());
        }
        return ResponseEntity.ok();
    }
}
