/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.ldt.notice.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.common.enums.ReceiverType;
import org.springblade.common.enums.SenderType;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.ldt.mall.entity.Mall;
import org.springblade.ldt.mall.service.IMallService;
import org.springblade.ldt.notice.entity.NoticeManagement;
import org.springblade.ldt.notice.entity.NoticeManagementContent;
import org.springblade.ldt.notice.mapper.NoticeManagementContentMapper;
import org.springblade.ldt.notice.service.INoticeManagementContentService;
import org.springblade.ldt.notice.service.INoticeManagementService;
import org.springblade.ldt.notice.vo.NoticeManagementContentVO;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;
import org.springblade.ldt.user.entity.LoginUser;
import org.springblade.ldt.user.entity.Member;
import org.springblade.ldt.user.service.ILoginUserService;
import org.springblade.ldt.user.service.IMemberService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author BladeX
 * @since 2021-11-09
 */
@Service
@AllArgsConstructor
public class NoticeManagementContentServiceImpl extends BaseServiceImpl<NoticeManagementContentMapper, NoticeManagementContent> implements INoticeManagementContentService {

	@Override
	public IPage<NoticeManagementContentVO> selectNoticeContentPage(IPage<NoticeManagementContentVO> page, NoticeManagementContentVO noticeContent) {
		return page.setRecords(baseMapper.selectNoticeContentPage(page, noticeContent));
	}

	private IMallService mallService;
	private IShopService shopService;
	private IMemberService memberService;
	private ILoginUserService loginUserService;
	private INoticeManagementService noticeService;

	@Override
	@Transactional
	public R submitAndPush(NoticeManagementContentVO notice, BladeUser bladeUser) {
		Mall mall = null;
		if (notice.getSenderType().equals(SenderType.MALL)) {
			mall = mallService.getOne(Wrappers.<Mall>lambdaQuery().eq(Mall::getTenantId, bladeUser.getTenantId()));
		}
		Map<ReceiverType, Map<Long, String>> receiver = new HashMap<>();
		for (ReceiverType receiverType : notice.getReceiverTypes()) {
			if (receiverType.equals(ReceiverType.MALL)) {//平台可发
				List<Mall> malls = mallService.list();
				receiver.put(ReceiverType.MALL, malls.stream().collect(Collectors.toMap(Mall::getId, Mall::getMallName)));
			}
			if (receiverType.equals(ReceiverType.SHOP)) {//平台，商场可发
				List<Shop> shops = shopService.list(Wrappers.<Shop>lambdaQuery()
					.eq(notice.getSenderType().equals(SenderType.MALL) && mall != null, Shop::getMallId, mall.getId()));
				receiver.put(ReceiverType.SHOP, shops.stream().collect(Collectors.toMap(Shop::getId, Shop::getName)));
			}
			if (receiverType.equals(ReceiverType.MEMBER)) {//商场可发
				List<Member> members = memberService.list(Wrappers.<Member>lambdaQuery().eq(mall != null, Member::getMallId, mall.getId()));
				receiver.put(ReceiverType.MEMBER, members.stream().collect(Collectors.toMap(Member::getId, Member::getNickName)));
			}
			if (receiverType.equals(ReceiverType.CONSUMER)) {//平台可发
				List<LoginUser> loginUsers = loginUserService.list();
				receiver.put(ReceiverType.CONSUMER, loginUsers.stream().collect(Collectors.toMap(LoginUser::getId, LoginUser::getNickName)));
			}
		}

		//保存通知内容
		if (notice.getSenderType().equals(SenderType.MALL)) {
			notice.setSenderId(mall.getId());
			notice.setSender(mall.getMallName());
		} else {
			notice.setSenderId(bladeUser.getUserId());
			notice.setSender("[平台]");
		}
		saveOrUpdate(notice);

		//发布给指定人
		List<NoticeManagement> noticeManagements = new ArrayList<>();
		for (Map.Entry<ReceiverType, Map<Long, String>> data : receiver.entrySet()) {
			for (Map.Entry<Long, String> item : data.getValue().entrySet()) {
				NoticeManagement noticeManagement = new NoticeManagement();
				noticeManagement.setReceiver(item.getValue());
				noticeManagement.setReceiverId(item.getKey());
				noticeManagement.setReceiverType(data.getKey());
				noticeManagement.setContentId(notice.getId());
				noticeManagement.setIsRead(false);
				noticeManagements.add(noticeManagement);
			}
		}
		noticeService.saveBatch(noticeManagements);
		return R.status(true);
	}

}
