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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.canteen.core.common.enums.DeliveryTypeEnum;
import com.bonus.canteen.core.customer.vo.CustPayVO;
import com.bonus.canteen.core.order.common.constants.DeductionTypeEnum;
import com.bonus.canteen.core.order.common.constants.DishesStateEnum;
import com.bonus.canteen.core.order.common.constants.OrderRefundStateEnum;
import com.bonus.canteen.core.order.common.constants.OrderStateEnum;
import com.bonus.canteen.core.order.utils.LeNumUtil;
import com.bonus.canteen.core.pay.api.dto.UnifyPayDTO;
import com.bonus.canteen.core.pay.common.constants.PayChannelEnum;
import com.bonus.canteen.core.pay.common.constants.PayStateEnum;
import com.bonus.canteen.core.pay.common.constants.PayTypeEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.format.annotation.DateTimeFormat;

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

@TableName("order_info")
public class OrderInfo {
   private Long orderId;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd"
   )
   private LocalDate orderDate;
   private String macOrderId;
   private String machineSn;
   private String machineNum;
   private String plateSerialNum;
   private Long custId;
   private Long orgId;
   private String orgNum;
   private String orgFullName;
   private Integer psnType;
   private String psnTypeName;
   private Long operatorId;
   private String operatorName;
   private Long cashierId;
   private String cashierName;
   private Integer nuClearMode;
   private Integer consumMode;
   private Integer sourceType;
   private Integer ifOnline;
   private Long canteenId;
   private String canteenName;
   private Long stallId;
   private String stallName;
   private Long roomId;
   private String roomName;
   private String permissionCode;
   private Integer mealtimeType;
   private String mealtimeName;
   private String mealCode;
   private String couponId;
   private BigDecimal payableAmount;
   private BigDecimal discountsAmount;
   private BigDecimal deliveryAmount;
   private BigDecimal packingAmount;
   private BigDecimal realAmount;
   private BigDecimal couponAmount;
   private BigDecimal accPayAmount;
   private BigDecimal outPayAmount;
   private BigDecimal refundAmount;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   private LocalDateTime orderTime;
   private Integer orderType;
   private Integer orderState;
   private Integer orderRefundState;
   /** @deprecated */
   @Deprecated
   private Integer deductionType;
   private String inventoryOutId;
   private Integer ifQuickFail;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   private LocalDateTime expireTime;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   private LocalDateTime payTime;
   private Integer payType;
   private Integer payChannel;
   private Integer payState;
   private String partPayReason;
   private String payParam;
   private Integer deliveryType;
   private Integer dishesState;
   private Integer commentState;
   private String imgUrl;
   private String remark;
   private JsonNode extParam;
   private String crby;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   private LocalDateTime crtime;
   private String upby;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   private LocalDateTime uptime;

   public String getPermissionCode() {
      long var10000 = this.canteenId != null ? this.canteenId : -1L;
      return "" + var10000 + "_" + (this.stallId != null ? this.stallId : -1L);
   }

   public BigDecimal calcNeedPayAmount() {
      return NumberUtil.sub(this.payableAmount, this.discountsAmount);
   }

   public BigDecimal calcUnRefundAmount() {
      return NumberUtil.sub(this.realAmount, this.refundAmount);
   }

   public void resetAmountsZero() {
      this.payableAmount = BigDecimal.ZERO;
      this.discountsAmount = BigDecimal.ZERO;
      this.couponAmount = BigDecimal.ZERO;
      this.deliveryAmount = BigDecimal.ZERO;
      this.packingAmount = BigDecimal.ZERO;
      this.realAmount = BigDecimal.ZERO;
      this.accPayAmount = BigDecimal.ZERO;
      this.outPayAmount = BigDecimal.ZERO;
      this.refundAmount = BigDecimal.ZERO;
   }

   public void resetState() {
      this.orderState = OrderStateEnum.WAIT_PLACE.getKey();
      this.payState = PayStateEnum.UN_PAY.getKey();
      this.orderRefundState = OrderRefundStateEnum.UN_APPLY.getKey();
      this.deductionType = DeductionTypeEnum.PAY_PLACE.getKey();
      this.dishesState = DishesStateEnum.UN_MAKE.getKey();
      this.commentState = LeConstants.COMMON_NO;
   }

   public void plusAmounts(OrderInfo additionalOrderInfo) {
      this.payableAmount = NumberUtil.add(this.payableAmount, additionalOrderInfo.getPayableAmount());
      this.discountsAmount = NumberUtil.add(this.discountsAmount, additionalOrderInfo.getDiscountsAmount());
      this.couponAmount = NumberUtil.add(this.couponAmount, additionalOrderInfo.getCouponAmount());
      this.deliveryAmount = NumberUtil.add(this.deliveryAmount, additionalOrderInfo.getDeliveryAmount());
      this.packingAmount = NumberUtil.add(this.packingAmount, additionalOrderInfo.getPackingAmount());
      this.realAmount = NumberUtil.add(this.realAmount, additionalOrderInfo.getRealAmount());
      this.accPayAmount = NumberUtil.add(this.accPayAmount, additionalOrderInfo.getAccPayAmount());
      this.outPayAmount = NumberUtil.add(this.outPayAmount, additionalOrderInfo.getOutPayAmount());
      this.refundAmount = NumberUtil.add(this.refundAmount, additionalOrderInfo.getRefundAmount());
   }

   public void plusDiscountAmount(BigDecimal discountAmount) {
      this.discountsAmount = NumberUtil.add(this.discountsAmount, discountAmount);
   }

   public void plusRefundAmount(BigDecimal refundAmount) {
      this.refundAmount = NumberUtil.add(this.refundAmount, refundAmount);
   }

   public OrderInfo copyInstance() {
      return (OrderInfo)BeanUtil.copyProperties(this, OrderInfo.class, new String[0]);
   }

   public static OrderInfo newDefaultInstance() {
      OrderInfo orderInfo = new OrderInfo();
      orderInfo.setIfOnline(LeConstants.COMMON_YES);
      orderInfo.setOrderDate(LocalDate.now());
      orderInfo.setOrderTime(LocalDateTime.now());
      orderInfo.setPayType(PayTypeEnum.MEAL_CARD.getKey());
      orderInfo.setPayChannel(PayChannelEnum.ACC.getKey());
      orderInfo.setDeliveryType(DeliveryTypeEnum.SELF_TAKE.getKey());
      orderInfo.setIfQuickFail(LeConstants.COMMON_YES);
      orderInfo.resetAmountsZero();
      orderInfo.resetState();
      return orderInfo;
   }

   public void fillCustInfo(CustPayVO custPayVO) {
      if (custPayVO != null) {
         this.custId = custPayVO.getCustId();
         this.orgId = custPayVO.getOrgId();
         this.orgNum = custPayVO.getOrgNum();
         this.orgFullName = custPayVO.getOrgFullName();
         this.psnType = custPayVO.getPsnType();
         this.psnTypeName = custPayVO.getPsnTypeName();
      }

   }
