package cn.axzo.msg.center.dal;


import cn.axzo.basics.common.constant.enums.TableIsDeleteEnum;
import cn.axzo.msg.center.api.enums.MsgStateEnum;
import cn.axzo.msg.center.api.enums.MsgTypeEnum;
import cn.axzo.msg.center.api.enums.ReceiveTypeEnum;
import cn.axzo.msg.center.api.request.QueryMessageRecordReq;
import cn.axzo.msg.center.api.response.MessageRecordRes;
import cn.axzo.msg.center.dal.mapper.MessageRecordMapper;
import cn.axzo.msg.center.domain.dto.CmsMsgQueryReqDTO;
import cn.axzo.msg.center.domain.dto.MessageNewResDTO;
import cn.axzo.msg.center.domain.dto.MsgStatisticsDTO;
import cn.axzo.msg.center.domain.dto.UpdateReadDTO;
import cn.axzo.msg.center.domain.entity.MessageRecord;
import cn.axzo.msg.center.domain.enums.ModuleBizTypeEnum;
import cn.axzo.msg.center.domain.persistence.BaseEntity;
import cn.axzo.msg.center.domain.request.InsideCmsReadMsgReq;
import cn.axzo.msg.center.service.dto.IdentifyAndReceiveType;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * 消息记录(MessageRecord)表数据库访问层
 *
 * @author zhangran
 * @since 2022-03-28 14:59:16
 */
@Service
@Slf4j
public class MessageRecordDao extends
    ServiceImpl<MessageRecordMapper, MessageRecord> {


  /**
   * 通过id获取消息列表
   *
   * @param msgIds
   * @return
   */
  public List<MessageRecord> listByIds(List<Long> msgIds) {
    return lambdaQuery().eq(BaseEntity::getIsDelete, TableIsDeleteEnum.NORMAL.value)
        .in(BaseEntity::getId, msgIds).list();
  }

  /**
   * 已读消息修改
   *
   * @param req
   * @param personId   message_record 表中的 person_id
   * @param identityId
   */
  public void readMsg(InsideCmsReadMsgReq req,
                      Long personId, Long identityId) {
    lambdaUpdate().set(MessageRecord::getState, MsgStateEnum.HAVE_READ)
        .eq(MessageRecord::getIsDelete, 0L)
        .eq(req.getMsgId() != -1L, MessageRecord::getId, req.getMsgId())
        .eq(MessageRecord::getType, MsgTypeEnum.GENERAL_MESSAGE)
        .update();
  }

  public void readAllMsg(InsideCmsReadMsgReq req, Long personId, Long identityId) {
    List<MessageRecord> list = this.lambdaQuery()
            .and(nested -> nested.eq(MessageRecord::getPersonId, personId)
                    .or()
                    .eq(MessageRecord::getToId, identityId))
            .eq(MessageRecord::getIsDelete, 0L)
            .eq(MessageRecord::getType, MsgTypeEnum.GENERAL_MESSAGE)
            .notIn(MessageRecord::getState, MsgStateEnum.HAVE_READ, MsgStateEnum.COMPLETE)
            .select(MessageRecord::getId)
            .list();
    if(!CollectionUtils.isEmpty(list)) {
      this.baseMapper.readAllMsg(list.stream().map(MessageRecord::getId).collect(Collectors.toList()));
    }

  }

  public IPage<MessageNewResDTO> pageMsgInfo(
          CmsMsgQueryReqDTO req, IPage page, Long personId,
          List<Integer> states, List<IdentifyAndReceiveType> identities) {
    return baseMapper.pageMsgInfo(req, page, personId, states, identities);
  }

  public IPage<MessageNewResDTO> mixIdentityPageMsgInfo(CmsMsgQueryReqDTO req, List<Long> identityIds,
                                                        Long personId, ArrayList<Integer> states) {
    return baseMapper.mixIdentityPageMsgInfo(req, req.toPage(), identityIds, personId, states);
  }

  public List<MsgStatisticsDTO> statisticsMsg4Construction(Long identityId, Integer receiveType) {
    return baseMapper.statisticsMsg(ModuleBizTypeEnum.CONSTRUCTION.getCode(), null, identityId,
        receiveType);
  }


  public List<MsgStatisticsDTO> statisticsMsg4Trade(Long personId) {
    return baseMapper.statisticsMsg(ModuleBizTypeEnum.TRADE.getCode(), personId, null,
            ReceiveTypeEnum.NOT_IDENTITY.getCode());
  }

  public IPage<MessageNewResDTO> pageMsgInfo4WechatAndTrade(CmsMsgQueryReqDTO req,
      List<Long> moduleIds, Long personId, ArrayList<Integer> states) {
    return baseMapper.pageMsgInfo4WechatAndTrade(req, req.toPage(), moduleIds, personId,
            ReceiveTypeEnum.NOT_IDENTITY.getCode(), states);
  }

  /**
   *  修改成已读接口
   * @param request
   * @return
   */
  public int updateHasRead(UpdateReadDTO request){
    return baseMapper.updateHasRead(request);
  }

  public IPage<MessageNewResDTO> queryUnReadList(QueryMessageRecordReq request){
    return baseMapper.queryUnReadList(request,request.toPage());
  }

  public IPage<MessageRecordRes> querySent(QueryMessageRecordReq request){
    IPage<MessageRecordRes> page = new Page<>(request.getPage(), request.getPageSize());
    return baseMapper.querySent(page,request);
  }
}

