package com.red.circle.order.inner.service.impl;

import com.red.circle.framework.dto.PageResult;
import com.red.circle.order.infra.database.rds.entity.order.OrderAbnormalLog;
import com.red.circle.order.infra.database.rds.service.order.OrderAbnormalLogService;
import com.red.circle.order.inner.convertor.OrderAbnormalLogInnerConvertor;
import com.red.circle.order.inner.model.cmd.inapp.OrderAbnormalPageQryCmd;
import com.red.circle.order.inner.model.dto.inapp.OrderAbnormalLogDTO;
import com.red.circle.order.inner.model.enums.inapp.OrderAbnormalStatusEnum;
import com.red.circle.order.inner.service.OrderAbnormalLogClientService;
import com.red.circle.tool.core.text.StringUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 异常订单.
 *
 * @author pengliang on 2023/11/3
 */
@Service
@RequiredArgsConstructor
public class OrderAbnormalLogClientServiceImpl implements OrderAbnormalLogClientService {

  private final OrderAbnormalLogService orderAbnormalLogService;
  private final OrderAbnormalLogInnerConvertor orderAbnormalLogInnerConvertor;

  @Override
  public PageResult<OrderAbnormalLogDTO> pageOps(OrderAbnormalPageQryCmd qryCmd) {
    return orderAbnormalLogInnerConvertor.toPageOrderAbnormalLogDTO(
        orderAbnormalLogService.query()
            .eq(OrderAbnormalLog::getStatus, qryCmd.getStatus())
            .eq(StringUtils.isNotBlank(qryCmd.getSysOrigin()), OrderAbnormalLog::getSysOrigin,
                qryCmd.getSysOrigin())
            .eq(StringUtils.isNotBlank(qryCmd.getPlatform()), OrderAbnormalLog::getPlatform,
                qryCmd.getPlatform())
            .ge(Objects.nonNull(qryCmd.getStartTime()), OrderAbnormalLog::getCreateTime,
                qryCmd.startTimeToLocalDateTime())
            .le(Objects.nonNull(qryCmd.getEndTime()), OrderAbnormalLog::getCreateTime,
                qryCmd.endTimeToLocalDateTime())
            .orderByDesc(OrderAbnormalLog::getId)
            .page(qryCmd.getPageQuery())
    );
  }

  @Override
  public String getCertificateById(Long id) {
    return orderAbnormalLogService.getCertificateById(id);
  }

  @Override
  public void updateStatus(Long id, OrderAbnormalStatusEnum status) {
    orderAbnormalLogService.updateStatus(id, status);
  }

}
