package cn.axzo.msg.center.dal.impl;

import cn.axzo.msg.center.dal.MNSMessageDao;
import cn.axzo.msg.center.dal.mapper.MNSMessageMapper;
import cn.axzo.msg.center.domain.entity.MNSMessage;
import cn.axzo.msg.center.domain.query.MessageQuery;
import cn.axzo.msg.center.notices.common.enums.IsDeletedEnum;
import cn.axzo.msg.center.notices.common.enums.MessageStatusEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * <p>
 * 消息记录表 Dao实现类
 * </p>
 *
 * @author zhaoyong
 * @since 2021-05-19
 */
@Repository("mnsMessageDao")
public class MNSMessageDaoImpl extends ServiceImpl<MNSMessageMapper, MNSMessage> implements MNSMessageDao {

    @Override
    public IPage<MNSMessage> queryByPage(MessageQuery query) {
        return this.page(query.toPage(), new LambdaQueryWrapper<MNSMessage>()
                .eq(query.getId() != null, MNSMessage::getId, query.getId())
                .ge(query.getStartCreateTime() != null, MNSMessage::getCreateAt, query.getStartCreateTime())
                .le(query.getEndCreateTime() != null, MNSMessage::getCreateAt, query.getEndCreateTime())
                .in(CollectionUtils.isNotEmpty(query.getStatuses()), MNSMessage::getStatus, query.getStatuses())
                .eq(StringUtils.isNoneBlank(query.getChannelCode()), MNSMessage::getChannelCode, query.getChannelCode())
                .eq(MNSMessage::getIsDelete, IsDeletedEnum.NO.getCode())
                .orderByDesc(MNSMessage::getCreateAt));
    }

    @Override
    public boolean channelException(Long id) {
        return this.lambdaUpdate()
            .set(MNSMessage::getStatus, MessageStatusEnum.EXCEPTION.getCode())
            .set(MNSMessage::getUpdateAt, new Date())
            .eq(MNSMessage::getId, id)
            .eq(MNSMessage::getStatus, MessageStatusEnum.INIT.getCode())
            .update();
    }

    @Override
    public Boolean updateExceptionToFail(String messageOrderNo) {
        return lambdaUpdate()
            .set(MNSMessage::getStatus, MessageStatusEnum.FAIL.getCode())
            .eq(MNSMessage::getMessageOrderNo, messageOrderNo)
            .eq(MNSMessage::getStatus, MessageStatusEnum.EXCEPTION.getCode())
            .update();
    }

  @Override
  public MNSMessage getByBatchNoAndPhone(String batchNo, String mobile) {
    return lambdaQuery()
        .eq(MNSMessage::getBatchNo,batchNo)
        .eq(MNSMessage::getTargetAddress,mobile)
        .eq(MNSMessage::getStatus,MessageStatusEnum.INIT.getCode())
        .eq(MNSMessage::getIsDelete,0)
        .one();
  }

  @Override
  public MNSMessage getByChannelMsgId(String channelMsgId, String mobile) {
    return lambdaQuery()
        .eq(MNSMessage::getTargetAddress,mobile)
        .eq(MNSMessage::getChannelRequestId, channelMsgId)
        .eq(MNSMessage::getIsDelete,0)
        .one();
  }

  @Override
  public boolean updateBatchMessageChannelInfo(String channelCode, String channelName, String batchNo) {
    return lambdaUpdate()
        .set(MNSMessage::getChannelCode, channelCode)
        .set(MNSMessage::getChannelName, channelName)
        .eq(MNSMessage::getBatchNo, batchNo)
        .eq(MNSMessage::getIsDelete, IsDeletedEnum.NO.getCode())
        .update();
  }

  @Override
  public boolean updateAllBatchMessageFail(String errorMsg, String batchNo) {
    return lambdaUpdate()
        .set(MNSMessage::getRemark, errorMsg)
        .set(MNSMessage::getStatus, MessageStatusEnum.FAIL.getCode())
        .eq(MNSMessage::getBatchNo, batchNo)
        .eq(MNSMessage::getIsDelete, IsDeletedEnum.NO.getCode())
        .update();
  }

  @Override
  public boolean updateStatusAndRemarkById(Long id, String status, String remark) {

    return lambdaUpdate()
        .eq(MNSMessage::getId,id)
        .set(MNSMessage::getStatus,status)
        .set(MNSMessage::getUpdateAt, new Date())
        .set(!StringUtils.isEmpty(remark),MNSMessage::getRemark,remark)
        .update();
  }

}
