AdvertOrderService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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\Common;
  12. use App\Models\AdvertModel;
  13. use App\Models\AdvertOrderModel;
  14. use App\Models\MemberModel;
  15. use App\Services\Api\MemberPaymentService;
  16. use App\Services\BaseService;
  17. use App\Services\ConfigService;
  18. use App\Services\RedisService;
  19. /**
  20. * 用户广告订单-服务类
  21. * Class AdvertOrderService
  22. * @package App\Services\Common
  23. */
  24. class AdvertOrderService extends BaseService
  25. {
  26. // 静态对象
  27. protected static $instance = null;
  28. /**
  29. * 构造函数
  30. * @since 2020/11/10
  31. * LoginService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new AdvertOrderModel();
  36. $this->advertModel = new AdvertModel();
  37. $this->memberModel = new MemberModel();
  38. }
  39. /**
  40. * 静态入口
  41. * @return static|null
  42. */
  43. public static function make()
  44. {
  45. if (!self::$instance) {
  46. self::$instance = (new static());
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * 订单列表
  52. * @param $params
  53. * @param int $pageSize
  54. * @return array
  55. */
  56. public function getDataList($params, $pageSize = 15)
  57. {
  58. $list = $this->model->from('advert_order as a')
  59. ->leftJoin('member as b', 'b.id', '=', 'a.business_id')
  60. ->leftJoin('member as c', 'c.id', '=', 'a.user_id')
  61. ->where(function ($query) use ($params) {
  62. $query->where(['a.mark' => 1])->where('a.status', '>', 0);
  63. $orderNo = isset($params['order_no']) && $params['order_no'] ? trim($params['order_no']) : '';
  64. if ($orderNo) {
  65. $query->where('a.order_no', 'like', "%{$orderNo}%");
  66. }
  67. $type = isset($params['type']) ? intval($params['type']) : 0;
  68. if ($type > 0) {
  69. $query->where(['a.type' => $type]);
  70. }
  71. // 日期
  72. $date = isset($params['date']) ? $params['date'] : [];
  73. $start = isset($date[0])? $date[0] : '';
  74. $end = isset($date[1])? $date[1] : '';
  75. $end = $start>=$end? '' : $end;
  76. if ($start) {
  77. $query->where('a.create_time','>=', strtotime($start));
  78. }
  79. if($end){
  80. $query->where('a.create_time','<', strtotime($end));
  81. }
  82. $exceptionStatus = isset($params['exception_status']) ? intval($params['exception_status']) : 0;
  83. if ($exceptionStatus > 0) {
  84. $query->where(['a.exception_status' => $exceptionStatus]);
  85. }
  86. $status = isset($params['status']) ? intval($params['status']) : 0;
  87. if ($status > 0) {
  88. $query->where(['a.status' => $status]);
  89. }
  90. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  91. if ($userId > 0) {
  92. $query->where('a.user_id', $userId);
  93. }
  94. $businessId = isset($params['business_id']) ? $params['business_id'] : 0;
  95. if ($businessId > 0) {
  96. $query->where('a.business_id', $businessId);
  97. }
  98. })
  99. ->select(['a.*', 'b.username', 'c.username as c_username'])
  100. ->orderBy('a.create_time', 'desc')
  101. ->orderBy('a.id', 'desc')
  102. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  103. $list = $list ? $list->toArray() : [];
  104. if ($list) {
  105. $payTypes = [1 => '银行卡', 2 => '微信', 3 => '支付宝', 4 => '其他'];
  106. $overTime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  107. $overTime = $overTime ? $overTime : 0;
  108. foreach ($list['data'] as &$item) {
  109. $item['idcardData'] = $item['idcard_data'] ? json_decode($item['idcard_data'], true) : [];
  110. $item['paymentData'] = $item['payment_data'] ? json_decode($item['payment_data'], true) : [];
  111. $item['create_time_text'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  112. $item['update_time_text'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
  113. $item['time_text'] = $item['create_time'] ? datetime($item['create_time'], 'H:i') : '';
  114. $item['utime_text'] = $item['update_time'] ? datetime($item['update_time'], 'H:i') : '';
  115. $item['pay_time_text'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i') : '';
  116. $item['username_text'] = $item['username'] ? format_account($item['username']) : '';
  117. $item['c_username_text'] = $item['c_username'] ? format_account($item['c_username']) : '';
  118. $item['exception_img'] = $item['exception_img'] ? get_image_url($item['exception_img']) : '';
  119. $item['pay_img'] = $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  120. $item['paymentData']['qrcode'] = isset($item['paymentData']['qrcode']) && $item['paymentData']['qrcode'] ? get_image_url($item['paymentData']['qrcode']) : '';
  121. $overTime = max(0, intval($item['create_time']) + $overTime * 60 - time());
  122. $item['overtime_text'] = in_array($item['status'], [1, 2]) && $overTime ? date('H:i', $overTime) : '';
  123. $payType = isset($item['pay_type']) ? $item['pay_type'] : 0;
  124. $item['pay_name'] = isset($payTypes[$payType]) ? $payTypes[$payType] : '其他';
  125. $item['chat_key'] = getChatKey($item['user_id'],$item['business_id']);
  126. }
  127. }
  128. return [
  129. 'pageSize' => $pageSize,
  130. 'total' => isset($list['total']) ? $list['total'] : 0,
  131. 'list' => isset($list['data']) ? $list['data'] : []
  132. ];
  133. }
  134. /**
  135. * 购买
  136. * @param $userId
  137. * @param $params
  138. * @return false|int|number
  139. */
  140. public function buy($userId, $params)
  141. {
  142. $id = isset($params['id']) ? intval($params['id']) : 0;
  143. $num = isset($params['num']) ? intval($params['num']) : 0;
  144. if ($id <= 0 || $num<=0) {
  145. $this->error = '1013';
  146. return false;
  147. }
  148. // 验证参数
  149. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  150. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  151. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_buy_price'] : 0;
  152. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  153. // 是否开启交易
  154. if ($tradeOpen != 1) {
  155. $this->error = '1013';
  156. return false;
  157. }
  158. $info = AdvertService::make()->getInfo($id);
  159. $tradeType = isset($info['type'])? $info['type'] : 0;
  160. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  161. $price = isset($info['price'])? $info['price'] : 0;
  162. $businessId = isset($info['user_id'])? $info['user_id'] : 0;
  163. if(empty($info) || $info['status'] != 1 || $businessId<=0){
  164. $this->error = '4001';
  165. return false;
  166. }
  167. if($tradeType != 2){
  168. $this->error = '4003';
  169. return false;
  170. }
  171. if ($tradePrice <= 0 && $priceType == 2) {
  172. $this->error = '3002';
  173. return false;
  174. }
  175. // 浮动价格计算
  176. if ($priceType == 2) {
  177. $price = floatval($tradePrice + $price);
  178. }
  179. // 总价
  180. $total = floatval($price * $num);
  181. if($total<=0){
  182. $this->error = '4002';
  183. return false;
  184. }
  185. // 单笔限额
  186. if($total< $info['limit_min'] || $total> $info['limit_max']){
  187. $this->error = lang('4005',['min'=> $info['limit_min'],'max'=>$info['limit_max']]);
  188. return false;
  189. }
  190. // 购买用户信息
  191. $userInfo = MemberService::make()->getInfo($userId);
  192. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  193. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  194. $username = isset($userInfo['username']) ? format_account($userInfo['username']) : '';
  195. if (empty($userInfo) || $status != 1) {
  196. $this->error = '2009';
  197. return false;
  198. }
  199. if($idcardCheck != 1){
  200. $this->error = '2014';
  201. return false;
  202. }
  203. // 未处理订单
  204. $noCatchOrder = $this->checkOrderNoCatch($userId, 1);
  205. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  206. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  207. return false;
  208. }
  209. // 交易商家
  210. $businessInfo = MemberService::make()->getInfo($businessId);
  211. if (empty($businessInfo)) {
  212. $this->error = '3004';
  213. return false;
  214. }
  215. // 购买者身份信息
  216. $idcardData = [
  217. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  218. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  219. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  220. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  221. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  222. ];
  223. // 收款方式
  224. $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
  225. if (empty($payment)) {
  226. $this->error = '3015';
  227. return false;
  228. }
  229. $this->model->startTrans();
  230. $orderNo = get_order_num('TA');
  231. $data = [
  232. 'user_id' => $userId,
  233. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  234. 'order_no' => $orderNo,
  235. 'type' => 1,
  236. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  237. 'price' => $price,
  238. 'num' => $num,
  239. 'total' => $total,
  240. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  241. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  242. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  243. 'create_time' => time(),
  244. 'update_time' => time(),
  245. 'status' => 1,
  246. 'mark' => 1,
  247. ];
  248. if (!$order = $this->model->edit($data)) {
  249. $this->model->rollBack();
  250. $this->error = '3023';
  251. return false;
  252. }
  253. if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
  254. $this->model->rollBack();
  255. $this->error = '3020';
  256. return false;
  257. }
  258. $data = [
  259. 'order_no'=> $orderNo,
  260. 'user_id'=> $businessInfo['id'],
  261. 'type'=> 2,
  262. 'pay_type'=> 1,
  263. 'trade_type'=> 2,
  264. 'change_type'=> 2,
  265. 'num'=> $num,
  266. 'total'=> $total,
  267. 'balance'=> floatval($businessInfo['usdt_num']-$num),
  268. 'create_time'=> time(),
  269. 'update_time'=> time(),
  270. 'status'=> 1,
  271. 'mark'=>1,
  272. 'remark'=> '交易员卖出',
  273. ];
  274. if(!$this->capitalModel->edit($data)){
  275. $this->model->rollBack();
  276. $this->error = '3014';
  277. return false;
  278. }
  279. // 订单通知
  280. $data = [
  281. 'from_uid' => $userId,
  282. 'to_uid' => $businessInfo['id'],
  283. 'type' => 3,
  284. 'order_no' => $orderNo,
  285. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  286. 'message' => "您有来自客户购买广告订单:{$orderNo}的消息,请尽快回复!",
  287. 'message_type' => 1,
  288. 'create_time' => time(),
  289. 'update_time' => time(),
  290. 'status' => 1,
  291. 'mark' => 1,
  292. ];
  293. if (!ChatMessageService::make()->pushMessage($data)) {
  294. $this->model->rollBack();
  295. $this->error = '3031';
  296. return false;
  297. }
  298. $this->model->commit();
  299. return $order;
  300. }
  301. /**
  302. * 购买
  303. * @param $userId
  304. * @param $params
  305. * @return false|int|number
  306. */
  307. public function sell($userId, $params)
  308. {
  309. $id = isset($params['id']) ? intval($params['id']) : 0;
  310. $num = isset($params['num']) ? intval($params['num']) : 0;
  311. $paymentId = isset($params['payment_id']) ? intval($params['payment_id']) : 0;
  312. if ($id <= 0 || $num<=0 || $paymentId<=0) {
  313. $this->error = '1013';
  314. return false;
  315. }
  316. // 验证参数
  317. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  318. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  319. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_sell_price'] : 0;
  320. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  321. // 是否开启交易
  322. if ($tradeOpen != 1) {
  323. $this->error = '1013';
  324. return false;
  325. }
  326. $info = AdvertService::make()->getInfo($id);
  327. $tradeType = isset($info['type'])? $info['type'] : 0;
  328. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  329. $price = isset($info['price'])? $info['price'] : 0;
  330. $businessId = isset($info['user_id'])? $info['user_id'] : 0;
  331. if(empty($info) || $info['status'] != 1){
  332. $this->error = '4001';
  333. return false;
  334. }
  335. if($tradeType != 1){
  336. $this->error = '4004';
  337. return false;
  338. }
  339. if ($tradePrice <= 0 && $priceType == 2) {
  340. $this->error = '3002';
  341. return false;
  342. }
  343. // 验证数量或金额
  344. if ($priceType == 2) {
  345. $price = floatval($tradePrice + $price);
  346. }
  347. // 单笔限额
  348. $total = floatval($price * $num);
  349. if($total<=0){
  350. $this->error = '4002';
  351. return false;
  352. }
  353. if($total< $info['limit_min'] || $total> $info['limit_max']){
  354. $this->error = lang('4005',['min'=> $info['limit_min'],'max'=>$info['limit_max']]);
  355. return false;
  356. }
  357. // 用户信息
  358. $userInfo = MemberService::make()->getInfo($userId);
  359. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  360. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  361. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  362. if ($status != 1) {
  363. $this->error = '2009';
  364. return false;
  365. }
  366. if($idcardCheck != 1){
  367. $this->error = '2014';
  368. return false;
  369. }
  370. // 未处理订单
  371. $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
  372. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  373. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  374. return false;
  375. }
  376. // 交易商家
  377. $businessInfo = MemberService::make()->getInfo($businessId);
  378. if (empty($businessInfo)) {
  379. $this->error = '3004';
  380. return false;
  381. }
  382. // 购买者身份信息
  383. $idcardData = [
  384. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  385. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  386. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  387. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  388. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  389. ];
  390. // 收款方式
  391. $paymentInfo = MemberPaymentService::make()->getInfo($paymentId);
  392. if (empty($paymentInfo)) {
  393. $this->error = '3010';
  394. return false;
  395. }
  396. $paymentData = [
  397. 'payment_id' => $paymentInfo['id'],
  398. 'type' => $paymentInfo['type'],
  399. 'logo' => $paymentInfo['logo'] ? get_image_url($paymentInfo['logo']) : '',
  400. 'real_name' => $paymentInfo['real_name'],
  401. 'bank_name' => $paymentInfo['bank_name'],
  402. 'bank_card' => $paymentInfo['bank_card'],
  403. 'branch_name' => $paymentInfo['branch_name'],
  404. 'qrcode' => $paymentInfo['qrcode'] ? get_image_url($paymentInfo['qrcode']) : '',
  405. 'account' => $paymentInfo['account'],
  406. ];
  407. $this->model->startTrans();
  408. $orderNo = get_order_num('TA');
  409. $data = [
  410. 'user_id' => $userId,
  411. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  412. 'order_no' => $orderNo,
  413. 'type' => 2,
  414. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  415. 'price' => $price,
  416. 'num' => $num,
  417. 'total' => $total,
  418. 'payment_id' => $paymentId,
  419. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  420. 'payment_data' => $paymentData ? json_encode($paymentData, 256) : '',
  421. 'create_time' => time(),
  422. 'update_time' => time(),
  423. 'status' => 1,
  424. 'mark' => 1,
  425. ];
  426. if (!$order = $this->model->edit($data)) {
  427. $this->model->rollBack();
  428. $this->error = '3023';
  429. return false;
  430. }
  431. // 扣除出售用户的USDT
  432. if(!$this->memberModel->where(['id'=> $userId])->decrement('usdt_num', $num)){
  433. $this->model->rollBack();
  434. $this->error = '3020';
  435. return false;
  436. }
  437. $data = [
  438. 'order_no'=> $orderNo,
  439. 'user_id'=> $userId,
  440. 'type'=> 2,
  441. 'pay_type'=> 1,
  442. 'trade_type'=> 2,
  443. 'change_type'=> 2,
  444. 'num'=> $num,
  445. 'total'=> $total,
  446. 'balance'=> floatval($userInfo['usdt_num']-$num),
  447. 'create_time'=> time(),
  448. 'update_time'=> time(),
  449. 'status'=> 1,
  450. 'mark'=>1,
  451. 'remark'=> '客户卖出',
  452. ];
  453. if(!$this->capitalModel->edit($data)){
  454. $this->model->rollBack();
  455. $this->error = '3014';
  456. return false;
  457. }
  458. // 订单通知
  459. $data = [
  460. 'from_uid' => $userId,
  461. 'to_uid' => $businessInfo['id'],
  462. 'type' => 3,
  463. 'order_no' => $orderNo,
  464. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  465. 'message' => "您有来自客户出售广告订单:{$orderNo}的消息,请尽快回复!",
  466. 'message_type' => 1,
  467. 'create_time' => time(),
  468. 'update_time' => time(),
  469. 'status' => 1,
  470. 'mark' => 1,
  471. ];
  472. if (!ChatMessageService::make()->pushMessage($data)) {
  473. $this->model->rollBack();
  474. $this->error = '3031';
  475. return false;
  476. }
  477. $this->model->commit();
  478. return $order;
  479. }
  480. /**
  481. * 获取未支付或处理的订单数
  482. * @param $userId
  483. * @param int $type
  484. * @return mixed
  485. */
  486. public function checkOrderNoCatch($userId, $type = 1)
  487. {
  488. return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])
  489. ->whereIn('status', [1, 2, 5, 7])
  490. ->count('id');
  491. }
  492. /**
  493. * 自动取消广告订单处理
  494. * @return false
  495. */
  496. public function catchInvalidOrder(){
  497. $cacheKey = "caches:adverts:cancels:";
  498. if(RedisService::get($cacheKey.'lock')){
  499. return false;
  500. }
  501. RedisService::set($cacheKey.'lock', 1, rand(3, 5));
  502. $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  503. $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
  504. $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
  505. $catchNum = $catchNum > 0 ? $catchNum : 200;
  506. // 处理超时订单
  507. if ($overtime > 0) {
  508. $this->model->where(['mark' => 1])
  509. ->where('status', '<=', 2)
  510. ->where('create_time', '<=', time() - $overtime * 60)
  511. ->update(['status' => 7, 'catch_at' => time()]);
  512. }
  513. if ($cancelTime <= 0) {
  514. $this->error = '1023';
  515. return false;
  516. }
  517. $fail = 0;
  518. $success = 0;
  519. $this->model->where(function ($query) use ($cancelTime) {
  520. // 已更新为超时的订单
  521. $query->where(['mark' => 1, 'status' => 7])
  522. ->where('catch_at', '<=', time() - $cancelTime * 60);
  523. })
  524. ->orWhere(function ($query) use ($cancelTime, $overtime) {
  525. $query->where('mark', '=', 1)
  526. ->where('status', '<=', 2)
  527. ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
  528. })
  529. ->select(['id', 'user_id', 'business_id','advert_id','order_no', 'type', 'num','total'])
  530. ->take($catchNum)
  531. ->get()
  532. ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
  533. // 客户卖出订单退还
  534. $date = date('Y-m-d H:i:s');
  535. $type = isset($item['type']) ? $item['type'] : 0;
  536. if ($type == 2) {
  537. if(!$this->orderReback($item['user_id'], $item)){
  538. $fail++;
  539. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  540. }else{
  541. $success++;
  542. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  543. }
  544. }
  545. else{
  546. if(!$this->orderReback($item['business_id'], $item)){
  547. $fail++;
  548. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  549. }else{
  550. $success++;
  551. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  552. }
  553. }
  554. });
  555. return ['success'=> $success,'fail'=> $fail];
  556. }
  557. /**
  558. * 订单取消退还处理
  559. * @param $userId
  560. * @param $orderInfo
  561. * @return bool
  562. */
  563. protected function orderReback($userId, $orderInfo){
  564. try {
  565. if($orderInfo['num']<=0){
  566. return false;
  567. }
  568. $this->model->startTrans();
  569. $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_remark'=>'系统取消'];
  570. if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
  571. $this->model->rollBack();
  572. $this->error = '3043';
  573. return false;
  574. }
  575. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  576. if (empty($info)) {
  577. $this->model->rollBack();
  578. $this->error = '3019';
  579. return false;
  580. }
  581. // 退还币给客户
  582. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  583. $this->model->rollBack();
  584. $this->error = '3019';
  585. return false;
  586. }
  587. // 明细处理
  588. $data = [
  589. 'order_no' => $orderInfo['order_no'],
  590. 'user_id' => $userId,
  591. 'type' => 3,
  592. 'pay_type' => 1,
  593. 'trade_type' => 2,
  594. 'change_type' => 1,
  595. 'num' => $orderInfo['num'],
  596. 'total' => $orderInfo['total'],
  597. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  598. 'create_time' => time(),
  599. 'update_time' => time(),
  600. 'remark' => '系统自动取消退还',
  601. 'status' => 1,
  602. 'mark' => 1,
  603. ];
  604. if (!$this->capitalModel->edit($data)) {
  605. $this->error = '3014';
  606. $this->model->rollBack();
  607. return false;
  608. }
  609. $this->model->commit();
  610. $this->error = '3044';
  611. return true;
  612. } catch (\Exception $exception){
  613. $this->error = $exception->getMessage();
  614. return false;
  615. }
  616. }
  617. }