package com.bohuikeji.delivery.backstage.entity;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.frame.module.admin.annotation.TableNewField;
import com.bohuikeji.frame.module.admin.annotation.TableShowField;
import com.bohuikeji.frame.module.admin.base.EntityBase;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.TableField;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * <p>
 * 开放平台订单通知回调
 * </p>
 *
 * @author 10552
 * @since 2021-06-08
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@ApiModel(value="TOpenCallback对象", description="开放平台订单通知回调")
public class TOpenCallback extends EntityBase<TOpenCallback> {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    @TableShowField(value = "ID",id = true)
    @TableNewField(show = false)
    private Integer id;

    @ApiModelProperty(value = "订单ID")
    @TableField("order_id")
    @TableShowField("订单ID")
    @TableNewField
    private Integer orderId;

    @ApiModelProperty(value = "订单号")
    @TableField("order_num")
    @TableShowField("订单号")
    @TableNewField
    private String orderNum;

    @ApiModelProperty(value = "开放平台ID")
    @TableField("open_id")
    @TableShowField("开放平台ID")
    @TableNewField
    private Integer openId;

    @ApiModelProperty(value = "回调地址")
    @TableField("callback_url")
    @TableShowField("回调地址")
    @TableNewField
    private String callbackUrl;

    @ApiModelProperty(value = "回调类型")
    @TableField("callback_type")
    @TableShowField("回调类型")
    @TableNewField
    private String callbackType;

    @ApiModelProperty(value = "创建时间")
    @TableField("gmt_create")
    @TableShowField("创建时间")
    @TableNewField
    private LocalDateTime gmtCreate;

    @ApiModelProperty(value = "最后一次更新时间")
    @TableField("gmt_update")
    @TableShowField("最后一次更新时间")
    @TableNewField
    private LocalDateTime gmtUpdate;

    /**
     * 使用订单号
     * @param orderNum
     * @return
     */
    public static TOpenCallback createInstance(String orderNum,Integer customerId) {
        TOpenCallback callback=new TOpenCallback().selectOne(new QueryWrapper<TOpenCallback>().eq("order_num",orderNum));
        if (callback==null){
            TOpen open=new TOpen().selectOne((new QueryWrapper<TOpen>().eq("custemer_id",customerId)));
            if (open!=null&& StringUtils.isNotBlank(open.getNoticeUrl())){
                callback=new TOpenCallback();
                callback.setCallbackUrl(open.getNoticeUrl());
                callback.setOrderNum(orderNum);
                callback.setOpenId(open.getId());
            }
        }
        return callback;
    }
}

