Browse Source

监测商品价格变化逻辑

shensong 5 years ago
parent
commit
4d9304b269
2 changed files with 105 additions and 3 deletions
  1. 1 1
      app/Console/Commands/Monitor.php
  2. 104 2
      app/Console/Commands/SyncMjWarehouse.php

+ 1 - 1
app/Console/Commands/Monitor.php

@@ -50,7 +50,7 @@ class Monitor extends Command {
50 50
             #发送验证码报警
51 51
             self::sendMsg('13161864516');
52 52
             self::sendMsg('18410900527');
53
-            error_log(date('Y-m-d H:i:s',time()).' pv:'.$pv." \n ",3,"/log/seafood_log/".date('Y-m-d',time()).'Monitor.log');
53
+            error_log(date('Y-m-d H:i:s',time()).' pv:'.$pv." \n ",3,"/log/seafood_log/script_success/".date('Y-m-d',time()).'Monitor.log');
54 54
         }
55 55
 
56 56
     }

+ 104 - 2
app/Console/Commands/SyncMjWarehouse.php

@@ -5,6 +5,14 @@ use Illuminate\Console\Command;
5 5
 use DB;
6 6
 use App\Order;
7 7
 use App\OrderScript;
8
+use App\libs\sms;
9
+//define("YP_SMS_KEY", "fbdb5f2ddae13c2f4a592348bfe52137");
10
+//define('YP_SMS_YHQ', '73a74eb72c42b765669acd8e94096b9f');
11
+//define("YP_SMS_KEY_FAMLI",'995629e02beaaf47118b84ac19c4b5b9');
12
+//define("YP_VOICE_URL", "http://voice.yunpian.com/v2/voice/send.json");
13
+//define("YP_TPL_URL", "https://sms.yunpian.com/v2/sms/tpl_single_send.json");
14
+//define("YP_TPL_ID", "3383382");
15
+
8 16
 class SyncMjWarehouse extends Command {
9 17
 
10 18
     protected $signature = 'SyncMjWarehouse';
@@ -17,16 +25,22 @@ class SyncMjWarehouse extends Command {
17 25
     protected $description = '同步库存';
18 26
     protected $limit = 20;
19 27
     protected $diffRealPriceSkuListArr = array();
28
+    protected $YP_TPL_ID = '3383382';
20 29
 
21 30
 
22 31
     public function handle()
23 32
     {
24 33
         $this->SyncMjWarehouse();
34
+        if(count($this->diffRealPriceSkuListArr) > 0){
35
+//            print_r(json_encode($this->diffRealPriceSkuListArr));
36
+            $this->skuRealPriceMonitor();
37
+        }
38
+
25 39
     }
26 40
     public function SyncMjWarehouse(){
27 41
         $params = array();
28 42
         $params['eshopCode'] = config('constants.ESHOP_CODE');
29
-        $params['warehouseCode'] = config('constants.WAREHOUSE_CODE'); //仓库编码
43
+      //  $params['warehouseCode'] = config('constants.WAREHOUSE_CODE'); //仓库编码
30 44
         $params['pageIndex'] = '1';
31 45
         $params['pageSize'] = $this->limit;
32 46
         $mjRes = OrderScript::mjWarehouseSkuGet($params);
@@ -61,7 +75,7 @@ class SyncMjWarehouse extends Command {
61 75
             //2.更新商品库存总成本(有可能为空)
62 76
             //3.计算商品真实价格
63 77
             //4.计算商品外部价格
64
-            $cost = DB::table('goods_skus')->where('code', $code)->select('referenceCost', 'is_weigh', 'outPrice' ,'realPrice')->first(); //获取规格成本/是否称重
78
+            $cost = DB::table('goods_skus')->where('code', $code)->select('code', 'referenceCost', 'is_weigh', 'outPrice' ,'realPrice')->first(); //获取规格成本/是否称重
65 79
             if($cost){
66 80
                 $quantity = $cost->is_weigh == 1 ? $sku['quantity']/2 : $sku['quantity']; //对应规格数量
67 81
                 //实际库存=库存-冻结
@@ -77,8 +91,16 @@ class SyncMjWarehouse extends Command {
77 91
                 if($sku['quantity'] != 0 ){
78 92
                     $up['realPrice'] = $sku['totalCost'] / $quantity;
79 93
 
94
+                    #监控realPrice变化是否超过10%
80 95
                     $newRealPrice = $up['realPrice'];
81 96
                     $oldRealPrice = $cost->realPrice;
97
+                    if($oldRealPrice != 0){
98
+                        $result = $this->getNumberDiffAbs($newRealPrice,$oldRealPrice);
99
+                        if($result >= 0.1){
100
+                            $this->diffRealPriceSkuListArr[] = $code;
101
+                        }
102
+                    }
103
+
82 104
                 }
83 105
 //                if($cost->outPrice == 0){
84 106
 //                    if(isset($up['realPrice']) && $up['realPrice'] != 0){
@@ -94,4 +116,84 @@ class SyncMjWarehouse extends Command {
94 116
         return true;
95 117
     }
96 118
 
119
+    /**
120
+     * 发送短信验证码通知
121
+     */
122
+    public function skuRealPriceMonitor(){
123
+        $text = json_encode($this->diffRealPriceSkuListArr);
124
+        #发送验证码报警
125
+        $this->sendMsg('13161864516',$text);
126
+//        self::sendMsg('18410900527');
127
+        error_log(date('Y-m-d H:i:s',time()).' skuList:'.$text." \n ",3,"/log/seafood_log/script_success/".date('Y-m-d',time()).'MonitorRealPrice.log');
128
+
129
+    }
130
+
131
+    /**
132
+     * 计算两个数之间差值的绝对值
133
+     * @param $newNumber
134
+     * @param $oldNumber
135
+     * @return float|int
136
+     */
137
+    public function getNumberDiffAbs($newNumber,$oldNumber){
138
+        $diff = abs(($newNumber-$oldNumber));
139
+        return $diff/$oldNumber;
140
+    }
141
+
142
+    private function init(){
143
+        $ch = curl_init();
144
+        /* 设置验证方式 */
145
+        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
146
+            'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
147
+        /* 设置返回结果为流 */
148
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
149
+
150
+        /* 设置超时时间*/
151
+        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
152
+
153
+        /* 设置通信方式 */
154
+        curl_setopt($ch, CURLOPT_POST, 1);
155
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
156
+        return $ch;
157
+
158
+    }
159
+
160
+    public function sendMsg($phone,$params){
161
+
162
+        $tpl_id = $this->YP_TPL_ID;
163
+        $ch=$this->init();
164
+        //$data=array('tpl_id' => $tpl_id,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
165
+        $data = [
166
+            'apikey' => YP_SMS_KEY,
167
+            'mobile' => $phone,
168
+            'tpl_id' => $tpl_id,
169
+            'tpl_value' => ('#sku_id#').'='.$params,
170
+        ];
171
+        $json_data = $this->tpl_send($ch,$data);
172
+        //print_r($json_data);    ******************************maybe影响验证码发出
173
+        $array = json_decode($json_data,true);
174
+        // echo '<pre>';print_r($array);
175
+        curl_close($ch);
176
+        return $array;
177
+    }
178
+
179
+    private function tpl_send($ch,$data){
180
+        curl_setopt ($ch, CURLOPT_URL, YP_TPL_URL);
181
+        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
182
+        $result = curl_exec($ch);
183
+        $error = curl_error($ch);
184
+        $this->checkErr($result,$error);
185
+        return $result;
186
+    }
187
+
188
+    private static function checkErr($result,$error) {
189
+        if($result === false)
190
+        {
191
+            echo 'Curl error: ' . $error;
192
+        }
193
+//        else
194
+//        {
195
+//            echo '操作完成没有任何错误';
196
+//        }
197
+    }
198
+
97 199
 }