package com.ruoyi.web.modules.message.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.entry.constant.MerchantEntryApplyConstants;
import com.ruoyi.web.modules.entry.domain.BizMemberMessage;
import com.ruoyi.web.modules.entry.mapper.BizMemberMessageMapper;
import com.ruoyi.web.modules.entry.mapper.BizMerchantEntryApplyMapper;
import com.ruoyi.web.modules.message.constant.MemberMessageConstants;
import com.ruoyi.web.modules.message.service.IMemberMessageAppService;
import com.ruoyi.web.modules.message.vo.MemberMessageDetailVO;
import com.ruoyi.web.modules.message.vo.MemberMessageListItemVO;
import com.ruoyi.web.modules.message.vo.MemberMessageUnreadVO;

/**
 * C 端站内消息 Service 实现
 */
@Service
public class MemberMessageAppServiceImpl implements IMemberMessageAppService
{
    @Autowired
    private BizMemberMessageMapper messageMapper;

    @Autowired
    private BizMerchantEntryApplyMapper applyMapper;

    @Override
    public MemberMessageUnreadVO getUnreadCount(Long memberId)
    {
        MemberMessageUnreadVO vo = new MemberMessageUnreadVO();
        vo.setUnreadCount(messageMapper.countUnreadByMemberId(memberId));
        return vo;
    }

    @Override
    public List<MemberMessageListItemVO> listMessages(Long memberId)
    {
        List<BizMemberMessage> rows = messageMapper.selectListByMemberId(memberId);
        if (rows == null || rows.isEmpty())
        {
            return Collections.emptyList();
        }
        List<MemberMessageListItemVO> list = new ArrayList<>(rows.size());
        for (BizMemberMessage row : rows)
        {
            list.add(toListItem(row));
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MemberMessageDetailVO getDetailAndMarkRead(Long memberId, Long messageId)
    {
        BizMemberMessage message = messageMapper.selectByIdAndMemberId(messageId, memberId);
        if (message == null)
        {
            throw new ServiceException(MemberMessageConstants.MSG_NOT_FOUND, 404);
        }
        if (MemberMessageConstants.READ_UNREAD.equals(message.getReadFlag()))
        {
            messageMapper.updateReadFlag(messageId, memberId, MemberMessageConstants.READ_READ);
            message.setReadFlag(MemberMessageConstants.READ_READ);
        }
        return toDetail(message);
    }

    private MemberMessageListItemVO toListItem(BizMemberMessage message)
    {
        MemberMessageListItemVO vo = new MemberMessageListItemVO();
        vo.setMessageId(message.getMessageId());
        vo.setTitle(message.getTitle());
        vo.setSummary(toSummary(message.getContent()));
        vo.setReadFlag(message.getReadFlag());
        vo.setCreateTime(message.getCreateTime());
        vo.setBizType(message.getBizType());
        vo.setBizId(message.getBizId());
        return vo;
    }

    private MemberMessageDetailVO toDetail(BizMemberMessage message)
    {
        MemberMessageDetailVO vo = new MemberMessageDetailVO();
        vo.setMessageId(message.getMessageId());
        vo.setTitle(message.getTitle());
        vo.setContent(message.getContent());
        vo.setReadFlag(message.getReadFlag());
        vo.setCreateTime(message.getCreateTime());
        vo.setBizType(message.getBizType());
        vo.setBizId(message.getBizId());
        fillLinkInfo(vo, message.getBizType(), message.getBizId());
        return vo;
    }

    private void fillLinkInfo(MemberMessageDetailVO vo, String bizType, Long bizId)
    {
        if (MerchantEntryApplyConstants.BIZ_TYPE_ENTRY_AUDIT.equals(bizType))
        {
            vo.setLinkType(MemberMessageConstants.LINK_TYPE_MERCHANT_ENTRY);
            vo.setLinkAvailable(bizId != null && applyMapper.selectById(bizId) != null);
            return;
        }
        vo.setLinkType(null);
        vo.setLinkAvailable(false);
    }

    private String toSummary(String content)
    {
        if (StringUtils.isEmpty(content))
        {
            return "";
        }
        int max = MemberMessageConstants.SUMMARY_MAX_LENGTH;
        if (content.length() <= max)
        {
            return content;
        }
        return content.substring(0, max - 3) + "...";
    }
}
