t7.product_inner_code as 'product_inner_code', -- 内部商品编码 new
t7.sum_quantity_init_advise as 'goods_quantity_init_advise', -- 商品本次的建议下单数量 new
t7.max_moq as 'goods_moq', -- 商品的moq new
t7.sku_codes as 'good_sku_codes', -- 商品涉及的下单所有sku new
t7.sum_history_fourteenday_sales as 'goods_history_fourteenday_sales', -- 商品的最近14日总日均销量
t1.history_fourteenday_sales as 'history_fourteenday_sales', -- sku的最近14日日均
0 as 'quantity_actual', -- 真实下单数量
@main_id as 'main_id',
t2.forecast_formula as 'forecast_formula',
t2.fit_forecast_formula as 'fit_forecast_formula',
t1.turnover_days,
t1.supplier_delivery,
t1.inspection_delivery,
t1.transfer_delivery,
t1.sales_upper_limit,
0 as 'ispush',
(case when t1.quantity_out_stock>0 then 2 when t1.`status` then 1 else 3 end) as 'type',
t6.suppliers_id,
t1.quantity_out_stock,
(
case when t6.buyer_name in ('张莹霞','张莹霞1') and t1.warehouse_code='GZBLWH' then 1
else 0
end
) as 'purchase_type_jit',
t1.auto_replace_logistics_task_id,
tr_task.logistics_code,
tr_task.transport_type as 'task_transport_type'
from dc_auto_turnover as t1
left join dc_auto_sales as t2 on t1.bailun_sku = t2.bailun_sku and t1.warehouse_code = t2.warehouse_code
left join dc_auto_config_sku_warehouse as t5 on t1.bailun_sku = t5.bailun_sku and t1.warehouse_code = t5.warehouse_code
left join dc_base_sku as t6 on t1.bailun_sku = t6.bailun_sku
left join view_goods_moq as t7 on t6.product_inner_code = t7.product_inner_code and t1.warehouse_code = t7.warehouse_code and t6.suppliers_id = t7.suppliers_id
left join dc_auto_jit_tag as t8 on t1.tag_id = t8.id
left join dc_auto_replace_logistics_task as tr_task on t1.auto_replace_logistics_task_id = tr_task.id
where t1.gmt_modified>=@btime
and t6.`status`=2 and (t1.history_fourteenday_sales>0 or t1.quantity_out_stock>0 ) and ( t5.`status`=0 or t5.`status` is null ) and t7.sum_quantity_init_advise>0
and (
( ( t6.buyer_name not in ('赵美聪','甄杰靖','张莹霞','张莹霞1','冯兆欣') or t1.warehouse_code not in ('GZBLWH','GZBLYS') ) and t1.warehouse_code!='BLGZ03' )
_connection.Execute(@" update dc_auto_purchase_advise_detailed as t1,
dc_base_sku as t2
set t1.quantity_final_advise=2
where t2.`status`=2 and t1.bailun_sku = t2.bailun_sku and t1.quantity_final_advise=1 and t1.quantity_init_advise<=0 and ( t2.buyer_name not in ('赵美聪','甄杰靖','张莹霞','张莹霞1') or t1.warehouse_code!='GZBLWH' ) and t1.main_id=@main_id ",new{main_id=mainID},commandTimeout:0);
// 初始建议数为1,,系统建议数为1时 改为3 ( 只补缺货的不管 )
_connection.Execute(@" update dc_auto_purchase_advise_detailed as t1,
dc_base_sku as t2
set quantity_final_advise=3
where t2.`status`=2 and t1.bailun_sku = t2.bailun_sku and quantity_final_advise=1 and quantity_init_advise=1 and ( t2.buyer_name not in ('赵美聪','甄杰靖','张莹霞','张莹霞1') or t1.warehouse_code!='GZBLWH' ) and main_id=@main_id ",new{main_id=mainID},commandTimeout:0);
//张莹霞1 and 广州01 去掉缺货数
_connection.Execute(@" update dc_auto_purchase_advise_detailed as t1,
dc_mid_transit as t2 ,
dc_base_sku as t3
set t1.quantity_final_advise=GREATEST(0,t1.quantity_final_advise-t2.quantity_out_stock)
where t1.bailun_sku = t2.bailun_sku and t1.warehouse_code = t2.warehouse_code and t1.bailun_sku=t3.bailun_sku
and t1.quantity_init_advise>=0 and ( t3.buyer_name in ('张莹霞1') and t1.warehouse_code='GZBLWH' ) and t1.main_id=@main_id ",new{main_id=mainID},commandTimeout:0);
// 调整因为舍弃小数导致的moq不足
vardatas=_connection.Query<dc_auto_purchase_advise_detailed>(" select * from dc_auto_purchase_advise_detailed where main_id=@main_id ",new{main_id=mainID},commandTimeout:0).AsList();
_connection.Execute(" delete from dc_auto_purchase_advise_detailed where id=@id ",new{id=item.id});
abatement-=item.quantity_final_advise;
}
else
{
_connection.Execute(" update dc_auto_purchase_advise_detailed set quantity_final_advise=@quantity_final_advise where id=@id ",new{id=item.id,quantity_final_advise=item.quantity_final_advise-abatement});