123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use DB;
- use App\Order;
- use App\OrderScript;
- use App\libs\sms;
- //define("YP_SMS_KEY", "fbdb5f2ddae13c2f4a592348bfe52137");
- //define('YP_SMS_YHQ', '73a74eb72c42b765669acd8e94096b9f');
- //define("YP_SMS_KEY_FAMLI",'995629e02beaaf47118b84ac19c4b5b9');
- //define("YP_VOICE_URL", "http://voice.yunpian.com/v2/voice/send.json");
- //define("YP_TPL_URL", "https://sms.yunpian.com/v2/sms/tpl_single_send.json");
- //define("YP_TPL_ID", "3383382");
- class SyncMjWarehouse extends Command {
- protected $signature = 'SyncMjWarehouse';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '同步库存';
- protected $limit = 20;
- protected $diffRealPriceSkuListArr = array();
- protected $YP_TPL_ID = '3383382';
- public function handle()
- {
- try {
- $this->SyncMjWarehouse();
- // $this->diffRealPriceSkuListArr = array(
- // 0=>array('code'=>'banjiexia-03','oldPrice'=>'10','newPrice'=>'20'),
- // 1=>array('code'=>'csxpxx-01','oldPrice'=>'100','newPrice'=>'50'),
- // );
- if (count($this->diffRealPriceSkuListArr) > 0) {
- $this->skuRealPriceMonitor();
- }
- }catch (\Exception $exception){
- echo 'line:'.$exception->getLine().' message:'.$exception->getMessage();
- }
- }
- public function SyncMjWarehouse(){
- $params = array();
- $params['eshopCode'] = config('constants.ESHOP_CODE');
- $params['warehouseCode'] = config('constants.WAREHOUSE_CODE'); //仓库编码
- $params['pageIndex'] = '1';
- $params['pageSize'] = $this->limit;
- $mjRes = OrderScript::mjWarehouseSkuGet($params);
- if($mjRes == false){
- exit('获取卖家云数据出错');
- }
- //插入第一页:
- $this->insertData( $mjRes['resultSet']['skuList'] );
- $count = $mjRes['resultSet']['totalNum'];
- $pages = ceil($count / $this->limit);
- //插入其他页面
- for($i=2; $i<=$pages; $i++){
- $params['pageIndex'] = $i;
- $mjRes = OrderScript::mjWarehouseSkuGet($params);
- $this->insertData( $mjRes['resultSet']['skuList'] );
- }
- }
- /**
- * 获取订单信息,可用来判断订单是否存在,同步订单状态
- */
- public function insertData($data){
- foreach($data as $k=>$sku){
- $code = $sku['skuProductCode'];
- $up = array();
- //1.更新商品库存数量(有可能为负数,有可能为0)
- //2.更新商品库存总成本(有可能为空)
- //3.计算商品真实价格
- //4.计算商品外部价格
- $cost = DB::table('goods_skus')->where('code', $code)->select('code', 'referenceCost', 'is_weigh', 'outPrice' ,'realPrice')->first(); //获取规格成本/是否称重
- if($cost){
- $quantity = $cost->is_weigh == 1 ? $sku['quantity']/2 : $sku['quantity']; //对应规格数量
- //实际库存=库存-冻结
- if(isset($sku['fi']) && ($sku['fi'] < 0)){
- $real_quantity = $sku['quantity'];
- } else {
- $real_quantity = $sku['quantity'] - $sku['fi'];
- }
- $real_quantity = $cost->is_weigh == 1 ? $real_quantity/2 : $real_quantity; //对应规格数量
- $up['quantity'] = $real_quantity;
- $up['totalCost'] = isset($sku['totalCost']) ? $sku['totalCost'] : 0;
- if($sku['quantity'] != 0 ){
- if(isset($sku['totalCost'])){
- $up['realPrice'] = $sku['totalCost'] / $quantity;
- #监控realPrice变化是否超过10%
- $newRealPrice = $up['realPrice'];
- $oldRealPrice = $cost->realPrice;
- if($oldRealPrice != 0){
- $result = $this->getNumberDiffAbs($newRealPrice,$oldRealPrice);
- if($result >= 0.1){
- $json['code'] = $code;
- $json['oldPrice'] = $oldRealPrice;
- $json['newPrice'] = $newRealPrice;
- $this->diffRealPriceSkuListArr[] = $json;
- }
- }
- }
- }
- // if($cost->outPrice == 0){
- // if(isset($up['realPrice']) && $up['realPrice'] != 0){
- // $up['outPrice'] = ceil($up['realPrice']*1.15);
- // }
- // }
- $up['cost'] = isset($sku['cost']) ? $sku['cost'] : 0;
- $up_re = DB::table('goods_skus')->where('code', $code)->update($up);
- }
- }
- return true;
- }
- /**
- * 发送短信验证码通知
- */
- public function skuRealPriceMonitor(){
- $text = json_encode($this->diffRealPriceSkuListArr);
- #发送验证码报警
- $this->sendMsg('13161864516',$text);
- $this->sendMsg('18410900527',$text);
- // 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');
- }
- /**
- * 计算两个数之间差值的绝对值
- * @param $newNumber
- * @param $oldNumber
- * @return float|int
- */
- public function getNumberDiffAbs($newNumber,$oldNumber){
- $diff = abs(($newNumber-$oldNumber));
- return $diff/$oldNumber;
- }
- private function init(){
- $ch = curl_init();
- /* 设置验证方式 */
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
- 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
- /* 设置返回结果为流 */
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- /* 设置超时时间*/
- curl_setopt($ch, CURLOPT_TIMEOUT, 10);
- /* 设置通信方式 */
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- return $ch;
- }
- public function sendMsg($phone,$params){
- $tpl_id = $this->YP_TPL_ID;
- $ch=$this->init();
- //$data=array('tpl_id' => $tpl_id,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
- $data = [
- 'apikey' => YP_SMS_KEY,
- 'mobile' => $phone,
- 'tpl_id' => $tpl_id,
- 'tpl_value' => ('#sku_id#').'='.$params,
- ];
- $json_data = $this->tpl_send($ch,$data);
- //print_r($json_data); ******************************maybe影响验证码发出
- $array = json_decode($json_data,true);
- // echo '<pre>';print_r($array);
- curl_close($ch);
- return $array;
- }
- private function tpl_send($ch,$data){
- curl_setopt ($ch, CURLOPT_URL, YP_TPL_URL);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
- $result = curl_exec($ch);
- $error = curl_error($ch);
- $this->checkErr($result,$error);
- return $result;
- }
- private static function checkErr($result,$error) {
- if($result === false)
- {
- echo 'Curl error: ' . $error;
- }
- // else
- // {
- // echo '操作完成没有任何错误';
- // }
- }
- }
|