AdvertOrderService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. 'data_type' => 2,
  289. 'create_time' => time(),
  290. 'update_time' => time(),
  291. 'status' => 1,
  292. 'mark' => 1,
  293. ];
  294. if (!ChatMessageService::make()->pushMessage($data)) {
  295. $this->model->rollBack();
  296. $this->error = '3031';
  297. return false;
  298. }
  299. $this->model->commit();
  300. return $order;
  301. }
  302. /**
  303. * 购买
  304. * @param $userId
  305. * @param $params
  306. * @return false|int|number
  307. */
  308. public function sell($userId, $params)
  309. {
  310. $id = isset($params['id']) ? intval($params['id']) : 0;
  311. $num = isset($params['num']) ? intval($params['num']) : 0;
  312. $paymentId = isset($params['payment_id']) ? intval($params['payment_id']) : 0;
  313. if ($id <= 0 || $num<=0 || $paymentId<=0) {
  314. $this->error = '1013';
  315. return false;
  316. }
  317. // 验证参数
  318. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  319. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  320. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_sell_price'] : 0;
  321. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  322. // 是否开启交易
  323. if ($tradeOpen != 1) {
  324. $this->error = '1013';
  325. return false;
  326. }
  327. $info = AdvertService::make()->getInfo($id);
  328. $tradeType = isset($info['type'])? $info['type'] : 0;
  329. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  330. $price = isset($info['price'])? $info['price'] : 0;
  331. $businessId = isset($info['user_id'])? $info['user_id'] : 0;
  332. if(empty($info) || $info['status'] != 1){
  333. $this->error = '4001';
  334. return false;
  335. }
  336. if($tradeType != 1){
  337. $this->error = '4004';
  338. return false;
  339. }
  340. if ($tradePrice <= 0 && $priceType == 2) {
  341. $this->error = '3002';
  342. return false;
  343. }
  344. // 验证数量或金额
  345. if ($priceType == 2) {
  346. $price = floatval($tradePrice + $price);
  347. }
  348. // 单笔限额
  349. $total = floatval($price * $num);
  350. if($total<=0){
  351. $this->error = '4002';
  352. return false;
  353. }
  354. if($total< $info['limit_min'] || $total> $info['limit_max']){
  355. $this->error = lang('4005',['min'=> $info['limit_min'],'max'=>$info['limit_max']]);
  356. return false;
  357. }
  358. // 用户信息
  359. $userInfo = MemberService::make()->getInfo($userId);
  360. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  361. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  362. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  363. if ($status != 1) {
  364. $this->error = '2009';
  365. return false;
  366. }
  367. if($idcardCheck != 1){
  368. $this->error = '2014';
  369. return false;
  370. }
  371. // 未处理订单
  372. $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
  373. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  374. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  375. return false;
  376. }
  377. // 交易商家
  378. $businessInfo = MemberService::make()->getInfo($businessId);
  379. if (empty($businessInfo)) {
  380. $this->error = '3004';
  381. return false;
  382. }
  383. // 购买者身份信息
  384. $idcardData = [
  385. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  386. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  387. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  388. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  389. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  390. ];
  391. // 收款方式
  392. $paymentInfo = MemberPaymentService::make()->getInfo($paymentId);
  393. if (empty($paymentInfo)) {
  394. $this->error = '3010';
  395. return false;
  396. }
  397. $paymentData = [
  398. 'payment_id' => $paymentInfo['id'],
  399. 'type' => $paymentInfo['type'],
  400. 'logo' => $paymentInfo['logo'] ? get_image_url($paymentInfo['logo']) : '',
  401. 'real_name' => $paymentInfo['real_name'],
  402. 'bank_name' => $paymentInfo['bank_name'],
  403. 'bank_card' => $paymentInfo['bank_card'],
  404. 'branch_name' => $paymentInfo['branch_name'],
  405. 'qrcode' => $paymentInfo['qrcode'] ? get_image_url($paymentInfo['qrcode']) : '',
  406. 'account' => $paymentInfo['account'],
  407. ];
  408. $this->model->startTrans();
  409. $orderNo = get_order_num('TA');
  410. $data = [
  411. 'user_id' => $userId,
  412. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  413. 'order_no' => $orderNo,
  414. 'type' => 2,
  415. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  416. 'price' => $price,
  417. 'num' => $num,
  418. 'total' => $total,
  419. 'payment_id' => $paymentId,
  420. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  421. 'payment_data' => $paymentData ? json_encode($paymentData, 256) : '',
  422. 'create_time' => time(),
  423. 'update_time' => time(),
  424. 'status' => 1,
  425. 'mark' => 1,
  426. ];
  427. if (!$order = $this->model->edit($data)) {
  428. $this->model->rollBack();
  429. $this->error = '3023';
  430. return false;
  431. }
  432. // 扣除出售用户的USDT
  433. if(!$this->memberModel->where(['id'=> $userId])->decrement('usdt_num', $num)){
  434. $this->model->rollBack();
  435. $this->error = '3020';
  436. return false;
  437. }
  438. $data = [
  439. 'order_no'=> $orderNo,
  440. 'user_id'=> $userId,
  441. 'type'=> 2,
  442. 'pay_type'=> 1,
  443. 'trade_type'=> 2,
  444. 'change_type'=> 2,
  445. 'num'=> $num,
  446. 'total'=> $total,
  447. 'balance'=> floatval($userInfo['usdt_num']-$num),
  448. 'create_time'=> time(),
  449. 'update_time'=> time(),
  450. 'status'=> 1,
  451. 'mark'=>1,
  452. 'remark'=> '客户卖出',
  453. ];
  454. if(!$this->capitalModel->edit($data)){
  455. $this->model->rollBack();
  456. $this->error = '3014';
  457. return false;
  458. }
  459. // 订单通知
  460. $data = [
  461. 'from_uid' => $userId,
  462. 'to_uid' => $businessInfo['id'],
  463. 'type' => 3,
  464. 'order_no' => $orderNo,
  465. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  466. 'message' => "您有来自客户出售广告订单:{$orderNo}的消息,请尽快回复!",
  467. 'message_type' => 1,
  468. 'data_type' => 3,
  469. 'create_time' => time(),
  470. 'update_time' => time(),
  471. 'status' => 1,
  472. 'mark' => 1,
  473. ];
  474. if (!ChatMessageService::make()->pushMessage($data)) {
  475. $this->model->rollBack();
  476. $this->error = '3031';
  477. return false;
  478. }
  479. $this->model->commit();
  480. return $order;
  481. }
  482. /**
  483. * 获取未支付或处理的订单数
  484. * @param $userId
  485. * @param int $type
  486. * @return mixed
  487. */
  488. public function checkOrderNoCatch($userId, $type = 1)
  489. {
  490. return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])
  491. ->whereIn('status', [1, 2, 5, 7])
  492. ->count('id');
  493. }
  494. /**
  495. * 自动取消广告订单处理
  496. * @return false
  497. */
  498. public function catchInvalidOrder(){
  499. $cacheKey = "caches:adverts:cancels:";
  500. if(RedisService::get($cacheKey.'lock')){
  501. return false;
  502. }
  503. RedisService::set($cacheKey.'lock', 1, rand(3, 5));
  504. $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  505. $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
  506. $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
  507. $catchNum = $catchNum > 0 ? $catchNum : 200;
  508. // 处理超时订单
  509. if ($overtime > 0) {
  510. $this->model->where(['mark' => 1])
  511. ->where('status', '<=', 2)
  512. ->where('create_time', '<=', time() - $overtime * 60)
  513. ->update(['status' => 7, 'catch_at' => time()]);
  514. }
  515. if ($cancelTime <= 0) {
  516. $this->error = '1023';
  517. return false;
  518. }
  519. $fail = 0;
  520. $success = 0;
  521. $this->model->where(function ($query) use ($cancelTime) {
  522. // 已更新为超时的订单
  523. $query->where(['mark' => 1, 'status' => 7])
  524. ->where('catch_at', '<=', time() - $cancelTime * 60);
  525. })
  526. ->orWhere(function ($query) use ($cancelTime, $overtime) {
  527. $query->where('mark', '=', 1)
  528. ->where('status', '<=', 2)
  529. ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
  530. })
  531. ->select(['id', 'user_id', 'business_id','advert_id','order_no', 'type', 'num','total'])
  532. ->take($catchNum)
  533. ->get()
  534. ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
  535. // 客户卖出订单退还
  536. $date = date('Y-m-d H:i:s');
  537. $type = isset($item['type']) ? $item['type'] : 0;
  538. if ($type == 2) {
  539. if(!$this->orderReback($item['user_id'], $item)){
  540. $fail++;
  541. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  542. }else{
  543. $success++;
  544. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  545. }
  546. }
  547. else{
  548. if(!$this->orderReback($item['business_id'], $item)){
  549. $fail++;
  550. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  551. }else{
  552. $success++;
  553. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  554. }
  555. }
  556. });
  557. return ['success'=> $success,'fail'=> $fail];
  558. }
  559. /**
  560. * 订单取消退还处理
  561. * @param $userId
  562. * @param $orderInfo
  563. * @return bool
  564. */
  565. protected function orderReback($userId, $orderInfo){
  566. try {
  567. if($orderInfo['num']<=0){
  568. return false;
  569. }
  570. $this->model->startTrans();
  571. $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_remark'=>'系统取消'];
  572. if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
  573. $this->model->rollBack();
  574. $this->error = '3043';
  575. return false;
  576. }
  577. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  578. if (empty($info)) {
  579. $this->model->rollBack();
  580. $this->error = '3019';
  581. return false;
  582. }
  583. // 退还币给客户
  584. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  585. $this->model->rollBack();
  586. $this->error = '3019';
  587. return false;
  588. }
  589. // 明细处理
  590. $data = [
  591. 'order_no' => $orderInfo['order_no'],
  592. 'user_id' => $userId,
  593. 'type' => 3,
  594. 'pay_type' => 1,
  595. 'trade_type' => 2,
  596. 'change_type' => 1,
  597. 'num' => $orderInfo['num'],
  598. 'total' => $orderInfo['total'],
  599. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  600. 'create_time' => time(),
  601. 'update_time' => time(),
  602. 'remark' => '系统自动取消退还',
  603. 'status' => 1,
  604. 'mark' => 1,
  605. ];
  606. if (!$this->capitalModel->edit($data)) {
  607. $this->error = '3014';
  608. $this->model->rollBack();
  609. return false;
  610. }
  611. $this->model->commit();
  612. $this->error = '3044';
  613. return true;
  614. } catch (\Exception $exception){
  615. $this->error = $exception->getMessage();
  616. return false;
  617. }
  618. }
  619. }