package com.fdkankan.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.ucenter.entity.ProductCooperation;
import com.fdkankan.ucenter.entity.ProductOrder;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.mapper.IProductCooperationMapper;
import com.fdkankan.ucenter.service.IProductCooperationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.ucenter.service.IProductOrderService;
import com.fdkankan.ucenter.service.ISceneCooperationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2025-03-13
 */
@Service
public class ProductCooperationServiceImpl extends ServiceImpl<IProductCooperationMapper, ProductCooperation> implements IProductCooperationService {

    @Autowired
    ISceneCooperationService sceneCooperationService;
    @Autowired
    IProductOrderService productOrderService;

    @Override
    public void add(ProductOrder productOrder, List<User> users,List<String> list , HashMap<String,String> map1, HashMap<String,String> map2, String sceneType) {

        if(StringUtils.isBlank(sceneType)){
            this.saveList(productOrder,users,list,map1,"mesh");
            this.saveList(productOrder,users,list,map2,"laser");
        }else {
            this.saveList(productOrder,users,list,map1,sceneType);
        }

    }

    private void saveList(ProductOrder productOrder, List<User> users,List<String> list ,  HashMap<String,String> map1,String sceneType){
        List<ProductCooperation> productCooperations = new ArrayList<>();

        for (String num : list) {
            for (User user : users) {
                ProductCooperation productCooperation = new ProductCooperation();
                productCooperation.setOrderSn(productOrder.getOrderSn());
                productCooperation.setCooperationUserId(user.getId());
                productCooperation.setNum(num);
                productCooperation.setSceneType(sceneType);
                if(map1.get(user.getId()+","+num) != null){
                    productCooperation.setNeedPay(1);
                }
                productCooperations.add(productCooperation);
            }
        }
        if(!productCooperations.isEmpty()){
            this.saveBatch(productCooperations);
        }
    }

    @Override
    public void paySuccess(String orderSn) {
        ProductOrder productOrder = productOrderService.getByOrderSn(orderSn);
        if(productOrder == null){
            return;
        }

        LambdaQueryWrapper<ProductCooperation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductCooperation::getOrderSn,orderSn);
        List<ProductCooperation> list = this.list(wrapper);
        if(list.isEmpty()){
            return;
        }
        List<String> numList = list.stream().map(ProductCooperation::getNum).collect(Collectors.toList());
        List<ProductCooperation> needPay = list.stream().filter(e -> e.getNeedPay() == 1).collect(Collectors.toList());
        Set<Long> userIds = list.stream().map(ProductCooperation::getCooperationUserId).collect(Collectors.toSet());

        if(productOrder.getCameraId() != null){
            sceneCooperationService.successAddCooperation(numList,new ArrayList<>(userIds),productOrder.getUserId(), productOrder.getCameraId(), productOrder.getLang(),null,needPay);
        }else {
            sceneCooperationService.successAddCooperation(numList,new ArrayList<>(userIds),productOrder.getUserId(), null, productOrder.getLang(),productOrder.getSceneType(),needPay);
        }
    }
}
