Quellcode durchsuchen

计费模型按单计费单量分类为TOC单

Zhouzhendong vor 5 Jahren
Ursprung
Commit
a0a5edd221

+ 4 - 1
app/Console/Commands/BeforeCreateOwnerReport.php

@@ -42,7 +42,10 @@ class BeforeCreateOwnerReport extends Command
                     "counting_month" => date("Y-m-d"),
                     "created_at"     => $date
                 ];
-                Cache::put(date("Y-m")."|".$owner->id,0,2764800);
+                //B:business C:customer D:date
+                Cache::put(date("Y-m")."|B|".$owner->id,0,2764800);
+                Cache::put(date("Y-m")."|C|".$owner->id,0,2764800);
+                Cache::put(date("Y-m")."|D|".$owner->id,time(),2764800);
             }
             OwnerReport::query()->insert($insert);
         }

+ 7 - 10
app/Http/Controllers/TestController.php

@@ -56,6 +56,7 @@ use App\OwnerFeeDetail;
 use App\OwnerFeeDetailLogistic;
 use App\OwnerPriceOperation;
 use App\OwnerPriceOperationItem;
+use App\OwnerReport;
 use App\OwnerStoragePriceModel;
 use App\Package;
 use App\Process;
@@ -156,16 +157,12 @@ class TestController extends Controller
         dd(Region::query()->where("id",">=",404)->where("id","<=",432)->delete());
     }
     public function tt1(){
-        $owners = Owner::query()->withCount(["ownerPriceDirectLogistics","ownerPriceLogistics","ownerPriceExpresses","ownerPriceOperations","ownerStoragePriceModels"])->get();
-        foreach ($owners as $owner){
-            $arr = [];
-            if ($owner->owner_storage_price_models_count > 0)$arr[] = 0;
-            if ($owner->owner_price_operations_count > 0)$arr[] = 1;
-            if ($owner->owner_price_expresses_count > 0)$arr[] = 2;
-            if ($owner->owner_price_logistics_count > 0)$arr[] = 3;
-            if ($owner->owner_price_direct_logistics_count > 0)$arr[] = 4;
-            $owner->update(["relevance"=>json_encode($arr)]);
-        }
+        $logistics = [1,2,3];
+        $logisticSql = "(''";
+        foreach ($logistics as $logistic)$logisticSql.=",".$logistic;
+        $logisticSql .= ")";
+        $sql = "SELECT COUNT(1) c FROM orders WHERE logistic_id IN {$logisticSql} AND wms_status = ? AND wms_edittime BETWEEN ? AND ?";
+        dd($sql);
     }
     public function zzd(){
         ini_set('max_execution_time',2500);

+ 73 - 3
app/Services/OrderService.php

@@ -14,6 +14,8 @@ use App\OrderIssue;
 use App\Owner;
 use App\OwnerFeeDetail;
 use App\OwnerFeeDetailLogistic;
+use App\OwnerPriceExpress;
+use App\OwnerReport;
 use App\Province;
 use App\RejectedBill;
 use App\Services\common\BatchUpdateService;
@@ -995,6 +997,76 @@ class OrderService
         return Order::query()->create($values);
     }
 
