package com.bohuikeji.delivery.backstage.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.bohuikeji.frame.module.admin.thread.CachedThreadPool;
import com.bohuikeji.delivery.backstage.thread.OrderCalLBackThread;
import com.bohuikeji.frame.module.admin.annotation.TableKeyField;
import com.bohuikeji.frame.module.admin.annotation.TableQueryField;
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.annotation.TableShowField;
import com.bohuikeji.frame.module.admin.annotation.TableNewField;
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 2019-08-22
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@ApiModel(value="TOrderCircu对象", description="订单流转记录表")
@TableName("t_order_circu")
public class TOrderCircu extends EntityBase<TOrderCircu> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value="订单流转id(新增时请留空或null)")
    @TableId(value = "order_circu_id", type = IdType.AUTO)
    @TableShowField(value = "ID",id = true)
    @TableNewField(show = false)
    private Integer orderCircuId;

    @ApiModelProperty(value = "订单id")
    @TableField("order_id")
    @TableShowField("订单id")
    @TableNewField
    @TableQueryField
    @TableKeyField(key =TOrder.class,keyShowFields = "orderNum",keyLikeFields = "orderNum")
    private Integer orderId;

    @ApiModelProperty(value = "派送员id")
    @TableField("courier_id")
    @TableShowField("派送员")
    @TableNewField
    @TableQueryField
    @TableKeyField(table = "t_courier",keyShowFields = "orderNum",keyLikeFields = "orderNum",key = TCourier.class)
    private Integer courierId;

    @ApiModelProperty(value = "订单编号")
    @TableField("order_num")
    @TableShowField("订单编号")
    @TableNewField
    @TableQueryField
    @TableKeyField(key =TOrder.class,keyShowFields = "courierName",keyLikeFields = {"courierName","courierNumber","courierPhone"})
    private String orderNum;

    @ApiModelProperty(value = "变更后状态")
    @TableField("change_status")
    @TableShowField("变更后状态")
    @TableNewField
    private String changeStatus;

    @ApiModelProperty(value = "状态说明")
    @TableField("change_desc")
    @TableShowField("状态说明")
    @TableNewField
    private String changeDesc;

    @ApiModelProperty(value = "变更时间")
    @TableField("change_time")
    @TableShowField("变更时间")
    @TableNewField
    private LocalDateTime changeTime=LocalDateTime.now();

    /**
     * 插入订单流转记录
     * @param orderId
     * @param orderNum
     * @param changeStatus
     */
    public static boolean insertCircu(Integer orderId,Integer courierId,String orderNum,String changeStatus){
        TOrderCircu circu=new TOrderCircu();
        circu.setOrderId(orderId);
        circu.setCourierId(courierId);
        circu.setOrderNum(orderNum);
        circu.setChangeStatus(changeStatus);
        return circu.insert();

    }
    /**
     * 插入订单流转记录
     */
    public static void insertCircu(TOrder order){
        if(insertCircu(order.getOrderId(),order.getCourierId(),order.getOrderNum(),order.getOrderStatus())){
            //推送开放平台消息
            TOpenCallback callback=TOpenCallback.createInstance(order.getOrderNum(),order.getPlayerId());
            if (callback!=null){
                try {
                    CachedThreadPool.execute(new OrderCalLBackThread(callback,order.getOrderStatus()));
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        }

    }

