OrderService.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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\CartModel;
  14. use App\Models\GoodsModel;
  15. use App\Models\GoodsSkuModel;
  16. use App\Models\MemberModel;
  17. use App\Models\OrderGoodsModel;
  18. use App\Models\OrderModel;
  19. use App\Models\StoreModel;
  20. use App\Services\BaseService;
  21. use App\Services\ConfigService;
  22. use App\Services\Kd100Service;
  23. use App\Services\PaymentService;
  24. use App\Services\RedisService;
  25. use Illuminate\Support\Facades\DB;
  26. /**
  27. * 订单-服务类
  28. * @author laravel开发员
  29. * @since 2020/11/11
  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. */
  41. public function __construct()
  42. {
  43. $this->model = new OrderModel();
  44. }
  45. /**
  46. * 静态入口
  47. */
  48. public static function make()
  49. {
  50. if (!self::$instance) {
  51. self::$instance = new static();
  52. }
  53. return self::$instance;
  54. }
  55. /**
  56. * 订单列表
  57. * @param $params
  58. * @param int $pageSize
  59. * @return array
  60. */
  61. public function getDataList($params, $pageSize = 15)
  62. {
  63. $model = $this->getQuery($params);
  64. // 数据
  65. $list = $model->where(function ($query) use ($params) {
  66. $status = isset($params['status']) ? $params['status'] : 0;
  67. if ($status > 0 && is_array($status)) {
  68. $query->whereIn('a.status', $status)->where('a.refund_status', 0);
  69. } else if ($status == 6) {
  70. $query->where('a.status', '>', 1)->where('a.refund_status', '>', 0);
  71. } else if ($status > 0) {
  72. $query->where('a.status', $status)->where('a.refund_status', 0);
  73. }
  74. })->select(['a.*'])
  75. ->orderBy('a.id', 'desc')
  76. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  77. $list = $list ? $list->toArray() : [];
  78. if ($list) {
  79. $statusArr = [1 => '待支付', 2 => '待发货', 3 => '待收货', 4 => '确认收货'];
  80. foreach ($list['data'] as &$item) {
  81. $item['create_time'] = $item['create_time'] ? dateFormat(strtotime($item['create_time']), 'Y-m-d') : '';
  82. $status = isset($item['status']) ? $item['status'] : 0;
  83. $item['status_text'] = '待支付';
  84. if ($status) {
  85. $item['status_text'] = isset($statusArr[$status]) ? $statusArr[$status] : '';
  86. }
  87. if ($item['refund_status'] > 0) {
  88. $item['status_text'] = '退款/售后';
  89. }
  90. $item['goods'] = isset($item['goods']) && $item['goods'] ? $item['goods'] : [];
  91. $item['real_total'] = $item['pay_total'];
  92. $item['pay_total'] = moneyFormat($item['pay_total'] + $item['delivery_fee'], 2);
  93. }
  94. unset($item);
  95. }
  96. return [
  97. 'pageSize' => $pageSize,
  98. 'total' => isset($list['total']) ? $list['total'] : 0,
  99. 'list' => isset($list['data']) ? $list['data'] : []
  100. ];
  101. }
  102. /**
  103. * 查询条件
  104. * @param $params
  105. * @return mixed
  106. */
  107. public function getQuery($params)
  108. {
  109. $where = ['a.is_hide' => 0,'a.main_no'=>'', 'a.mark' => 1];
  110. return $this->model->from('orders as a')
  111. ->with(['orderGoods', 'store'])
  112. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  113. ->where($where)
  114. ->where(function ($query) use ($params) {
  115. $userId = isset($params['user_id']) ? intval($params['user_id']) : 0;
  116. if ($userId > 0) {
  117. $query->where('a.user_id', $userId);
  118. }
  119. // 订单类型
  120. $type = isset($params['type']) ? intval($params['type']) : 0;
  121. if ($type > 0) {
  122. $query->where('a.type', $type);
  123. }
  124. })
  125. ->where(function ($query) use ($params) {
  126. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  127. if ($keyword) {
  128. $query->where('a.order_no', 'like', "%{$keyword}%")
  129. ->orWhere('b.mobile', 'like', "%{$keyword}%");
  130. }
  131. });
  132. }
  133. /**
  134. * 订单详情
  135. * @param $id
  136. */
  137. public function getOrderInfo($id, $no = '')
  138. {
  139. $where = ['a.order_no' => $no, 'a.id' => $id, 'a.mark' => 1];
  140. if ($no) {
  141. unset($where['a.id']);
  142. } else {
  143. unset($where['a.order_no']);
  144. }
  145. $statusArr = [1 => '待支付', 2 => '待发货', 3 => '待收货', 4 => '已完成'];
  146. $info = $this->model->from('orders as a')
  147. ->with(['orderGoods', 'store'])
  148. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  149. ->where($where)
  150. ->select(['a.*'])
  151. ->first();
  152. if ($info) {
  153. $info = $info->toArray();
  154. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  155. $info['pay_at'] = $info['pay_at'] ? datetime(strtotime($info['pay_at']), 'Y-m-d H:i:s') : '';
  156. $info['receiver_mobile_text'] = $info['receiver_mobile'] ? format_mobile($info['receiver_mobile']) : '';
  157. $info['real_total'] = $info['pay_total'];
  158. $info['pay_total'] = moneyFormat($info['pay_total'] + $info['delivery_fee'], 2);
  159. $status = isset($info['status']) ? $info['status'] : 0;
  160. $info['status_text'] = '待支付';
  161. if ($status) {
  162. $info['status_text'] = isset($statusArr[$status]) ? $statusArr[$status] : '';
  163. }
  164. }
  165. return $info;
  166. }
  167. /**
  168. * 创建订单
  169. * @param $userId 用户
  170. * @param $params 参数
  171. * @return array|false
  172. */
  173. public function createOrder($userId, $params)
  174. {
  175. $type = isset($params['type']) && $params['type'] ? $params['type'] : 1;
  176. $submitType = isset($params['submit_type']) && $params['submit_type'] ? $params['submit_type'] : '';
  177. $addressId = isset($params['address_id']) && $params['address_id'] ? $params['address_id'] : 0;
  178. $goods = isset($params['goods']) && $params['goods'] ? $params['goods'] : [];
  179. $ids = $goods ? array_column($goods, 'id') : [];
  180. // 参数验证
  181. if (empty($goods) || empty($ids)) {
  182. $this->error = '商品参数不为空';
  183. return false;
  184. }
  185. if ($addressId <= 0) {
  186. $this->error = '请选择收货地址';
  187. return false;
  188. }
  189. // 缓存锁
  190. $cacheLockKey = "caches:orders:submit_lock:{$userId}";
  191. if (RedisService::get($cacheLockKey)) {
  192. $this->error = '订单处理中~';
  193. return false;
  194. }
  195. // 用户信息
  196. RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
  197. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  198. ->select(['id', 'openid','nickname','member_level','parent_id','point_id','points', 'buy_type','property','balance', 'bd_score', 'status'])
  199. ->first();
  200. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  201. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : 0;
  202. $buyType = isset($userInfo['buy_type']) ? $userInfo['buy_type'] : 0;
  203. $bdScore = isset($userInfo['bd_score']) ? $userInfo['bd_score'] : 0;
  204. $discountPoint = isset($userInfo['discount_point']) ? $userInfo['discount_point'] : 0; // 折扣
  205. if (empty($userInfo) || $status != 1) {
  206. $this->error = 1045;
  207. return false;
  208. }
  209. if (empty($openid)) {
  210. $this->error = 1042;
  211. return false;
  212. }
  213. // 商品数据
  214. $orderNo = get_order_num('JK');
  215. $result = GoodsService::make()->getOrderGoods($ids, $goods, $userId, $orderNo, $discountPoint);
  216. if (empty($result)) {
  217. RedisService::clear($cacheLockKey);
  218. $this->error = GoodsService::make()->getError();
  219. return false;
  220. }
  221. $orderGoods = isset($result['goods']) ? $result['goods'] : [];
  222. $goodsTotal = isset($result['goods_total']) ? $result['goods_total'] : 0; // 商品总价
  223. $orderTotal = isset($result['order_total']) ? $result['order_total'] : 0; // 订单结算金额(折后)
  224. $bdScoreTotal = isset($result['bd_score_total']) ? $result['bd_score_total'] : 0; // 报单积分
  225. $profitTotal = isset($result['profit_total']) ? $result['profit_total'] : 0; // 利润
  226. $discountTotal = isset($result['discount_total']) ? $result['discount_total'] : 0; // 折扣金额
  227. $orderCount = isset($result['count']) ? $result['count'] : 0;
  228. $deliveryFee = isset($result['delivery_fee']) ? $result['delivery_fee'] : 0; // 运费
  229. $storeId = isset($result['store_id']) ? $result['store_id'] : 0; // 企业/商家
  230. if (empty($orderGoods)) {
  231. RedisService::clear($cacheLockKey);
  232. $this->error = '获取订单商品错误~';
  233. return false;
  234. }
  235. if ($orderTotal <= 0) {
  236. RedisService::clear($cacheLockKey);
  237. $this->error = '订单金额错误~';
  238. return false;
  239. }
  240. if ($orderCount <= 0) {
  241. RedisService::clear($cacheLockKey);
  242. $this->error = '订单商品数量错误~';
  243. return false;
  244. }
  245. // 创业大礼包
  246. $mealId = 0;
  247. if ($type == 2) {
  248. $mealId = isset($ids[0]) ? $ids[0] : 0;
  249. if ($buyType != $mealId) {
  250. RedisService::clear($cacheLockKey);
  251. $this->error = '该礼包您暂时不满足购买条件';
  252. return false;
  253. }
  254. // 报单积分是否够
  255. if ($bdScore < $bdScoreTotal) {
  256. RedisService::clear($cacheLockKey);
  257. $this->error = '抱歉,您的报单积分不足';
  258. return false;
  259. }
  260. // 是否购买过
  261. $buyLimit = ConfigService::make()->getConfigByCode('business_buy_limit', 0);
  262. $buyLimit = $buyLimit>0?$buyLimit:0;
  263. if($buyLimit>0){
  264. $checkOrderCount = $this->checkBusinessOrder($userId, $mealId);
  265. if ($checkOrderCount >= $buyLimit) {
  266. RedisService::clear($cacheLockKey);
  267. $this->error = $buyLimit>1?"抱歉该创业礼包仅限购买{$buyLimit}次":'抱歉,您已经购买过该礼包';
  268. return false;
  269. }
  270. }
  271. }
  272. // 收货地址信息
  273. $addressInfo = MemberAddressService::make()->getBindInfo($userId, $addressId);
  274. $realname = isset($addressInfo['realname']) ? $addressInfo['realname'] : '';
  275. $mobile = isset($addressInfo['mobile']) ? $addressInfo['mobile'] : '';
  276. $area = isset($addressInfo['area']) ? $addressInfo['area'] : '';
  277. $address = isset($addressInfo['address']) ? $addressInfo['address'] : '';
  278. if (empty($addressInfo) || empty($realname) || empty($mobile) || empty($area) || empty($address)) {
  279. RedisService::clear($cacheLockKey);
  280. $this->error = '收货地址信息错误,请核对后重试~';
  281. return false;
  282. }
  283. // 是否开启分账功能
  284. $payTotal = moneyFormat($orderTotal + $deliveryFee, 2); // 含运费支付金额
  285. if (env('PAY_DEBUG')) {
  286. $payTotal = 0.01;
  287. }
  288. // 订单数据
  289. $order = [
  290. 'order_no' => $orderNo,
  291. 'user_id' => $userId,
  292. 'store_id' => $storeId,
  293. 'meal_id' => $mealId,
  294. 'type' => $type,
  295. 'total' => $goodsTotal, // 商品总价
  296. 'num' => $orderCount,
  297. 'bd_score' => $bdScoreTotal, // 折扣后商品总价(不含运费)
  298. 'profit_total' => $profitTotal, // 折扣后商品总价(不含运费)
  299. 'pay_money' => $orderTotal, // 折扣后商品总价(不含运费)
  300. 'pay_total' => $type==2 && $bdScoreTotal>0?moneyFormat($orderTotal + $bdScoreTotal,2):$orderTotal, // 折扣后商品总价(不含运费)
  301. 'pay_status' => $type==2 && $bdScoreTotal>0? 30 : 10, // 折扣后商品总价(不含运费)
  302. 'discount_point' => $discountPoint,
  303. 'discount_total' => $discountTotal, // 折扣金额
  304. 'delivery_fee' => $deliveryFee, // 运费
  305. 'receiver_name' => $realname,
  306. 'receiver_mobile' => $mobile,
  307. 'receiver_area' => $area,
  308. 'receiver_address' => $address,
  309. 'create_time' => time(),
  310. 'update_time' => time(),
  311. 'status' => 1,
  312. 'mark' => 1,
  313. ];
  314. // 订单处理
  315. DB::beginTransaction();
  316. if (!$orderId = $this->model->insertGetId($order)) {
  317. DB::rollBack();
  318. $this->error = '创建订单失败';
  319. RedisService::clear($cacheLockKey);
  320. return false;
  321. }
  322. // 订单商品
  323. if ($orderGoods && !OrderGoodsModel::insert($orderGoods)) {
  324. DB::rollBack();
  325. $this->error = '处理订单商品错误';
  326. RedisService::clear($cacheLockKey);
  327. return false;
  328. }
  329. // 创业礼包分单
  330. if ($type == 2) {
  331. $data = $order;
  332. $data['total'] = moneyFormat($data['total'] / 6);
  333. $data['bd_score'] = moneyFormat($bdScoreTotal / 6, 2);
  334. $data['profit_total'] = moneyFormat($profitTotal / 6, 2);
  335. $data['pay_total'] = moneyFormat($orderTotal / 6, 2);
  336. $data['pay_money'] = moneyFormat($order['pay_money'] / 6, 2);
  337. $subOrders = [];
  338. for ($i = 1; $i <= 6; $i++) {
  339. $data['order_no'] = get_order_num('BS');
  340. $data['main_no'] = $orderNo;
  341. if ($i == 6) {
  342. $data['total'] = round($order['total'] / 6, 2);
  343. $data['bd_score'] = round($bdScoreTotal / 6, 2);
  344. $data['profit_total'] = round($profitTotal / 6, 2);
  345. $data['pay_total'] = round($orderTotal / 6, 2);
  346. $data['pay_money'] = round($order['pay_money'] / 6, 2);
  347. }
  348. $subOrders[] = $data;
  349. }
  350. //
  351. if ($subOrders && !$this->model->insert($subOrders)) {
  352. DB::rollBack();
  353. $this->error = '处理商品订单失败';
  354. RedisService::clear($cacheLockKey);
  355. return false;
  356. }
  357. // 扣除报单积分
  358. if($bdScoreTotal>0){
  359. if(!MemberModel::where(['id'=>$userId])->update(['bd_score'=>DB::raw("bd_score - {$bdScoreTotal}"),'update_time'=>time()])){
  360. DB::rollBack();
  361. $this->error = '报单积分扣除失败';
  362. RedisService::clear($cacheLockKey);
  363. return false;
  364. }
  365. $data = [
  366. 'user_id'=>$userId,
  367. 'source_order_no'=>$orderNo,
  368. 'user_type'=> 1,
  369. 'account_type'=> 3, // 报单积分
  370. 'type'=> 1,
  371. 'money'=> -$bdScoreTotal,
  372. 'after_money'=>moneyFormat($bdScore - $bdScoreTotal,2),
  373. 'date'=>date('Y-m-d'),
  374. 'create_time'=>time(),
  375. 'remark'=> '创业礼包支付',
  376. 'status'=>1,
  377. 'mark'=>1
  378. ];
  379. if(!AccountLogModel::insertGetId($data)){
  380. Db::rollBack();
  381. $this->error = '付款处理失败';
  382. return false;
  383. }
  384. }
  385. }
  386. // 佣金数据
  387. $order['id'] = $orderId;
  388. if(!$commission = SettleService::make()->commissionCount($order,$orderGoods,$userInfo, $type)){
  389. Db::rollBack();
  390. $this->error = '订单处理失败';
  391. return false;
  392. }
  393. // 获取支付参数
  394. /* TODO 支付处理 */
  395. $payOrder = [
  396. 'type' => 1,
  397. 'order_no' => $orderNo,
  398. 'is_revenue' => isset($order['is_revenue'])?$order['is_revenue']:2,
  399. 'pay_money' => $payTotal,
  400. 'body' => '订单付款',
  401. 'openid' => $openid
  402. ];
  403. // 调起支付
  404. $payment = PaymentService::make()->minPay($userInfo, $payOrder, 'store');
  405. if (empty($payment)) {
  406. //DB::rollBack();
  407. RedisService::clear($cacheLockKey);
  408. $this->error = PaymentService::make()->getError();
  409. return false;
  410. }
  411. // 商品库存扣除
  412. if ($orderGoods) {
  413. foreach ($orderGoods as $item) {
  414. $id = isset($item['goods_id']) ? $item['goods_id'] : 0;
  415. $num = isset($item['num']) ? $item['num'] : 0;
  416. $skuId = isset($item['sku_id']) ? $item['sku_id'] : 0;
  417. if ($id && !GoodsModel::where(['id' => $id])->update(['stock' => DB::raw("stock - {$num}"), 'update_time' => time()])) {
  418. DB::rollBack();
  419. RedisService::clear($cacheLockKey);
  420. $this->error = '商品库存处理失败';
  421. return false;
  422. }
  423. if ($skuId && !GoodsSkuModel::where(['id' => $skuId])->update(['stock' => DB::raw("stock - {$num}"), 'update_time' => time()])) {
  424. DB::rollBack();
  425. RedisService::clear($cacheLockKey);
  426. $this->error = '商品库存处理失败';
  427. return false;
  428. }
  429. }
  430. // 清空购物车结算商品
  431. if($submitType == 'cart'){
  432. $skuIds = $goods ? array_column($goods, 'sku_id') : [];
  433. CartModel::whereIn('goods_id',$ids)->whereIn('sku_id',$skuIds)->where(['user_id'=>$userId,'mark'=>1])->update(['mark'=>0,'num'=>0,'update_time'=>time()]);
  434. RedisService::clear("caches:goods:cartCount:{$userId}");
  435. }
  436. }
  437. // 用户操作记录
  438. DB::commit();
  439. $this->error = '订单创建成功,请前往支付~';
  440. RedisService::clear($cacheLockKey);
  441. return [
  442. 'order_id' => $orderId,
  443. 'payment' => $payment,
  444. 'total' => $payOrder['pay_money'],
  445. 'pay_type' => 10,
  446. ];
  447. }
  448. /**
  449. * 创业大礼包验证
  450. * @param $userId
  451. * @param $goodsId
  452. * @return array|mixed
  453. */
  454. public function checkBusinessOrder($userId, $goodsId)
  455. {
  456. $cacheKey = "caches:orders:check_order:{$userId}_{$goodsId}";
  457. $data = RedisService::get($cacheKey);
  458. if ($data) {
  459. return $data;
  460. }
  461. $data = $this->model->where(['user_id' => $userId, 'meal_id' => $goodsId, 'mark' => 1])
  462. ->where('status', '<=', 5)
  463. ->where('refund_status', '<=', 0)
  464. ->count('id');
  465. $data = $data ? $data->toArray() : [];
  466. if ($data) {
  467. RedisService::set($cacheKey, $data, rand(5, 10));
  468. }
  469. return $data;
  470. }
  471. /**
  472. * 订单支付
  473. * @param $userId
  474. * @param $id
  475. * @return array|false
  476. */
  477. public function pay($userId, $id)
  478. {
  479. if ($id <= 0) {
  480. $this->error = '请选择支付订单';
  481. return false;
  482. }
  483. // 缓存锁
  484. $cacheLockKey = "caches:orders:pay_lock:{$userId}_{$id}";
  485. if (RedisService::get($cacheLockKey)) {
  486. $this->error = '订单处理中~';
  487. return false;
  488. }
  489. // 商品数据
  490. RedisService::set($cacheLockKey, ['order_id' => $id, 'user_id' => $userId], rand(3, 5));
  491. // 用户信息
  492. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  493. ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
  494. ->first();
  495. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  496. $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
  497. if (empty($userInfo) || $status != 1) {
  498. $this->error = 1045;
  499. RedisService::clear($cacheLockKey);
  500. return false;
  501. }
  502. if (empty($openid)) {
  503. $this->error = '用户微信未授权,请重新授权登录';
  504. RedisService::clear($cacheLockKey);
  505. return false;
  506. }
  507. // 订单信息
  508. $info = $this->model->where(['id' => $id, 'mark' => 1])
  509. ->select(['id', 'order_no', 'pay_total', 'delivery_fee', 'status'])
  510. ->first();
  511. $orderTotal = isset($info['pay_total']) ? $info['pay_total'] : 0;
  512. $deliveryFee = isset($info['delivery_fee']) ? $info['delivery_fee'] : 0;
  513. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  514. $status = isset($info['status']) ? $info['status'] : 0;
  515. if (empty($info) || empty($orderNo)) {
  516. $this->error = '订单信息不存在';
  517. RedisService::clear($cacheLockKey);
  518. return false;
  519. }
  520. if ($status != 1) {
  521. $this->error = '订单已支付';
  522. RedisService::clear($cacheLockKey);
  523. return false;
  524. }
  525. // 获取支付参数
  526. /* TODO 支付处理 */
  527. $payOrder = [
  528. 'type' => 1,
  529. 'order_no' => $orderNo,
  530. 'pay_money' => moneyFormat($orderTotal + $deliveryFee),
  531. 'body' => '购物消费',
  532. 'openid' => $openid
  533. ];
  534. // 调起支付
  535. $payment = PaymentService::make()->minPay($userInfo, $payOrder, 'store');
  536. if (empty($payment)) {
  537. DB::rollBack();
  538. RedisService::clear($cacheLockKey);
  539. $this->error = PaymentService::make()->getError();
  540. return false;
  541. }
  542. // 用户操作记录
  543. DB::commit();
  544. $this->error = '支付请求成功,请前往支付~';
  545. RedisService::clear($cacheLockKey);
  546. return [
  547. 'order_id' => $id,
  548. 'payment' => $payment,
  549. 'total' => $payOrder['pay_money'],
  550. 'pay_type' => 10,
  551. ];
  552. }
  553. /**
  554. * 订单取消
  555. * @param $userId
  556. * @param $orderId
  557. * @return array|false
  558. */
  559. public function cancel($userId, $orderId)
  560. {
  561. if ($orderId <= 0) {
  562. $this->error = '请选择订单';
  563. return false;
  564. }
  565. // 缓存锁
  566. $cacheLockKey = "caches:orders:cancel_lock:{$userId}_{$orderId}";
  567. if (RedisService::get($cacheLockKey)) {
  568. $this->error = '订单处理中~';
  569. return false;
  570. }
  571. RedisService::set($cacheLockKey, ['order_id' => $orderId, 'user_id' => $userId], rand(3, 5));
  572. // 用户信息
  573. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  574. ->select(['id', 'openid', 'mobile', 'nickname', 'realname','bd_score', 'balance', 'status'])
  575. ->first();
  576. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  577. $bdScore = isset($userInfo['bd_score']) ? $userInfo['bd_score'] : 0;
  578. if (empty($userInfo) || $status != 1) {
  579. $this->error = 1045;
  580. RedisService::clear($cacheLockKey);
  581. return false;
  582. }
  583. // 订单信息
  584. $info = $this->model->where(['id' => $orderId, 'mark' => 1])
  585. ->select(['id', 'order_no', 'pay_total', 'status'])
  586. ->first();
  587. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  588. $status = isset($info['status']) ? $info['status'] : 0;
  589. $bdScoreTotal = isset($info['bd_score']) ? $info['bd_score'] : 0;
  590. if (empty($info) || empty($orderNo)) {
  591. $this->error = '订单信息不存在';
  592. RedisService::clear($cacheLockKey);
  593. return false;
  594. }
  595. if ($status != 1) {
  596. $this->error = '订单已支付';
  597. RedisService::clear($cacheLockKey);
  598. return false;
  599. }
  600. $orderGoods = OrderGoodsModel::where(['order_no' => $orderNo, 'mark' => 1])
  601. ->select(['goods_id', 'num', 'sku_id'])
  602. ->get();
  603. DB::beginTransaction();
  604. if ($orderGoods) {
  605. foreach ($orderGoods as $goods) {
  606. $goodsId = isset($goods['goods_id']) ? $goods['goods_id'] : 0;
  607. $num = isset($goods['num']) ? $goods['num'] : 0;
  608. $skuId = isset($goods['sku_id']) ? $goods['sku_id'] : 0;
  609. if ($goodsId) {
  610. GoodsModel::where(['id' => $goodsId])->update(['stock' => DB::raw("stock + {$num}"), 'update_time' => time()]);
  611. }
  612. if ($skuId) {
  613. GoodsSkuModel::where(['id' => $skuId])->update(['stock' => DB::raw("stock + {$num}"), 'update_time' => time()]);
  614. }
  615. }
  616. }
  617. // 退还报单积分
  618. if($bdScoreTotal>0){
  619. if(!MemberModel::where(['id'=>$userId])->update(['bd_score'=>DB::raw("bd_score + {$bdScoreTotal}"),'update_time'=>time()])){
  620. DB::rollBack();
  621. $this->error = '报单积分退还失败';
  622. RedisService::clear($cacheLockKey);
  623. return false;
  624. }
  625. $data = [
  626. 'user_id'=>$userId,
  627. 'source_order_no'=>$orderNo,
  628. 'user_type'=> 1,
  629. 'account_type'=> 3, // 报单积分
  630. 'type'=> 3,
  631. 'money'=> $bdScoreTotal,
  632. 'after_money'=>moneyFormat($bdScore + $bdScoreTotal,2),
  633. 'date'=>date('Y-m-d'),
  634. 'create_time'=>time(),
  635. 'remark'=> '创业礼包订单取消',
  636. 'status'=>1,
  637. 'mark'=>1
  638. ];
  639. if(!AccountLogModel::insertGetId($data)){
  640. Db::rollBack();
  641. $this->error = '订单取消处理失败';
  642. return false;
  643. }
  644. }
  645. $this->error = '取消订单成功';
  646. $this->model->where(['user_id' => $userId, 'mark' => 0])->where('update_time', '<=', time() - 300)->delete();
  647. OrderGoodsModel::where(['order_no' => $orderNo, 'mark' => 0])->where('update_time', '<=', time() - 300)->delete();
  648. $this->model->where(['id' => $orderId])->update(['mark' => 0, 'update_time' => time()]);
  649. OrderGoodsModel::where(['order_no' => $orderNo])->update(['mark' => 0, 'update_time' => time()]);
  650. DB::commit();
  651. return ['id' => $orderId];
  652. }
  653. /**
  654. * 订单删除隐藏
  655. * @param $userId
  656. * @param $orderId
  657. * @return array|false
  658. */
  659. public function hide($userId, $orderId)
  660. {
  661. if ($orderId <= 0) {
  662. $this->error = '请选择订单';
  663. return false;
  664. }
  665. // 缓存锁
  666. $cacheLockKey = "caches:orders:hide_lock:{$userId}_{$orderId}";
  667. if (RedisService::get($cacheLockKey)) {
  668. $this->error = '订单删除中~';
  669. return false;
  670. }
  671. RedisService::set($cacheLockKey, ['order_id' => $orderId, 'user_id' => $userId], rand(3, 5));
  672. // 用户信息
  673. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  674. ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
  675. ->first();
  676. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  677. if (empty($userInfo) || $status != 1) {
  678. $this->error = 1045;
  679. RedisService::clear($cacheLockKey);
  680. return false;
  681. }
  682. // 订单信息
  683. $info = $this->model->where(['id' => $orderId, 'mark' => 1])
  684. ->select(['id', 'order_no', 'pay_total', 'status'])
  685. ->first();
  686. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  687. $status = isset($info['status']) ? $info['status'] : 0;
  688. if (empty($info) || empty($orderNo)) {
  689. $this->error = '订单信息不存在';
  690. RedisService::clear($cacheLockKey);
  691. return false;
  692. }
  693. if ($status != 4) {
  694. $this->error = '订单未完成';
  695. RedisService::clear($cacheLockKey);
  696. return false;
  697. }
  698. $this->error = '删除订单成功';
  699. $this->model->where(['id' => $orderId])->update(['is_hide' => 1, 'update_time' => time()]);
  700. return ['id' => $orderId];
  701. }
  702. /**
  703. * 订单完成
  704. * @param $userId 订单用户ID
  705. * @param $id 订单ID
  706. * @return array|false
  707. */
  708. public function complete($userId, $id, $check = true)
  709. {
  710. if ($id <= 0) {
  711. $this->error = '请选择订单';
  712. return false;
  713. }
  714. // 缓存锁
  715. $cacheLockKey = "caches:orders:complete_lock:{$userId}_{$id}";
  716. if (RedisService::get($cacheLockKey)) {
  717. $this->error = '订单处理中~';
  718. return false;
  719. }
  720. // 商品数据
  721. RedisService::set($cacheLockKey, ['order_id' => $id, 'user_id' => $userId], rand(3, 5));
  722. // 用户信息
  723. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  724. ->select(['id', 'openid', 'mobile', 'parent_id', 'nickname', 'realname', 'balance', 'status'])
  725. ->first();
  726. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  727. $parentId = isset($userInfo['parent_id']) ? $userInfo['parent_id'] : 0;
  728. if ($check && (empty($userInfo) || $status != 1)) {
  729. $this->error = 1045;
  730. RedisService::clear($cacheLockKey);
  731. return false;
  732. }
  733. // 订单信息
  734. $info = $this->model->with(['orderGoods','commission'])->where(['id' => $id, 'mark' => 1])
  735. ->select(['id', 'order_no','type', 'user_id', 'store_id', 'total', 'pay_total', 'delivery_no', 'delivery_company', 'delivery_code', 'status'])
  736. ->first();
  737. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  738. $deliveryNo = isset($info['delivery_no']) ? $info['delivery_no'] : '';
  739. $deliverCompany = isset($info['delivery_company']) ? $info['delivery_company'] : '';
  740. $orderUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  741. $storeId = isset($info['store_id']) ? $info['store_id'] : 0;
  742. $orderType = isset($info['type']) ? $info['type'] : 0;
  743. $orderTotal = isset($info['total']) ? $info['total'] : 0;
  744. $status = isset($info['status']) ? $info['status'] : 0;
  745. $commission = isset($info['commission']) ? $info['commission'] : [];
  746. $bonus = isset($commission['bonus'])?$commission['bonus'] : 0;
  747. $orderGoods = isset($info['order_goods']) ? $info['order_goods'] : [];
  748. if (empty($info) || empty($orderNo)) {
  749. $this->error = '订单信息不存在';
  750. RedisService::clear($cacheLockKey);
  751. return false;
  752. }
  753. if ($status != 3) {
  754. $this->error = '订单未发货';
  755. RedisService::clear($cacheLockKey);
  756. return false;
  757. }
  758. if (empty($deliveryNo) || empty($deliverCompany)) {
  759. $this->error = '订单发货信息错误,请联系客服';
  760. $this->model->where(['id' => $id])->update(['is_complete' => 1, 'complete_remark' => $this->error]);
  761. RedisService::clear("caches:orders:completeList");
  762. RedisService::clear($cacheLockKey);
  763. return false;
  764. }
  765. DB::beginTransaction();
  766. $completeStatus = env('ORDER_COMPLETE_STATUS', 5);
  767. $updateData = ['status' => $completeStatus, 'complete_at' => date('Y-m-d H:i:s'), 'update_time' => time()];
  768. if (!$check) {
  769. $updateData['is_complete'] = 1;
  770. $updateData['complete_remark'] = '自动收货';
  771. }
  772. $this->model->where(['id' => $id])->update($updateData);
  773. // 商家订单数据统计
  774. $updateData = ['order_count' => DB::raw('order_count+1'), 'order_total' => DB::raw("order_total + {$orderTotal}")];
  775. StoreModel::where(['id' => $storeId])->update($updateData);
  776. // 商品销量数据
  777. if ($orderGoods) {
  778. $counts = [];
  779. foreach ($orderGoods as $item) {
  780. $counts[$item['goods_id']] = isset($counts[$item['goods_id']]) ? $counts[$item['goods_id']] : 0;
  781. $counts[$item['goods_id']] += $item['num'];
  782. }
  783. if ($counts) {
  784. foreach ($counts as $id => $v) {
  785. GoodsModel::where(['id' => $id])->update(['sales' => DB::raw("sales + {$v}"), 'update_time' => time()]);
  786. }
  787. }
  788. }
  789. // 商城订单结算
  790. if($orderType == 1){
  791. SettleService::make()->commissionSettle($id);
  792. }
  793. DB::commit();
  794. $this->error = '确认收货成功';
  795. RedisService::clear("caches:orders:completeList");
  796. RedisService::clear("caches:orders:revenueList");
  797. return ['id' => $id, 'msg' => $this->error];
  798. }
  799. /**
  800. * 售后或退款
  801. * @param $userId
  802. * @param $params
  803. * @return array|false
  804. */
  805. public function after($userId, $params)
  806. {
  807. $id = isset($params['id']) ? $params['id'] : 0;
  808. $afterType = isset($params['after_type']) ? $params['after_type'] : 1;
  809. if ($id <= 0) {
  810. $this->error = '请选择订单';
  811. return false;
  812. }
  813. // 缓存锁
  814. $cacheLockKey = "caches:orders:after_lock:{$userId}_{$id}";
  815. if (RedisService::get($cacheLockKey)) {
  816. $this->error = '订单处理中~';
  817. return false;
  818. }
  819. // 商品数据
  820. RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
  821. // 用户信息
  822. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
  823. ->select(['id', 'openid', 'mobile', 'nickname', 'realname', 'balance', 'status'])
  824. ->first();
  825. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  826. if (empty($userInfo) || $status != 1) {
  827. $this->error = 1045;
  828. RedisService::clear($cacheLockKey);
  829. return false;
  830. }
  831. // 订单信息
  832. $info = $this->model->where(['id' => $id, 'mark' => 1])
  833. ->select(['id', 'order_no', 'after_type', 'refund_status', 'pay_total', 'status'])
  834. ->first();
  835. $orderNo = isset($info['order_no']) ? $info['order_no'] : '';
  836. $status = isset($info['status']) ? $info['status'] : 0;
  837. $refundStatus = isset($info['refund_status']) ? $info['refund_status'] : 0;
  838. if (empty($info) || empty($orderNo)) {
  839. $this->error = '订单信息不存在';
  840. RedisService::clear($cacheLockKey);
  841. return false;
  842. }
  843. if ($status == 1) {
  844. $this->error = '订单未支付';
  845. RedisService::clear($cacheLockKey);
  846. return false;
  847. }
  848. if ($status == 4 && $afterType == 2) {
  849. $this->error = '订单已完成';
  850. RedisService::clear($cacheLockKey);
  851. return false;
  852. }
  853. if ($refundStatus > 0 && $refundStatus != 4) {
  854. $this->error = '订单售后处理中';
  855. RedisService::clear($cacheLockKey);
  856. return false;
  857. }
  858. $afterRealname = isset($params['after_realname']) ? $params['after_realname'] : '';
  859. $afterPhone = isset($params['after_phone']) ? $params['after_phone'] : '';
  860. $afterRemark = isset($params['after_remark']) ? $params['after_remark'] : '';
  861. if ($afterType == 1) {
  862. if (empty($afterRealname) || empty($afterPhone) || empty($afterRemark)) {
  863. $this->error = '请填写售后信息';
  864. RedisService::clear($cacheLockKey);
  865. return false;
  866. }
  867. }
  868. $data = [
  869. 'after_type' => $afterType,
  870. 'after_realname' => $afterRealname,
  871. 'after_phone' => $afterPhone,
  872. 'after_remark' => $afterRemark,
  873. 'refund_remark' => isset($params['refund_remark']) ? $params['refund_remark'] : '',
  874. 'refund_status' => 3,
  875. 'update_time' => time()
  876. ];
  877. $this->model->where(['id' => $id])->update($data);
  878. $this->error = '订单申请售后成功';
  879. return ['id' => $id];
  880. }
  881. /**
  882. * 物流查询
  883. * @param $id
  884. * @return array|false|mixed
  885. */
  886. public function getDelivery($id)
  887. {
  888. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  889. $deliveryNo = isset($info['delivery_no']) ? $info['delivery_no'] : '';
  890. $deliveryCode = isset($info['delivery_code']) ? $info['delivery_code'] : '';
  891. $mobile = isset($info['receiver_mobile']) ? $info['receiver_mobile'] : '';
  892. $receiverArea = isset($info['receiver_area']) && $info['receiver_area'] ? $info['receiver_area'] : '';
  893. if (empty($info)) {
  894. $this->error = '请选择订单';
  895. return false;
  896. }
  897. $cacheKey = "caches:kd100:order_{$id}";
  898. $data = RedisService::get($cacheKey);
  899. if ($data) {
  900. return $data;
  901. }
  902. $result = Kd100Service::make()->query($deliveryNo, $mobile, $deliveryCode, $receiverArea);
  903. RedisService::set($cacheKey . '_result', $result, 300);
  904. $status = isset($result['status']) ? $result['status'] : 0;
  905. $data = isset($result['data']) ? $result['data'] : [];
  906. $courierInfo = isset($result['courierInfo']) ? $result['courierInfo'] : [];
  907. $arrivalTime = isset($result['arrivalTime']) ? $result['arrivalTime'] : '';
  908. $predictedRoute = isset($result['predictedRoute']) ? $result['predictedRoute'] : [];
  909. $predictedData = $predictedRoute ? end($predictedRoute) : [];
  910. $arrivalData = [];
  911. if ($arrivalTime) {
  912. $arrivalData['arrivalTime'] = dayFormat(strtotime($arrivalTime . ':00:00'));
  913. $arrivalData['predictedData'] = $predictedData;
  914. }
  915. if ($courierInfo && $courierInfo['deliveryManPhone']) {
  916. $courierInfo['deliveryManPhone'] = explode(',', $courierInfo['deliveryManPhone']);
  917. $courierInfo['deliveryPhone'] = $courierInfo['deliveryManPhone'][1] ? $courierInfo['deliveryManPhone'][1] : $courierInfo['deliveryManPhone'][0];
  918. }
  919. if ($data && $status == 200) {
  920. RedisService::set($cacheKey, ['info' => $courierInfo, 'arrivalData' => $arrivalData, 'list' => $data], 1200);
  921. }
  922. return $data ? ['info' => $courierInfo, 'arrivalData' => $arrivalData, 'list' => $data] : [];
  923. }
  924. /**
  925. * 已发货待完成订单
  926. * @return array|mixed
  927. */
  928. public function getCompleteOrders()
  929. {
  930. $cacheKey = "caches:orders:completeList";
  931. $datas = RedisService::get($cacheKey);
  932. if ($datas) {
  933. return $datas;
  934. }
  935. $completeDay = ConfigService::make()->getConfigByCode('order_complete_day', 7);
  936. $limitNum = ConfigService::make()->getConfigByCode('order_complete_batch_num', 300);
  937. $limitNum = $limitNum > 10 && $limitNum < 2000 ? $limitNum : 300;
  938. $completeDay = $completeDay >= 1 && $completeDay < 30 ? $completeDay : 7;
  939. $datas = $this->model->where(['status' => 3, 'is_complete' => 2, 'mark' => 1])
  940. ->whereNotNull('delivery_no')
  941. ->select(['id', 'user_id', 'order_no', 'status'])
  942. ->where('pay_at', '<=', date('Y-m-d H:i:s', time() - $completeDay * 86400))
  943. ->limit($limitNum)
  944. ->get();
  945. $datas = $datas ? $datas->toArray() : [];
  946. if ($datas) {
  947. RedisService::set($cacheKey, $datas, rand(300, 600));
  948. }
  949. return $datas;
  950. }
  951. /**
  952. * 已完成,待分账订单
  953. * @return array|mixed
  954. */
  955. public function getRevenueOrderList()
  956. {
  957. $cacheKey = "caches:orders:revenueList";
  958. $datas = RedisService::get($cacheKey);
  959. if ($datas) {
  960. return $datas;
  961. }
  962. $limitNum = ConfigService::make()->getConfigByCode('order_revenue_batch_num', 300);
  963. $limitNum = $limitNum > 10 && $limitNum < 2000 ? $limitNum : 300;
  964. $datas = $this->model->where(['status' => 4, 'is_revenue' => 1, 'revenue_status' => 2, 'mark' => 1])
  965. ->select(['id', 'user_id', 'pay_total', 'order_no', 'revenue_no', 'transaction_id', 'status'])
  966. ->limit($limitNum)
  967. ->get();
  968. $datas = $datas ? $datas->toArray() : [];
  969. if ($datas) {
  970. RedisService::set($cacheKey, $datas, rand(300, 600));
  971. }
  972. return $datas;
  973. }
  974. /**
  975. * 订单分账处理
  976. * @param $id
  977. * @return false
  978. * @throws \Yansongda\Pay\Exception\ContainerException
  979. * @throws \Yansongda\Pay\Exception\InvalidParamsException
  980. */
  981. public function revenue($id)
  982. {
  983. if ($id <= 0) {
  984. $this->error = '订单参数错误';
  985. return false;
  986. }
  987. // 缓存锁
  988. $cacheLockKey = "caches:orders:revenue_lock:{$id}";
  989. if (RedisService::get($cacheLockKey)) {
  990. $this->error = '订单分账处理中~';
  991. return false;
  992. }
  993. // 是否设置分账账户
  994. $revenueOpenid = ConfigService::make()->getConfigByCode('order_revenue_openid', '');
  995. if (empty($revenueOpenid)) {
  996. $this->error = '订单分账账户未配置';
  997. return false;
  998. }
  999. // 订单信息
  1000. RedisService::set($cacheLockKey, ['id' => $id, 'date' => date('Y-m-d H:i:s')], 20);
  1001. $info = $this->model->where(['id' => $id, 'mark' => 1])
  1002. ->select(['id', 'order_no', 'revenue_no', 'user_id', 'transaction_id', 'pay_total', 'is_revenue', 'revenue_status', 'revenue_amount', 'status'])
  1003. ->first();
  1004. $revenueOrderNo = isset($info['revenue_no']) ? $info['revenue_no'] : '';
  1005. $revenueAmount = isset($info['revenue_amount']) ? $info['revenue_amount'] : 0;
  1006. $status = isset($info['status']) ? $info['status'] : 0;
  1007. $isRevenue = isset($info['is_revenue']) ? $info['is_revenue'] : 0;
  1008. $revenueStatus = isset($info['revenue_status']) ? $info['revenue_status'] : 0;
  1009. if ($status != 4) {
  1010. RedisService::clear($cacheLockKey);
  1011. $this->error = '订单状态错误,未完成收货的订单无法分账';
  1012. return false;
  1013. }
  1014. if ($isRevenue != 1) {
  1015. RedisService::clear($cacheLockKey);
  1016. $this->error = '该订单不是分账订单';
  1017. return false;
  1018. }
  1019. if ($revenueStatus != 2) {
  1020. RedisService::clear($cacheLockKey);
  1021. $this->error = '该订单分账已处理完成';
  1022. return false;
  1023. }
  1024. if ($revenueAmount <= 0) {
  1025. RedisService::clear($cacheLockKey);
  1026. $this->error = '订单分账金额不足';
  1027. return false;
  1028. }
  1029. $order = [
  1030. 'transaction_id' => $info['transaction_id'],
  1031. 'out_order_no' => $revenueOrderNo,
  1032. 'body' => '订单分账',
  1033. 'amount' => $revenueAmount,
  1034. 'unsplit' => true, // 只分一次
  1035. ];
  1036. $result = PaymentService::make()->profitsharing($revenueOpenid, $order);
  1037. $state = isset($result->state) ? $result->state : 0;
  1038. $message = isset($result->message) ? $result->message : '分账失败';
  1039. $orderId = isset($result->order_id) ? $result->order_id : '';
  1040. if ($state == 'FINISHED') {
  1041. $this->model->where(['id' => $id])->update(['revenue_status' => 1, 'revenue_order_id' => $orderId, 'update_time' => time()]);
  1042. RedisService::clear($cacheLockKey);
  1043. RedisService::clear("caches:orders:revenueList");
  1044. $this->error = '分账处理成功';
  1045. return true;
  1046. } else {
  1047. RedisService::clear($cacheLockKey);
  1048. $this->error = $message;
  1049. return false;
  1050. }
  1051. }
  1052. }