package com.red.circle.order.infra.database.rds.service.order.impl;

import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.order.infra.database.rds.dao.order.OrderAbnormalLogDAO;
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.model.enums.inapp.OrderAbnormalStatusEnum;
import java.util.Optional;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 存在丢失订单日志记录 服务实现类
 * </p>
 *
 * @author pengliang
 * @since 2020-11-24
 */
@Service
public class OrderAbnormalLogServiceImpl extends
    BaseServiceImpl<OrderAbnormalLogDAO, OrderAbnormalLog> implements OrderAbnormalLogService {

  @Override
  public void saveUnique(OrderAbnormalLog orderAbnormalLog) {
    try {
      save(orderAbnormalLog);
    } catch (DuplicateKeyException ignore) {
      // ignore
    }
  }


  @Override
  public String getCertificateById(Long id) {
    return Optional.ofNullable(query().select(OrderAbnormalLog::getCertificate)
            .eq(OrderAbnormalLog::getId, id)
            .last(PageConstant.LIMIT_ONE)
            .getOne())
        .map(OrderAbnormalLog::getCertificate)
        .orElse(null);
  }

  @Override
  public void updateStatus(Long id, OrderAbnormalStatusEnum status) {
    update().set(OrderAbnormalLog::getStatus, status)
        .eq(OrderAbnormalLog::getId, id)
        .last(PageConstant.LIMIT_ONE)
        .execute();
  }

}
