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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import com.bonus.canteen.core.common.utils.SysUtil;
import com.bonus.canteen.core.device.manage.model.DeviceInfo;
import com.bonus.canteen.core.order.common.constants.OrderStateMixMobileEnum;
import com.bonus.canteen.core.order.common.model.OrderDetail;
import com.bonus.canteen.core.order.common.model.OrderInfo;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class OrderListPayMobileVO {
   @ApiModelProperty("商家订单号")
   private Long orderId;
   @ApiModelProperty("设备订单号（设备下单时）")
   private String macOrderId;
   @ApiModelProperty("来源类型")
   private Integer sourceType;
   @ApiModelProperty("订单日期(区别于下单时间)")
   private LocalDate orderDate;
   @ApiModelProperty("食堂id")
   private Long canteenId;
   @ApiModelProperty("食堂名称")
   private String canteenName;
   @ApiModelProperty("档口id")
   private Long stallId;
   @ApiModelProperty("档口名称")
   private String stallName;
   @ApiModelProperty("餐次类型")
   private String mealtimeType;
   @ApiModelProperty("餐次名称")
   private String mealtimeName;
   @ApiModelProperty("设备序列号（设备下单时）")
   private String machineSn;
   @ApiModelProperty("设备编号（设备下单时）")
   private String machineNum;
   @ApiModelProperty("设备名称（设备下单时）")
   private String machineName;
   @ApiModelProperty("托盘物理号（自助餐）")
   private String plateSerialNum;
   @ApiModelProperty("托盘编号")
   private String plateNum;
   @ApiModelProperty("流水号")
   private String mealCode;
   @ApiModelProperty("订单总金额 单位为分")
   private BigDecimal payableAmount;
   @ApiModelProperty("打折减免 单位为分")
   private BigDecimal discountsAmount;
   @ApiModelProperty("餐券抵扣金额 单位为分")
   private BigDecimal couponAmount;
   @ApiModelProperty("配送费 单位为分")
   private BigDecimal deliveryAmount;
   @ApiModelProperty("包装费")
   private BigDecimal packingAmount;
   @ApiModelProperty("实际付款金额 单位为分")
   private BigDecimal realAmount;
   @ApiModelProperty("实际应付款金额 单位为分")
   private BigDecimal needPayAmount;
   @ApiModelProperty("已退款金额")
   private BigDecimal refundAmount;
   @ApiModelProperty("下单时间")
   private LocalDateTime orderTime;
   @ApiModelProperty("支付时间")
   private LocalDateTime payTime;
   @ApiModelProperty("支付方式")
   private Integer payType;
   @ApiModelProperty("支付状态 1 未支付 2 支付中  3 支付成功  4 支付失败  5 支付关闭")
   private Integer payState;
   @ApiModelProperty("扣款类型 1 下单扣款 2 核销扣款")
   private Integer deductionType;
   @ApiModelProperty("订单类型")
   private Integer orderType;
   @ApiModelProperty("订单状态 1 已下单 2 已完成 3 已取消")
   private Integer orderState;
   private Integer dishesState;
   @ApiModelProperty("订单退款状态 1 未申请 2 已退单 3 部分退单")
   private Integer orderRefundState;
   @ApiModelProperty("评价状态 1 已评价 2 未评价")
   private Integer commentState;
   @ApiModelProperty("审核状态 1 待审核 2 审核通过 3 审核失败")
   private Integer checkState;
   @ApiModelProperty("退款时间")
   private LocalDateTime refundTime;
   @ApiModelProperty("订单图片")
   private String imgUrl;
   @ApiModelProperty("订单明细数量")
   private Integer orderDetailSum;
   private Integer orderStateMixMobile;
   private BigDecimal lessAmountForDelivery;
   @DateTimeFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   @JsonFormat(
      pattern = "yyyy-MM-dd HH:mm:ss"
   )
   private LocalDateTime expireTime;
   @ApiModelProperty("订单明细list")
   private List<OrderDetailPayMobileVO> orderDetailList;

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

   public Integer getOrderDetailSum() {
      return CollUtil.size(this.orderDetailList);
   }

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

   public Integer getOrderStateMixMobile() {
      return OrderStateMixMobileEnum.mixStateBy(this.orderState, this.payState, this.orderRefundState, this.dishesState);
   }

   public void fillDeviceInfo(DeviceInfo deviceInfo) {
      if (deviceInfo != null) {
         this.machineSn = deviceInfo.getDeviceSn();
         this.machineNum = deviceInfo.getDeviceNum();
         this.machineName = deviceInfo.getDeviceName();
      }

   }

   public static List<OrderListPayMobileVO> listOf(List<OrderInfo> orderInfoList, List<OrderDetail> orderDetailList, Map<Long, Long> shoppingCartIdMapByDetailId) {
      if (CollUtil.isEmpty(orderInfoList)) {
         return CollUtil.newArrayList(new OrderListPayMobileVO[0]);
      } else {
         List<OrderListPayMobileVO> orderListMobileVOList = BeanUtil.copyToList(orderInfoList, OrderListPayMobileVO.class);
         if (CollUtil.isNotEmpty(orderDetailList)) {
            Iterator var4 = orderListMobileVOList.iterator();

            while(var4.hasNext()) {
               OrderListPayMobileVO orderListMobileVO = (OrderListPayMobileVO)var4.next();
               List<OrderDetailPayMobileVO> detailVOList = (List)orderDetailList.stream().filter((s) -> {
                  return s.getOrderId().equals(orderListMobileVO.getOrderId());
               }).map((s) -> {
                  return OrderDetailPayMobileVO.of(s, (Long)shoppingCartIdMapByDetailId.get(s.getDetailId()));
               }).collect(Collectors.toList());
               orderListMobileVO.setOrderDetailList(detailVOList);
            }
         }

         return orderListMobileVOList;
      }
   }

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

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

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

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

   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 String getMealtimeType() {
      return this.mealtimeType;
   }

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

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

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

   public String getMachineName() {
      return this.machineName;
   }

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

   public String getPlateNum() {
      return this.plateNum;
   }

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

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

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

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

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

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

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

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

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

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

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

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

   public Integer getDeductionType() {
      return this.deductionType;
   }

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

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

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

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

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

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

   public LocalDateTime getRefundTime() {
      return this.refundTime;
   }

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

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

   public List<OrderDetailPayMobileVO> getOrderDetailList() {
      return this.orderDetailList;
   }

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

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

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

   public OrderListPayMobileVO setOrderDate(final LocalDate orderDate) {
      this.orderDate = orderDate;
      return this;
   }

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

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

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

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

   public OrderListPayMobileVO setMealtimeType(final String mealtimeType) {
      this.mealtimeType = mealtimeType;
      return this;
   }

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

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

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

   public OrderListPayMobileVO setMachineName(final String machineName) {
      this.machineName = machineName;
      return this;
   }

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

   public OrderListPayMobileVO setPlateNum(final String plateNum) {
      this.plateNum = plateNum;
      return this;
   }

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

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

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

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

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

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

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

   public OrderListPayMobileVO setNeedPayAmount(final BigDecimal needPayAmount) {
      this.needPayAmount = needPayAmount;
      return this;
   }

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

   public OrderListPayMobileVO setOrderTime(final LocalDateTime orderTime) {
      this.orderTime = orderTime;
      return this;
   }

   public OrderListPayMobileVO setPayTime(final LocalDateTime payTime) {
      this.payTime = payTime;
      return this;
   }

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

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

   public OrderListPayMobileVO setDeductionType(final Integer deductionType) {
      this.deductionType = deductionType;
      return this;
   }

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

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

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

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

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

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

   public OrderListPayMobileVO setRefundTime(final LocalDateTime refundTime) {
      this.refundTime = refundTime;
      return this;
   }

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

   public OrderListPayMobileVO setOrderDetailSum(final Integer orderDetailSum) {
      this.orderDetailSum = orderDetailSum;
      return this;
   }

   public OrderListPayMobileVO setOrderStateMixMobile(final Integer orderStateMixMobile) {
      this.orderStateMixMobile = orderStateMixMobile;
      return this;
   }

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

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

   public OrderListPayMobileVO setOrderDetailList(final List<OrderDetailPayMobileVO> orderDetailList) {
      this.orderDetailList = orderDetailList;
      return this;
   }
}
