package com.njzscloud.supervisory.order.pojo.param;

import com.njzscloud.common.mvc.validator.Constrained;
import com.njzscloud.common.mvc.validator.Constraint;
import com.njzscloud.common.mvc.validator.ValidRule;
import com.njzscloud.supervisory.order.contant.CarWay;
import com.njzscloud.supervisory.order.contant.OrderCategory;
import com.njzscloud.supervisory.order.contant.PayerCategory;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 订单信息
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
@Constraint
public class AddOrderInfoParam implements Constrained {
    /**
     * 装货地址 Id
     */
    @Valid
    @NotNull(message = "未指定装货地址")
    private AddOrderCargoPlaceParam cargoPlace;
    /**
     * 产品 Id; order_goods.id
     */
    private Long goodsId;
    private Long projectId;
    /**
     * 清运公司 Id
     */
    private Long transCompanyId;
    /**
     * 联系人
     */
    private String contacts;
    private String sn;

    /**
     * 联系方式
     */
    private String phone;

    /**
     * 期望运输时间
     */
    private String expectTime;

    /**
     * 预估量
     */
    private Integer estimatedQuantity;
    /**
     * 站点 Id
     */
    private Long stationId;

    /**
     * 目标站点 Id
     */
    private Long targetStationId;

    /**
     * 短驳入订单 Id
     */
    private Long targetOrderId;

    /**
     * 派车方式：项目-->project 平台-->platform  自选-->optional
     */
    private CarWay carWay;

    /**
     * 订单类型; 字典代码：order_category
     */
    private OrderCategory orderCategory;
    private Integer truckCount;

    /**
     * 付款方资金账户 Id
     */
    private Long payer;

    /**
     * 支付方类型; 字典代码：payer_category
     */
    private PayerCategory payerCategory;
    /**
     * 现场照片
     */
    private List<String> sitePhotos;
    /**
     * 客户备注
     */
    private String customerMemo;

    @Override
    public ValidRule[] rules() {
        return new ValidRule[]{
                ValidRule.of(() -> goodsId != null && goodsId >= 0, "未指定产品"),
                ValidRule.of(() -> stationId != null && stationId >= 0, "未指定站点"),
                ValidRule.of(() -> orderCategory != null, "未指定订单类型"),
                ValidRule.of(() -> orderCategory == OrderCategory.PuTong || orderCategory == OrderCategory.DuanBoChu, "订单类型必须是普通订单、短驳出"),
                ValidRule.of(() -> {
                    if (orderCategory != OrderCategory.PuTong) {
                        return targetStationId != null && targetStationId >= 0;
                    } else return true;
                }, "普通订单不能指定目标站点"),
        };
    }
}
