package com.cyksj.service.user.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.constant.Constant;
import com.cyksj.mapper.UserSysMsgNotifyMapper;
import com.cyksj.model.entity.GroupsRelation;
import com.cyksj.model.entity.UserSysMsgNotify;
import com.cyksj.model.manage.views.GroupsRelationView;
import com.cyksj.service.user.UserSysMsgNotifyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 项目名: yhlxj2
 * 文件名: UserSysMsgNotifyServiceImpl
 * 创建者: JavaZou
 * 创建时间:2024/2/4 16:03
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserSysMsgNotifyServiceImpl extends ServiceImpl<UserSysMsgNotifyMapper, UserSysMsgNotify> implements UserSysMsgNotifyService {
	private final UserSysMsgNotifyMapper userSysMsgNotifyMapper;


	@Override
	public void recordUserAccountUpdateSysMsg(Long goodsId, Long skuId, List<GroupsRelationView> relations, String account, String title) {
		String sysMsg = String.format(Constant.ACCOUNT_PWD_UPDATE_SYS_MSG_NOTIFY, title);
		Set<Long> userSet = new HashSet<>();
		List<GroupsRelationView> collect = relations.stream().filter(e -> {
			if (userSet.contains(e.getUserId())) {
				return false;
			}
			userSet.add(e.getUserId());
			return true;
		}).collect(Collectors.toList());
		if (CollUtil.isNotEmpty(collect)) {
			userSysMsgNotifyMapper.recordUserAccountUpdateSysMsg(goodsId, skuId, collect, account, sysMsg);
		}
	}

	@Override
	public void recordUserAccountSetSysMsg(Long goodsId, Long skuId, List<GroupsRelation> relations, String account, String title) {
		String sysMsg = String.format(Constant.ACCOUNT_SET_SYS_MSG_NOTIFY, title);
		Set<Long> userSet = new HashSet<>();
		List<GroupsRelation> collect = relations.stream().filter(e -> {
			if (userSet.contains(e.getUserId())) {
				return false;
			}
			userSet.add(e.getUserId());
			return true;
		}).collect(Collectors.toList());
		if (CollUtil.isNotEmpty(collect)) {
			userSysMsgNotifyMapper.recordUserAccountSetSysMsg(goodsId, skuId, collect, account, sysMsg);
		}
	}
}
