AdvertOrderService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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['pay_time_text'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i') : '';
  115. $item['username_text'] = $item['username'] ? format_account($item['username']) : '';
  116. $item['c_username_text'] = $item['c_username'] ? format_account($item['c_username']) : '';
  117. $item['exception_img'] = $item['exception_img'] ? get_image_url($item['exception_img']) : '';
  118. $item['pay_img'] = $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  119. $item['paymentData']['qrcode'] = isset($item['paymentData']['qrcode']) && $item['paymentData']['qrcode'] ? get_image_url($item['paymentData']['qrcode']) : '';
  120. $overTime = max(0, intval($item['create_time']) + $overTime * 60 - time());
  121. $item['overtime_text'] = in_array($item['status'], [1, 2]) && $overTime ? date('H:i', $overTime) : '';
  122. $payType = isset($item['pay_type']) ? $item['pay_type'] : 0;
  123. $item['pay_name'] = isset($payTypes[$payType]) ? $payTypes[$payType] : '其他';
  124. }
  125. }
  126. return [
  127. 'pageSize' => $pageSize,
  128. 'total' => isset($list['total']) ? $list['total'] : 0,
  129. 'list' => isset($list['data']) ? $list['data'] : []
  130. ];
  131. }
  132. /**
  133. * 购买
  134. * @param $userId
  135. * @param $params
  136. * @return false|int|number
  137. */
  138. public function buy($userId, $params)
  139. {
  140. $id = isset($params['id']) ? intval($params['id']) : 0;
  141. $num = isset($params['num']) ? intval($params['num']) : 0;
  142. if ($id <= 0 || $num<=0) {
  143. $this->error = '1013';
  144. return false;
  145. }
  146. // 验证参数
  147. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  148. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  149. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_buy_price'] : 0;
  150. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  151. // 是否开启交易
  152. if ($tradeOpen != 1) {
  153. $this->error = '1013';
  154. return false;
  155. }
  156. $info = AdvertService::make()->getInfo($id);
  157. $tradeType = isset($info['type'])? $info['type'] : 0;
  158. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  159. $price = isset($info['price'])? $info['price'] : 0;
  160. $businessId = isset($info['business_id'])? $info['business_id'] : 0;
  161. if(empty($info) || $info['status'] != 1){
  162. $this->error = '4001';
  163. return false;
  164. }
  165. if ($tradePrice <= 0 && $priceType == 2) {
  166. $this->error = '3002';
  167. return false;
  168. }
  169. // 浮动价格计算
  170. if ($priceType == 2) {
  171. $price = floatval($tradePrice + $price);
  172. }
  173. // 总价
  174. $total = floatval($price * $num);
  175. if($total<=0){
  176. $this->error = '4002';
  177. return false;
  178. }
  179. // 购买用户信息
  180. $userInfo = MemberService::make()->getInfo($userId);
  181. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  182. if (empty($userInfo) || $status != 1) {
  183. $this->error = '2009';
  184. return false;
  185. }
  186. // 未处理订单
  187. $noCatchOrder = $this->checkOrderNoCatch($userId, 1);
  188. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  189. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  190. return false;
  191. }
  192. // 交易商家
  193. $businessInfo = MemberService::make()->getInfo($businessId);
  194. if (empty($businessInfo)) {
  195. $this->error = '3004';
  196. return false;
  197. }
  198. // 购买者身份信息
  199. $idcardData = [
  200. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  201. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  202. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  203. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  204. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  205. ];
  206. // 收款方式
  207. $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
  208. if (empty($payment)) {
  209. $this->error = '3015';
  210. return false;
  211. }
  212. $this->model->startTrans();
  213. $orderNo = get_order_num('OT');
  214. $data = [
  215. 'user_id' => $userId,
  216. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  217. 'order_no' => $orderNo,
  218. 'type' => 1,
  219. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  220. 'price' => $price,
  221. 'num' => $num,
  222. 'total' => $total,
  223. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  224. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  225. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  226. 'create_time' => time(),
  227. 'update_time' => time(),
  228. 'status' => 1,
  229. 'mark' => 1,
  230. ];
  231. if (!$order = $this->model->edit($data)) {
  232. $this->model->rollBack();
  233. $this->error = '3023';
  234. return false;
  235. }
  236. if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
  237. $this->model->rollBack();
  238. $this->error = '3020';
  239. return false;
  240. }
  241. $data = [
  242. 'order_no'=> $orderNo,
  243. 'user_id'=> $businessInfo['id'],
  244. 'type'=> 2,
  245. 'pay_type'=> 1,
  246. 'trade_type'=> 2,
  247. 'change_type'=> 2,
  248. 'num'=> $num,
  249. 'total'=> $total,
  250. 'balance'=> floatval($businessInfo['usdt_num']-$num),
  251. 'create_time'=> time(),
  252. 'update_time'=> time(),
  253. 'status'=> 1,
  254. 'mark'=>1,
  255. 'remark'=> '交易员卖出',
  256. ];
  257. if(!$this->capitalModel->edit($data)){
  258. $this->model->rollBack();
  259. $this->error = '3014';
  260. return false;
  261. }
  262. // 订单通知
  263. $data = [
  264. 'from_uid' => $userId,
  265. 'to_uid' => $businessInfo['id'],
  266. 'type' => 2,
  267. 'order_no' => $orderNo,
  268. 'chat_key' => ChatMessageService::make()->getChatKey($userId, $businessInfo['id']),
  269. 'message' => "来自用户{$username}的购买订单,金额{$total},单号{$orderNo},时间" . date('Y-m-d H:i:s'),
  270. 'message_type' => 1,
  271. 'create_time' => time(),
  272. 'update_time' => time(),
  273. 'status' => 1,
  274. 'mark' => 1,
  275. ];
  276. if (!ChatMessageService::make()->pushMessage($data)) {
  277. $this->model->rollBack();
  278. $this->error = '3031';
  279. return false;
  280. }
  281. $this->model->commit();
  282. return $order;
  283. }
  284. /**
  285. * 购买
  286. * @param $userId
  287. * @param $params
  288. * @return false|int|number
  289. */
  290. public function sell($userId, $params)
  291. {
  292. $id = isset($params['id']) ? intval($params['id']) : 0;
  293. $num = isset($params['num']) ? intval($params['num']) : 0;
  294. if ($id <= 0 || $num<=0) {
  295. $this->error = '1013';
  296. return false;
  297. }
  298. // 验证参数
  299. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  300. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  301. $tradeMinNum = isset($config['trade_min_num']) ? $config['trade_min_num'] : 0;
  302. $tradeMaxNum = isset($config['trade_max_num']) ? $config['trade_max_num'] : 0;
  303. $trademinMoney = isset($config['trade_min_money']) ? $config['trade_min_money'] : 0;
  304. $tradeMaxMoney = isset($config['trade_max_money']) ? $config['trade_max_money'] : 0;
  305. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_sell_price'] : 0;
  306. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  307. // 是否开启交易
  308. if ($tradeOpen != 1) {
  309. $this->error = '1013';
  310. return false;
  311. }
  312. $info = AdvertService::make()->getInfo($id);
  313. $tradeType = isset($info['type'])? $info['type'] : 0;
  314. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  315. $price = isset($info['price'])? $info['price'] : 0;
  316. $businessId = isset($info['business_id'])? $info['business_id'] : 0;
  317. if(empty($info) || $info['status'] != 1){
  318. $this->error = '4001';
  319. return false;
  320. }
  321. if ($tradePrice <= 0 && $priceType == 2) {
  322. $this->error = '3002';
  323. return false;
  324. }
  325. // 验证数量或金额
  326. if ($priceType == 2) {
  327. $price = floatval($tradePrice + $price);
  328. }
  329. $total = floatval($price * $num);
  330. // 用户信息
  331. $userInfo = MemberService::make()->getInfo($userId);
  332. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  333. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  334. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  335. if ($status != 1) {
  336. $this->error = '2009';
  337. return false;
  338. }
  339. // 未处理订单
  340. $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
  341. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  342. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  343. return false;
  344. }
  345. // 交易商家
  346. $businessInfo = MemberService::make()->getInfo($businessId);
  347. if (empty($businessInfo)) {
  348. $this->error = '3004';
  349. return false;
  350. }
  351. // 购买者身份信息
  352. $idcardData = [
  353. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  354. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  355. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  356. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  357. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  358. ];
  359. // 收款方式
  360. $payment = MemberPaymentService::make()->getPayment($userId);
  361. if (empty($payment)) {
  362. $this->error = '3015';
  363. return false;
  364. }
  365. $this->model->startTrans();
  366. $orderNo = get_order_num('OT');
  367. $data = [
  368. 'user_id' => $userId,
  369. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  370. 'order_no' => $orderNo,
  371. 'type' => 1,
  372. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  373. 'price' => $price,
  374. 'num' => $num,
  375. 'total' => $total,
  376. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  377. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  378. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  379. 'create_time' => time(),
  380. 'update_time' => time(),
  381. 'status' => 1,
  382. 'mark' => 1,
  383. ];
  384. if (!$order = $this->model->edit($data)) {
  385. $this->model->rollBack();
  386. $this->error = '3023';
  387. return false;
  388. }
  389. if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
  390. $this->model->rollBack();
  391. $this->error = '3020';
  392. return false;
  393. }
  394. $data = [
  395. 'order_no'=> $orderNo,
  396. 'user_id'=> $businessInfo['id'],
  397. 'type'=> 2,
  398. 'pay_type'=> 1,
  399. 'trade_type'=> 2,
  400. 'change_type'=> 2,
  401. 'num'=> $num,
  402. 'total'=> $total,
  403. 'balance'=> floatval($businessInfo['usdt_num']-$num),
  404. 'create_time'=> time(),
  405. 'update_time'=> time(),
  406. 'status'=> 1,
  407. 'mark'=>1,
  408. 'remark'=> '交易员卖出',
  409. ];
  410. if(!$this->capitalModel->edit($data)){
  411. $this->model->rollBack();
  412. $this->error = '3014';
  413. return false;
  414. }
  415. // 订单通知
  416. $data = [
  417. 'from_uid' => $userId,
  418. 'to_uid' => $businessInfo['id'],
  419. 'type' => 2,
  420. 'order_no' => $orderNo,
  421. 'chat_key' => ChatMessageService::make()->getChatKey($userId, $businessInfo['id']),
  422. 'message' => "来自用户{$username}的购买订单,金额{$total},单号{$orderNo},时间" . date('Y-m-d H:i:s'),
  423. 'message_type' => 1,
  424. 'create_time' => time(),
  425. 'update_time' => time(),
  426. 'status' => 1,
  427. 'mark' => 1,
  428. ];
  429. if (!ChatMessageService::make()->pushMessage($data)) {
  430. $this->model->rollBack();
  431. $this->error = '3031';
  432. return false;
  433. }
  434. $this->model->commit();
  435. return $order;
  436. }
  437. /**
  438. * 获取未支付或处理的订单数
  439. * @param $userId
  440. * @param int $type
  441. * @return mixed
  442. */
  443. public function checkOrderNoCatch($userId, $type = 1)
  444. {
  445. return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])
  446. ->whereIn('status', [1, 2, 7])
  447. ->count('id');
  448. }
  449. /**
  450. * 自动取消广告订单处理
  451. * @return false
  452. */
  453. public function catchInvalidOrder(){
  454. $cacheKey = "caches:adverts:cancels:";
  455. if(RedisService::get($cacheKey.'lock')){
  456. return false;
  457. }
  458. RedisService::set($cacheKey.'lock', 1, rand(3, 5));
  459. $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  460. $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
  461. $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
  462. $catchNum = $catchNum > 0 ? $catchNum : 200;
  463. // 处理超时订单
  464. if ($overtime > 0) {
  465. $this->model->where(['mark' => 1])
  466. ->where('status', '<=', 2)
  467. ->where('create_time', '<=', time() - $overtime * 60)
  468. ->update(['status' => 7, 'catch_at' => time()]);
  469. }
  470. if ($cancelTime <= 0) {
  471. $this->error = '1023';
  472. return false;
  473. }
  474. $fail = 0;
  475. $success = 0;
  476. $this->model->where(function ($query) use ($cancelTime) {
  477. // 已更新为超时的订单
  478. $query->where(['mark' => 1, 'status' => 7])
  479. ->where('catch_at', '<=', time() - $cancelTime * 60);
  480. })
  481. ->orWhere(function ($query) use ($cancelTime, $overtime) {
  482. $query->where('mark', '=', 1)
  483. ->where('status', '<=', 2)
  484. ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
  485. })
  486. ->select(['id', 'user_id', 'business_id','advert_id','order_no', 'type', 'num','total'])
  487. ->take($catchNum)
  488. ->get()
  489. ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
  490. // 客户卖出订单退还
  491. $date = date('Y-m-d H:i:s');
  492. $type = isset($item['type']) ? $item['type'] : 0;
  493. if ($type == 2) {
  494. if(!$this->orderReback($item['user_id'], $item)){
  495. $fail++;
  496. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  497. }else{
  498. $success++;
  499. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  500. }
  501. }
  502. else{
  503. if(!$this->orderReback($item['business_id'], $item)){
  504. $fail++;
  505. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  506. }else{
  507. $success++;
  508. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  509. }
  510. }
  511. });
  512. return ['success'=> $success,'fail'=> $fail];
  513. }
  514. /**
  515. * 订单取消退还处理
  516. * @param $userId
  517. * @param $orderInfo
  518. * @return bool
  519. */
  520. protected function orderReback($userId, $orderInfo){
  521. try {
  522. if($orderInfo['num']<=0){
  523. return false;
  524. }
  525. $this->model->startTrans();
  526. $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_remark'=>'系统取消'];
  527. if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
  528. $this->model->rollBack();
  529. $this->error = '3043';
  530. return false;
  531. }
  532. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  533. if (empty($info)) {
  534. $this->model->rollBack();
  535. $this->error = '3019';
  536. return false;
  537. }
  538. // 退还币给客户
  539. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  540. $this->model->rollBack();
  541. $this->error = '3019';
  542. return false;
  543. }
  544. // 明细处理
  545. $data = [
  546. 'order_no' => $orderInfo['order_no'],
  547. 'user_id' => $userId,
  548. 'type' => 3,
  549. 'pay_type' => 1,
  550. 'trade_type' => 2,
  551. 'change_type' => 1,
  552. 'num' => $orderInfo['num'],
  553. 'total' => $orderInfo['total'],
  554. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  555. 'create_time' => time(),
  556. 'update_time' => time(),
  557. 'remark' => '系统自动取消退还',
  558. 'status' => 1,
  559. 'mark' => 1,
  560. ];
  561. if (!$this->capitalModel->edit($data)) {
  562. $this->error = '3014';
  563. $this->model->rollBack();
  564. return false;
  565. }
  566. $this->model->commit();
  567. $this->error = '3044';
  568. return true;
  569. } catch (\Exception $exception){
  570. $this->error = $exception->getMessage();
  571. return false;
  572. }
  573. }
  574. }