package com.persagy.adm.server.delivery.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.persagy.adm.server.delivery.entity.BossProjectDeliveryPlan;
import com.persagy.adm.server.delivery.entity.BossSkuDictRel;
import com.persagy.adm.server.delivery.service.AdmDeliverPlanService;
import com.persagy.adm.server.delivery.service.BossProjectDeliveryPlanService;
import com.persagy.adm.server.delivery.service.BossSkuDictRelService;
import com.persagy.adm.steward.entity.ProjectDeliverReq;
import com.persagy.dmp.delivery.client.DigitalObjectTypeCollectFacade;
import com.persagy.dmp.delivery.dto.DeliveryPlanDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class AdmDeliverPlanServiceImpl implements AdmDeliverPlanService {

    @Autowired
    private BossSkuDictRelService bossSkuDictRelService;

    @Autowired
    private BossProjectDeliveryPlanService bossDeliveryPlanService;

    /**
     * 处理BOSS产生的交付计划
     * @param projectDeliverReq : 交付计划信息
     * @author : lijie
     * Update By 2022/1/14 17:22
     */
    @Override
    public void relayDeliverPlan(ProjectDeliverReq projectDeliverReq) {
        // 1.根据商品code与项目任务模板的映射数据与规则生成项目任务数据
        // 2.调用dmp-rwd服务根据商品code生成交付范围
        createProjectDeliveryPlan(projectDeliverReq);
    }

    /**
     * 生成项目交付计划
     * @param projectDeliverReq
     */
    private void createProjectDeliveryPlan(ProjectDeliverReq projectDeliverReq){
        // 存储项目交付信息
        List<DeliveryPlanDTO> result = new ArrayList<>();
        // 存储原BOSS订单信息
        List<BossProjectDeliveryPlan> bossProjectDeliveryPlan = new ArrayList<>();
        // 获取项目基础信息，后期调整为从上下文获取
        Long projectId = projectDeliverReq.getProjectId();
        String groupCode = projectDeliverReq.getBlocCode();

        //1. 查询商品sku与数据字典的映射关系
        Map<String, Set<String>> typeRelMap = queryBossBdtpTypeRel();
        if(CollUtil.isEmpty(typeRelMap)){
            return ;
        }
        //2. 获取项目下的计划
        List<ProjectDeliverReq.PlanDeliverReq> planList = projectDeliverReq.getPlanDeliverReqs();
        if(CollUtil.isEmpty(planList)){
            log.info("项目下无计划任务，项目ID：{}",projectId);
        }
        for (ProjectDeliverReq.PlanDeliverReq plan : planList) {
            Long planId = plan.getId();
            //获取计划下的商品信息
            List<ProjectDeliverReq.PlanInfoDeliverReq> planInfoList = plan.getPlanInfoDeliverReqList();
            if(CollUtil.isEmpty(planInfoList)){
                log.info("计划下无商品信息，项目ID：{}，计划ID:{}",projectId,plan.getId());
            }
            //生成商品交付信息
            for (ProjectDeliverReq.PlanInfoDeliverReq planInfo : planInfoList) {
                String skuCode = planInfo.getProductSkuCode();
                Set<String> classCodes = typeRelMap.get(skuCode);
                classCodes.forEach(item->{
                    DeliveryPlanDTO deliveryPlanDTO = new DeliveryPlanDTO();
                    deliveryPlanDTO.setGroupCode(groupCode);
                    deliveryPlanDTO.setProjectId("Pj"+projectId);
                    deliveryPlanDTO.setClassCode(item);
                    result.add(deliveryPlanDTO);
                });

                //存储原BOSS订单信息
                BossProjectDeliveryPlan bossDeliveryPlan = new BossProjectDeliveryPlan();
                bossDeliveryPlan.setProjectId(projectId);
                bossDeliveryPlan.setPlanId(planId);
                bossDeliveryPlan.setPlanInfoId(planInfo.getPlanInfoId());
                bossDeliveryPlan.setProductSkuCode(skuCode);
                bossDeliveryPlan.setProductNum(planInfo.getProductNum());
                bossDeliveryPlan.setStewardAccount(projectDeliverReq.getStewardAccount());
                bossProjectDeliveryPlan.add(bossDeliveryPlan);
            }
        }
        //生成项目交付信息
        DigitalObjectTypeCollectFacade.createProjectDelivery(groupCode,"Pj"+ projectId, null, null, result);
        //存储原BOSS订单信息
        bossDeliveryPlanService.insert(bossProjectDeliveryPlan);
    }

    /**
     * 查询商品类型映射关系
     * @return
     */
    private Map<String,Set<String>> queryBossBdtpTypeRel(){
        List<BossSkuDictRel> typeRelList = bossSkuDictRelService.queryByCondition(new QueryWrapper<>());
        if(CollUtil.isEmpty(typeRelList)){
            return new HashMap<>();
        }
        return typeRelList.stream().collect(Collectors.groupingBy(BossSkuDictRel::getSkuCode,
                Collectors.mapping(BossSkuDictRel::getClassCode, Collectors.toSet())));
    }


}
