暫無描述

SyncOrderToMj.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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\FxOrder;
  8. use App\GoodsSkus;
  9. use App\OrderGoodsSkus;
  10. use App\FxOrderGoodsSkus;
  11. use App\Goods;
  12. use App\Oplog;
  13. class SyncOrderToMj extends Command {
  14. protected $signature = 'SyncOrderToMj';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '同步订单到卖家云';
  21. public function handle()
  22. {
  23. $this->SyncOrder();
  24. //$this->SyncFxOrder();
  25. }
  26. public function SyncOrder(){
  27. //查询需要同步到卖家云的订单
  28. $today_date = date('Y-m-d');
  29. echo "\n订单同步时间: " . date('Y-m-d H:i:s');
  30. $result = Order::where('delivery_date', $today_date)->where('warehouse', 3)->where('is_del', 0)->where('status', 2)->whereNull('orderCode')->lists('id');
  31. if(empty($result)){
  32. return;
  33. }
  34. foreach($result as $id){
  35. #再次判断订单是否可同步
  36. $thisOrder = Order::select('id')->where('delivery_date', $today_date)->where('id', $id)->where('warehouse', 3)->where('is_del', 0)->where('status', 2)->whereNull('orderCode')->first();
  37. if(empty($thisOrder)){
  38. continue;
  39. }
  40. #新加逻辑 查询收件人所在城市天气情况并更新
  41. OrderScript::updateWeather($id);
  42. $res = $this->syncOrderToMj($id);
  43. if(!$res){
  44. echo "\n订单ID:".$id." 同步失败";
  45. }else{
  46. #同步到卖家云,更新order_goods_skus状态
  47. $mj_status = array();
  48. $mj_status['mj_status'] = 1;
  49. OrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
  50. // +++++ 减预占库存到redis
  51. $sku_redis = OrderGoodsSkus::select(DB::raw('sum(num) as num, sku_id'))->where('order_id', $id)->where('is_del', 0)->groupBy('sku_id')->get();
  52. $sku_redis = json_decode(json_encode($sku_redis),true);
  53. Order::diffSkuToRedis('', $sku_redis, -1);
  54. echo "\n订单ID:".$id." 同步成功";
  55. }
  56. #记录操作日志
  57. $self_id = 0;
  58. $self_name = '脚本';
  59. $context = "同步订单到卖家云。返回结果:".json_encode($res);
  60. $type = 0;
  61. $tables = 'order';
  62. $data_id = $id;
  63. Oplog::addLog($self_id, $self_name, $context, $type, $tables, $data_id);
  64. }
  65. }
  66. public function SyncFxOrder(){
  67. //查询需要同步到卖家云的订单
  68. $today_date = date('Y-m-d');
  69. echo "\n订单同步时间: " . date('Y-m-d H:i:s');
  70. $result = FxOrder::where('delivery_date', '=', $today_date)->where('warehouse', 3)->where('is_del', 0)->whereIn('status', [2,3])->lists('id');
  71. if(empty($result)){
  72. return;
  73. }
  74. foreach($result as $id){
  75. $mjOrder = $this->getFxMjOrder($id);
  76. if(!empty($mjOrder)){
  77. if($mjOrder['status'] == '已取消'){
  78. $res = $this->updateFxOrderToMj($id);
  79. echo "\n订单ID:".$id." 卖家取消订单重新更新";
  80. #同步到卖家云,更新order_goods_skus状态
  81. $mj_status = array();
  82. $mj_status['mj_status'] = 1;
  83. FxOrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
  84. }
  85. }else{
  86. #新加逻辑 查询收件人所在城市天气情况并更新
  87. FxOrder::updateWeather($id);
  88. $res = $this->syncFxOrderToMj($id);
  89. if(!$res){
  90. echo "\n订单ID:".$id." 同步失败";
  91. }else{
  92. #同步到卖家云,更新order_goods_skus状态
  93. $mj_status = array();
  94. $mj_status['mj_status'] = 1;
  95. FxOrderGoodsSkus::where('order_id', $id)->where('is_del', 0)->update($mj_status);
  96. echo "\n订单ID:".$id." 同步成功";
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * 订单同步到卖家云
  103. */
  104. public function syncOrderToMj($id, $order = []){
  105. if( empty($order) ){
  106. $order = Order::select('eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
  107. $order = json_decode(json_encode($order), true);
  108. $skus = DB::table('order_goods_skus')->where('order_id', $id)->where('is_del', 0)->get();
  109. $skus = json_decode(json_encode($skus), true);
  110. foreach($skus as $k=>$v){
  111. $sku_info = GoodsSkus::where('id', $v['sku_id'])->first();
  112. $goods_info = Goods::where('id', $v['goods_id'])->first();
  113. $params = array();
  114. $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
  115. $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
  116. $params['goodsCode'] = $sku_info->goodsCode;//
  117. $params['skuCode'] = $v['sku_id'];
  118. #商品名称
  119. $params['goodsName'] = $goods_info->name;
  120. $params['propertiesName'] = $sku_info->is_weigh==1 ? $sku_info->propsName.'【按半斤称重】' : $sku_info->propsName;
  121. //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
  122. $params['price'] = $sku_info->is_weigh==1 ? round($v['price']/2,2) : $v['price'];
  123. $params['quantity'] = (int)($sku_info->is_weigh==1 ? $v['num']*2 : $v['num']);
  124. $params['discountFee'] = 0;
  125. $params['adjustFee'] = 0;
  126. $params['refundStatus'] = '正常';
  127. $order['skuList'][] = $params;
  128. }
  129. }
  130. //卖家云添加订单 ->改为审核通过再同步卖家云
  131. $mj_add = OrderScript::mjOrderAdd($order);
  132. //同步卖家云订单数据到本系统
  133. if( isset($mj_add['order']['code']) ){
  134. $mjorder = array();
  135. $mjorder['orderCode'] = $mj_add['order']['code'];
  136. Order::where('id', $id)->update($mjorder);
  137. }
  138. return $mj_add;
  139. }
  140. /**
  141. * 获取订单信息,可用来判断订单是否存在,同步订单状态
  142. */
  143. public function getMjOrder($id, $outerCode=''){
  144. $order = array();
  145. $order['eshopCode'] = config('constants.ESHOP_CODE');
  146. if(!$outerCode){
  147. $outerCode = Order::where('id', $id)->pluck('outerCode');
  148. }
  149. $order['outerCode'] = $outerCode;
  150. $mjOrder = OrderScript::mjOrderGet($order);
  151. if(!isset($mjOrder['order']['code'])){
  152. return false;
  153. }
  154. return $mjOrder['order'];
  155. }
  156. /**
  157. * 订单更新同步到卖家云
  158. */
  159. public function updateOrderToMj($id, $order = []){
  160. if( empty($order) ){
  161. $order = Order::select('orderCode as code','eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
  162. $order = json_decode(json_encode($order), true);
  163. }
  164. $mj_add = OrderScript::mjOrderUpdate($order);
  165. return $mj_add;
  166. }
  167. /**
  168. * 订单同步到卖家云
  169. */
  170. public function syncFxOrderToMj($id, $order = []){
  171. if( empty($order) ){
  172. $order = FxOrder::select('eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
  173. $order = json_decode(json_encode($order), true);
  174. $skus = DB::table('fx_order_goods_skus')->where('order_id', $id)->where('is_del', 0)->get();
  175. $skus = json_decode(json_encode($skus), true);
  176. foreach($skus as $k=>$v){
  177. $sku_info = GoodsSkus::where('id', $v['sku_id'])->first();
  178. $goods_info = Goods::where('id', $v['goods_id'])->first();
  179. $params = array();
  180. $params['outerCode'] = $order['outerCode']+$k+1; //外部平台中子订单编码
  181. $params['outerParentCode'] = $order['outerCode']; //外部平台中订单编码
  182. $params['goodsCode'] = $sku_info->goodsCode;//
  183. $params['skuCode'] = $v['sku_id'];
  184. #商品名称
  185. $params['goodsName'] = $goods_info->name;
  186. $params['propertiesName'] = $sku_info->is_weigh==1 ? $sku_info->propsName.'【按半斤称重】' : $sku_info->propsName;
  187. //$params['goodsName'] = '海钓黄翅鱼';//$goods->code;
  188. $params['price'] = $sku_info->is_weigh==1 ? round($v['price']/2,2) : $v['price'];
  189. $params['quantity'] = (int)($sku_info->is_weigh==1 ? $v['num']*2 : $v['num']);
  190. $params['discountFee'] = 0;
  191. $params['adjustFee'] = 0;
  192. $params['refundStatus'] = '正常';
  193. $order['skuList'][] = $params;
  194. }
  195. }
  196. //卖家云添加订单 ->改为审核通过再同步卖家云
  197. $mj_add = OrderScript::mjOrderAdd($order);
  198. //同步卖家云订单数据到本系统
  199. if( isset($mj_add['order']['code']) ){
  200. $mjorder = array();
  201. $mjorder['orderCode'] = $mj_add['order']['code'];
  202. FxOrder::where('id', $id)->update($mjorder);
  203. }
  204. return $mj_add;
  205. }
  206. /**
  207. * 获取订单信息,可用来判断订单是否存在,同步订单状态
  208. */
  209. public function getFxMjOrder($id, $outerCode=''){
  210. $order = array();
  211. $order['eshopCode'] = config('constants.ESHOP_CODE');
  212. if(!$outerCode){
  213. $outerCode = FxOrder::where('id', $id)->pluck('outerCode');
  214. }
  215. $order['outerCode'] = $outerCode;
  216. $mjOrder = OrderScript::mjOrderGet($order);
  217. if(!isset($mjOrder['order']['code'])){
  218. return false;
  219. }
  220. return $mjOrder['order'];
  221. }
  222. /**
  223. * 订单更新同步到卖家云
  224. */
  225. public function updateFxOrderToMj($id, $order = []){
  226. if( empty($order) ){
  227. $order = FxOrder::select('orderCode as code','eshopCode','outerCode','status','customerName','shippingType','expressCompanyCode','expressCompanyName','receiverName','receiverMobile','receiverPhone','receiverState','receiverCity','receiverDistrict','receiverStreet','receiverAddress','buyerPostageFee','discountFee','adjustFee','serviceFee','paymentMethod','paymentType','paymentAccount','paymentTime','receivedAmount','buyerMemo','sellerMemo','createTime','modifyTime')->where('id', $id)->first();
  228. $order = json_decode(json_encode($order), true);
  229. }
  230. $mj_add = OrderScript::mjOrderUpdate($order);
  231. return $mj_add;
  232. }
  233. }