package com.storlead.sales.service.impl.customer;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.sales.pojo.entity.customer.BulletinReadEntity;
import com.storlead.sales.mapper.customer.BulletinReadMapper;
import com.storlead.sales.service.customer.BulletinReadService;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import org.springframework.stereotype.Service;
import com.storlead.sales.pojo.vo.customer.BulletinReadVO;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 公告阅读表 服务实现类
 * </p>
 *
 * @author YPZ
 * @since 2024-10-25
 */
@Service
public class BulletinReadServiceImpl extends MyBaseServiceImpl<BulletinReadMapper, BulletinReadEntity> implements BulletinReadService {
    @Resource
    BulletinReadMapper bulletinReadMapper;

    @Override
    public Boolean add(Set<Long> userIds, Long bulletinId) {
        if(CollectionUtil.isEmpty(userIds)){
            return false;
        }
        List<BulletinReadEntity> list = genBulletinRead(userIds,bulletinId);
        return this.saveBatch(list);
    }

    @Override
    public void edit(Set<Long> userIds, Long bulletinId,boolean needResetRead) {
        //查询
        List<BulletinReadEntity> curBulletinReads = listByBulletinId(bulletinId);
        List<BulletinReadEntity> newBulletinReads = genBulletinRead(userIds, bulletinId);
        Set<Long> curUserIds = curBulletinReads.stream().map(BulletinReadEntity::getReadUserId).collect(Collectors.toSet());
        //取 curBulletinReads 有 newBulletinReads 没有的记录
        List<BulletinReadEntity> deleteEntitirs = curBulletinReads.stream().filter((t) -> !userIds.contains(t.getReadUserId())).collect(Collectors.toList());

        //取 newBulletinReads 没有 curBulletinReads 有的记录
        List<BulletinReadEntity> addEntitirs = newBulletinReads.stream().filter((t) -> !curUserIds.contains(t.getReadUserId())).collect(Collectors.toList());
        //删除
        delete(deleteEntitirs.stream().map(BulletinReadEntity::getId).collect(Collectors.toSet()));
        //重置为未读
        if(needResetRead){
            resetReadByBulletinId(bulletinId);
        }
        //新增
        if(CollectionUtil.isNotEmpty(addEntitirs)){
            this.saveBatch(addEntitirs);
        }
    }

    @Override
    public Boolean delete(Long bulletinId) {
        LambdaUpdateWrapper<BulletinReadEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(BulletinReadEntity::getBulletinId,bulletinId);
        wrapper.set(BulletinReadEntity::getIsDelete,CommonConstant.DEL_FLAG_1);
        return this.update(wrapper);
    }

    @Override
    public Map<Long, List<BulletinReadVO>> getMapByBulletinIds(Collection<Long> bulletinIds) {
        List<BulletinReadVO> bulletinReadVOS = this.listByBulletinIds(bulletinIds);
        return  bulletinReadVOS.stream().collect(Collectors.groupingBy(BulletinReadVO::getBulletinId));
    }

    @Override
    public List<BulletinReadVO> listByBulletinIds(Collection<Long> bulletinIds) {
        return bulletinReadMapper.listByBulletinIds(bulletinIds);
    }

    @Override
    public List<BulletinReadVO> listByBulletinById(Long bulletinIds) {
        return bulletinReadMapper.listByBulletinById(bulletinIds);
    }

    public Boolean delete(Collection<Long> bulletinReadIds) {
        if (CollectionUtils.isEmpty(bulletinReadIds)){
            return true;
        }
        LambdaUpdateWrapper<BulletinReadEntity> wrapper =Wrappers.lambdaUpdate();
        wrapper.in(BulletinReadEntity::getId,bulletinReadIds);
        wrapper.set(BulletinReadEntity::getIsDelete,CommonConstant.DEL_FLAG_1);
        return this.update(wrapper);
    }
    @Override
    public boolean resetReadByBulletinId(Long bulletinId){
        LambdaUpdateWrapper<BulletinReadEntity> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.set(BulletinReadEntity::getHasRead,Boolean.FALSE);
        updateWrapper.eq(BulletinReadEntity::getIsDelete,CommonConstant.DEL_FLAG_0);
        updateWrapper.eq(BulletinReadEntity::getBulletinId,bulletinId);
        return this.update(updateWrapper);
    }
    /**
     * 生成新增记录
     * @param userIds
     * @param bulletinId
     * @return
     */
    private List<BulletinReadEntity> genBulletinRead(Collection<Long> userIds,Long bulletinId){
        List<BulletinReadEntity> list = new ArrayList<>();
        userIds.forEach(
                (userId)->{
                    BulletinReadEntity bulletinReadEntity = new BulletinReadEntity();
                    bulletinReadEntity.setHasRead(false);
                    bulletinReadEntity.setReadUserId(userId);
                    bulletinReadEntity.setBulletinId(bulletinId);
                    list.add(bulletinReadEntity);
                }
        );
        return list;
    }
    private List<BulletinReadEntity> listByBulletinId(Long bulletinId){
        LambdaQueryWrapper<BulletinReadEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(BulletinReadEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.eq(BulletinReadEntity::getBulletinId,bulletinId);
        List<BulletinReadEntity> list = this.list(wrapper);
        return list;
    }
}
