package com.zanxiang.game.module.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zanxiang.game.module.manage.enums.KfRoomMsgOwnerEnum;
import com.zanxiang.game.module.manage.enums.KfRoomMsgTypeEnum;
import com.zanxiang.game.module.manage.pojo.dto.KfWebSocketMsgDTO;
import com.zanxiang.game.module.manage.service.IKfRoomMsgService;
import com.zanxiang.game.module.manage.service.IKfSessionUserService;
import com.zanxiang.game.module.manage.service.IRoleOperateService;
import com.zanxiang.game.module.mybatis.entity.KfRoomMsg;
import com.zanxiang.game.module.mybatis.entity.KfSessionUser;
import com.zanxiang.game.module.mybatis.mapper.KfSessionUserMapper;
import com.zanxiang.module.util.JsonUtil;
import com.zanxiang.module.util.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : lingfeng
 * @time : 2024-02-23
 * @description : 客服聊天玩家信息
 */
@Slf4j
@Service
public class KfSessionUserServiceImpl extends ServiceImpl<KfSessionUserMapper, KfSessionUser> implements IKfSessionUserService {

    @Autowired
    private IKfRoomMsgService kfRoomMsgService;

    @Autowired
    private IRoleOperateService roleOperateService;

    @Override
    public KfSessionUser getById(String openId, Long gameId) {
        return super.getOne(new LambdaQueryWrapper<KfSessionUser>()
                .eq(KfSessionUser::getOpenId, openId)
                .eq(KfSessionUser::getGameId, gameId)
        );
    }

    @Override
    public List<KfWebSocketMsgDTO.WaitUserBean> getWaitUserList(Long gameId) {
        return super.list(new LambdaQueryWrapper<KfSessionUser>()
                .eq(KfSessionUser::getGameId, gameId)
                .eq(KfSessionUser::getIsWait, Boolean.TRUE)
                .orderByDesc(KfSessionUser::getWaitStartTime)
                .last("limit 16")
        ).stream().map(this::transform).collect(Collectors.toList());
    }

    private KfWebSocketMsgDTO.WaitUserBean transform(KfSessionUser kfSessionUser) {
        if (kfSessionUser == null) {
            return null;
        }
        KfWebSocketMsgDTO.WaitUserBean waitUserBean = BeanUtil.copy(kfSessionUser, KfWebSocketMsgDTO.WaitUserBean.class);
        Page<KfRoomMsg> kfRoomMsgPage = kfRoomMsgService.page(new Page<>(1, 10),
                new QueryWrapper<KfRoomMsg>().lambda()
                        .eq(KfRoomMsg::getGameId, kfSessionUser.getGameId())
                        .eq(KfRoomMsg::getOpenId, kfSessionUser.getOpenId())
                        .eq(KfRoomMsg::getReadStatus, Boolean.FALSE)
                        .isNull(KfRoomMsg::getRoomId)
                        .eq(KfRoomMsg::getMsgOwner, KfRoomMsgOwnerEnum.KF_MSG_OWNER_USER.getValue())
                        .orderByDesc(KfRoomMsg::getSortTime));
        List<KfWebSocketMsgDTO.WaitUserMsgBean> waitUserMsgList = kfRoomMsgPage.getRecords()
                .stream().map(this::transform).collect(Collectors.toList());
        waitUserBean.setWaitUserMsgList(waitUserMsgList);
        waitUserBean.setUnReadMsgCount(kfRoomMsgPage.getTotal());
        //玩家是否加企微
        waitUserBean.setAddCorpWechat(roleOperateService.userAddCorpWechat(kfSessionUser.getGameId(), kfSessionUser.getUserId()));
        return waitUserBean;
    }

    private KfWebSocketMsgDTO.WaitUserMsgBean transform(KfRoomMsg kfRoomMsg) {
        if (kfRoomMsg == null) {
            return null;
        }
        //等待消息对象
        KfWebSocketMsgDTO.WaitUserMsgBean waitUserMsgBean = BeanUtil.copy(kfRoomMsg, KfWebSocketMsgDTO.WaitUserMsgBean.class);
        //消息内容解析
        KfWebSocketMsgDTO.MsgContentBean msgContent = JsonUtil.toObj(waitUserMsgBean.getContent(), KfWebSocketMsgDTO.MsgContentBean.class);
        //消息内容转换
        if (Objects.equals(waitUserMsgBean.getMsgType(), KfRoomMsgTypeEnum.KF_MSG_TYPE_TEXT.getValue())) {
            waitUserMsgBean.setContent(msgContent.getText());
        }
        if (Objects.equals(waitUserMsgBean.getMsgType(), KfRoomMsgTypeEnum.KF_MSG_TYPE_IMAGE.getValue())) {
            waitUserMsgBean.setContent("[图片]");
        }
        if (Objects.equals(waitUserMsgBean.getMsgType(), KfRoomMsgTypeEnum.KF_MSG_TYPE_LINK.getValue())) {
            waitUserMsgBean.setContent("[充值]");
        }
        if (Objects.equals(waitUserMsgBean.getMsgType(), KfRoomMsgTypeEnum.KF_MSG_TYPE_MINI_APP.getValue())) {
            waitUserMsgBean.setContent("[小程序]");
        }
        return waitUserMsgBean;
    }
}
