TongbuOrderJob.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Activity;
  4. use App\Models\Address;
  5. use App\Models\DataInfo;
  6. use App\Models\Gifts;
  7. use App\Models\Goods;
  8. use App\Models\Order;
  9. use App\Models\OrderDetail;
  10. use App\Models\OrderGift;
  11. use App\Models\TongBuList;
  12. use App\Models\UrlList;
  13. use App\Models\User;
  14. use Carbon\Carbon;
  15. use GuzzleHttp\Client;
  16. use Illuminate\Bus\Queueable;
  17. use Illuminate\Contracts\Queue\ShouldQueue;
  18. use Illuminate\Foundation\Bus\Dispatchable;
  19. use Illuminate\Queue\InteractsWithQueue;
  20. use Illuminate\Queue\SerializesModels;
  21. use Illuminate\Support\Facades\DB;
  22. use Illuminate\Support\Facades\Log;
  23. class TongbuOrderJob implements ShouldQueue
  24. {
  25. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. protected $data;
  32. public function __construct($data)
  33. {
  34. $this->data=$data;
  35. }
  36. /**
  37. * Execute the job.
  38. *
  39. * @return void
  40. */
  41. public function handle()
  42. {
  43. $data_info=$this->data;
  44. Log::error($data_info);
  45. $info=json_decode($data_info['data'],true);
  46. $url=mb_substr($info['Domain'],0,-1);
  47. $url_info=UrlList::where('url',$url)->first();
  48. if ($url_info->time<(time()+30)){
  49. $token=$this->getToekn($url,$url_info->id);
  50. }else{
  51. $token=$url_info->token;
  52. }
  53. $type=$info['Type'];
  54. $order_arr=explode('-',$info['Code']);
  55. if (count($order_arr)>1){
  56. Log::error($info['Code']);
  57. return null;
  58. }
  59. if ($type==3){
  60. $order=Order::where('order_num',$info['Code'])->first();
  61. if ($order){
  62. if (!$order->is_deleted){
  63. if ($order->pay_money>0){
  64. $order->wait_back_money=$order->pay_money;
  65. $order->pay_status=3;
  66. }
  67. }
  68. $order->is_deleted=1;
  69. $order->save();
  70. }
  71. return;
  72. }
  73. $json = '{"QueryType":"0","Code":"","BrandIDS":[],"StartDate":"","EndDate":"","StartMoney":"","EndMoney":"","WarehouseIDS":[],"AccountMethod":"","CurrencyIDS":[],"AddrName":"","AddrPhone":"","Addr":"","IsChina":"","AddrID":"","Remark":"","SourceName":"","SourcePhone":"","ProductName":"","ProductCode":"","SourceAuthorizationCode":"","SalesRemark":"","SelectOrderIDs":[],"TeamID":"","PageNo":1,"PageSize":10,"DeliveryType":"","IsCreateOrderSales":null,"Sort":"","IsAsc":false,"PackagesType":[],"BrandLevels":[],"SelfDeliveryStoreName":"","BusinessContactName":"","StateIDs":[],"TeamIDs":[],"ExpressAccountMethod":"","CustomerOrderCategoryID":0,"EMSCompany":"","DateType":"","EMSCode":"","AccuracyQueryKey":[],"NotLikeQueryKey":[],"GratisOrReachedToPay":""}';
  74. $client=new Client();
  75. $url = $url.'/api/Admin/CompanyCustomerOrderList/GetMyCustomerOrderList';
  76. $data=json_decode($json,true);
  77. $data['Code']=$info['Code'];
  78. $data['AccuracyQueryKey']=['Code'];
  79. $re=$client->post($url,[
  80. 'headers' => [
  81. 'Content-Type' => 'application/json', // 设置请求头
  82. 'Auth-AdminToken' => $token, // 如果需要身份验证令牌
  83. ],
  84. 'json'=>$data
  85. ]);
  86. $res=$re->getBody()->getContents();
  87. $order_info=json_decode($res,true);
  88. $arr=$order_info['data']['Models'];
  89. $this->DelOrder($arr,mb_substr($info['Domain'],0,-1));
  90. }
  91. protected function getToekn($url,$id){
  92. $client=new Client();
  93. $re=$client->post($url.'/api/Admin/System/MainSubLogin',[
  94. 'headers' => [
  95. 'Content-Type' => 'application/json', // 设置请求头
  96. // 'Auth-AdminToken' => 'SjI5Ym5US0xZVzZjSVd4V1FheHg4VG5hL1dpWDJwVi82RDdMY09WU3NTeENIdmJUdmxxTEFVOHZSZkRiaXM1c3RuYUlZTGVIcmI2NGlUdXJ4MXZ6dkREWmRoY283UG5TclQ2RnI5amV3UGYzYjBjNkZ0RTFzUElzRzRhV2F1d0M=', // 如果需要身份验证令牌
  97. ],
  98. 'json'=>['UserName'=>'dwadmin','Password'=>'zS76UMV9']
  99. ]);
  100. $res=$re->getBody()->getContents();
  101. $info=json_decode($res,true);
  102. $urlinfo=UrlList::find($id);
  103. // Log::error($res);
  104. // Log::error($info);
  105. if (!$info['errcode']){
  106. $token=$info['data']['accessToken'];
  107. $urlinfo->token=$token;
  108. $urlinfo->time=time()+9*60;
  109. $urlinfo->save();
  110. return $token;
  111. }else{
  112. Log::error($urlinfo->name.'生成token出错===='.$info['errcode']);
  113. return 0;
  114. }
  115. }
  116. protected function DelOrder($arr,$url){
  117. if (!$arr){
  118. return;
  119. }
  120. try {
  121. $activity_id=Activity::where('is_ing',1)->value('id');
  122. foreach ($arr as $k=>$v){
  123. //批发商
  124. $Sourcemobile=$v['SourceCustomerModel']['Telephone'];
  125. $Sourcenickname=$v['SourceCustomerModel']['CustomerName'];
  126. $Sourcecode=$v['SourceCustomerModel']['AuthorizationCode'];
  127. $user=User::where('auth_code',$Sourcecode)->first();
  128. Log::info($Sourcecode);
  129. if (!$user){
  130. $user=new User();
  131. $user->name=$Sourcenickname;
  132. $user->nickname=$Sourcenickname;
  133. $user->realname=$Sourcenickname;
  134. $user->remark_name=$Sourcenickname;
  135. $user->mobile=$Sourcemobile;
  136. $user->uuid=$Sourcemobile;
  137. $user->auth_code=$Sourcecode;
  138. $user->level=3;
  139. $user->url=$url;
  140. $user->save();
  141. }
  142. $TopCode=$v['TopCustomerModel']['AuthorizationCode'];
  143. $belongUser=User::where('auth_code',$TopCode)->first();
  144. if (!$belongUser){
  145. //经销商
  146. $belongUser=new User();
  147. $belongUser->name=$v['TopCustomerModel']['CustomerName'];
  148. $belongUser->nickname=$v['TopCustomerModel']['CustomerName'];
  149. $belongUser->realname=$v['TopCustomerModel']['CustomerName'];
  150. $belongUser->remark_name=$v['TopCustomerModel']['CustomerName'];
  151. $belongUser->mobile=$v['TopCustomerModel']['Telephone'];
  152. $belongUser->uuid=$v['TopCustomerModel']['Telephone'];
  153. $belongUser->auth_code=$TopCode;
  154. $belongUser->level=2;
  155. $belongUser->url=$url;
  156. $belongUser->save();
  157. }
  158. $order_num=$v['Code'];
  159. Log::info($order_num);
  160. $order=Order::where('order_num',$order_num)->first();
  161. if ($order){
  162. $address=Address::where('id',$order->address_id)->first();
  163. $address->user_id=$user->id;
  164. $address->username=$v['AddressDetail']['Name'];
  165. $address->mobile=$v['AddressDetail']['Phone'];
  166. $address->province=$v['AddressDetail']['Province'];
  167. $address->city=$v['AddressDetail']['City'];
  168. $address->town=$v['AddressDetail']['District'];
  169. $address->address=$v['AddressDetail']['Address'];
  170. $address->type=0;
  171. $address->save();
  172. if ($order->money!=$v['TotalCash']){
  173. OrderDetail::where('order_id',$order->id)->delete();
  174. }
  175. $order->money=$v['TotalCash'];
  176. if ($order->money==$order->pay_money){
  177. $order->no_pay_money=0;//未支付变为0
  178. $order->pay_status=2;//支付状态2,为全部支付
  179. $order->wait_back_money=0;//待退回金额为0
  180. }elseif ($order->money<$order->pay_money){//订单金额小于支付金额
  181. $order->no_pay_money=0;//未支付金额变为0
  182. $order->pay_status=3;//支付状态3,待退款状态
  183. $order->wait_back_money=$order->pay_money-$order->money;//待退款金额为支付金额减去订单金额
  184. }elseif ($order->money>$order->pay_money){//订单金额大于支付金额
  185. if ($order->pay_money==0){//如果支付金额为0
  186. $order->pay_status=0;//则支付状态0,未支付
  187. }else{
  188. $order->pay_status=1;//否则支付状态1,部分支付
  189. }
  190. $order->no_pay_money=$order->money-$order->pay_money;//未支付金额等于订单金额减去支付金额
  191. $order->wait_back_money=0;//待退款为0
  192. }
  193. $order->total=$v['TotalQty'];
  194. $order->State=$v['State'];
  195. $order->save();
  196. $detail=$v['OrderDetailOutput'];
  197. $activity_status=false;
  198. $orderdrtail_id=[];
  199. foreach ($detail as $ke=>$va){
  200. if ($va['IsGift']){
  201. $activity_status=true;
  202. $gift_name=$va['ProductName'];
  203. $num=$va['Qty'];
  204. $gift=Gifts::where('name',$gift_name)->where('activity_id',$activity_id)->first();
  205. $ordergift=OrderGift::where('order_id',$order->id)
  206. ->where('gift_id',$gift->id)->first();
  207. if (!$ordergift){
  208. $ordergift=new OrderGift();
  209. }
  210. $order->total-=$num;
  211. $order->save();
  212. $ordergift->gift_id=$gift->id;
  213. $ordergift->order_id=$order->id;
  214. $ordergift->activity_id=$activity_id;
  215. $ordergift->num=$num;
  216. $ordergift->limit_num=1;
  217. $ordergift->save();
  218. }else{
  219. // Log::error($va);
  220. $GroupID=$va['ProductGroupSKUID'];
  221. $num=$va['Qty'];
  222. $goods=Goods::where('groupID',$GroupID)->first();
  223. if (!$goods){
  224. Log::error('不存在商品'.$GroupID);
  225. }
  226. $orderDetail=OrderDetail::where('order_id',$order->id)
  227. ->where('goods_id',$goods->id)->first();
  228. if (!$orderDetail){
  229. $orderDetail=new OrderDetail();
  230. }
  231. $orderDetail->order_id=$order->id;
  232. $orderDetail->goods_id=$goods->id;
  233. $orderDetail->num=$num;
  234. $orderDetail->goods_price=$va['Price'];
  235. $orderDetail->money=$num*$va['Price'];
  236. $orderDetail->goods_name=$goods->name;
  237. $orderDetail->goods_size=$goods->size;
  238. $orderDetail->goods_unit=$goods->unit;
  239. $orderDetail->goods_sku=$va['SKUCode'];
  240. $orderDetail->save();
  241. $order_id[]=$orderDetail->id;
  242. }
  243. }
  244. }else{
  245. $address=new Address();
  246. $address->user_id=$user->id;
  247. $address->username=$v['AddressDetail']['Name'];
  248. $address->mobile=$v['AddressDetail']['Phone'];
  249. $address->province=$v['AddressDetail']['Province'];
  250. $address->city=$v['AddressDetail']['City'];
  251. $address->town=$v['AddressDetail']['District'];
  252. $address->address=$v['AddressDetail']['Address'];
  253. $address->type=0;
  254. $address->uid=$belongUser->id;
  255. $address->save();
  256. $order=new Order();
  257. $order->order_num=$order_num;
  258. $order->money=$v['TotalCash'];
  259. $order->no_pay_money=$v['TotalCash'];
  260. $order->user_id=$user->id;
  261. $order->total=$v['TotalQty'];
  262. $order->created_at=$v['CreateDate'];
  263. $order->ZDL_id=$v['CustomerOrderID'];
  264. $order->url=$url;
  265. $order->State=$v['State'];
  266. $order->address_id=$address->id;
  267. $order->good_user_id=$belongUser->id;
  268. if ($v['State']=='7'){
  269. $order->is_deleted=1;
  270. }
  271. $order->save();
  272. $detail=$v['OrderDetailOutput'];
  273. $activity_status=false;
  274. foreach ($detail as $ke=>$va){
  275. if ($va['IsGift']){
  276. $activity_status=true;
  277. $gift_name=$va['ProductName'];
  278. $num=$va['Qty'];
  279. $gift=Gifts::where('name',$gift_name)->where('activity_id',$activity_id)->first();
  280. $ordergift=new OrderGift();
  281. $ordergift->gift_id=$gift->id;
  282. $ordergift->order_id=$order->id;
  283. $ordergift->activity_id=$activity_id;
  284. $ordergift->num=$num;
  285. $ordergift->limit_num=1;
  286. $ordergift->save();
  287. $order->total-=$num;
  288. $order->save();
  289. }else{
  290. $GroupID=$va['ProductGroupSKUID'];
  291. $num=$va['Qty'];
  292. $goods=Goods::where('groupID',$GroupID)->first();
  293. if (!$goods){
  294. Log::error('不存在该商品'.$GroupID);
  295. }
  296. $orderDetail=new OrderDetail();
  297. $orderDetail->order_id=$order->id;
  298. $orderDetail->goods_id=$goods->id;
  299. $orderDetail->num=$num;
  300. $orderDetail->goods_price=$va['Price'];
  301. $orderDetail->money=$num*$va['Price'];
  302. $orderDetail->goods_name=$goods->name;
  303. $orderDetail->goods_size=$goods->size;
  304. $orderDetail->goods_unit=$goods->unit;
  305. $orderDetail->goods_sku=$va['SKUCode'];
  306. $orderDetail->save();
  307. }
  308. }
  309. if ($activity_status){
  310. $order->activity_id=$activity_id;
  311. $order->save();
  312. }
  313. }
  314. }
  315. }catch (\Exception $exception){
  316. // Log::error($arr);
  317. // DB::table('data_in')
  318. // ->insert(['uuid'=>$data_id]);
  319. Log::error($exception->getMessage());
  320. }
  321. }
  322. }