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.C2cMsgContactDao;
import com.qnfhq.modules.c2c.entity.C2cMsgContactEntity;
import com.qnfhq.modules.c2c.entity.C2cMsgContentEntity;
import com.qnfhq.modules.c2c.service.C2cMsgContactService;
import com.qnfhq.modules.user.entity.AppUserEntity;
import com.qnfhq.modules.user.service.AppUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * 最近联系人
 *
 * @author yelz 30262728@qq.com
 * @since 1.0.0 2025-11-25
 */
@Service
public class C2cMsgContactServiceImpl extends BaseServiceImpl<C2cMsgContactDao, C2cMsgContactEntity> implements C2cMsgContactService {

    @Resource
    private AppUserService appUserService;


    @Override
    public PageData<C2cMsgContactEntity> selectContactPage(Map<String, Object> params) {
        Long ownerUid = (Long)params.get("ownerUid");

        LambdaQueryWrapper<C2cMsgContactEntity> wrapper = new LambdaQueryWrapper<C2cMsgContactEntity>()
                .eq(Objects.nonNull(ownerUid),C2cMsgContactEntity::getOwnerUid, ownerUid);

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

    @Override
    public C2cMsgContactEntity selectByOwnerUidOtherUid(Long ownerUid, Long otherUid) {
        LambdaQueryWrapper<C2cMsgContactEntity> wrapper = new LambdaQueryWrapper<C2cMsgContactEntity>()
                .eq(Objects.nonNull(ownerUid),C2cMsgContactEntity::getOwnerUid, ownerUid)
                .eq(Objects.nonNull(otherUid),C2cMsgContactEntity::getOtherUid, otherUid);
        return baseDao.selectOne(wrapper);
    }

    private void updateByOwnerUidOtherUid(C2cMsgContactEntity contactEntity) throws Exception{
        baseDao.update(contactEntity, new LambdaQueryWrapper<C2cMsgContactEntity>()
                .eq(Objects.nonNull(contactEntity.getOwnerUid()),C2cMsgContactEntity::getOwnerUid, contactEntity.getOwnerUid())
                .eq(Objects.nonNull(contactEntity.getOtherUid()),C2cMsgContactEntity::getOtherUid, contactEntity.getOtherUid()));
    }


    @Override
    public void saveByMsgContact(C2cMsgContentEntity msgEntity) throws Exception{
        AppUserEntity receiptUser = appUserService.getById(msgEntity.getRecipientId());
        AppUserEntity senderUser = appUserService.getById(msgEntity.getSenderId());
        // 更新发件人的最近联系人
        C2cMsgContactEntity senderContact = selectByOwnerUidOtherUid(msgEntity.getSenderId(), msgEntity.getRecipientId());
        if(senderContact!=null) {
            senderContact.setMid(msgEntity.getMid());
            senderContact.setMsgType(msgEntity.getMsgType());
            senderContact.setCreateTime(new Date());
            updateByOwnerUidOtherUid(senderContact);
        } else {
            senderContact = new C2cMsgContactEntity();
            senderContact.setOwnerUid(msgEntity.getSenderId());
            senderContact.setOtherUid(msgEntity.getRecipientId());
            senderContact.setOtherNickname(receiptUser.getNickname());
            senderContact.setOtherAvatar(receiptUser.getAvatar());
            senderContact.setMid(msgEntity.getMid());
            senderContact.setMsgType(msgEntity.getMsgType());
            senderContact.setCreateTime(new Date());
            insert(senderContact);
        }

        // 更新收件人的最近联系人
        C2cMsgContactEntity receipterContact = selectByOwnerUidOtherUid(msgEntity.getRecipientId(),msgEntity.getSenderId());
        if(receipterContact!=null) {
            receipterContact.setMid(msgEntity.getMid());
            receipterContact.setMsgType(msgEntity.getMsgType());
            receipterContact.setCreateTime(new Date());
            updateByOwnerUidOtherUid(receipterContact);
        } else {
            receipterContact = new C2cMsgContactEntity();
            receipterContact.setOwnerUid(msgEntity.getRecipientId());
            receipterContact.setOtherUid(msgEntity.getSenderId());
            receipterContact.setOtherNickname(senderUser.getNickname());
            receipterContact.setOtherAvatar(senderUser.getAvatar());
            receipterContact.setMid(msgEntity.getMid());
            receipterContact.setMsgType(msgEntity.getMsgType());
            receipterContact.setCreateTime(new Date());
            insert(receipterContact);
        }
    }
}