package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.vo.LifeFansVo;
import shop.alien.entity.store.vo.LifeMessageVo;
import shop.alien.mapper.LifeMessageMapper;
import shop.alien.store.service.PlatformLifeMessageService;
import shop.alien.util.type.LifeMessageUtil;
import shop.alien.util.type.PhoneTypeIdResult;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class PlatformLifeMessageServiceImpl implements PlatformLifeMessageService {

    private final LifeMessageMapper lifeMessageMapper;

    private final LifeMessageUtil lifeMessageUtil;

    @Override
    public List<LifeMessageVo> getMessageList(String receiverId) throws Exception {
        try {
            QueryWrapper<LifeFansVo> wrapper = new QueryWrapper<>();
            wrapper.eq("message.num", 1);
            wrapper.orderByDesc("message.created_time");
            PhoneTypeIdResult participant = lifeMessageUtil.resolveIdentity(receiverId);
            Integer participantUserType = participant != null ? participant.getType() : -1;
            Integer participantRefId = participant != null ? participant.getId() : -1;
            return lifeMessageMapper.getLifeMessagePageByPhoneId(
                    receiverId, participantUserType, participantRefId, wrapper);
        } catch (Exception e) {
            log.error("LifeMessageServiceImpl.getMessageList Error Msg={}", e.getMessage());
            throw new Exception("获取消息列表失败");
        }
    }
}
