/*
 *      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.modules.platform.service.impl;

import cn.hutool.core.collection.CollUtil;
import org.springblade.common.enums.OrderTypeEnum;
import org.springblade.common.enums.YesOrNoEnum;
import org.springblade.modules.platform.entity.UserMessage;
import org.springblade.modules.platform.vo.UserMessageVO;
import org.springblade.modules.platform.mapper.UserMessageMapper;
import org.springblade.modules.platform.service.IUserMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 用户消息表 服务实现类
 *
 * @author BladeX
 * @since 2023-07-06
 */
@Service
public class UserMessageServiceImpl extends ServiceImpl<UserMessageMapper, UserMessage> implements IUserMessageService {

	@Override
	public IPage<UserMessageVO> selectUserMessagePage(IPage<UserMessageVO> page, UserMessageVO userMessage) {
		return page.setRecords(baseMapper.selectUserMessagePage(page, userMessage));
	}

	@Override
	public IPage getUserNoticeList(IPage page, Integer type, Long userId) {
		List<UserMessageVO> vos = baseMapper.selectUserNoticeList(page, type, userId);
		if (CollUtil.isNotEmpty(vos)) {
			for (UserMessageVO vo : vos) {
				if (OrderTypeEnum.CUSTOM_WINE.getValue().equals(vo.getOrderType())) {
					vo.setGoodsName("定制酒");
				}
			}
		}
		return page.setRecords(vos);
	}

	@Async
	@Override
	public void sendMessage(String title, Long orderId, Integer type, Long userId) {
		UserMessage userMessage = new UserMessageVO();
		userMessage.setIsRead(false);
		userMessage.setTitle(title);
		userMessage.setOrderId(orderId);
		userMessage.setType(type);
		userMessage.setUserId(userId);
		save(userMessage);
	}
}