//
//   public void fillCustInfo(CustInfo custInfo) {
//      if (custInfo != null) {
//         this.custId = custInfo.getCustId();
//         this.orgId = custInfo.getOrgId();
//         this.orgNum = custInfo.getOrgNum();
//         this.orgFullName = custInfo.getOrgFullName();
//         this.psnType = custInfo.getPsnType();
//         this.psnTypeName = custInfo.getPsnTypeName();
//      }
//
//   }

   public void fillCustInfo(UnifyPayDTO payDTO) {
      if (payDTO != null) {
         this.custId = payDTO.getCustId();
         this.orgId = payDTO.getOrgId();
         this.orgNum = payDTO.getOrgNum();
         this.orgFullName = payDTO.getOrgFullName();
         this.psnType = payDTO.getPsnType();
         this.psnTypeName = payDTO.getPsnTypeName();
      }

   }

   public boolean hasCustInfo(Long custId) {
      return LeNumUtil.idOrDefault(this.custId).equals(LeNumUtil.idOrDefault(custId)) && CharSequenceUtil.isNotBlank(this.orgFullName);
   }

   public static String[] logFields() {
      return new String[]{"orderId", "macOrderId", "machineSn", "custId", "sourceType", "canteenId", "stallId", "orderType", "orderDate", "orderTime", "orderState", "orderRefundState", "payType", "payChannel", "payState", "deliveryType", "dishesState", "commentState", "inventoryOutId", "ifQuickFail", "payableAmount", "discountsAmount", "couponAmount", "deliveryAmount", "packingAmount", "realAmount", "accPayAmount", "outPayAmount", "refundAmount", "payParam", "remark"};
   }

   public static String[] logFieldsRelated() {
      return new String[]{"orderId", "macOrderId", "machineSn", "custId", "sourceType", "canteenId", "stallId", "orderDate", "mealtimeType", "orderType"};
   }

   public static String[] logFieldsAmount() {
      return new String[]{"orderId", "macOrderId", "payableAmount", "discountsAmount", "couponAmount", "deliveryAmount", "packingAmount", "realAmount", "accPayAmount", "outPayAmount", "refundAmount"};
   }

   public static String[] logFieldsState() {
      return new String[]{"orderId", "macOrderId", "orderType", "orderState", "orderRefundState", "payState", "dishesState", "commentState", "ifQuickFail"};
   }

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

   public LocalDate getOrderDate() {
      return this.orderDate;
   }

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

   public String getMachineSn() {
      return this.machineSn;
   }

   public String getMachineNum() {
      return this.machineNum;
   }

   public String getPlateSerialNum() {
      return this.plateSerialNum;
   }

   public Long getCustId() {
      return this.custId;
   }

   public Long getOrgId() {
      return this.orgId;
   }

   public String getOrgNum() {
      return this.orgNum;
   }

   public String getOrgFullName() {
      return this.orgFullName;
   }

   public Integer getPsnType() {
      return this.psnType;
   }

   public String getPsnTypeName() {
      return this.psnTypeName;
   }

   public Long getOperatorId() {
      return this.operatorId;
   }

   public String getOperatorName() {
      return this.operatorName;
   }

   public Long getCashierId() {
      return this.cashierId;
   }

   public String getCashierName() {
      return this.cashierName;
   }

   public Integer getNuClearMode() {
      return this.nuClearMode;
   }

   public Integer getConsumMode() {
      return this.consumMode;
   }

   public Integer getSourceType() {
      return this.sourceType;
   }

   public Integer getIfOnline() {
      return this.ifOnline;
   }

   public Long getCanteenId() {
      return this.canteenId;
   }

   public String getCanteenName() {
      return this.canteenName;
   }

   public Long getStallId() {
      return this.stallId;
   }

   public String getStallName() {
      return this.stallName;
   }

   public Long getRoomId() {
      return this.roomId;
   }

   public String getRoomName() {
      return this.roomName;
   }

   public Integer getMealtimeType() {
      return this.mealtimeType;
   }

   public String getMealtimeName() {
      return this.mealtimeName;
   }

   public String getMealCode() {
      return this.mealCode;
   }

   public String getCouponId() {
      return this.couponId;
   }

   public BigDecimal getPayableAmount() {
      return this.payableAmount;
   }

   public BigDecimal getDiscountsAmount() {
      return this.discountsAmount;
   }

   public BigDecimal getDeliveryAmount() {
      return this.deliveryAmount;
   }

   public BigDecimal getPackingAmount() {
      return this.packingAmount;
   }

   public BigDecimal getRealAmount() {
      return this.realAmount;
   }

   public BigDecimal getCouponAmount() {
      return this.couponAmount;
   }

   public BigDecimal getAccPayAmount() {
      return this.accPayAmount;
   }

   public BigDecimal getOutPayAmount() {
      return this.outPayAmount;
   }

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

   public LocalDateTime getOrderTime() {
      return this.orderTime;
   }

   public Integer getOrderType() {
      return this.orderType;
   }

   public Integer getOrderState() {
      return this.orderState;
   }

   public Integer getOrderRefundState() {
      return this.orderRefundState;
   }

   /** @deprecated */
   @Deprecated
   public Integer getDeductionType() {
      return this.deductionType;
   }

   public String getInventoryOutId() {
      return this.inventoryOutId;
   }

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

   public LocalDateTime getExpireTime() {
      return this.expireTime;
   }

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

   public Integer getPayType() {
      return this.payType;
   }

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

   public Integer getPayState() {
      return this.payState;
   }

   public String getPartPayReason() {
      return this.partPayReason;
   }

   public String getPayParam() {
      return this.payParam;
   }

   public Integer getDeliveryType() {
      return this.deliveryType;
   }

   public Integer getDishesState() {
      return this.dishesState;
   }

   public Integer getCommentState() {
      return this.commentState;
   }

   public String getImgUrl() {
      return this.imgUrl;
   }

   public String getRemark() {
      return this.remark;
   }

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

   public String getCrby() {
      return this.crby;
   }

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

   public String getUpby() {
      return this.upby;
   }

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

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

   @JsonFormat(
      pattern = "yyyy-MM-dd"
   )
   public void setOrderDate(final LocalDate orderDate) {
      this.orderDate = orderDate;
   }

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

   public void setMachineSn(final String machineSn) {
      this.machineSn = machineSn;
   }

   public void setMachineNum(final String machineNum) {
      this.machineNum = machineNum;
   }

   public void setPlateSerialNum(final String plateSerialNum) {
      this.plateSerialNum = plateSerialNum;
   }

   public void setCustId(final Long custId) {
      this.custId = custId;
   }

   public void setOrgId(final Long orgId) {
      this.orgId = orgId;
   }

   public void setOrgNum(final String orgNum) {
      this.orgNum = orgNum;
   }

   public void setOrgFullName(final String orgFullName) {
      this.orgFullName = orgFullName;
   }

   public void setPsnType(final Integer psnType) {
      this.psnType = psnType;
   }

   public void setPsnTypeName(final String psnTypeName) {
      this.psnTypeName = psnTypeName;
   }

   public void setOperatorId(final Long operatorId) {
      this.operatorId = operatorId;
   }

   public void setOperatorName(final String operatorName) {
      this.operatorName = operatorName;
   }

   public void setCashierId(final Long cashierId) {
      this.cashierId = cashierId;
   }

   public void setCashierName(final String cashierName) {
      this.cashierName = cashierName;
   }

   public void setNuClearMode(final Integer nuClearMode) {
      this.nuClearMode = nuClearMode;
   }

   public void setConsumMode(final Integer consumMode) {
      this.consumMode = consumMode;
   }

   public void setSourceType(final Integer sourceType) {
      this.sourceType = sourceType;
   }

   public void setIfOnline(final Integer ifOnline) {
      this.ifOnline = ifOnline;
   }

   public void setCanteenId(final Long canteenId) {
      this.canteenId = canteenId;
   }

   public void setCanteenName(final String canteenName) {
      this.canteenName = canteenName;
   }

   public void setStallId(final Long stallId) {
      this.stallId = stallId;
   }

   public void setStallName(final String stallName) {
      this.stallName = stallName;
   }

   public void setRoomId(final Long roomId) {
      this.roomId = roomId;
   }

   public void setRoomName(final String roomName) {
      this.roomName = roomName;
   }

   public void setPermissionCode(final String permissionCode) {
      this.permissionCode = permissionCode;
   }

   public void setMealtimeType(final Integer mealtimeType) {
      this.mealtimeType = mealtimeType;
   }

   public void setMealtimeName(final String mealtimeName) {
      this.mealtimeName = mealtimeName;
   }

   public void setMealCode(final String mealCode) {
      this.mealCode = mealCode;
   }

   public void setCouponId(final String couponId) {
      this.couponId = couponId;
   }

   public void setPayableAmount(final BigDecimal payableAmount) {
      this.payableAmount = payableAmount;
   }

   public void setDiscountsAmount(final BigDecimal discountsAmount) {
      this.discountsAmount = discountsAmount;
   }

   public void setDeliveryAmount(final BigDecimal deliveryAmount) {
      this.deliveryAmount = deliveryAmount;
   }

   public void setPackingAmount(final BigDecimal packingAmount) {
      this.packingAmount = packingAmount;
   }

   public void setRealAmount(final BigDecimal realAmount) {
      this.realAmount = realAmount;
   }

   public void setCouponAmount(final BigDecimal couponAmount) {
      this.couponAmount = couponAmount;
   }

   public void setAccPayAmount(final BigDecimal accPayAmount) {
      this.accPayAmount = accPayAmount;
   }

   public void setOutPayAmount(final BigDecimal outPayAmount) {
      this.outPayAmount = outPayAmount;
   }

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

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

   public void setOrderType(final Integer orderType) {
      this.orderType = orderType;
   }

   public void setOrderState(final Integer orderState) {
      this.orderState = orderState;
   }

   public void setOrderRefundState(final Integer orderRefundState) {
      this.orderRefundState = orderRefundState;
   }

   /** @deprecated */
   @Deprecated
   public void setDeductionType(final Integer deductionType) {
      this.deductionType = deductionType;
   }

   public void setInventoryOutId(final String inventoryOutId) {
      this.inventoryOutId = inventoryOutId;
   }

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

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

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

   public void setPayType(final Integer payType) {
      this.payType = payType;
   }

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

   public void setPayState(final Integer payState) {
      this.payState = payState;
   }

   public void setPartPayReason(final String partPayReason) {
      this.partPayReason = partPayReason;
   }

   public void setPayParam(final String payParam) {
      this.payParam = payParam;
   }

   public void setDeliveryType(final Integer deliveryType) {
      this.deliveryType = deliveryType;
   }

   public void setDishesState(final Integer dishesState) {
      this.dishesState = dishesState;
   }

   public void setCommentState(final Integer commentState) {
      this.commentState = commentState;
   }

   public void setImgUrl(final String imgUrl) {
      this.imgUrl = imgUrl;
   }

   public void setRemark(final String remark) {
      this.remark = remark;
   }

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

   public void setCrby(final String crby) {
      this.crby = crby;
   }

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

   public void setUpby(final String upby) {
      this.upby = upby;
   }

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