AdvertOrderService.php 26 KB

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