package com.bonus.canteen.core.order.mobile.vo;

import com.bonus.common.houqin.constant.RetCodeEnum;
import com.bonus.canteen.core.order.common.vo.OrderPayResult;
import com.bonus.canteen.core.order.utils.LeNumUtil;
import com.bonus.canteen.core.pay.api.vo.UnifyPayVO;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@ApiModel("移动端订单支付返回结果")
public class OrderPayMobileVO implements OrderPayResult {
   @ApiModelProperty("错误码")
   private Integer code;
   @ApiModelProperty("支付结果信息描述")
   private String msg;
   @ApiModelProperty("交易id")
   private Long tradeId;
   @ApiModelProperty("支付所需参数字符串")
   private String payParamStr;
   @ApiModelProperty("库存不足的商品id集合")
   private Set<Long> goodIdSet;
   private Integer walletBal;
   private Integer subsidyBal;
   private Integer accBal;
   private Integer redEnvelope;
   private Integer payAmount;
   private Integer unpaidBalance;
   private JsonNode extParam;
   private BigDecimal lessAmountForDelivery;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   private LocalDateTime expireTime;
   private List<OrderListPayMobileVO> orderList;
   private List<OrderAmountChangeVO> orderAmountChangeList;

   @JsonIgnore
   public boolean success() {
      return this.code == null || RetCodeEnum.SUCC.getKey().equals(this.code);
   }

   public LocalDateTime getExpireTime() {
      LocalDateTime time = (LocalDateTime) LeNumUtil.reduceField(this.orderList, OrderListPayMobileVO::getExpireTime, (Object)null);
      return time != null && time.isAfter(LocalDateTime.now()) ? time : null;
   }

   public void fillPayVO(UnifyPayVO payVO) {
      this.fillPayResult(payVO, true);
      this.setTradeId(payVO.getTradeId());
      this.setPayParamStr(payVO.getPayParamStr());
      this.setPayAmount(payVO.getAmount() != null ? payVO.getAmount().intValue() : 0);
      if (payVO.getAccPayResultVO() != null) {
         this.setUnpaidBalance(payVO.getAccPayResultVO().getUnpaidBalanceInt());
         this.setAccBal(payVO.getAccPayResultVO().getAccBal() != null ? payVO.getAccPayResultVO().getAccBal().intValue() : null);
         this.setWalletBal(payVO.getAccPayResultVO().getWalletBal() != null ? payVO.getAccPayResultVO().getWalletBal().intValue() : null);
         this.setSubsidyBal(payVO.getAccPayResultVO().getSubBal() != null ? payVO.getAccPayResultVO().getSubBal().intValue() : null);
         this.setRedEnvelope(payVO.getAccPayResultVO().getRedEnvelopePay() != null ? payVO.getAccPayResultVO().getRedEnvelopePay().intValue() : null);
      }

   }

   public Integer getCode() {
      return this.code;
   }

   public String getMsg() {
      return this.msg;
   }

   public Long getTradeId() {
      return this.tradeId;
   }

   public String getPayParamStr() {
      return this.payParamStr;
   }

   public Set<Long> getGoodIdSet() {
      return this.goodIdSet;
   }

   public Integer getWalletBal() {
      return this.walletBal;
   }

   public Integer getSubsidyBal() {
      return this.subsidyBal;
   }

   public Integer getAccBal() {
      return this.accBal;
   }

   public Integer getRedEnvelope() {
      return this.redEnvelope;
   }

   public Integer getPayAmount() {
      return this.payAmount;
   }

   public Integer getUnpaidBalance() {
      return this.unpaidBalance;
   }

   public JsonNode getExtParam() {
      return this.extParam;
   }

   public BigDecimal getLessAmountForDelivery() {
      return this.lessAmountForDelivery;
   }

   public List<OrderListPayMobileVO> getOrderList() {
      return this.orderList;
   }

   public List<OrderAmountChangeVO> getOrderAmountChangeList() {
      return this.orderAmountChangeList;
   }

   public void setCode(final Integer code) {
      this.code = code;
   }

   public void setMsg(final String msg) {
      this.msg = msg;
   }

   public void setTradeId(final Long tradeId) {
      this.tradeId = tradeId;
   }

   public void setPayParamStr(final String payParamStr) {
      this.payParamStr = payParamStr;
   }

   public void setGoodIdSet(final Set<Long> goodIdSet) {
      this.goodIdSet = goodIdSet;
   }

   public void setWalletBal(final Integer walletBal) {
      this.walletBal = walletBal;
   }

   public void setSubsidyBal(final Integer subsidyBal) {
      this.subsidyBal = subsidyBal;
   }

   public void setAccBal(final Integer accBal) {
      this.accBal = accBal;
   }

   public void setRedEnvelope(final Integer redEnvelope) {
      this.redEnvelope = redEnvelope;
   }

   public void setPayAmount(final Integer payAmount) {
      this.payAmount = payAmount;
   }

   public void setUnpaidBalance(final Integer unpaidBalance) {
      this.unpaidBalance = unpaidBalance;
   }

   public void setExtParam(final JsonNode extParam) {
      this.extParam = extParam;
   }

   public void setLessAmountForDelivery(final BigDecimal lessAmountForDelivery) {
      this.lessAmountForDelivery = lessAmountForDelivery;
   }

   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   public void setExpireTime(final LocalDateTime expireTime) {
      this.expireTime = expireTime;
   }

   public void setOrderList(final List<OrderListPayMobileVO> orderList) {
      this.orderList = orderList;
   }

   public void setOrderAmountChangeList(final List<OrderAmountChangeVO> orderAmountChangeList) {
      this.orderAmountChangeList = orderAmountChangeList;
   }
}
