package com.qnfhq.modules.c2c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qnfhq.common.constant.Constant;
import com.qnfhq.common.page.PageData;
import com.qnfhq.common.service.impl.BaseServiceImpl;
import com.qnfhq.modules.c2c.dao.C2cMsgRelationDao;
import com.qnfhq.modules.c2c.entity.C2cMsgContentEntity;
import com.qnfhq.modules.c2c.entity.C2cMsgRelationEntity;
import com.qnfhq.modules.c2c.enums.MsgRelationEnum;
import com.qnfhq.modules.c2c.service.C2cMsgRelationService;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * 用户消息关联表
 *
 * @author yelz 30262728@qq.com
 * @since 1.0.0 2025-11-25
 */
@Service
public class C2cMsgRelationServiceImpl extends BaseServiceImpl<C2cMsgRelationDao, C2cMsgRelationEntity> implements C2cMsgRelationService {


    @Override
    public PageData<C2cMsgRelationEntity> selectChatPage(Map<String, Object> params) {
        Long ownerUid = (Long)params.get("ownerUid");
        Long otherUid = (Long)params.get("otherUid");
        Long mid = (Long)params.get("mid");

        LambdaQueryWrapper<C2cMsgRelationEntity> wrapper = new LambdaQueryWrapper<C2cMsgRelationEntity>()
                .eq(Objects.nonNull(ownerUid),C2cMsgRelationEntity::getOwnerUid, ownerUid)
                .eq(Objects.nonNull(otherUid),C2cMsgRelationEntity::getOtherUid, otherUid)
                .gt(Objects.nonNull(mid),C2cMsgRelationEntity::getMid,mid);

        IPage<C2cMsgRelationEntity> page = baseDao.selectPage(
                getPage(params, (String)params.get(Constant.ORDER_FIELD), "asc".equals(params.get(Constant.ORDER)) ? true : false),
                wrapper
        );
        return getPageData(page, C2cMsgRelationEntity.class);
    }

    @Override
    public void saveMsgRelation(C2cMsgContentEntity msgEntity) throws Exception{
        /**存发件人的发件箱*/
        saveSenderMsg(msgEntity);

        /**存收件人的收件箱*/
        saveRecipientMsg(msgEntity);
    }

    /**
     * 存发件人的发件箱
     */
    @Override
    public void saveSenderMsg(C2cMsgContentEntity msgEntity) throws Exception{
        C2cMsgRelationEntity messageRelationSender = new C2cMsgRelationEntity();
        messageRelationSender.setMid(msgEntity.getMid());
        messageRelationSender.setOwnerUid(msgEntity.getSenderId());
        messageRelationSender.setOtherUid(msgEntity.getRecipientId());
        messageRelationSender.setType(MsgRelationEnum.SEND_BOX.getCode());
        messageRelationSender.setCreateTime(new Date());
        insert(messageRelationSender);
    }

    /**
     * 存收件人的收件箱
     */
    @Override
    public void saveRecipientMsg(C2cMsgContentEntity msgEntity) throws Exception{
        C2cMsgRelationEntity messageRelationRecipient = new C2cMsgRelationEntity();
        messageRelationRecipient.setMid(msgEntity.getMid());
        messageRelationRecipient.setOwnerUid(msgEntity.getRecipientId());
        messageRelationRecipient.setOtherUid(msgEntity.getSenderId());
        messageRelationRecipient.setType(MsgRelationEnum.RECEIVE_BOX.getCode());
        messageRelationRecipient.setCreateTime(new Date());
        insert(messageRelationRecipient);
    }


}