package com.dgtly.system.service.impl;

import com.dgtly.common.core.controller.ApiBaseController;
import com.dgtly.common.utils.http.HttpUtils;
import com.dgtly.system.domain.DeliverOrderBase;
import com.dgtly.system.domain.SysBatchSignInfo;
import com.dgtly.system.domain.TmsLogisticsStatus;
import com.dgtly.system.mapper.SysBatchSignForMapper;
import com.dgtly.system.service.ISysBatchSignForService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

/**
 * 批量签收
 *
 * @author mxy
 * @date 2021-04-29
 */
@Slf4j
@Service
public class SysBatchSignForServiceImpl implements ISysBatchSignForService {

    /*@Autowired
    @Qualifier("deliverOrderBaseExecutor")
    private Executor deliverOrderBaseExecutor;*/
    @Autowired(required = false)
    @Qualifier("tmsLogisticsStatusExecutor")
    private Executor tmsLogisticsStatusExecutor;
    @Autowired(required = false)
    @Qualifier("tmsLogisticsStatusDeleteExcutor")
    private Executor tmsLogisticsStatusDeleteExcutor;
    @Value(value = "${ruoyi.clearCachePath}")
    private String clearCachePath;

    @Autowired
    private SysBatchSignForMapper sysBatchSignForMapper;

    protected final Logger logger = LoggerFactory.getLogger(SysBatchSignForServiceImpl.class);

    @Override
    public int updateData(List<SysBatchSignInfo> list) {

        //3个任务
        //根据交货单号删除tms_logistics_status
        //构建数据添加数据到tms_logistics_status
        //构建数据添加数据到deliver_order_base
        //异步线程
        List<String> listData = new ArrayList<>();
        list.forEach(s-> listData.add(s.getCode()));

        CompletableFuture.allOf(
                tmsLogisticsStatusDelete(listData),
                tmsLogisticsStatusModel(listData)
        ).join();

        //清订单列表缓存
        String result = HttpUtils.sendGet(clearCachePath);
        logger.info("result:::"+result);
        return 1;
    }



    //构建数据
    private CompletableFuture<Void> tmsLogisticsStatusModel(List<String> listData) {
        return CompletableFuture.runAsync(() ->{
        List<TmsLogisticsStatus> list = new ArrayList<TmsLogisticsStatus>();
        List<DeliverOrderBase> list2 = new ArrayList<DeliverOrderBase>();
        List<Map<String,String>> list3 = sysBatchSignForMapper.selectByDeliveryNumber(listData);
        for(int i=0;i<listData.size();i++){

            //通过deliveryNumber查询数据
            String deliverNum = listData.get(i);
            DeliverOrderBase deliverOrderBase = new DeliverOrderBase();
            TmsLogisticsStatus tmsLogisticsStatus = new TmsLogisticsStatus();
            List<Map<String,String>> filter = list3.stream()
                                                  .filter(map -> deliverNum.equals(map.get("deliverNum")))
                                                  .collect(Collectors.toList());
            if(filter !=null&&filter.size()>0){
                tmsLogisticsStatus.setOrderNumber(filter.get(0).get("docNumber"));
                tmsLogisticsStatus.setCustomerCode(filter.get(0).get("customerCode"));
                deliverOrderBase.setOrderNumber(filter.get(0).get("docNumber"));
                deliverOrderBase.setOrderCreationTime(filter.get(0).get("createDate"));
                deliverOrderBase.setBelongTo(filter.get(0).get("customerCode"));
            }else{
                tmsLogisticsStatus.setOrderNumber("");
                tmsLogisticsStatus.setCustomerCode("");
                deliverOrderBase.setOrderNumber("");
                deliverOrderBase.setOrderCreationTime("");
                deliverOrderBase.setBelongTo("");
            }

            tmsLogisticsStatus.setCreateBy("SSB");
            tmsLogisticsStatus.setCreateTime(new Date());
            tmsLogisticsStatus.setUpdateBy("SSB");
            tmsLogisticsStatus.setUpdateTime(new Date());
            tmsLogisticsStatus.setIsDelete("0");

            tmsLogisticsStatus.setOrderCreationTime("");
            tmsLogisticsStatus.setDeliveryNumber(listData.get(i));
            tmsLogisticsStatus.setTmsShipmentNumber(listData.get(i));

            tmsLogisticsStatus.setStatus("3");
            tmsLogisticsStatus.setDriverName("SSB");
            tmsLogisticsStatus.setCarNumber("随身帮");
            tmsLogisticsStatus.setDriverPhone("13888888888");
            tmsLogisticsStatus.setAssignTime(new Date());
            tmsLogisticsStatus.setGetGoodsTime(new Date());
            tmsLogisticsStatus.setFactoryTime(new Date());
            tmsLogisticsStatus.setArrivalGoodsTime(new Date());
            tmsLogisticsStatus.setTransferOrder("n");
            list.add(tmsLogisticsStatus);



            deliverOrderBase.setCreateBy("SSB");
            deliverOrderBase.setCreateTime(new Date());
            deliverOrderBase.setUpdateBy("SSB");
            deliverOrderBase.setUpdateTime(new Date());
            deliverOrderBase.setIsDelete("0");

            deliverOrderBase.setDeliveryNumber(listData.get(i));
            deliverOrderBase.setShipmentNumber(listData.get(i));

            deliverOrderBase.setConfirmTime(new Date());
            deliverOrderBase.setConfirmBy("SSB");
            deliverOrderBase.setConfirmNotice("n");
            deliverOrderBase.setPushDisWarehouse("n");
            deliverOrderBase.setEvaluationStatus("n");
            deliverOrderBase.setEvaluationTime(null);
            deliverOrderBase.setEvaluationBy(null);
            deliverOrderBase.setEvaluationServiceAttitude(null);
            deliverOrderBase.setEvaluationDamageCondition(null);
            deliverOrderBase.setEvaluationArriveTime(null);
            deliverOrderBase.setEvaluationHandlingConditions(null);
            deliverOrderBase.setEvaluation(null);
            list2.add(deliverOrderBase);
        }
        //插入数据
        sysBatchSignForMapper.saveTmsLogisticsStatus(list);
        sysBatchSignForMapper.saveDeliverOrderBaseList(list2);

        },tmsLogisticsStatusExecutor);
    }

    //执行删除
    private CompletableFuture<Void> tmsLogisticsStatusDelete(List<String> listData) {
        return CompletableFuture.runAsync(() ->{
            sysBatchSignForMapper.delete(listData);
        },tmsLogisticsStatusDeleteExcutor);

    }
}
