package com.bonus.canteen.core.order.domain.param;

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.order.constants.*;
import com.bonus.canteen.core.order.domain.OrderInfo;
import com.bonus.canteen.core.pay.constants.PayChannelEnum;
import com.bonus.canteen.core.pay.constants.PayStateEnum;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.security.utils.SecurityUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.Valid;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Data
public class ShopOrderAddParam {
    private @NotNull(
            message = "订单实付金额不能为空"
    ) @DecimalMin(
            value = "0",
            message = "订单实付金额不能小于0"
    ) BigDecimal realAmount;
    private @NotNull(
            message = "订单应付金额不能为空"
    ) @DecimalMin(
            value = "0",
            message = "订单应付金额不能小于0"
    ) BigDecimal payableAmount;
    @NotNull(message = "用户ID不能为空")
    private Long userId;
    @NotNull(message = "下单日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date placeDate;
    @NotNull(message = "支付类型不能为空")
    private Integer payType;
    @NotNull(message = "订单来源不能为空")
    private Integer sourceType;
    @NotNull(message = "订单在线离线状态为空")
    private Integer isOnline;
    @Valid
    @NotNull(message = "订单列表不能为空")
    private List<ShopOrderInfoAddParam> orderList;

    public List<OrderInfo> of(ShopOrderAddParam param) {
        List<OrderInfo> orderInfoList = new ArrayList<>();
        List<ShopOrderInfoAddParam> orderList = param.getOrderList();
        if(CollUtil.isNotEmpty(orderList)) {
            for(ShopOrderInfoAddParam orderInfoAddParam : orderList) {
                OrderInfo orderInfo = new OrderInfo();
                orderInfo.setDeviceOrderId(StringUtils.defaultString(orderInfoAddParam.getDeviceOrderId(), StringUtils.EMPTY));
                orderInfo.setDeviceSn(StringUtils.defaultString(orderInfoAddParam.getDeviceSn(), StringUtils.EMPTY));
                orderInfo.setDeviceNum(StringUtils.defaultString(orderInfoAddParam.getDeviceNum(), StringUtils.EMPTY));
                orderInfo.setUserId(param.getUserId());
                orderInfo.setSourceType(param.getSourceType());
                orderInfo.setCreateBy(SecurityUtils.getUsername());
                orderInfo.setCreateTime(DateUtils.getNowDate());
                orderInfo.setUpdateBy(SecurityUtils.getUsername());
                orderInfo.setUpdateTime(DateUtils.getNowDate());
                orderInfo.setPayType(param.getPayType());
                orderInfo.setOrderDate(orderInfoAddParam.getOrderDate());
                orderInfo.setOrderType(orderInfoAddParam.getOrderType());
                orderInfo.setIsOnline(Objects.isNull(param.getIsOnline()) ? 1 : param.getIsOnline());
                orderInfo.setOrderState(OrderStateEnum.WAIT_PLACE.getKey());
                orderInfo.setOrderRefundState(OrderRefundStateEnum.UN_REFUND.getKey());
                orderInfo.setDeductionType(DeductionTypeEnum.PAY_PLACE.getKey());
                orderInfo.setPayChannel(PayChannelEnum.ACC.getKey());
                orderInfo.setPayState(PayStateEnum.PAY_INPROCESS.getKey());
                orderInfo.setDeliveryAmount(BigDecimal.ZERO);
                orderInfo.setPackingAmount(BigDecimal.ZERO);
                orderInfo.setDeliveryType(DeliveryTypeEnum.SELF_TAKE.getKey());
                orderInfo.setCommentState(CommentStateEnum.UN_COMMENT.getKey());
                orderInfo.setOrderTime(DateUtils.getNowDate());
                orderInfo.setExternalPayAmount(BigDecimal.ZERO);
                orderInfo.setRefundAmount(BigDecimal.ZERO);
                orderInfo.setPayableAmount(param.getPayableAmount());
                orderInfo.setDiscountsAmount(BigDecimal.ZERO);
                orderInfo.setRealAmount(param.getRealAmount());
                orderInfo.setAccountPayAmount(param.getRealAmount());
                orderInfo.setCanteenId(-1L);
                orderInfo.setStallId(-1L);
                orderInfo.setMealtimeType(-1);
                orderInfo.setIdentityVerification(orderInfoAddParam.getIdentityVerification());
                orderInfoList.add(orderInfo);
            }
        }
        return orderInfoList;
    }
}
