package com.bonus.canteen.core.pay.api.vo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.pay.common.constants.PayStateEnum;
import com.bonus.canteen.core.pay.common.model.TradeRecord;
import com.bonus.canteen.core.pay.common.vo.TradeChannelVO;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

public class UnifyRefundSelectVO {
   private Integer code;
   private String msg;
   private Long tradeId;
   private String outTradeNo;
   private BigDecimal amount;
   private BigDecimal refundAmount;
   private List<TradeChannelVO> refundDetailList = CollUtil.newArrayList(new TradeChannelVO[0]);

   public BigDecimal getRefundAmount() {
      return PayStateEnum.isPaidState(this.code) && CollUtil.isNotEmpty(this.refundDetailList) ? (BigDecimal)this.refundDetailList.stream().filter((s) -> {
         return PayStateEnum.isPaidState(s.getTradeState());
      }).map(TradeChannelVO::getAmount).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add) : this.refundAmount;
   }

   public static UnifyRefundSelectVO of(TradeRecord tradeRecord) {
      UnifyRefundSelectVO androidPayVO = (UnifyRefundSelectVO)BeanUtil.copyProperties(tradeRecord, UnifyRefundSelectVO.class, new String[0]);
      androidPayVO.setTradeId(tradeRecord.getId());
      androidPayVO.setCode(tradeRecord.getTradeState());
      androidPayVO.setMsg(tradeRecord.getTradeResult());
      androidPayVO.setTradeId(tradeRecord.getId());
      androidPayVO.setRefundAmount(PayStateEnum.isPaidState(tradeRecord.getTradeState()) ? tradeRecord.getAmount() : BigDecimal.ZERO);
      return androidPayVO;
   }

   public static UnifyRefundSelectVO of(UnifyRefundVO unifyPayVO) {
      UnifyRefundSelectVO androidPayVO = (UnifyRefundSelectVO)BeanUtil.copyProperties(unifyPayVO, UnifyRefundSelectVO.class, new String[0]);
      androidPayVO.setTradeId(unifyPayVO.getRefundTradeId());
      return androidPayVO;
   }

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

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

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

   public String getOutTradeNo() {
      return this.outTradeNo;
   }

   public BigDecimal getAmount() {
      return this.amount;
   }

   public List<TradeChannelVO> getRefundDetailList() {
      return this.refundDetailList;
   }

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

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

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

   public UnifyRefundSelectVO setOutTradeNo(final String outTradeNo) {
      this.outTradeNo = outTradeNo;
      return this;
   }

   public UnifyRefundSelectVO setAmount(final BigDecimal amount) {
      this.amount = amount;
      return this;
   }

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

   public UnifyRefundSelectVO setRefundDetailList(final List<TradeChannelVO> refundDetailList) {
      this.refundDetailList = refundDetailList;
      return this;
   }
}
