package com.clx.performance.sqlProvider;

import com.clx.order.enums.VehicleUsageEnum;
import com.clx.performance.enums.OrderGoodsTruckBindEnum;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.stream.Collectors;

public class OrderGoodsTruckBindSqlProvider {

    public String saveBatchEntity(String orderNo, String orderGoodsNo, List<String> trackList, String now) {
        StringBuffer sqlList = new StringBuffer();

        sqlList.append(" INSERT INTO order_goods_truck_bind(order_no,order_goods_no,truck_no,create_time,modified_time,status)  VALUES ");
        for (int i = 0; i < trackList.size(); i++) {
            String truckNo = trackList.get(i);
            sqlList.append(" ('").append(orderNo).append("','").append(orderGoodsNo).append("','").append(truckNo).append("','")
                    .append(now).append("','").append(now).append("','").append(OrderGoodsTruckBindEnum.Status.NORMAL.getCode())
                    .append("')");
            if (i < trackList.size() - 1) {
                sqlList.append(",");
            }
        }
        return sqlList.toString();
    }


    public String findUnreleasedTruck(){
        return new SQL(){{
            SELECT(" truck_no ");
            FROM("order_goods_truck_bind");
            WHERE("status IN ("  + OrderGoodsTruckBindEnum.Status.GET.getCode() + "," +
                    OrderGoodsTruckBindEnum.Status.NORMAL.getCode() + ")" );

        }}.toString();
    }
    public String selectListByTruckNo(List<String> truckList){
        return new SQL(){{
            SELECT(" a.order_goods_no ");
            FROM("order_goods_truck_bind a");
            LEFT_OUTER_JOIN("order_goods b on a.order_goods_no = b.order_goods_no");
            WHERE("a.truck_no IN ("  + truckList.stream().map(code -> "'"+code+"'").collect(Collectors.joining(","))+ ")" );
            WHERE("b.vehicle_usage = " + VehicleUsageEnum.Status.OWN.getCode()  + " or a.`status` =" + OrderGoodsTruckBindEnum.Status.NORMAL.getCode());
        }}.toString();
    }

}
