OrderService.php 47 KB

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