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

import cn.axzo.msg.center.dal.MNSBatchMessageRequestDao;
import cn.axzo.msg.center.dal.mapper.MNSBatchMessageRequestMapper;
import cn.axzo.msg.center.domain.entity.MNSBatchMessageRequest;
import cn.axzo.msg.center.domain.query.BatchMessageRequestQuery;
import cn.axzo.msg.center.notices.common.enums.BatchMessageStatusEnum;
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.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * <p>
 * 渠道模板变量序列表 Dao实现类
 * </p>
 *
 * @author szg
 * @since 2021-08-09
 */
@Repository("mnsBatchMessageRequestDao")
public class MNSBatchMessageRequestDaoImpl extends
        ServiceImpl<MNSBatchMessageRequestMapper, MNSBatchMessageRequest> implements MNSBatchMessageRequestDao {

  @Override
  public IPage<MNSBatchMessageRequest> queryByPage(BatchMessageRequestQuery query) {

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

  }

  @Override
  public MNSBatchMessageRequest queryBatchMessage(String appCode, String requestNo) {
    return this.lambdaQuery()
        .eq(MNSBatchMessageRequest::getAppNo, appCode)
        .eq(MNSBatchMessageRequest::getRequestNo, requestNo)
        .one();
  }

  @Override
  public boolean updateStatusById(Long id, Integer status) {
    return lambdaUpdate()
        .set(MNSBatchMessageRequest::getStatus,status)
        .eq(MNSBatchMessageRequest::getId,id)
        .eq(MNSBatchMessageRequest::getIsDelete, 0)
        .update();
  }

  @Override
  public boolean updateBatchMessageRequestInfoById(Long id, Integer status,
      Integer successCount, Integer failCount) {
    return lambdaUpdate()
        .set(MNSBatchMessageRequest::getStatus,status)
        .set(MNSBatchMessageRequest::getSuccessCount,successCount)
        .set(MNSBatchMessageRequest::getFailCount,failCount)
        .eq(MNSBatchMessageRequest::getId,id)
        .eq(MNSBatchMessageRequest::getIsDelete, 0)
        .update();
  }

  @Override
  public boolean updateRequestSuccess(String remark, Long id) {
    return lambdaUpdate()
        .set(MNSBatchMessageRequest::getRemark, remark)
        .set(MNSBatchMessageRequest::getStatus, BatchMessageStatusEnum.SEND_SUCCESS.getCode())
        .eq(MNSBatchMessageRequest::getId, id)
        .update();
  }

}
