package com.bonus.canteen.core.order.common.model;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.bonus.canteen.core.order.common.constants.ApplyTypeEnum;
import com.bonus.canteen.core.order.common.constants.CheckStateEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

@TableName("order_refund")
public class OrderRefund {
   @TableId(
      type = IdType.INPUT
   )
   private Long orderRefundId;
   private Long orderId;
   private String macOrderId;
   private String macRefundId;
   private Long flowInstanceId;
   private BigDecimal refundAmount;
   private BigDecimal feeRate;
   private BigDecimal feeAmount;
   private BigDecimal realRefundAmount;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   private LocalDateTime applyTime;
   private String applyReason;
   private Integer applyType;
   private String resressPicurl;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   private LocalDateTime checkTime;
   private Integer checkState;
   private String checkResult;
   private String checkBy;
   private Integer ifQuickFail;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   private LocalDateTime crtime;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   private LocalDateTime uptime;

   public boolean ifCorrect() {
      return ApplyTypeEnum.CORRECT.getKey().equals(this.applyType);
   }

   public boolean ifWaitCheck() {
      return CheckStateEnum.WAIT.getKey().equals(this.checkState);
   }

   public void fillFeeAmountByFeeRate(BigDecimal feeRate) {
      this.feeRate = feeRate != null ? feeRate : BigDecimal.ZERO;
      this.feeAmount = BigDecimal.ZERO;
      if (this.feeRate.compareTo(BigDecimal.ZERO) > 0) {
         if (this.realRefundAmount != null && this.realRefundAmount.compareTo(BigDecimal.ZERO) > 0) {
            this.feeAmount = NumberUtil.mul(this.realRefundAmount, this.feeRate).setScale(0, RoundingMode.DOWN);
         } else if (this.refundAmount != null && this.refundAmount.compareTo(BigDecimal.ZERO) > 0) {
            this.feeAmount = NumberUtil.mul(this.refundAmount, this.feeRate).setScale(0, RoundingMode.DOWN);
         }
      }

   }

   public static String[] logFields() {
      return new String[]{"orderRefundId", "orderId", "macOrderId", "macRefundId", "refundAmount", "applyTime", "applyReason", "applyType", "resressPicurl", "realRefundAmount", "checkTime", "checkState", "checkResult", "checkBy"};
   }

   public static String[] logFieldsAmount() {
      return new String[]{"orderRefundId", "orderId", "refundAmount", "realRefundAmount", "checkState", "checkResult"};
   }

   public Long getOrderRefundId() {
      return this.orderRefundId;
   }

   public Long getOrderId() {
      return this.orderId;
   }

   public String getMacOrderId() {
      return this.macOrderId;
   }

   public String getMacRefundId() {
      return this.macRefundId;
   }

   public Long getFlowInstanceId() {
      return this.flowInstanceId;
   }

   public BigDecimal getRefundAmount() {
      return this.refundAmount;
   }

   public BigDecimal getFeeRate() {
      return this.feeRate;
   }

   public BigDecimal getFeeAmount() {
      return this.feeAmount;
   }

   public BigDecimal getRealRefundAmount() {
      return this.realRefundAmount;
   }

   public LocalDateTime getApplyTime() {
      return this.applyTime;
   }

   public String getApplyReason() {
      return this.applyReason;
   }

   public Integer getApplyType() {
      return this.applyType;
   }

   public String getResressPicurl() {
      return this.resressPicurl;
   }

   public LocalDateTime getCheckTime() {
      return this.checkTime;
   }

   public Integer getCheckState() {
      return this.checkState;
   }

   public String getCheckResult() {
      return this.checkResult;
   }

   public String getCheckBy() {
      return this.checkBy;
   }

   public Integer getIfQuickFail() {
      return this.ifQuickFail;
   }

   public LocalDateTime getCrtime() {
      return this.crtime;
   }

   public LocalDateTime getUptime() {
      return this.uptime;
   }

   public OrderRefund setOrderRefundId(final Long orderRefundId) {
      this.orderRefundId = orderRefundId;
      return this;
   }

   public OrderRefund setOrderId(final Long orderId) {
      this.orderId = orderId;
      return this;
   }

   public OrderRefund setMacOrderId(final String macOrderId) {
      this.macOrderId = macOrderId;
      return this;
   }

   public OrderRefund setMacRefundId(final String macRefundId) {
      this.macRefundId = macRefundId;
      return this;
   }

   public OrderRefund setFlowInstanceId(final Long flowInstanceId) {
      this.flowInstanceId = flowInstanceId;
      return this;
   }

   public OrderRefund setRefundAmount(final BigDecimal refundAmount) {
      this.refundAmount = refundAmount;
      return this;
   }

   public OrderRefund setFeeRate(final BigDecimal feeRate) {
      this.feeRate = feeRate;
      return this;
   }

   public OrderRefund setFeeAmount(final BigDecimal feeAmount) {
      this.feeAmount = feeAmount;
      return this;
   }

   public OrderRefund setRealRefundAmount(final BigDecimal realRefundAmount) {
      this.realRefundAmount = realRefundAmount;
      return this;
   }

   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   public OrderRefund setApplyTime(final LocalDateTime applyTime) {
      this.applyTime = applyTime;
      return this;
   }

   public OrderRefund setApplyReason(final String applyReason) {
      this.applyReason = applyReason;
      return this;
   }

   public OrderRefund setApplyType(final Integer applyType) {
      this.applyType = applyType;
      return this;
   }

   public OrderRefund setResressPicurl(final String resressPicurl) {
      this.resressPicurl = resressPicurl;
      return this;
   }

   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   public OrderRefund setCheckTime(final LocalDateTime checkTime) {
      this.checkTime = checkTime;
      return this;
   }

   public OrderRefund setCheckState(final Integer checkState) {
      this.checkState = checkState;
      return this;
   }

   public OrderRefund setCheckResult(final String checkResult) {
      this.checkResult = checkResult;
      return this;
   }

   public OrderRefund setCheckBy(final String checkBy) {
      this.checkBy = checkBy;
      return this;
   }

   public OrderRefund setIfQuickFail(final Integer ifQuickFail) {
      this.ifQuickFail = ifQuickFail;
      return this;
   }

   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   public OrderRefund setCrtime(final LocalDateTime crtime) {
      this.crtime = crtime;
      return this;
   }

   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   public OrderRefund setUptime(final LocalDateTime uptime) {
      this.uptime = uptime;
      return this;
   }
}
