package com.ruoyi.frequency.ordermessage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.controller.req.MessageReadOrDeleteReq;
import com.ruoyi.enums.message.OrderMessageEnums;
import com.ruoyi.frequency.ordermessage.entity.OrderMessageUtil;
import com.ruoyi.frequency.ordermessage.mapper.OrderMessageUtilMapper;
import com.ruoyi.frequency.ordermessage.service.OrderMessageUtilService;
import com.ruoyi.push.service.PushService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.PageBasic;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单消息Service业务层处理
 *
 * @author zj
 * @date 2023-10-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderMessageUtilServiceImpl extends ServiceImpl<OrderMessageUtilMapper, OrderMessageUtil>  implements OrderMessageUtilService
{

    @Autowired
    private PushService pushService;

    /**
     * 添加订单消息
     * @param userVo
     * @param orderId
     * @param orderType
     */
    @Override
    public void addOrderMessage(UserVo userVo, Long orderId, String orderNo,String amount, Integer orderType, OrderMessageEnums orderMessageEnums) {

        OrderMessageUtil orderMessageUtil = new OrderMessageUtil()
                .setMessageType(orderMessageEnums.getCode())
                .setTitle(orderMessageEnums.getTitle())
                .setContent(orderMessageEnums.getContent().replace("orderNo",orderNo).replace("amount",amount))
                .setUserId(userVo.getUserId())
                .setUserType(userVo.getUserType())
                .setOrderId(orderId)
                .setOrderType(orderType);
        this.baseMapper.insert(orderMessageUtil);

        //推送
        pushService.push(userVo,orderId,orderNo, orderMessageUtil.getId(), orderMessageEnums);

    }


    @Override
    public void addOrderMessage(UserVo userVo, Long orderId, String orderNo, Integer orderType, OrderMessageEnums orderMessageEnums, String... content) {
        OrderMessageUtil orderMessageUtil = new OrderMessageUtil()
                .setMessageType(orderMessageEnums.getCode())
                .setTitle(orderMessageEnums.getTitle())
                .setContent(String.format(orderMessageEnums.getContent(),content))
                .setUserId(userVo.getUserId())
                .setUserType(userVo.getUserType())
                .setOrderId(orderId)
                .setOrderType(orderType);

        this.baseMapper.insert(orderMessageUtil);

        //推送
        pushService.push(userVo,orderId,orderNo, orderMessageUtil.getId(), orderMessageEnums);
    }
}
