OrderPush.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace app\command;
  3. // +----------------------------------------------------------------------
  4. // | [ WE CAN DO IT MORE SIMPLE ]
  5. // +----------------------------------------------------------------------
  6. // | Copyright (c) 2018-2020 rights reserved.
  7. // +----------------------------------------------------------------------
  8. // | Author: TABLE ME
  9. // +----------------------------------------------------------------------
  10. // | Date: 2020-09-08 16:19
  11. // +----------------------------------------------------------------------
  12. use app\model\api\SiteProduct;
  13. use app\model\system\Product;
  14. use library\lib\hupun;
  15. use library\utils\Qiniu;
  16. use library\utils\Region;
  17. use think\console\Command;
  18. use think\console\Input;
  19. use think\console\Output;
  20. use think\facade\Db;
  21. class OrderPush extends Command
  22. {
  23. protected function configure()
  24. {
  25. // 指令配置
  26. $this->setName('Task')
  27. ->setDescription('推送定到ERP系统');
  28. }
  29. protected function execute(Input $input, Output $output)
  30. {
  31. $this->pushOrder();
  32. //$this->bindPro();
  33. $this->pushFind();
  34. }
  35. public function pushOrder(){
  36. $hupin = new hupun();
  37. $order = Db::name("order")->where(array('is_pay'=>1,'is_push'=>0))->select();
  38. foreach ($order as $v) {
  39. $orderInfo = Db::name("orderInfo")
  40. ->where('o_id',$v['id'])
  41. ->where('is_push',0)
  42. ->select();
  43. $sitePro = new SiteProduct();
  44. $region = new Region;
  45. $sitePro->setSass($v['sassid']);
  46. foreach ($orderInfo as $v2) {
  47. //获取产品信息
  48. $proData = $sitePro->getProItem($v['pro_id']);
  49. //获取地址
  50. $memberData = Db::name('member')->where('uid',$v2['uid'])->find();
  51. //获取仓库信息
  52. $warehouse = Db::name("warehouse")->where('id',$v2['warehouse_id'])->find();
  53. //推送数据
  54. $iisave['order_id'] = $v2['in_order_id'];
  55. $iisave['time'] = $v['time'];
  56. $iisave['payTime'] = $v['pay_time'];
  57. $iisave['storeID'] = $warehouse['code'];
  58. $iisave['pro_price'] =$v2['pro_price'];
  59. $iisave['exp_price'] = $v2['exp_price'];
  60. $iisave['ts_name'] = empty($memberData) ? '' : $memberData['mobile'];
  61. $iisave['name'] = $v2['name'];
  62. $rData = $region->getRegion($v2['address']);
  63. $iisave['province'] = $rData['province'];
  64. $iisave['city'] = $rData['city'];
  65. $iisave['area'] = $rData['area'];
  66. $iisave['address'] = $v2['address'];
  67. $iisave['mobile'] = $v2['mobile'];
  68. $iisave['in_order_id'] = $v2['in_order_id'];
  69. $iisave['mono'] = $v['mono'];
  70. //产品信息
  71. //$iisave['csno'] = $proData['csno'];
  72. $code = $this->getWlnCode($v2['pro_id'],$v2['warehouse_id'],$v2['exp_id'],$v2['platform_id']);
  73. $iisave['csno'] =$code;
  74. $iisave['pro_title'] = $proData['title'];
  75. $iisave['pro_price'] = $proData['price'];
  76. $iisave['pro_img'] = empty($proData['img']) ? '' : $proData['img'][0];
  77. $pusData = $hupin->tradesErp($iisave);
  78. //推送数据
  79. $pushSave = [];
  80. $pushSave['o_id'] = $v['id'];
  81. $pushSave['oi_id'] = $v2['id'];
  82. $pushSave['time'] = time();
  83. $pushSave['code'] = 'trades';//推送
  84. if($pusData['success']) {
  85. $pushSave['status'] = 1;
  86. $pushSave['text'] = $v2['in_order_id'].",联系人:".$v2['name'].",手机号码:".$v2['mobile'].",地址:"
  87. .$v2['address'].'推送成功';
  88. Db::name("orderInfo")->where('id',$v2['id'])->update(['is_push'=>1]);
  89. } else {
  90. $pushSave['status'] = 0;
  91. $pushSave['text'] = $v2['in_order_id'].",联系人:".$v2['name'].",手机号码:".$v2['mobile'].",地址:"
  92. .$v2['address'].'推送失败:'
  93. .$pusData['error_msg'];
  94. var_dump($pushSave);
  95. Db::name("orderInfo")->where('id',$v2['id'])->update(['is_push'=>-1]);
  96. }
  97. Db::name("orderPush")->insert($pushSave);
  98. echo "订单号:". $v2['in_order_id'].",联系人:".$v2['name'].",手机号码:".$v2['mobile'].",地址:".$v2['address'].PHP_EOL;
  99. }
  100. Db::name("order")->where('id',$v['id'])->update(['is_push'=>1]);
  101. }
  102. }
  103. /**
  104. * 获取订单信息
  105. */
  106. public function pushFind(){
  107. $true = true;
  108. $p = 1;
  109. while ($true) {
  110. $orderIds = Db::name("orderInfo")
  111. ->where('status', '>', 0)
  112. ->where('send_time', 0)
  113. ->page($p, 100)
  114. ->column('in_order_id');
  115. if(empty($orderIds)) {
  116. $true = false;
  117. continue;
  118. }
  119. if (count($orderIds) > 0) {
  120. $ids = join(',', $orderIds);
  121. $hupin = new hupun();
  122. $data = $hupin->orderFind($ids);
  123. if (!empty($data['response'])) {
  124. $response = json_decode($data['response'], true);
  125. $statuses = $response['statuses'];
  126. foreach ($statuses as $v) {
  127. if (!empty($v['waybill'])) {
  128. $oData = Db::name("orderInfo")->where('in_order_id', $v['tid'])->find();
  129. echo $v['tid']. PHP_EOL;
  130. if (!empty($oData)) {
  131. Db::name("orderInfo")->where('in_order_id', $v['tid'])->update([
  132. 'exp_number' => $v['waybill'],
  133. 'send_time' => time(),
  134. 'status' => 2
  135. ]);
  136. Db::name("order")->where('id', $oData['o_id'])->update(['status' => 2]);
  137. $this->setPush($oData);
  138. }
  139. }
  140. }
  141. }
  142. }
  143. $p++;
  144. }
  145. }
  146. /**
  147. * push 数据
  148. * @param $data
  149. */
  150. private function setPush($data) {
  151. $count = Db::name("weixin_push_user")->where('uid',$data['uid'])->count();
  152. if($count > 0) {
  153. $weixinPush = Db::name("weixin_order_push")
  154. ->where('uid', $data['uid'])
  155. ->where('status', 0)
  156. ->find();
  157. $post = [];
  158. if(!empty($weixinPush)) {
  159. $post = unserialize($weixinPush['data']);
  160. }
  161. $o_id = $data['o_id'];
  162. if(!empty($post[$o_id])) {
  163. $post[$o_id]['count'] += 1;
  164. } else {
  165. $post[$o_id] = ['count'=>1,'o_id'=>$o_id];
  166. }
  167. $save['uid'] = $data['uid'];
  168. $save['status'] = 0;
  169. $save['data'] = serialize($post);
  170. if(empty($weixinPush)) {
  171. $save['push_time'] = time();
  172. Db::name("weixin_order_push")->insert($save);
  173. } else {
  174. Db::name("weixin_order_push")->where('id',$weixinPush['id'])->update($save);
  175. }
  176. }
  177. }
  178. public function bindPro(){
  179. $data = Db::name("product")->where('csno','fslp00001')->select();
  180. foreach ($data as $v) {
  181. $hupin = new hupun();
  182. $hupin->addProduct([
  183. 'code' => $v['csno'],
  184. 'title' => $v['title'],
  185. 'price' => $v['price'],
  186. 'quantity' => $v['count'],
  187. 'csno' => $v['csno'],
  188. 'url' => 'http://www.lipinwawa.com/goods/detail/'.$v['id'].'.html',
  189. 'img' => $v['img'],
  190. 'status' =>1,
  191. 'time' => $v['add_time']
  192. ]);
  193. }
  194. }
  195. /**
  196. * 其他信息
  197. * @param $proId
  198. * @param $warehouseId
  199. * @param $expId
  200. * @param $platformId
  201. */
  202. private function getWlnCode($proId,$warehouseId,$expId,$platformId){
  203. $code = Db::name("wln_pro")
  204. ->where('pro_id',$proId)
  205. ->where('warehouse_id',$warehouseId)
  206. ->where('express_id',$expId)
  207. ->where('platform_id',$platformId)
  208. ->value('code');
  209. if(!empty($code)) return $code;
  210. $code = Db::name("wln_pro")
  211. ->where('pro_id',$proId)
  212. ->where('warehouse_id',$warehouseId)
  213. ->where('express_id',$expId)
  214. ->where('is_defulat',1)
  215. ->value('code');
  216. return $code;
  217. }
  218. }