package com.bonus.core.pay.common.po;

import com.bonus.core.common.utils.LogUtil;
import com.bonus.core.common.utils.TenantContextHolder;
import com.bonus.core.pay.api.vo.UnifyPayVO;
import com.bonus.core.pay.common.constants.PayStateEnum;
import com.bonus.core.pay.common.model.TradeRecord;
import com.bonus.framework.config.deserializer.DateTimeDeserializer;
import com.bonus.framework.config.serializer.DateTimeSerializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.time.LocalDateTime;

public class PayResultPO {
   private Long tradeId;
   private String traceId;
   private Long merchantId;
   private String outTradeNo;
   private Integer tradeType;
   private Integer tradeState;
   private String tradeResult;
   private Integer payChannel;
   @JsonSerialize(
      using = DateTimeSerializer.YYYY_MM_DD_HH_MM_SS.class
   )
   @JsonDeserialize(
      using = DateTimeDeserializer.class
   )
   private LocalDateTime payTime;

   public static PayResultPO of(TradeRecord tradeRecord) {
      PayResultPO payResultPO = new PayResultPO();
      payResultPO.setTradeId(tradeRecord.getId());
      payResultPO.setOutTradeNo(tradeRecord.getOutTradeNo());
      payResultPO.setTradeState(tradeRecord.getTradeState());
      payResultPO.setPayChannel(tradeRecord.getPayChannel());
      payResultPO.setMerchantId(TenantContextHolder.getTenantId());
      payResultPO.setTradeType(tradeRecord.getTradeType());
      payResultPO.setTradeResult(tradeRecord.getTradeResult());
      if (PayStateEnum.isPaidState(tradeRecord.getTradeState())) {
         payResultPO.setPayTime(tradeRecord.getTradeTime());
      }

      payResultPO.setTraceId(LogUtil.getCurrentTraceId());
      return payResultPO;
   }

   public static PayResultPO of(UnifyPayVO unifyPayVO) {
      PayResultPO payResultPO = new PayResultPO();
      payResultPO.setTradeId(unifyPayVO.getTradeId());
      payResultPO.setOutTradeNo(unifyPayVO.getOutTradeNo());
      payResultPO.setTradeState(unifyPayVO.getCode());
      payResultPO.setPayChannel(unifyPayVO.getPayChannel());
      payResultPO.setMerchantId(TenantContextHolder.getTenantId());
      payResultPO.setTradeType(unifyPayVO.getTradeType());
      payResultPO.setTradeResult(unifyPayVO.getMsg());
      payResultPO.setPayTime(unifyPayVO.getPayTime());
      payResultPO.setTraceId(LogUtil.getCurrentTraceId());
      return payResultPO;
   }

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

   public String getTraceId() {
      return this.traceId;
   }

   public Long getMerchantId() {
      return this.merchantId;
   }

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

   public Integer getTradeType() {
      return this.tradeType;
   }

   public Integer getTradeState() {
      return this.tradeState;
   }

   public String getTradeResult() {
      return this.tradeResult;
   }

   public Integer getPayChannel() {
      return this.payChannel;
   }

   public LocalDateTime getPayTime() {
      return this.payTime;
   }

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

   public void setTraceId(final String traceId) {
      this.traceId = traceId;
   }

   public void setMerchantId(final Long merchantId) {
      this.merchantId = merchantId;
   }

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

   public void setTradeType(final Integer tradeType) {
      this.tradeType = tradeType;
   }

   public void setTradeState(final Integer tradeState) {
      this.tradeState = tradeState;
   }

   public void setTradeResult(final String tradeResult) {
      this.tradeResult = tradeResult;
   }

   public void setPayChannel(final Integer payChannel) {
      this.payChannel = payChannel;
   }

   @JsonDeserialize(
      using = DateTimeDeserializer.class
   )
   public void setPayTime(final LocalDateTime payTime) {
      this.payTime = payTime;
   }

}
