package com.fzm.mall.server.admin.order.model;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.distribution.model.ParcelCenterGoods;
import com.fzm.mall.server.admin.order.mapper.OrderMapper;
import com.fzm.mall.server.admin.order.service.ILogService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 用户购物订单表
 * </p>
 *
 * @author wulixian
 * @since 2021-03-19
 */
@Data
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "Order对象", description = "用户购物订单表")
@TableName("order_info")
public class Order implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String ORDER_FRONT_REDIS_KEY = MALLGlobalConfig.PROJECT_PREFIX + "_good_order_";

    /**
     *  订单状态，1->交易成功
     */
    public static final Integer ORDER_STATE_SUCCESS = 1;

    /**
     *  订单状态，2->交易关闭
     */
    public static final Integer ORDER_STATE_CLOSED = 2;

    /**
     * 订单状态，4->待退款
     */
    public static final Integer ORDER_STATE_TO_REFUND = 4;

    /**
     * 订单状态，5->已退款
     */
    public static final Integer ORDER_STATE_REFUNDED = 5;

    /**
     * 订单状态，7->待支付
     */
    public static final Integer ORDER_STATE_TO_PAY = 7;

    /**
     * 订单状态，9->退款驳回
     */
    public static final Integer ORDER_STATE_REFUND_REFUSED = 9;

    /**
     * 订单状态，11->链上退款失败
     */
    public static final Integer ORDER_STATE_REFUNDED_ONCHAIN_FAIL = 11;

    /**
     * 订单状态，12->退款中
     */
    public static final Integer ORDER_STATE_REFUNDING = 12;

    /**
     * 订单状态，13->退款失败
     */
    public static final Integer ORDER_STATE_REFUND_FAIL = 13;

    /**
     * 订单状态，14->订单完成（订单完成n天后关闭）
     */
    public static final Integer ORDER_STATE_FINISH_AUTO_CLOSE = 14;

    @ApiModelProperty(value = "主键自增编号")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "订单编号")
    private String oid;

    @ApiModelProperty(value = "用户编号")
    private String uid;

    @ApiModelProperty(value = "支付单号")
    private String payId;

    @ApiModelProperty(value = "卖家编号")
    private String merchantId;

    @ApiModelProperty(value = "邮费")
    private BigDecimal postFee;

    @ApiModelProperty(value = "总价")
    private BigDecimal total;

    @ApiModelProperty(value = "是否接受数字货币：1->接受，2->不接受")
    private Integer acceptDigital;

    @ApiModelProperty(value = "支付方式，1->支付宝，2->微信，3->ccny")
    private Integer payType;

    @ApiModelProperty(value = "1.普通 2.预售 3.拍卖")
    private Integer orderType;

    @ApiModelProperty(value = "订单状态，1->交易成功，2->交易关闭，3->定金已付，4->待退款，5->已退款，6->支付中，7->待支付 ,8 撤销退款," +
            " 9->退款驳回, 10 待付尾款 11->链上退款失败 12->退款中 13->退款失败 14->交易结束")
    private Integer orderState;

    @ApiModelProperty(value = "是否直接提货，0->否，1->是")
    private Boolean delivery;

    @ApiModelProperty(value = "收货人名称")
    private String uname;

    @ApiModelProperty(value = "收人手机号")
    private String phone;

    @ApiModelProperty(value = "提货地址")
    private String address;

    @ApiModelProperty(value = "是否删除，0->否，1->是")
    private Boolean isDelete;

    @ApiModelProperty(value = "审核备注")
    private String vNote;

    @ApiModelProperty(value = "客户协商优惠金额")
    private BigDecimal consultDiscountAmount;

    @ApiModelProperty(value = "优惠类型(1->客户协商优惠)")
    private Integer consultDiscountType;

    @ApiModelProperty(value = "优惠金额")
    private BigDecimal discountAmount;

    @ApiModelProperty(value = "应付金额")
    private BigDecimal amount;

    @ApiModelProperty(value = "下单时间")
    @TableField(fill = FieldFill.INSERT)
    private Long createTime;

    @ApiModelProperty(value = "支付或取消时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Long updateTime;

    /**
     * 订单商品规格
     */
    @TableField(exist = false)
    private List<ParcelCenterGoods> goodsList;

    /**
     * 订单操作记录
     */
    @TableField(exist = false)
    private List<OrderLog> logList;

    /**
     * 支付信息记录
     */
    @TableField(exist = false)
    private List<PayRecord> payRecordList;

    /**
     * 订单操作记录
     *
     * @param orderLogService
     * @param oid
     */
    public void listLog(ILogService orderLogService, String oid) {
        List<OrderLog> orderLogList = orderLogService.listAll(oid);
        setLogList(orderLogList);
    }
}
