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

import cn.axzo.msg.center.dal.MNSBatchMessageDao;
import cn.axzo.msg.center.dal.mapper.MNSBatchMessageMapper;
import cn.axzo.msg.center.domain.dto.BatchMessageStatisticsResponseDto;
import cn.axzo.msg.center.domain.entity.MNSBatchMessage;
import cn.axzo.msg.center.domain.query.BatchMessageQuery;
import cn.axzo.msg.center.notices.common.enums.IsDeletedEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 渠道模板变量序列表 Dao实现类
 * </p>
 *
 * @author szg
 * @since 2021-08-09
 */
@Repository("mnsBatchMessageDao")
public class MNSBatchMessageDaoImpl extends ServiceImpl<MNSBatchMessageMapper, MNSBatchMessage> implements MNSBatchMessageDao {

  @Resource
  private MNSBatchMessageMapper batchMessageMapper;

  @Override
  public IPage<MNSBatchMessage> queryByPage(BatchMessageQuery query) {

    return this.page(query.toPage(), new LambdaQueryWrapper<MNSBatchMessage>()
        .eq(query.getId() != null, MNSBatchMessage::getId, query.getId())
        .ge(query.getStartCreateTime() != null, MNSBatchMessage::getCreateAt, query.getStartCreateTime())
        .le(query.getEndCreateTime() != null, MNSBatchMessage::getCreateAt, query.getEndCreateTime())
        .in(CollectionUtils.isNotEmpty(query.getStatuses()), MNSBatchMessage::getStatus, query.getStatuses())
        .eq(Objects.nonNull(query.getSendType()),MNSBatchMessage::getSendType,query.getSendType())
        .eq(MNSBatchMessage::getIsDelete, 0)
        .orderByDesc(MNSBatchMessage::getCreateAt)
    );
  }

  @Override
  public MNSBatchMessage queryBatchMessage(String batchNo) {
    return this.lambdaQuery()
        .eq(MNSBatchMessage::getBatchNo, batchNo)
        .eq(MNSBatchMessage::getIsDelete, IsDeletedEnum.NO.getCode())
        .one();
  }

  @Override
  public boolean updateBatchMessage(MNSBatchMessage batchMessage, String batchNo) {
    return this.update(batchMessage, new UpdateWrapper<MNSBatchMessage>()
        .eq("batch_no", batchNo)
        .eq("is_delete", IsDeletedEnum.NO.getCode()));
  }

  @Override
  public boolean updateChannelInfo(String channelCode, String channelTemplateNo, String batchNo) {
    return this.lambdaUpdate()
        .set(MNSBatchMessage::getChannelCode, channelCode)
        .set(MNSBatchMessage::getChannelTemplateNo, channelTemplateNo)
        .eq(MNSBatchMessage::getBatchNo, batchNo)
        .eq(MNSBatchMessage::getIsDelete, IsDeletedEnum.NO.getCode())
        .update();
  }

  @Override
  public BatchMessageStatisticsResponseDto getStatisticsMessageInfo(String appNo,
                                                                    String requestNo) {

    return batchMessageMapper.getStatisticsMessageInfo(appNo,requestNo);
  }

  @Override
  public Boolean updateStatusById(Long id, Integer status) {
    return lambdaUpdate()
        .set(MNSBatchMessage::getStatus,status)
        .eq(MNSBatchMessage::getId,id)
        .eq(MNSBatchMessage::getIsDelete, IsDeletedEnum.NO.getCode())
        .update();
  }

  @Override
  public Boolean updateBachMessageInfoById(Long id, Integer status,
      Integer successCount, Integer failCount) {
    return lambdaUpdate()
        .set(MNSBatchMessage::getStatus, status)
        .set(MNSBatchMessage::getSuccessCount,successCount)
        .set(MNSBatchMessage::getFailCount,failCount)
        .eq(MNSBatchMessage::getId, id)
        .eq(MNSBatchMessage::getIsDelete, IsDeletedEnum.NO.getCode())
        .update();
  }

  @Override
  public MNSBatchMessage getByChannelMsgIdAndLock(String msgId) {
    return batchMessageMapper.getByChannelMsgIdAndLock(msgId);

  }

}
