package cn.pluss.platform.entity;

import cn.pluss.platform.constants.Constant;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.util.Date;

/**
 * ALTER TABLE `shouyinbei`.`tb_pluss_merchant_profit`
 * ADD COLUMN `profitRate` int(11) NULL COMMENT '上级收益费率' AFTER `merchantParentPhone`;
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class MerchantProfit {

    /**
     * 状态, 有效
     */
    public static final String STATUS_VALID = "1";

    /**
     * 状态, 无效, 对应订单可能已经退款
     */
    public static final String STATUS_INVALID = "0";

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    private Long userId;
    private String type;
    private Double price;
    private String orderNumber;
    private String merchantCode;
    private String merchantName;
    private Double consumeFee;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createDt;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date orderDt;
    private String status;

    private String payTypeCode;
    private String payTypeName;
    private String merchantLogo;
    /**
     * 当前获取受益人，也就是userId对应的商户或者代理商对应的等级
     */
    private Integer level;
    private Long merchantParentId;
    private String merchantParentName;
    private String merchantParentLogo;
    private String phone;
    private String userName;
    /**
     * 商户的直属上级等级
     */
    private String merchantParentLevel;
    private String merchantParentPhone;

    /**
     * 超级服务商业务员id
     */
    private String agentStaffId;

    @TableField(exist = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;
    @TableField(exist = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;

     @DateTimeFormat(pattern = "yyyy-MM-dd")
     @TableField(select = false, insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
     private String recordDate;
    //
    // @TableField(select = false, value = "recordDate", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER, condition = SqlConditionExtra.GE)
    // private Date recordDateStart;
    //
    // @TableField(select = false, value = "recordDate", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER, condition = SqlConditionExtra.LE)
    // private Date recordDateEnd;
    //
    // @TableField(select = false, insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER)
    // private String recordMonth;
    //
    // @TableField(select = false, value = "recordMonth", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER, condition = SqlConditionExtra.GE)
    // private String recordMonthStart;
    //
    // @TableField(select = false, value = "recordMonth", insertStrategy = FieldStrategy.NEVER, updateStrategy = FieldStrategy.NEVER, condition = SqlConditionExtra.LE)
    // private String recordMonthEnd;

    /**
     * 上级收益费率，只有超级服务商下的一级直推才有这个值
     */
    private Integer profitRate;

    @TableField(exist = false)
    private BigDecimal rate;

    /**
     * 用户类型
     */
    @TableField(exist = false)
    private String userType;

    public MerchantProfit(UserApp userApp, UserApp profitUserApp, MerchantOrder order,BigDecimal profitAmt,BigDecimal profitRate,String type,String orderType) {
        this.userId = profitUserApp.getUserId();
        //V2.0模式
        this.type = type;
        this.price = profitAmt.doubleValue();
        this.orderNumber = order.getOrderNumber();
        this.merchantCode = order.getMerchantCode();
        String merchantName = order.getMerchantName();
        if("2".equals(merchantName)){
            merchantName = merchantName + "【缴费通】";
        }
        this.merchantName = merchantName;
        this.consumeFee = order.getConsumeFee();
        this.createDt = order.getTransDt() == null ? new Date() : order.getTransDt();
        this.orderDt = order.getCreateDt();
        this.status = "1";
        this.payTypeCode = order.getPayTypeCode();
        this.payTypeName = order.getPayTypeName();

        this.merchantLogo = userApp.getLogo();
        this.level = userApp.getLevel();
        if(StringUtil.isNotEmpty(userApp.getPhone())){
            this.phone = StringUtil.isHideMobile(userApp.getPhone());
        }
        if(StringUtil.isNotEmpty(userApp.getUserName())){
            this.userName = StringUtil.hideSecret(userApp.getUserName());
        }
        this.merchantParentId = userApp.getParentId();
        this.profitRate = profitRate.intValue();
        if(StringUtil.isNotEmpty(userApp.getAgentStaffId()) && !"general".equals(type)){
            this.setAgentStaffId(String.valueOf(userApp.getAgentStaffId()));
        }
    }

    public MerchantProfit(UserApp userApp, UserApp profitUserApp, JftReceiptOrder order,BigDecimal profitAmt,BigDecimal profitRate,String type,String orderType) {
        this.userId = profitUserApp.getUserId();
        this.type = type;
        this.price = profitAmt.doubleValue();
        this.orderNumber = order.getOrderNumber();
        this.merchantCode = order.getMerchantCode();
        String merchantName = order.getMerchantName();
        if("2".equals(merchantName)){
            merchantName = merchantName + "【缴费通】";
        }
        this.merchantName = merchantName;
        this.consumeFee = order.getAmount().doubleValue();
        this.createDt = order.getTransTime() == null ? new Date() : order.getTransTime();
        this.orderDt = order.getCreateTime();
        this.status = "1";
        this.payTypeCode = order.getPayType();
        this.payTypeName = Constant.PAY_TYPE_ALIPAY.equals(order.getPayType()) ? Constant.PAY_TYPE_NAME_ALIPAY : Constant.PAY_TYPE_WECHAT.equals(order.getPayType()) ? Constant.PAY_TYPE_NAME_WECHAT : Constant.PAY_TYPE_NAME_YSFPAY ;
        if(StringUtil.isNotEmpty(userApp.getPhone())){
            this.phone = StringUtil.isHideMobile(userApp.getPhone());
        }
        if(StringUtil.isNotEmpty(userApp.getUserName())){
            this.userName = StringUtil.hideSecret(userApp.getUserName());
        }
        this.merchantLogo = userApp.getLogo();
        this.level = userApp.getLevel();
        this.merchantParentId = userApp.getParentId();
        this.profitRate = profitRate.intValue();
        if(StringUtil.isNotEmpty(userApp.getAgentStaffId()) && !"general".equals(type)){
            this.setAgentStaffId(String.valueOf(userApp.getAgentStaffId()));
        }
    }
}

