AdvertOrderService.php 26 KB

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