Geen omschrijving

SyncMjWarehouse.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use App\Order;
  6. use App\OrderScript;
  7. use App\libs\sms;
  8. //define("YP_SMS_KEY", "fbdb5f2ddae13c2f4a592348bfe52137");
  9. //define('YP_SMS_YHQ', '73a74eb72c42b765669acd8e94096b9f');
  10. //define("YP_SMS_KEY_FAMLI",'995629e02beaaf47118b84ac19c4b5b9');
  11. //define("YP_VOICE_URL", "http://voice.yunpian.com/v2/voice/send.json");
  12. //define("YP_TPL_URL", "https://sms.yunpian.com/v2/sms/tpl_single_send.json");
  13. //define("YP_TPL_ID", "3383382");
  14. class SyncMjWarehouse extends Command {
  15. protected $signature = 'SyncMjWarehouse';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '同步库存';
  22. protected $limit = 20;
  23. protected $diffRealPriceSkuListArr = array();
  24. protected $YP_TPL_ID = '3383382';
  25. public function handle()
  26. {
  27. try {
  28. $this->SyncMjWarehouse();
  29. // $this->diffRealPriceSkuListArr = array(
  30. // 0=>array('code'=>'banjiexia-03','oldPrice'=>'10','newPrice'=>'20'),
  31. // 1=>array('code'=>'csxpxx-01','oldPrice'=>'100','newPrice'=>'50'),
  32. // );
  33. if (count($this->diffRealPriceSkuListArr) > 0) {
  34. $this->skuRealPriceMonitor();
  35. }
  36. }catch (\Exception $exception){
  37. echo 'line:'.$exception->getLine().' message:'.$exception->getMessage();
  38. }
  39. }
  40. public function SyncMjWarehouse(){
  41. $params = array();
  42. $params['eshopCode'] = config('constants.ESHOP_CODE');
  43. $params['warehouseCode'] = config('constants.WAREHOUSE_CODE'); //仓库编码
  44. $params['pageIndex'] = '1';
  45. $params['pageSize'] = $this->limit;
  46. $mjRes = OrderScript::mjWarehouseSkuGet($params);
  47. if($mjRes == false){
  48. exit('获取卖家云数据出错');
  49. }
  50. //插入第一页:
  51. $this->insertData( $mjRes['resultSet']['skuList'] );
  52. $count = $mjRes['resultSet']['totalNum'];
  53. $pages = ceil($count / $this->limit);
  54. //插入其他页面
  55. for($i=2; $i<=$pages; $i++){
  56. $params['pageIndex'] = $i;
  57. $mjRes = OrderScript::mjWarehouseSkuGet($params);
  58. $this->insertData( $mjRes['resultSet']['skuList'] );
  59. }
  60. }
  61. /**
  62. * 获取订单信息,可用来判断订单是否存在,同步订单状态
  63. */
  64. public function insertData($data){
  65. foreach($data as $k=>$sku){
  66. $code = $sku['skuProductCode'];
  67. $up = array();
  68. //1.更新商品库存数量(有可能为负数,有可能为0)
  69. //2.更新商品库存总成本(有可能为空)
  70. //3.计算商品真实价格
  71. //4.计算商品外部价格
  72. $cost = DB::table('goods_skus')->where('code', $code)->select('code', 'referenceCost', 'is_weigh', 'outPrice' ,'realPrice')->first(); //获取规格成本/是否称重
  73. if($cost){
  74. $quantity = $cost->is_weigh == 1 ? $sku['quantity']/2 : $sku['quantity']; //对应规格数量
  75. //实际库存=库存-冻结
  76. if(isset($sku['fi']) && ($sku['fi'] < 0)){
  77. $real_quantity = $sku['quantity'];
  78. } else {
  79. $real_quantity = $sku['quantity'] - $sku['fi'];
  80. }
  81. $real_quantity = $cost->is_weigh == 1 ? $real_quantity/2 : $real_quantity; //对应规格数量
  82. $up['quantity'] = $real_quantity;
  83. $up['totalCost'] = isset($sku['totalCost']) ? $sku['totalCost'] : 0;
  84. if($sku['quantity'] != 0 ){
  85. if(isset($sku['totalCost'])){
  86. $up['realPrice'] = $sku['totalCost'] / $quantity;
  87. #监控realPrice变化是否超过10%
  88. $newRealPrice = $up['realPrice'];
  89. $oldRealPrice = $cost->realPrice;
  90. if($oldRealPrice != 0){
  91. $result = $this->getNumberDiffAbs($newRealPrice,$oldRealPrice);
  92. if($result >= 0.1){
  93. $json['code'] = $code;
  94. $json['oldPrice'] = $oldRealPrice;
  95. $json['newPrice'] = $newRealPrice;
  96. $this->diffRealPriceSkuListArr[] = $json;
  97. }
  98. }
  99. }
  100. }
  101. // if($cost->outPrice == 0){
  102. // if(isset($up['realPrice']) && $up['realPrice'] != 0){
  103. // $up['outPrice'] = ceil($up['realPrice']*1.15);
  104. // }
  105. // }
  106. $up['cost'] = isset($sku['cost']) ? $sku['cost'] : 0;
  107. $up_re = DB::table('goods_skus')->where('code', $code)->update($up);
  108. }
  109. }
  110. return true;
  111. }
  112. /**
  113. * 发送短信验证码通知
  114. */
  115. public function skuRealPriceMonitor(){
  116. $text = json_encode($this->diffRealPriceSkuListArr);
  117. #发送验证码报警
  118. $this->sendMsg('13161864516',$text);
  119. $this->sendMsg('18410900527',$text);
  120. // 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');
  121. }
  122. /**
  123. * 计算两个数之间差值的绝对值
  124. * @param $newNumber
  125. * @param $oldNumber
  126. * @return float|int
  127. */
  128. public function getNumberDiffAbs($newNumber,$oldNumber){
  129. $diff = abs(($newNumber-$oldNumber));
  130. return $diff/$oldNumber;
  131. }
  132. private function init(){
  133. $ch = curl_init();
  134. /* 设置验证方式 */
  135. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
  136. 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
  137. /* 设置返回结果为流 */
  138. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  139. /* 设置超时时间*/
  140. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  141. /* 设置通信方式 */
  142. curl_setopt($ch, CURLOPT_POST, 1);
  143. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  144. return $ch;
  145. }
  146. public function sendMsg($phone,$params){
  147. $tpl_id = $this->YP_TPL_ID;
  148. $ch=$this->init();
  149. //$data=array('tpl_id' => $tpl_id,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
  150. $data = [
  151. 'apikey' => YP_SMS_KEY,
  152. 'mobile' => $phone,
  153. 'tpl_id' => $tpl_id,
  154. 'tpl_value' => ('#sku_id#').'='.$params,
  155. ];
  156. $json_data = $this->tpl_send($ch,$data);
  157. //print_r($json_data); ******************************maybe影响验证码发出
  158. $array = json_decode($json_data,true);
  159. // echo '<pre>';print_r($array);
  160. curl_close($ch);
  161. return $array;
  162. }
  163. private function tpl_send($ch,$data){
  164. curl_setopt ($ch, CURLOPT_URL, YP_TPL_URL);
  165. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  166. $result = curl_exec($ch);
  167. $error = curl_error($ch);
  168. $this->checkErr($result,$error);
  169. return $result;
  170. }
  171. private static function checkErr($result,$error) {
  172. if($result === false)
  173. {
  174. echo 'Curl error: ' . $error;
  175. }
  176. // else
  177. // {
  178. // echo '操作完成没有任何错误';
  179. // }
  180. }
  181. }