package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.controller.req.IsNeedGetOrderDetailReq2;
import com.ruoyi.controller.req.MessageReadOrDeleteReq;
import com.ruoyi.frequency.appmessage.entity.AppMessage;
import com.ruoyi.frequency.appmessage.service.AppMessageService;
import com.ruoyi.frequency.ordermessage.entity.OrderMessage;
import com.ruoyi.frequency.ordermessage.service.OrderMessageService;
import com.ruoyi.message.frequency.interactionmessagerecord.entity.InteractionMessageRecord;
import com.ruoyi.message.frequency.interactionmessagerecord.service.InteractionMessageRecordService;
import com.ruoyi.response.ResponseData;
import com.ruoyi.service.MessageService;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息 服务实现类
 *
 * @author liwenlong
 * @date 2023-04-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MessageServiceImpl implements MessageService {


    @Autowired
    private AppMessageService appMessageService;

    @Autowired
    private OrderMessageService orderMessageService;

    @Autowired
    private InteractionMessageRecordService interactionMessageRecordService;

    @Override
    public ResponseData messageCenter(UserVo userVo) {
        //返回值
        Map<String, Object> result = new HashMap<>();

        //未读的系统消息数量
        Integer systemMessageCount = appMessageService.systemMessageCount(userVo);
        result.put("systemMessageCount", systemMessageCount);

        //未读的系统消息
        AppMessage newAppMessage = appMessageService.systemMessageNew(userVo);
        result.put("newAppMessage", newAppMessage);

        List<OrderMessage> orderMessageList = orderMessageService.list(new QueryWrapper<OrderMessage>().lambda().eq(OrderMessage::getUserId, userVo.getUserId())
                .eq(OrderMessage::getUserType, userVo.getUserType()).eq(OrderMessage::getIsShow, PublicCommon.Message.未清空).
                eq(OrderMessage::getIsRead, PublicCommon.Message.未读).eq(OrderMessage::getDelFlag, PublicCommon.启用));
        Map<String, String> orderMessageMap = new HashMap<>();
        orderMessageList.stream().forEach(x -> {
            orderMessageMap.put(x.getOrderType() + "," + x.getOrderId(), "1");
        });

        result.put("orderMessageCount", orderMessageMap.size());

        //最新订单消息
        OrderMessage newOrderMessage = orderMessageService.getOne(new QueryWrapper<OrderMessage>().lambda()
                .eq(OrderMessage::getUserId, userVo.getUserId())
                .eq(OrderMessage::getUserType, userVo.getUserType())
                .eq(OrderMessage::getIsShow, PublicCommon.Message.未清空)
                .eq(OrderMessage::getDelFlag, PublicCommon.启用)
                .orderByDesc(OrderMessage::getCreateTime)
                .last("limit 1")
        );
        result.put("newOrderMessage", newOrderMessage);
        result.put("orderMessageDate", newOrderMessage == null ? null : newOrderMessage.getCreateTime());


        //未读的互动消息数量
        Integer interactionMessageCount = interactionMessageRecordService.count(new QueryWrapper<InteractionMessageRecord>().lambda().eq(InteractionMessageRecord::getUserId, userVo.getUserId())
                .eq(InteractionMessageRecord::getUserType, userVo.getUserType()).eq(InteractionMessageRecord::getIsShow, PublicCommon.Message.未清空).
                eq(InteractionMessageRecord::getIsRead, PublicCommon.Message.未读).eq(InteractionMessageRecord::getDelFlag, PublicCommon.启用));
        result.put("interactionMessageCount", interactionMessageCount);

        //最新互动消息
        InteractionMessageRecord newInteractionMessage = interactionMessageRecordService.getOne(new QueryWrapper<InteractionMessageRecord>().lambda()
                .eq(InteractionMessageRecord::getUserId, userVo.getUserId())
                .eq(InteractionMessageRecord::getUserType, userVo.getUserType())
                .eq(InteractionMessageRecord::getIsShow, PublicCommon.Message.未清空)
                .eq(InteractionMessageRecord::getDelFlag, PublicCommon.启用)
                .orderByDesc(InteractionMessageRecord::getCreateTime)
                .last("limit 1")
        );
        result.put("newInteractionMessage", newInteractionMessage);


        return ResponseData.success(result);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData messageReadOrDelete(MessageReadOrDeleteReq req, UserVo userVo) {
        //订单
        orderMessageService.orderMessageReadOrDelete(req, userVo);
        //系统
        appMessageService.appMessageReadOrDelete(req, userVo);
        //互动
        interactionMessageRecordService.interactionMessageReadOrDelete(req, userVo);
        return ResponseData.success();
    }

    @Override
    public ResponseData<Boolean> isNeedGetMessageCenter(IsNeedGetOrderDetailReq2 req, UserVo userVo) {
        OrderMessage newOrderMessage = orderMessageService.getOne(new QueryWrapper<OrderMessage>().lambda()
                .eq(OrderMessage::getUserId, userVo.getUserId())
                .eq(OrderMessage::getUserType, userVo.getUserType())
                .eq(OrderMessage::getIsShow, PublicCommon.Message.未清空)
                .eq(OrderMessage::getDelFlag, PublicCommon.启用)
                .orderByDesc(OrderMessage::getCreateTime)
                .last("limit 1")
        );
        if (newOrderMessage != null) {
            if (req.getDate() == null) {
                return ResponseData.success(true);
            }
            if (newOrderMessage.getCreateTime().getTime() > req.getDate().getTime()) {
                return ResponseData.success(true);
            }
        }

        return ResponseData.success(false);
    }
}
