package com.fdkankan.ucenter.service.impl;

import com.alibaba.druid.sql.visitor.functions.Concat;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.ucenter.entity.ProductCooperation;
import com.fdkankan.ucenter.entity.SceneCooperation;
import com.fdkankan.ucenter.entity.SceneCooperationCount;
import com.fdkankan.ucenter.entity.User;
import com.fdkankan.ucenter.mapper.ISceneCooperationCountMapper;
import com.fdkankan.ucenter.service.ISceneCooperationCountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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-20
 */
@Service
public class SceneCooperationCountServiceImpl extends ServiceImpl<ISceneCooperationCountMapper, SceneCooperationCount> implements ISceneCooperationCountService {

    @Autowired
    ISceneCooperationService sceneCooperationService;
    @Override
    public void saveCount(List<ProductCooperation> needPay, Integer size) {
        if(needPay == null || needPay.isEmpty() || size <=0){
            return;
        }
        HashMap<String,Integer> map = new HashMap<>();
        for (ProductCooperation productCooperation : needPay) {
            map.merge(productCooperation.getNum() + productCooperation.getSceneType(), 1, Integer::sum);
        }

        HashSet<String> hashSet = new HashSet<>();
        for (ProductCooperation productCooperation : needPay) {
            if(hashSet.contains(productCooperation.getNum()+productCooperation.getSceneType())){
                continue;
            }
            hashSet.add(productCooperation.getNum()+productCooperation.getSceneType());

            String num = productCooperation.getNum();
            SceneCooperationCount byNum = this.getByNum(num, productCooperation.getSceneType());
            if(byNum == null){
                SceneCooperationCount count = new SceneCooperationCount();
                count.setSceneType(productCooperation.getSceneType());
                count.setNum(num);
                count.setCount( map.get(num + productCooperation.getSceneType()) + 1);
                this.save(count);
            }else {
                LambdaUpdateWrapper<SceneCooperationCount> wrapper = new LambdaUpdateWrapper<>();
                wrapper.eq(SceneCooperationCount::getId,byNum.getId());
                wrapper.set(SceneCooperationCount::getCount, byNum.getCount() + map.get(num + productCooperation.getSceneType()));
                this.update(wrapper);
            }
        }

    }

    @Override
    public List<SceneCooperationCount> getByNumList(List<String> numList,String sceneType) {
        if(numList == null || numList.isEmpty()){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<SceneCooperationCount> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SceneCooperationCount::getNum,numList);
        wrapper.eq(SceneCooperationCount::getSceneType,sceneType);
        return this.list(wrapper);
    }

    @Override
    public SceneCooperationCount getByNum(String num,String sceneType) {
        LambdaQueryWrapper<SceneCooperationCount> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SceneCooperationCount::getNum,num);
        wrapper.eq(SceneCooperationCount::getSceneType,sceneType);
        List<SceneCooperationCount> list = this.list(wrapper);
        if(list == null || list.isEmpty()){
            return null;
        }
        return list.get(0);
    }
}