+    /**
+     * 订单量丢失补偿逻辑
+     *
+     * @param int $owner
+     * @param array|null $logistics
+     */
+    private function orderQuantityCompensationLogic($owner, $logistics = null)
+    {
+        if (!$logistics)$logistics = app("OwnerPriceExpressService")->getBuildLogistic($owner);
+        $query = DB::raw(<<<sql
+SELECT COUNT(1) count,logistic_id FROM orders WHERE wms_status = '订单完成' 
+AND owner_id = ?
+AND wms_edittime like ? GROUP BY logistic_id
+sql
+        );
+        $statistics = DB::select($query,[$owner,date("Y-m")."%"]);
+        $toB = 0;
+        $toC = 0;
+        foreach ($statistics as $statistic){
+            if (array_search($statistic->logistic_id,$logistics)===false)$toB += $statistic->count;
+            else $toC += $statistic->count;
+        }
+        Cache::put(date("Y-m")."|B|".$owner,$toB,2764800);
+        Cache::put(date("Y-m")."|C|".$owner,$toC,2764800);
+    }
+
+    /**
+     * 设置订单量
+     *
+     * @param int $owner
+     * @param int $logistic
+     */
+    public function setOrderQuantity($owner, $logistic)
+    {
+        $logistics = app("OwnerPriceExpressService")->getBuildLogistic($owner);
+        $date = date("Y-m");
+        if (array_search($logistic,$logistics)===false)$type = "|B|";
+        else $type = "|C|";
+        if (!Cache::has($date.$type.$owner)){
+            //补偿逻辑
+            $this->orderQuantityCompensationLogic($owner,$logistics);
+        }
+        Cache::increment($date.$type.$owner);
+        $key = $date."|D|".$owner;
+        if (!Cache::has($key) || time()-Cache::get($key)>60){
+            //向数据库持久化
+            OwnerReport::query()->where("owner_id",$owner)
+                ->where("counting_month","like",$date."%")
+                ->update(["to_business_quantity"=>Cache::get($date."|B|".$owner),"to_customer_quantity"=>Cache::get($date."|C|".$owner)]);
+        }
+        Cache::put($key,time());
+    }
+
+    /**
+     * 获取货主下有效订单量 对C端
+     *
+     * @param int $owner
+     * @param bool $isToB
+     *
+     * @return int
+     */
+    public function getOrderQuantity($owner, $isToB)
+    {
+        $date = date("Y-m");
+        if ($isToB)$type = "|B|";
+        else $type = "|C|";
+        if (!Cache::has($date.$type.$owner))$this->orderQuantityCompensationLogic($owner);
+        return Cache::get($date.$type.$owner);
+    }
+
     /**
      * 生成即时账单
      *
@@ -1008,9 +1080,7 @@ class OrderService
         if (!$order || $order->wms_status != "订单完成")return true;
         if (OwnerFeeDetail::query()->where("outer_table_name","orders")->where("outer_id",$order->id)->first())return true;
 
-        $key = date("Y-m")."_".$order->owner_id;
-        if (Cache::has($key))Cache::increment($key);
-        else Cache::put($key,1,2678400);
+        $this->setOrderQuantity($order->owner_id,$order->logistic_id);
 
         $order->loadMissing(["logistic","shop","packages.commodities.commodity","batch"]);
 

+ 21 - 0
app/Services/OwnerPriceExpressService.php

@@ -117,6 +117,27 @@ class OwnerPriceExpressService
         return $query->update($values);
     }
 
+    /**
+     * 获取绑定承运商
+     *
+     * @param $owner
+     * @return array
+     */
+    public function getBuildLogistic($owner)
+    {
+        return app(CacheService::class)->getOrExecute("logistics_owner_".$owner,function ()use($owner){
+            $query = DB::raw(<<<sql
+    SELECT logistic_id FROM `owner_price_express_owner` e 
+    LEFT JOIN owner_price_express_logistic l 
+    ON e.owner_price_express_id = l.owner_price_express_id 
+    WHERE e.owner_id = ?
+sql
+            );
+            $logistics = DB::select($query,[$owner]);
+            return array_column($logistics,"logistic_id");
+        },null);
+    }
+
     /**
      * CODE: -1:未找到计费模型 -2:重量无效
      *

+ 1 - 1
app/Services/OwnerPriceOperationService.php

@@ -124,7 +124,7 @@ class OwnerPriceOperationService
 
         if (!$rules)return -2;  //规则不存在跳出
 
-        $total = Cache::get(date("Y-m")."|".$matchObject["owner_id"]); //获取该货主本月单量
+        $total = app("OrderService")->getOrderQuantity($owner_id);//获取该货主本月C端单量
         foreach ($rules as $rule){
             if (!$rule->items)continue; //不存在子规则跳出
             $isDiscount = false;    //是否存在满减

+ 6 - 2
app/Services/OwnerStoragePriceModelService.php

@@ -59,16 +59,20 @@ class OwnerStoragePriceModelService
             case "按单减免":
                 if ($owner_id && $month){
                     if ($model->timeUnit->name == '月'){
-                        $report = OwnerReport::query()->select("id","total")
+                        $report = OwnerReport::query()->select("id",DB::raw("(to_business_quantity+to_customer_quantity) AS total"))
                             ->where("owner_id",$owner_id)
                             ->where("counting_month","like",$month."%")->first();
                         if ($report && $report->total>=0)$area -= floor($report->total/$model->discount_value);
                     }else{
                         $days = date('t', strtotime($month."-01"));
                         $area *= $days;
+                        $logistics = app("OwnerPriceExpressService")->getBuildLogistic($owner_id);
+                        $logisticSql = "(''";
+                        foreach ($logistics as $logistic)$logisticSql.=",".$logistic;
+                        $logisticSql .= ")";
                         for($i=1;$i<=$days;$i++){
                             $d = $i<10 ? "0".$i : $i;
-                            $query = DB::raw("SELECT COUNT(1) c FROM orders WHERE wms_status = ? and updated_at between ? and ?");
+                            $query = DB::raw("SELECT COUNT(1) c FROM orders WHERE logistic_id IN {$logisticSql} AND wms_status = ? AND wms_edittime BETWEEN ? AND ?");
                             $count = DB::selectOne($query,['订单完成',$month."-".$d." 00:00:00",$month."-".$d." 23:59:59"]);
                             if ($count && $count->c>=0)$area -= floor($count->c/$model->discount_value);
                         }

+ 1 - 1
resources/views/customer/project/index.blade.php

@@ -98,7 +98,7 @@
                 owners : [
                     @foreach($owners as $owner)
                     {   id : "{{$owner->id}}",
-                        relevance:@json($owner->relevance ?? []),
+                        relevance:{!! $owner->relevance ?? [] !!},
                         customer_name:"{{$owner->customer ? $owner->customer->name : ''}}",
                         //tax_rate  : "{{--{{$owner->tax_rate}}--}}",
                         name : "{{$owner->name}}",