OrderService.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\AccountLogModel;
  13. use App\Models\CartsModel;
  14. use App\Models\GoodsModel;
  15. use App\Models\MemberCouponModel;
  16. use App\Models\MemberModel;
  17. use App\Models\MerchantModel;
  18. use App\Models\OrderGoodsModel;
  19. use App\Models\OrderModel;
  20. use App\Services\BaseService;
  21. use App\Services\ConfigService;
  22. use App\Services\RedisService;
  23. use App\Services\SupplyService;
  24. use Illuminate\Support\Facades\DB;
  25. /**
  26. * 订单管理-服务类
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * Class OrderService
  30. * @package App\Services\Api
  31. */
  32. class OrderService extends BaseService
  33. {
  34. // 静态对象
  35. protected static $instance = null;
  36. /**
  37. * 构造函数
  38. * @author laravel开发员
  39. * @since 2020/11/11
  40. * OrderService constructor.
  41. */
  42. public function __construct()
  43. {
  44. $this->model = new OrderModel();
  45. }
  46. /**
  47. * 静态入口
  48. * @return static|null
  49. */
  50. public static function make()
  51. {
  52. if (!self::$instance) {
  53. self::$instance = (new static());
  54. }
  55. return self::$instance;
  56. }
  57. /**
  58. * @param $params
  59. * @param int $pageSize
  60. * @return array
  61. */
  62. public function getDataList($params, $pageSize = 15)
  63. {
  64. $where = ['a.mark' => 1,'a.is_hide'=>0];
  65. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  66. if ($merchId > 0) {
  67. $where['a.merch_id'] = $merchId;
  68. }
  69. $merchUid = isset($params['merch_uid']) ? $params['merch_uid'] : 0;
  70. if ($merchUid > 0) {
  71. $where['a.merch_uid'] = $merchUid;
  72. }
  73. $list = $this->model->from('orders as a')->with(['goods'])
  74. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  75. ->where($where)
  76. ->where(function ($query) use ($params) {
  77. $keyword = isset($params['kw']) ? $params['kw'] : '';
  78. if ($keyword) {
  79. $query->where('a.order_no', 'like', "%{$keyword}%")->orWhere('a.plat_order_no', 'like', "%{$keyword}%");
  80. }
  81. })
  82. ->where(function ($query) use ($params) {
  83. $status = isset($params['status']) ? $params['status'] : 0;
  84. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  85. $type = isset($params['type']) ? $params['type'] : 0;
  86. if ($userId) {
  87. $query->where('a.user_id', '=', $userId);
  88. }
  89. if ($type > 0) {
  90. $query->where('a.type', '=', $type);
  91. }
  92. if ($status > 0) {
  93. $query->where('a.status', '=', $status);
  94. }else{
  95. $query->where('a.status', '>', 1);
  96. }
  97. })
  98. ->select(['a.*', 'b.nickname', 'b.trc_url'])
  99. ->orderBy('a.pay_time', 'desc')
  100. ->orderBy('a.id', 'desc')
  101. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  102. $list = $list ? $list->toArray() : [];
  103. if ($list) {
  104. $refundStatusArr = [1=>'退款中',2=>'已退款',3=>'退款失败'];
  105. $statusArr = [1=>'待付款',2=>'待发货',3=>'待收货',4=>'已完成',5=>'已售后'];
  106. $supplyList = config('goods.supplyList');
  107. foreach ($list['data'] as &$item) {
  108. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  109. $item['pay_time'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i:s') : '';
  110. $status = isset($item['status'])? $item['status'] : 0;
  111. $item['status_text'] = '待付款';
  112. $item['refund_status_text'] = '';
  113. if($status){
  114. if($item['type'] == 2){
  115. $item['status_text'] = isset($serviceStatusArr[$status])? $serviceStatusArr[$status] : '';
  116. }else{
  117. $item['status_text'] = isset($statusArr[$status])? $statusArr[$status] : '';
  118. }
  119. }
  120. $refundStatus = isset($item['refund_status'])? $item['refund_status'] : 0;
  121. if($refundStatus && $status != 4){
  122. $item['refund_status_text'] = isset($refundStatusArr[$refundStatus])? $refundStatusArr[$refundStatus] : '';
  123. }
  124. $goods = isset($item['goods'])? $item['goods'] : [];
  125. if($goods){
  126. $isRefund = false;
  127. foreach ($goods as &$v){
  128. $v['supply_name'] = isset($supplyList[$v['supply_type']]) ? $supplyList[$v['supply_type']] : '';
  129. $v['main_img'] = isset($v['main_img']) && $v['main_img']? get_image_url($v['main_img']) : '';
  130. $v['sku_attr'] = isset($v['sku_attr']) && $v['sku_attr']? json_decode($v['sku_attr'], true) : [];
  131. if(!in_array($v['refund_status'],[1,2,3]) && $item['status'] != 4){
  132. $isRefund = true;
  133. $v['is_refund'] = 1;
  134. }
  135. }
  136. unset($v);
  137. $item['is_refund'] = $isRefund;
  138. }
  139. $item['goods'] = $goods? $goods : [];
  140. }
  141. unset($item);
  142. }
  143. return [
  144. 'pageSize' => $pageSize,
  145. 'total' => isset($list['total']) ? $list['total'] : 0,
  146. 'counts' => [],
  147. 'list' => isset($list['data']) ? $list['data'] : []
  148. ];
  149. }
  150. /**
  151. * 购买商品
  152. * @param $userId 用户ID
  153. * @param $params
  154. * @return array|false
  155. */
  156. public function buySubmit($userId, $params)
  157. {
  158. $payType = isset($params['pay_type'])? intval($params['pay_type']) : 0;
  159. $type = isset($params['type']) && $params['type']? intval($params['type']) : 1;
  160. $amount = isset($params['total']) && $params['total']? floatval($params['total']) : 0;
  161. $couponId = isset($params['coupon_id'])? intval($params['coupon_id']) : 0;
  162. $addressId = isset($params['address_id'])? intval($params['address_id']) : 0;
  163. $freightAddressId = isset($params['freight_address_id'])? intval($params['freight_address_id']) : 0;
  164. $payPassword = isset($params['pay_password'])? trim($params['pay_password']) : '';
  165. $skuList = isset($params['sku_list'])? $params['sku_list'] : [];
  166. $ids = isset($params['ids'])? $params['ids'] : [];
  167. $cartIds = isset($params['cart_ids'])? trim($params['cart_ids']) : '';
  168. $cartIds = $cartIds? explode('|', $cartIds) : [];
  169. if(empty($skuList) || empty($ids) || $addressId<=0 || $freightAddressId<=0 || $payType<=0){
  170. $this->error = 2420;
  171. return false;
  172. }
  173. // 锁
  174. $cacheKey = "caches:orders:buy:{$userId}";
  175. if(RedisService::get($cacheKey)){
  176. $this->error = 1053;
  177. return false;
  178. }
  179. // 订单商品
  180. $goodsList = GoodsService::make()->getOrderGoods($userId, $ids);
  181. if(empty($goodsList)){
  182. $this->error = 2903;
  183. return false;
  184. }
  185. // 格式化
  186. $skuArr = [];
  187. $skuAttr = [];
  188. foreach ($skuList as $v){
  189. $skuArr[$v['goods_id']] = [
  190. 'sku_id'=> $v['sku_id'],
  191. 'num'=> intval($v['num']),
  192. ];
  193. $skuAttr[$v['goods_id']] = $v['attr'];
  194. }
  195. $skuList = $skuArr;
  196. $goodsNum = 0;
  197. $orderTotal = 0;
  198. $orderXdTotal = 0;
  199. $merchId = 0;
  200. $orderNo = get_order_num('XS');
  201. // 价格参数
  202. $goods = [];
  203. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  204. if($usdtPrice<=0){
  205. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  206. $usdtPrice = $usdtCnyPrice>0 && $usdtCnyPrice< 100? $usdtCnyPrice : 0;
  207. }
  208. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  209. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  210. foreach($goodsList as $item){
  211. $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
  212. $merchId = isset($item['merch_id'])? $item['merch_id'] : 0;
  213. $num = isset($skuList[$goodsId]['num'])? intval($skuList[$goodsId]['num']) : 0;
  214. $skuId = isset($skuList[$goodsId]['sku_id'])? intval($skuList[$goodsId]['sku_id']) : 0;
  215. $skuInfo = GoodsService::make()->getSkuInfo($skuId);
  216. $price = isset($skuInfo['plat_price'])? $skuInfo['plat_price'] : 0;
  217. $attr = isset($skuAttr[$goodsId])? $skuAttr[$goodsId] : [];
  218. if($num>0 && $skuId >0 && $price>0){
  219. $item['price'] = GoodsService::make()->getRealSalePrice($price); // 浮动后星豆价
  220. $price = GoodsService::make()->getRealSalePrice($price,3); // 浮动后原售价
  221. $total = moneyFormat($price * $num, 2);
  222. $xdTotal = moneyFormat($item['price'] * $num, 2);
  223. // 订单商品
  224. $skuSn = isset($skuInfo['sku_sn'])? $skuInfo['sku_sn'] : '';
  225. $item['order_no'] = $orderNo;
  226. $item['sku_id'] = $skuId;
  227. $item['sku_sn'] = $skuSn;
  228. $item['total'] = $total;
  229. $item['xd_total'] = $xdTotal;
  230. $item['num'] = $num;
  231. $item['sku_attr'] = $attr? json_encode($attr, 256) : '';
  232. $item['create_time'] = time();
  233. $item['update_time'] = time();
  234. $item['status'] = 1;
  235. $item['mark'] = 1;
  236. // 订单金额
  237. $orderTotal += $total;
  238. $orderXdTotal += $xdTotal;
  239. $goodsNum += $num;
  240. unset($item['sku_list']);
  241. $goods[$skuSn] = $item;
  242. }else{
  243. unset($item);
  244. }
  245. }
  246. unset($item);
  247. if($orderXdTotal<=0 || $goodsNum<=0){
  248. $this->error = 2904;
  249. return false;
  250. }
  251. // 优惠券
  252. $couponPrice = 0;
  253. if($couponId > 0){
  254. }
  255. // 验证用户
  256. $userInfo = MemberModel::where(['id'=> $userId,'mark'=> 1,'status'=> 1])
  257. ->select(['id','nickname','pay_password','balance','parent_id','parents'])
  258. ->first();
  259. $userInfo = $userInfo? $userInfo->toArray() : [];
  260. $userPayPassword = isset($userInfo['pay_password'])? $userInfo['pay_password'] : '';
  261. $userXd = isset($userInfo['balance'])? floatval($userInfo['balance']) : 0.00;
  262. if(empty($userInfo)){
  263. $this->error = 2024;
  264. return false;
  265. }
  266. // 余额支付支付密码验证
  267. if($payType == 10 && empty($userPayPassword)){
  268. $this->error =1040;
  269. return false;
  270. }
  271. // 星豆余额支付交易密码
  272. $payPassword = get_password($payPassword);
  273. if($payType == 10 && $payPassword != $userPayPassword){
  274. $this->error = 2038;
  275. return false;
  276. }
  277. // 运费
  278. $skus = array_values($skuList);
  279. if(!$freightData = GoodsService::make()->getFreight($userId, $freightAddressId, $skus)){
  280. $this->error = GoodsService::make()->getError()?? 2905;
  281. return false;
  282. }
  283. $freight = isset($freightData['freight'])? floatval($freightData['freight']) : 0;
  284. $payTotal = moneyFormat($orderXdTotal+$freight,2);
  285. if(intval($amount) != intval($payTotal)){
  286. $this->error = 2906;
  287. return false;
  288. }
  289. // 星豆余额支付验证
  290. if($payType == 10 && $userXd < $payTotal){
  291. $this->error = 2304;
  292. return false;
  293. }
  294. // 收货地址
  295. $addressInfo = MemberAddressService::make()->getBindInfo($userId, $addressId);
  296. $addressText = isset($addressInfo['address_text'])? $addressInfo['address_text'] : '';
  297. $city = isset($addressInfo['city'])? $addressInfo['city'] : '';
  298. $realname = isset($addressInfo['realname'])? $addressInfo['realname'] : '';
  299. $mobile = isset($addressInfo['mobile'])? $addressInfo['mobile'] : '';
  300. $address = isset($addressInfo['address'])? $addressInfo['address'] : '';
  301. if(empty($addressText) || empty($addressInfo) || empty($realname) || empty($mobile)){
  302. $this->error = 2902;
  303. return false;
  304. }
  305. $merchUId = 0;
  306. // 奖励待返积分
  307. $waitScoreRate = ConfigService::make()->getConfigByCode('shop_award_score_rate',0);
  308. $waitScoreRate = $waitScoreRate>=0 && $waitScoreRate<=1000? $waitScoreRate : 0;
  309. $awardWaitScore = moneyFormat($orderXdTotal * $waitScoreRate/100, 2);
  310. // 算力奖励
  311. $powerRate = ConfigService::make()->getConfigByCode('shop_award_power_rate',0);
  312. $powerRate = $powerRate>=0 && $powerRate<=1000? $powerRate : 0;
  313. $awardPowerNum = moneyFormat($orderXdTotal * $powerRate/100, 2);
  314. // 订单数据
  315. $order = [
  316. 'user_id'=> $userId,
  317. 'order_no'=> $orderNo,
  318. 'merch_id'=> $merchId,
  319. 'merch_uid'=> $merchUId,
  320. 'type'=> $type,
  321. 'price'=> 0.00,
  322. 'xd_price'=> $xdPrice,
  323. 'num'=> $goodsNum,
  324. 'city'=> $city,
  325. 'real_name'=> $realname,
  326. 'mobile'=> $mobile,
  327. 'address'=> $address && $addressText? $addressText.' '.$address : $addressText,
  328. 'total'=> $orderTotal,
  329. 'xd_total'=> $orderXdTotal,
  330. 'pay_type'=> $payType,
  331. 'pay_money'=> $payTotal,
  332. 'delivery_type'=> 1,
  333. 'coupon_id'=> $couponId,
  334. 'coupon_price'=> $couponPrice,
  335. 'postage'=> $freight,
  336. 'award_wait_score'=> $awardWaitScore,
  337. 'award_power_num'=> $awardPowerNum,
  338. 'create_time'=> time(),
  339. 'update_time'=> time(),
  340. 'remark'=> isset($params['remark'])? trim($params['remark']) : '',
  341. 'status'=> 1,
  342. 'mark'=> 1,
  343. ];
  344. // 站外订单提交
  345. RedisService::set($cacheKey, $order, rand(2,3));
  346. $orderParams = [
  347. 'third_order'=> $orderNo,
  348. 'address_id'=> $freightAddressId,
  349. 'sku_list'=> $skus,
  350. 'receiver'=> $realname,
  351. 'receiver_phone'=> $mobile,
  352. 'address'=> $address,
  353. 'other'=> isset($params['remark'])? $params['remark'] : '',
  354. ];
  355. $result = SupplyService::make()->getApiData('orderSubmit', $orderParams);
  356. $orderSku = isset($result['sku'])? $result['sku'] : [];
  357. $outOrderNo = isset($result['out_order_no'])? $result['out_order_no'] : '';
  358. if(empty($result)){
  359. RedisService::clear($cacheKey);
  360. $this->error = SupplyService::make()->getError();
  361. return false;
  362. }
  363. // 订单回单数据
  364. $order['out_order_no'] = $outOrderNo;
  365. if($orderSku){
  366. foreach ($orderSku as $item){
  367. if($item['goodsSkuCode'] && isset($goods[$item['goodsSkuCode']])){
  368. $goods[$item['goodsSkuCode']]['expense'] = isset($item['expense'])? $item['expense'] : 0;
  369. $goods[$item['goodsSkuCode']]['service_price'] = isset($item['service_price'])? $item['service_price'] : 0;
  370. }
  371. }
  372. }
  373. if(!$orderId = $this->model->insertGetId($order)){
  374. $this->error = 2907;
  375. RedisService::clear($cacheKey);
  376. return false;
  377. }
  378. // 写入订单商品
  379. $goods = array_values($goods);
  380. if($orderId && !OrderGoodsModel::insert($goods)){
  381. $this->error = 2908;
  382. RedisService::clear($cacheKey);
  383. return false;
  384. }
  385. DB::beginTransaction();
  386. // 支付处理
  387. $payment = [];
  388. $dateTime = date('Y-m-d H:i:s');
  389. switch($payType){
  390. case 10: // 星豆余额支付
  391. // 扣除余额
  392. $updateData = [
  393. 'balance'=> DB::raw("balance - {$payTotal}"), // 扣除星豆
  394. 'update_time'=>time()
  395. ];
  396. if(!MemberModel::where(['id'=> $userId])->update($updateData)){
  397. DB::rollBack();
  398. $this->error = 1042;
  399. RedisService::clear($cacheKey);
  400. return false;
  401. }
  402. // 余额明细
  403. $log = [
  404. 'user_id' => $userId,
  405. 'source_id' => $orderId,
  406. 'source_order_no' => $orderNo,
  407. 'type' => 3,
  408. 'coin_type' => 2,
  409. 'user_type'=> 1,
  410. 'money' => $payTotal,
  411. 'actual_money' => $payTotal,
  412. 'balance' => $userXd,
  413. 'create_time' => time(),
  414. 'update_time' => time(),
  415. 'remark' => "商城购物消费",
  416. 'status' => 1,
  417. 'mark' => 1,
  418. ];
  419. if(!AccountLogModel::insertGetId($log)){
  420. DB::rollBack();
  421. $this->error = 2029;
  422. RedisService::clear($cacheKey);
  423. return false;
  424. }
  425. // 更新订单状态
  426. $updateData = ['status'=>2,'pay_status'=> 1,'pay_time'=>date('Y-m-d H:i:s'),'update_time'=>time()];
  427. if(!$this->model->where(['id'=> $orderId,'mark'=>1])->update($updateData)){
  428. DB::rollBack();
  429. $this->error = 2909;
  430. RedisService::clear($cacheKey);
  431. return false;
  432. }
  433. $payStatus = 1;
  434. break;
  435. default:
  436. $this->error = 1030;
  437. return false;
  438. }
  439. // 更新优惠券状态
  440. if($couponId>0 && !MemberCouponModel::where(['id'=> $couponId,'mark'=>1])->update(['status'=> 2,'update_time'=>time()])){
  441. DB::rollBack();
  442. $this->error = 2908;
  443. RedisService::clear($cacheKey);
  444. return false;
  445. }
  446. // 购物车处理
  447. if($cartIds){
  448. CartsModel::where(['user_id'=> $userId,'status'=>1,'mark'=>1])
  449. ->whereIn('id', $cartIds)
  450. ->update(['status'=>2,'update_time'=>time()]);
  451. RedisService::clear("caches:members:cartList:{$userId}");
  452. RedisService::clear("caches:members:cartCount:{$userId}");
  453. }
  454. // 已支付
  455. RedisService::clear($cacheKey);
  456. if($payStatus == 1){
  457. // 支付成功订单通知
  458. $message = "您在{$dateTime}(UTC+8)创建的订单【{$orderNo}】,金额{$payTotal}星豆,已支付成功,请耐心等候发货。";
  459. MessageService::make()->pushMessage($userId,"订单支付成功",$message, 2);
  460. DB::commit();
  461. $this->error = 2910;
  462. return [
  463. 'id'=> $orderId,
  464. 'out_order_no'=> $outOrderNo,
  465. 'total'=> $payTotal,
  466. 'pay_type'=> $payType,
  467. ];
  468. }else{
  469. // 下单成功订单通知
  470. $message = "您在{$dateTime}(UTC+8)创建订单【{$orderNo}】成功,请尽快完成支付。";
  471. MessageService::make()->pushMessage($userId,"订单创建成功",$message, 2);
  472. DB::commit();
  473. $this->error = 2911;
  474. return [
  475. 'id'=> $orderId,
  476. 'out_order_no'=> $outOrderNo,
  477. 'total'=> $payTotal,
  478. 'pay_type'=> $payType,
  479. ];
  480. }
  481. }
  482. /**
  483. * 详情
  484. * @param $id
  485. * @return mixed
  486. */
  487. public function getInfo($id, $userId=0)
  488. {
  489. $where = [ 'a.mark' => 1];
  490. if(intval($id) == $id){
  491. $where['a.id'] = $id;
  492. }else{
  493. $where['a.order_no'] = $id;
  494. }
  495. $info = $this->model->with(['goods'])
  496. ->from('orders as a')
  497. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  498. ->where($where)
  499. ->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile'])
  500. ->first();
  501. if ($info) {
  502. $info['create_time_text'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  503. $info['pay_time'] = $info['pay_time']? datetime($info['pay_time'], 'Y-m-d H:i:s') : '';
  504. $info['complete_time'] = $info['pay_time']? dateFormat($info['complete_time'], 'Y-m-d H:i:s') : '';
  505. $info['mobile_text'] = isset($info['mobile'])? format_mobile($info['mobile']) : '';
  506. if(isset($info['goods']) && $info['goods']){
  507. foreach ($info['goods'] as &$item){
  508. $item['main_img'] = $item['main_img']? get_image_url($item['main_img']) : '';
  509. $item['sku_attr'] = $item['sku_attr']? json_decode($item['sku_attr'],true) :[];
  510. $item['retail_price1'] = $item['retail_price'];
  511. $item['retail_price'] = GoodsService::make()->getRealSalePrice($item['cost_price']);
  512. }
  513. unset($item);
  514. }
  515. $payTypes = [10=>'星豆余额支付'];
  516. if(isset($info['pay_type'])){
  517. $info['pay_text'] = isset($payTypes[$info['pay_type']])? $payTypes[$info['pay_type']] : '其他';
  518. }
  519. $deliveryTypes = [1=>'快递配送'];
  520. if(isset($info['pay_type'])){
  521. $info['delivery_text'] = isset($deliveryTypes[$info['delivery_type']])? $deliveryTypes[$info['delivery_type']] : '其他';
  522. }
  523. }
  524. return $info;
  525. }
  526. /**
  527. * 订单支付
  528. * @param $userId
  529. * @param $params
  530. */
  531. public function pay($userId, $params)
  532. {
  533. return true;
  534. }
  535. /**
  536. * 申请退款
  537. * @param $userId
  538. * @param $params
  539. * @return bool
  540. */
  541. public function refund($userId, $params)
  542. {
  543. $id = isset($params['id']) ? $params['id'] : 0;
  544. $refundType = isset($params['refund_type']) ? $params['refund_type'] : 0;
  545. $refundRemark = isset($params['remark']) ? $params['remark'] : '';
  546. $skuSn = isset($params['sku_sn']) ? $params['sku_sn'] : '';
  547. $info = $this->model->with(['goods'])->where(['id' => $id, 'mark' => 1])
  548. ->select(['id','order_no','out_order_no','user_id','merch_id','pay_money','coupon_id','pay_status','status','refund_status','pay_time'])
  549. ->first();
  550. $status = isset($info['status']) ? $info['status'] : 0;
  551. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  552. $outOrderNo = isset($info['out_order_no']) ? $info['out_order_no'] : '';
  553. $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  554. $payStatus = isset($info['pay_status']) ? $info['pay_status'] : 0;
  555. $refundStatus = isset($info['refund_status']) ? $info['refund_status'] : 0;
  556. $goods = isset($info['goods']) ? $info['goods'] : [];
  557. if (!$id || empty($info)) {
  558. $this->error = 2912;
  559. return false;
  560. }
  561. // 非法操作
  562. if($userId != $orderUserId){
  563. $this->error = 2913;
  564. return false;
  565. }
  566. if(!in_array($status, [2,3,5])){
  567. $this->error = 2914;
  568. return false;
  569. }
  570. if($refundStatus>0 && $refundStatus != 2){
  571. $this->error = 2915;
  572. return false;
  573. }
  574. if(!in_array($refundType,[1,2,3,4])){
  575. $this->error = 2922;
  576. return false;
  577. }
  578. // 多商品退款暂不开放
  579. /*if(count($goods)>1){
  580. $this->error = 2920;
  581. return false;
  582. }*/
  583. // 站外售后验证
  584. $result = SupplyService::make()->getApiData('getAfterCan',['plat_order_no'=> $outOrderNo]);
  585. $orderGoods = isset($result['order_goods'])? $result['order_goods'] : [];
  586. $orderGoodsInfo = isset($orderGoods[0])? $orderGoods[0] : [];
  587. $orderGoodsId = isset($orderGoodsInfo['order_goods_id'])? $orderGoodsInfo['order_goods_id'] : 0;
  588. $orderGoodsSkuSn = isset($orderGoodsInfo['goods_sku'])? $orderGoodsInfo['goods_sku'] : '';
  589. $orderAfterNum= isset($orderGoodsInfo['after_num'])? $orderGoodsInfo['after_num'] : 0;
  590. if(empty($orderGoods) || empty($orderGoodsInfo) || empty($orderGoodsId) || empty($orderGoodsSkuSn)){
  591. $this->error = 2918;
  592. return false;
  593. }
  594. // 商品验证
  595. if($skuSn){
  596. // 多商品中的一个
  597. $canRefund = false;
  598. foreach($orderGoods as $item){
  599. $outRefundStatus = isset($item['status'])? intval($item['status']) : 0;
  600. $goodsSkuSn = isset($item['goods_sku'])? $item['goods_sku'] : '';
  601. $orderAfterNum = isset($item['after_num'])? $item['after_num'] : 0;
  602. if($goodsSkuSn == $skuSn && $outRefundStatus ==1 && $orderAfterNum>0){
  603. $canRefund = true;
  604. $orderGoodsId = isset($item['order_goods_id'])? $item['order_goods_id'] : '';
  605. }
  606. }
  607. if(!$canRefund){
  608. $this->error = 2921;
  609. return false;
  610. }
  611. }
  612. // 单商品
  613. else{
  614. $skuSn = $orderGoodsSkuSn;
  615. $outRefundStatus = isset($orderGoodsInfo['status'])? $orderGoodsInfo['status'] : 0;
  616. if($outRefundStatus == 0){
  617. $this->error = 2921;
  618. return false;
  619. }
  620. }
  621. // 站外申请售后
  622. $data = [
  623. 'plat_order_no'=> $outOrderNo,
  624. 'goods_sku'=> $skuSn,
  625. 'after_num'=> $orderAfterNum,
  626. 'order_goods_id'=> $orderGoodsId,
  627. 'after_type'=> $refundType,
  628. 'explain'=> $refundRemark,
  629. ];
  630. $result = SupplyService::make()->getApiData('applyAfter', $data);
  631. $platAfterNo = isset($result['plat_after_no'])? $result['plat_after_no'] : '';
  632. if(empty($platAfterNo) || empty($result)){
  633. $this->error = 2925;
  634. return false;
  635. }
  636. // 处理
  637. DB::beginTransaction();
  638. if(!$this->model->where(['id'=> $id,'mark'=>1])->update(['status'=> 5,'refund_status'=>1,'refund_temp_status'=> $status,'refund_type'=> $refundType,'refund_remark'=> $refundRemark,'update_time'=>time()])){
  639. $this->error = 2917;
  640. DB::rollBack();
  641. return false;
  642. }
  643. if(!OrderGoodsModel::where(['order_no'=> $orderNo,'sku_sn'=>$skuSn])->update(['plat_after_no'=> $platAfterNo,'order_goods_id'=> $orderGoodsId,'refund_status'=>1,'update_time'=>time()])){
  644. $this->error = 2917;
  645. DB::rollBack();
  646. return false;
  647. }
  648. $this->error = 2916;
  649. DB::commit();
  650. return ['status_text'=>'已售后','refund_text'=>'待审核','id'=> $id];
  651. }
  652. /**
  653. * 申请退款取消
  654. * @param $userId
  655. * @param $params
  656. * @return bool
  657. */
  658. public function refundCancel($userId, $params)
  659. {
  660. $id = isset($params['id']) ? $params['id'] : 0;
  661. $info = $this->model->where(['id' => $id, 'mark' => 1])
  662. ->select(['id','user_id','merch_id','order_no','pay_money','coupon_id','status','refund_status','refund_temp_status','pay_time'])
  663. ->first();
  664. $status = isset($info['status']) ? $info['status'] : 0;
  665. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  666. $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  667. $refundStatus = isset($info['refund_status']) ? $info['refund_status'] : 0;
  668. $tempStatus = isset($info['refund_temp_status']) ? $info['refund_temp_status'] : 0;
  669. if (!$id || empty($info)) {
  670. $this->error = 2912;
  671. return false;
  672. }
  673. // 非法操作
  674. if($userId != $orderUserId){
  675. $this->error = 2913;
  676. return false;
  677. }
  678. if($status != 5){
  679. $this->error = 2914;
  680. return false;
  681. }
  682. if($refundStatus != 1){
  683. $this->error = 2927;
  684. return false;
  685. }
  686. // 售后商品
  687. $platAfterNo = OrderGoodsModel::where(['order_no'=> $orderNo])->whereIn('refund_status',[1,2])->value('plat_after_no');
  688. if(empty($platAfterNo)){
  689. $this->error = 2928;
  690. return false;
  691. }
  692. // 取消售后
  693. $result = SupplyService::make()->getApiData('applyAfterCancel',['after_order_no'=> $platAfterNo,'explain'=>'用户取消']);
  694. if(!$result){
  695. $this->error = SupplyService::make()->getError();
  696. return false;
  697. }
  698. // 处理
  699. DB::beginTransaction();
  700. $updateData = ['status'=> $tempStatus,'refund_status'=>0,'refund_remark'=> '','update_time'=>time()];
  701. if(!$this->model->where(['id'=> $id,'mark'=>1])->update($updateData)){
  702. DB::rollBack();
  703. $this->error = 2929;
  704. return false;
  705. }
  706. if(!OrderGoodsModel::where(['order_no'=> $orderNo,'plat_after_no'=>$platAfterNo])->update(['refund_status'=>0,'update_time'=>time()])){
  707. $this->error = 2929;
  708. DB::rollBack();
  709. return false;
  710. }
  711. DB::commit();
  712. $statusArr = [1=>'待付款',2=>'待发货',3=>'待收货',4=>'已完成',5=>'已售后'];
  713. $this->error = 2930;
  714. return ['status'=> $tempStatus,'refund_text'=>'','status_text'=>isset($statusArr[$tempStatus])? $statusArr[$tempStatus]:''];
  715. }
  716. /**
  717. * 售后确认通过
  718. * @param $userId
  719. * @param $params
  720. * @return bool
  721. */
  722. public function refundConfirm($userId, $params)
  723. {
  724. $orderId = isset($params['id']) ? $params['id'] : 0;
  725. $goodsId = isset($params['goods_id']) ? $params['goods_id'] : 0;
  726. $platAfterNo = isset($params['plat_after_no']) ? $params['plat_after_no'] : '';
  727. $info = $this->model->with(['goods','member'])->where(['id' => $orderId, 'mark' => 1])
  728. ->select(['id','user_id','merch_id','order_no','xd_total','pay_money','coupon_id','status','refund_status','pay_time'])
  729. ->first();
  730. $status = isset($info['status']) ? $info['status'] : 0;
  731. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  732. $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  733. $refundStatus = isset($info['refund_status']) ? $info['refund_status'] : 0;
  734. $payTotal = isset($info['pay_money']) ? $info['pay_money'] : 0;
  735. $userInfo = isset($info['member']) ? $info['member'] : [];
  736. $goods = isset($info['goods']) ? $info['goods'] : [];
  737. if (!$orderId || empty($info)) {
  738. $this->error = 2912;
  739. return false;
  740. }
  741. if($status != 5){
  742. $this->error = 2914;
  743. return false;
  744. }
  745. if($refundStatus != 1){
  746. $this->error = 2927;
  747. return false;
  748. }
  749. if(empty($goods)){
  750. $this->error = 2933;
  751. return false;
  752. }
  753. if(empty($userInfo)){
  754. $this->error = 3003;
  755. return false;
  756. }
  757. // 售后商品处理
  758. DB::beginTransaction();
  759. $refundTotal = 0;
  760. $goodsName = '';
  761. foreach ($goods as $item){
  762. $id = isset($item['goods_id'])? $item['goods_id'] : 0;
  763. $goodsRefundStatus = isset($item['refund_status'])? $item['refund_status'] : 0;
  764. // $goodsPlatAfterNo = isset($item['plat_after_no'])? $item['plat_after_no'] : 0;
  765. $xdTotal = isset($item['xd_total'])? $item['xd_total'] : 0;
  766. if(($goodsId == 0 && $goodsRefundStatus != 3) || (in_array($goodsRefundStatus,[1,2]) && $id == $goodsId)){
  767. if(!OrderGoodsModel::where(['goods_id'=> $id,'order_no'=> $orderNo])->update(['refund_status'=>3,'update_time'=>time()])){
  768. DB::rollBack();
  769. $this->error = 2934;
  770. return false;
  771. }
  772. $refundTotal+= $xdTotal;
  773. $goodsName = $goodsId? $item['goods_name'] : '全部';
  774. }
  775. }
  776. // 统一退款处理
  777. if($refundTotal<=0){
  778. DB::rollBack();
  779. $this->error = 2935;
  780. return false;
  781. }
  782. // 订单售后状态
  783. if(!$this->model->where(['id'=> $orderId,'mark'=>1])->update(['refund_status'=> 3,'update_time'=>time()])){
  784. DB::rollBack();
  785. $this->error = 2932;
  786. return false;
  787. }
  788. // 退款进账
  789. $updateData = ['balance'=> DB::raw("balance + {$refundTotal}"),'update_time'=>time()];
  790. if(!MemberModel::where(['id'=> $orderUserId])->update($updateData)){
  791. DB::rollBack();
  792. $this->error = 2028;
  793. return false;
  794. }
  795. // 账户明细
  796. $userXd = isset($userInfo['balance'])? $userInfo['balance'] : 0;
  797. $log = [
  798. 'user_id' => $orderUserId,
  799. 'source_id' => $orderId,
  800. 'source_order_no' => $orderNo,
  801. 'type' => 7,
  802. 'coin_type' => 2,
  803. 'user_type'=> 1,
  804. 'money' => $refundTotal,
  805. 'actual_money' => $refundTotal,
  806. 'balance' => $userXd,
  807. 'create_time' => time(),
  808. 'update_time' => time(),
  809. 'remark' => "商城订单售后退款",
  810. 'status' => 1,
  811. 'mark' => 1,
  812. ];
  813. if(!AccountLogModel::insertGetId($log)){
  814. DB::rollBack();
  815. $this->error = 2029;
  816. return false;
  817. }
  818. // 退款消息
  819. $dateTime = date('Y-m-d H:i:s');
  820. if($goodsId){
  821. $message = "您的订单在{$dateTime}(UTC+8)已成功售后:\n售后商品:{$goodsName}\n售后订单:{$orderNo}\n售后交易订单:{$platAfterNo}\n订单金额:{$payTotal}\n退款星豆:{$refundTotal} \n退款前:{$userXd} 星豆\n";
  822. }else{
  823. $message = "您的订单在{$dateTime}(UTC+8)已成功售后:\n售后商品:{$goodsName}\n售后订单:{$orderNo}\n订单金额:{$payTotal}\n退款星豆:{$refundTotal} \n退款前:{$userXd} 星豆\n";
  824. }
  825. MessageService::make()->pushMessage($userId, '每日积分返还星豆奖励', $message,3);
  826. DB::commit();
  827. $this->error = 2936;
  828. return ['order_id'=>$orderId,'goods_id'=>$goodsId,'plat_after_no'=>$platAfterNo,'pay_total'=>$payTotal,'refund_total'=>$refundTotal];
  829. }
  830. /**
  831. * 收货/已完成
  832. * @return bool
  833. */
  834. public function complete($userId, $params)
  835. {
  836. $id = isset($params['id']) ? $params['id'] : 0;
  837. $info = $this->model->with(['goods'])
  838. ->where(['id' => $id, 'mark' => 1])
  839. ->select(['id','user_id','order_no','out_order_no','merch_uid','xd_total','pay_money','type','postage','award_wait_score','award_power_num','award_score','merch_bonus','coupon_id','status','refund_status','pay_time'])
  840. ->first();
  841. $status = isset($info['status']) ? $info['status'] : 0;
  842. $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  843. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  844. $goods = isset($info['goods'])? $info['goods'] : [];
  845. if (!$id || empty($info)) {
  846. $this->error = 2912;
  847. return false;
  848. }
  849. // 非法操作
  850. if($userId && $orderUserId != $userId){
  851. $this->error = 2913;
  852. return false;
  853. }
  854. // 订单状态
  855. if (!in_array($status, [2,3,5])) {
  856. $this->error = 2914;
  857. return false;
  858. }
  859. $realXdTotal = 0;
  860. $hasGoods = false;
  861. foreach($goods as $item){
  862. if(!in_array($item['refund_status'],[3])){
  863. $hasGoods = true;
  864. $realXdTotal += $item['xd_total'];
  865. }
  866. }
  867. if(!$hasGoods || $realXdTotal <=0){
  868. $this->error = 2931;
  869. return false;
  870. }
  871. // TODO 确认数据处理
  872. DB::beginTransaction();
  873. // 更新订单状态
  874. if (!$this->model->where(['id' => $id])->update(['status' => 4, 'update_time' => time()])) {
  875. DB::rollBack();
  876. $this->error = 2932;
  877. return false;
  878. }
  879. // 更新商品/服务销量
  880. if($goods){
  881. foreach($goods as $item){
  882. $num = isset($item['num'])? $item['num'] : 0;
  883. if($num>0){
  884. GoodsModel::where(['goods_id'=> $item['goods_id'],'mark'=>1])->update([
  885. 'sales'=> DB::raw("sales + {$num}"),'update_time'=> time()
  886. ]);
  887. }
  888. }
  889. }
  890. // 完成订单消息
  891. $dateTime = date('Y-m-d H:i:s');
  892. $message = "您在{$dateTime}(UTC+8)成功完成购物订单【{$orderNo}】交易。";
  893. MessageService::make()->pushMessage($orderUserId,"订单交易完成",$message, 2);
  894. // 订单结算奖励和佣金
  895. if(!$result = FinanceService::make()->settleOrder($info, $realXdTotal)){
  896. DB::rollBack();
  897. $this->error = FinanceService::make()->getError();
  898. return false;
  899. }
  900. DB::commit();
  901. $this->error = 1002;
  902. return $result;
  903. }
  904. /**
  905. * 隐藏删除
  906. * @return false
  907. */
  908. public function hide($userId, $params)
  909. {
  910. $id = isset($params['id']) ? $params['id'] : 0;
  911. $info = $this->model->where(['id' => $id, 'mark' => 1])
  912. ->select(['id','user_id','order_no','status','is_hide'])
  913. ->first();
  914. if (!$id || empty($info)) {
  915. $this->error = 2912;
  916. return false;
  917. }
  918. $isHide = isset($info['is_hide'])? $info['is_hide'] : 0;
  919. if($isHide >0){
  920. $this->error = 2914;
  921. return false;
  922. }
  923. if($this->model->where(['id'=> $id])->update(['is_hide'=>2,'update_time'=>time()]))
  924. {
  925. $this->error = 1002;
  926. return true;
  927. }else{
  928. $this->error = 1003;
  929. return false;
  930. }
  931. }
  932. /**
  933. * 查询物流信息
  934. * @param $userId 用户
  935. * @param $params
  936. * @return array|false|mixed
  937. */
  938. public function getTrack($userId, $params)
  939. {
  940. $outOrderNo = isset($params['out_order_no'])? $params['out_order_no'] : '';
  941. if(empty($outOrderNo)){
  942. $this->error = 2014;
  943. return false;
  944. }
  945. $cacheKey = "caches:orders:track_{$userId}_{$outOrderNo}";
  946. $data = RedisService::get($cacheKey);
  947. if($data || RedisService::exists($cacheKey)){
  948. return $data? $data : [];
  949. }
  950. $result = SupplyService::make()->getApiData('getOrderTrack',['out_order_no'=> $outOrderNo]);
  951. $data = isset($result['logistics'])? $result['logistics'] : [];
  952. if($data){
  953. RedisService::set($cacheKey, $data, rand(30, 60));
  954. $this->error = 1010;
  955. return $data;
  956. }else{
  957. $this->error = 2924;
  958. return false;
  959. }
  960. }
  961. /**
  962. * 查询售后信息
  963. * @param $userId 用户
  964. * @param $params
  965. * @return array|false|mixed
  966. */
  967. public function getAfter($userId, $params)
  968. {
  969. $platAfterNo = isset($params['plat_after_no'])? $params['plat_after_no'] : '';
  970. if(empty($platAfterNo)){
  971. $this->error = 2014;
  972. return false;
  973. }
  974. $cacheKey = "caches:orders:after_{$userId}_{$platAfterNo}";
  975. $data = RedisService::get($cacheKey);
  976. if($data || RedisService::exists($cacheKey)){
  977. return $data? $data : [];
  978. }
  979. $result = SupplyService::make()->getApiData('getAfterStatus',['plat_after_no'=> $platAfterNo]);
  980. if($result){
  981. $result['goods'] = [];
  982. $goods = OrderGoodsModel::where(['plat_after_no'=> $platAfterNo])->first();
  983. if($goods){
  984. $goods['sku_attr'] = isset($goods['sku_attr']) && $goods['sku_attr']? json_decode($goods['sku_attr'], true) : [];
  985. $result['goods'] = $goods;
  986. }
  987. RedisService::set($cacheKey, $result, rand(30, 60));
  988. $this->error = 1010;
  989. return $result;
  990. }else{
  991. $this->error = 2926;
  992. return false;
  993. }
  994. }
  995. /**
  996. * 订单发货状态自动处理
  997. * @return array|false
  998. */
  999. public function updateOrderStatus()
  1000. {
  1001. set_time_limit(0);
  1002. $cacheKey = "cache:orders:update_lock";
  1003. if(RedisService::get($cacheKey)){
  1004. $this->error = 3001;
  1005. return false;
  1006. }
  1007. $page = RedisService::get('caches:orders:update_page');
  1008. $page = $page? $page+1 : 1;
  1009. $catchNum = ConfigService::make()->getConfigByCode('update_order_num',100);
  1010. $pageSize = $catchNum>10 && $catchNum<=200? $catchNum : 100;
  1011. $orders = $this->model->whereIn('status',[2,3])
  1012. ->where(['mark'=>1])
  1013. ->select(['id','user_id','order_no','out_order_no','status'])
  1014. ->orderBy('pay_time','asc')
  1015. ->offset(($page-1)*$pageSize)
  1016. ->limit($pageSize)
  1017. ->get();
  1018. $orders = $orders? $orders->toArray() : [];
  1019. RedisService::set($cacheKey, $orders, rand(30,60));
  1020. $success = 0;
  1021. $deliverCount = 0;
  1022. if($orders){
  1023. foreach ($orders as $item){
  1024. $orderId = isset($item['id'])? $item['id'] : 0;
  1025. $orderNo = isset($item['order_no'])? $item['order_no'] : '';
  1026. $outOrderNo = isset($item['out_order_no'])? $item['out_order_no'] : '';
  1027. if(empty($orderNo) || empty($outOrderNo)){
  1028. continue;
  1029. }
  1030. $lockCacheKey = "caches:orders:update:{$orderNo}_{$outOrderNo}";
  1031. if(RedisService::get($lockCacheKey.'_lock')){
  1032. continue;
  1033. }
  1034. $data = [
  1035. 'out_order_no'=> $outOrderNo,
  1036. 'third_order_no'=> $orderNo,
  1037. ];
  1038. $result = SupplyService::make()->getApiData('getOrderDetail', $data);
  1039. if(empty($result)){
  1040. RedisService::clear($lockCacheKey.'_lock');
  1041. continue;
  1042. }
  1043. $orderStatus = isset($result['status'])? $result['status'] : 0;
  1044. $deliveryTime = isset($result['delivery_time']) && $result['delivery_time']? strtotime($result['delivery_time']) : time();
  1045. $supplyOrderNo = isset($result['supply_order_no']) && $result['supply_order_no']? $result['supply_order_no'] : '';
  1046. // 已发货,待收货
  1047. if($orderStatus == 3){
  1048. if($this->model->where(['order_no'=> $orderNo])->update(['supply_order_no'=>$supplyOrderNo,'express_time'=> $deliveryTime,'status'=>3,'update_time'=>time()])){
  1049. $deliverCount++;
  1050. }
  1051. }
  1052. // 已完成
  1053. else if(in_array($orderStatus,[4,7]))
  1054. {
  1055. if($this->complete(0,['order_id'=> $orderId,'supply_order_no'=>$supplyOrderNo])){
  1056. $success++;
  1057. }
  1058. }
  1059. RedisService::clear($lockCacheKey.'_lock');
  1060. }
  1061. }
  1062. RedisService::clear($cacheKey);
  1063. return ['count'=> count($orders),'success'=> $success, 'delivery'=>$deliverCount];
  1064. }
  1065. /**
  1066. * 售后订单自动处理
  1067. * @return array|false
  1068. */
  1069. public function updateOrderRefundStatus()
  1070. {
  1071. set_time_limit(0);
  1072. $cacheKey = "cache:orders:update_lock";
  1073. if(RedisService::get($cacheKey)){
  1074. $this->error = 3001;
  1075. return false;
  1076. }
  1077. $page = RedisService::get('caches:orders:update_page');
  1078. $page = $page? $page+1 : 1;
  1079. $catchNum = ConfigService::make()->getConfigByCode('update_order_num',100);
  1080. $pageSize = $catchNum>10 && $catchNum<=200? $catchNum : 100;
  1081. $orders = OrderGoodsModel::from('orders_goods as a')
  1082. ->leftJoin('orders as b','b.order_no','=','a.order_no')
  1083. ->whereIn('a.refund_status',[1,2])
  1084. ->where(['a.mark'=>1])
  1085. ->select(['b.id','b.user_id','b.order_no','b.out_order_no','a.refund_status','a.goods_id','a.plat_after_no','b.status','a.goods_id','a.sku_sn'])
  1086. ->orderBy('create_time','asc')
  1087. ->offset(($page-1)*$pageSize)
  1088. ->limit($pageSize)
  1089. ->get();
  1090. $orders = $orders? $orders->toArray() : [];
  1091. RedisService::set($cacheKey, $orders, rand(30,60));
  1092. $success = 0;
  1093. $refundCount = 0;
  1094. if($orders){
  1095. foreach ($orders as $item){
  1096. $orderId = isset($item['id'])? $item['id'] : 0;
  1097. $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
  1098. $orderNo = isset($item['order_no'])? $item['order_no'] : '';
  1099. $outOrderNo = isset($item['out_order_no'])? $item['out_order_no'] : '';
  1100. $platAfterNo = isset($item['plat_after_no'])? $item['plat_after_no'] : '';
  1101. if(empty($orderNo) || empty($outOrderNo) || empty($platAfterNo)){
  1102. continue;
  1103. }
  1104. $lockCacheKey = "caches:orders:refund:{$orderNo}_{$platAfterNo}";
  1105. if(RedisService::get($lockCacheKey.'_lock')){
  1106. continue;
  1107. }
  1108. $data = [
  1109. 'out_order_no'=> $outOrderNo,
  1110. 'third_order_no'=> $orderNo,
  1111. ];
  1112. $result = SupplyService::make()->getApiData('getAfterStatus', $data);
  1113. if(empty($result)){
  1114. RedisService::clear($lockCacheKey.'_lock');
  1115. continue;
  1116. }
  1117. $orderStatus = isset($result['status'])? $result['status'] : 0;
  1118. $goodsSkuSn = isset($result['goods_sku']) && $result['goods_sku']? $result['goods_sku'] : '';
  1119. // 售后中
  1120. if(in_array($orderStatus,[2,4,5,6])){
  1121. if(OrderGoodsModel::where(['plat_after_no'=> $platAfterNo])->update(['refund_status'=>$orderStatus,'update_time'=>time()])){
  1122. $refundCount++;
  1123. }
  1124. }
  1125. // 已完成
  1126. else if(in_array($orderStatus,[3]))
  1127. {
  1128. if($this->refundConfirm(0,['order_id'=> $orderId,'goods_id'=>$goodsId,'plat_after_no'=>$platAfterNo])){
  1129. $success++;
  1130. }
  1131. }
  1132. RedisService::clear($lockCacheKey.'_lock');
  1133. }
  1134. }
  1135. RedisService::clear($cacheKey);
  1136. return ['count'=> count($orders),'success'=> $success, 'refund'=>$refundCount];
  1137. }
  1138. }