AdvertOrderService.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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. use App\Services\UsdtWalletService;
  22. /**
  23. * 用户广告订单-服务类
  24. * Class AdvertOrderService
  25. * @package App\Services\Common
  26. */
  27. class AdvertOrderService extends BaseService
  28. {
  29. // 静态对象
  30. protected static $instance = null;
  31. /**
  32. * 构造函数
  33. * @since 2020/11/10
  34. * LoginService constructor.
  35. */
  36. public function __construct()
  37. {
  38. $this->model = new AdvertOrderModel();
  39. $this->advertModel = new AdvertModel();
  40. $this->memberModel = new MemberModel();
  41. $this->capitalModel = new CapitalLogModel();
  42. }
  43. /**
  44. * 静态入口
  45. * @return static|null
  46. */
  47. public static function make()
  48. {
  49. if (!self::$instance) {
  50. self::$instance = (new static());
  51. }
  52. return self::$instance;
  53. }
  54. /**
  55. * 订单列表
  56. * @param $params
  57. * @param int $pageSize
  58. * @return array
  59. */
  60. public function getDataList($params, $pageSize = 15)
  61. {
  62. $list = $this->model->from('advert_order as a')
  63. ->leftJoin('member as b', 'b.id', '=', 'a.business_id')
  64. ->leftJoin('member as c', 'c.id', '=', 'a.user_id')
  65. ->where(function ($query) use ($params) {
  66. $query->where(['a.mark' => 1])->where('a.status', '>', 0);
  67. $orderNo = isset($params['order_no']) && $params['order_no'] ? trim($params['order_no']) : '';
  68. if ($orderNo) {
  69. $query->where('a.order_no', 'like', "%{$orderNo}%");
  70. }
  71. $type = isset($params['type']) ? intval($params['type']) : 0;
  72. if ($type > 0) {
  73. $query->where(['a.type' => $type]);
  74. }
  75. $businessId = isset($params['business_id']) ? $params['business_id'] : 0;
  76. $userType = isset($params['user_type']) ? intval($params['user_type']) : 0;
  77. if ($userType > 0 && $businessId<=0) {
  78. $query->where(['c.user_type' => $userType]);
  79. }
  80. // 日期
  81. $date = isset($params['date']) ? $params['date'] : [];
  82. $start = isset($date[0])? $date[0] : '';
  83. $end = isset($date[1])? $date[1] : '';
  84. $end = $start>=$end? '' : $end;
  85. if ($start) {
  86. $query->where('a.create_time','>=', strtotime($start));
  87. }
  88. if($end){
  89. $query->where('a.create_time','<', strtotime($end));
  90. }
  91. $exceptionStatus = isset($params['exception_status']) ? intval($params['exception_status']) : 0;
  92. if ($exceptionStatus > 0) {
  93. $query->where(['a.exception_status' => $exceptionStatus]);
  94. }
  95. $status = isset($params['status']) ? intval($params['status']) : 0;
  96. if ($status > 0) {
  97. $query->where(['a.status' => $status]);
  98. }
  99. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  100. if ($userId > 0) {
  101. $query->where('a.user_id', $userId);
  102. }
  103. if ($businessId > 0) {
  104. $query->where('a.business_id', $businessId);
  105. }
  106. })
  107. ->select(['a.*', 'b.username', 'c.username as c_username'])
  108. ->orderBy('a.create_time', 'desc')
  109. ->orderBy('a.id', 'desc')
  110. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  111. $list = $list ? $list->toArray() : [];
  112. if ($list) {
  113. $payTypes = [1 => '银行卡', 2 => '微信', 3 => '支付宝', 4 => '其他'];
  114. $overTime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  115. $overTime = $overTime ? $overTime : 0;
  116. foreach ($list['data'] as &$item) {
  117. $item['idcardData'] = $item['idcard_data'] ? json_decode($item['idcard_data'], true) : [];
  118. $item['paymentData'] = $item['payment_data'] ? json_decode($item['payment_data'], true) : [];
  119. $item['create_time_text'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  120. $item['update_time_text'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
  121. $item['time_text'] = $item['create_time'] ? datetime($item['create_time'], 'H:i') : '';
  122. $item['utime_text'] = $item['update_time'] ? datetime($item['update_time'], 'H:i') : '';
  123. $item['pay_time_text'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i') : '';
  124. $item['username_text'] = $item['username'] ? format_account($item['username']) : '';
  125. $item['c_username_text'] = $item['c_username'] ? format_account($item['c_username']) : '';
  126. $item['exception_img'] = $item['exception_img'] ? get_image_url($item['exception_img']) : '';
  127. $item['exception_img1'] = $item['exception_img1'] ? get_image_url($item['exception_img1']) : '';
  128. $item['exception_img2'] = $item['exception_img2'] ? get_image_url($item['exception_img2']) : '';
  129. $item['pay_img'] = $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  130. $item['paymentData']['qrcode'] = isset($item['paymentData']['qrcode']) && $item['paymentData']['qrcode'] ? get_image_url($item['paymentData']['qrcode']) : '';
  131. $overTime = max(0, intval($item['create_time']) + $overTime * 60 - time());
  132. $item['overtime_text'] = in_array($item['status'], [1, 2]) && $overTime ? date('H:i', $overTime) : '';
  133. $payType = isset($item['pay_type']) ? $item['pay_type'] : 0;
  134. $item['pay_name'] = isset($payTypes[$payType]) ? $payTypes[$payType] : '其他';
  135. $item['chat_key'] = getChatKey($item['user_id'],$item['business_id']);
  136. }
  137. }
  138. return [
  139. 'pageSize' => $pageSize,
  140. 'total' => isset($list['total']) ? $list['total'] : 0,
  141. 'list' => isset($list['data']) ? $list['data'] : []
  142. ];
  143. }
  144. /**
  145. * 购买
  146. * @param $userId
  147. * @param $params
  148. * @return false|int|number
  149. */
  150. public function buy($userId, $params)
  151. {
  152. $id = isset($params['id']) ? intval($params['id']) : 0;
  153. $num = isset($params['num']) ? intval($params['num']) : 0;
  154. if ($id <= 0 || $num<=0) {
  155. $this->error = '1013';
  156. return false;
  157. }
  158. // 验证参数
  159. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  160. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  161. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_buy_price'] : 0;
  162. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  163. // 是否开启交易
  164. if ($tradeOpen != 1) {
  165. $this->error = '1013';
  166. return false;
  167. }
  168. $info = AdvertService::make()->getInfo($id);
  169. $tradeType = isset($info['type'])? $info['type'] : 0;
  170. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  171. $price = isset($info['price'])? $info['price'] : 0;
  172. $businessId = isset($info['user_id'])? $info['user_id'] : 0;
  173. if(empty($info) || $info['status'] != 1 || $businessId<=0){
  174. $this->error = '4001';
  175. return false;
  176. }
  177. $expiredAt = isset($info['expired_at']) && $info['expired_at']? strtotime($info['expired_at']) : 0;
  178. if($expiredAt <= time()){
  179. $this->error = '4007';
  180. return false;
  181. }
  182. if($tradeType != 2){
  183. $this->error = '4003';
  184. return false;
  185. }
  186. if ($tradePrice <= 0 && $priceType == 2) {
  187. $this->error = '3002';
  188. return false;
  189. }
  190. // 浮动价格计算
  191. if ($priceType == 2) {
  192. $price = floatval($tradePrice + $price);
  193. }
  194. // 总价
  195. $total = floatval($price * $num);
  196. if($total<=0){
  197. $this->error = '4002';
  198. return false;
  199. }
  200. // 单笔限额
  201. if($total< $info['limit_min'] || $total> $info['limit_max']){
  202. $this->error = lang('4005',['min'=> $info['limit_min'],'max'=>$info['limit_max']]);
  203. return false;
  204. }
  205. // 购买用户信息
  206. $userInfo = MemberService::make()->getInfo($userId);
  207. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  208. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  209. $username = isset($userInfo['username']) ? format_account($userInfo['username']) : '';
  210. if (empty($userInfo) || $status != 1) {
  211. $this->error = '2009';
  212. return false;
  213. }
  214. if($idcardCheck != 1){
  215. $this->error = '2014';
  216. return false;
  217. }
  218. // 未处理订单
  219. $noCatchOrder = $this->checkOrderNoCatch($userId, 1);
  220. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  221. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  222. return false;
  223. }
  224. // 交易商家
  225. $businessInfo = MemberService::make()->getInfo($businessId);
  226. if (empty($businessInfo)) {
  227. $this->error = '3004';
  228. return false;
  229. }
  230. $setting = MemberSettingService::make()->getInfo($businessId);
  231. $advertOnline = isset($setting['advert_online'])? $setting['advert_online'] : 0;
  232. if($advertOnline != 1){
  233. $this->error = '4006';
  234. return false;
  235. }
  236. // 购买者身份信息
  237. $idcardData = [
  238. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  239. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  240. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  241. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  242. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  243. ];
  244. // 收款方式
  245. $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
  246. if (empty($payment)) {
  247. $this->error = '3015';
  248. return false;
  249. }
  250. $this->model->startTrans();
  251. $orderNo = get_order_num('TA');
  252. $data = [
  253. 'user_id' => $userId,
  254. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  255. 'order_no' => $orderNo,
  256. 'type' => 1,
  257. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  258. 'price' => $price,
  259. 'num' => $num,
  260. 'total' => $total,
  261. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  262. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  263. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  264. 'create_time' => time(),
  265. 'update_time' => time(),
  266. 'status' => 1,
  267. 'mark' => 1,
  268. ];
  269. if (!$order = $this->model->edit($data)) {
  270. $this->model->rollBack();
  271. $this->error = '3023';
  272. return false;
  273. }
  274. if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
  275. $this->model->rollBack();
  276. $this->error = '3020';
  277. return false;
  278. }
  279. $data = [
  280. 'order_no'=> $orderNo,
  281. 'user_id'=> $businessInfo['id'],
  282. 'type'=> 2,
  283. 'pay_type'=> 1,
  284. 'trade_type'=> 2,
  285. 'change_type'=> 2,
  286. 'num'=> $num,
  287. 'total'=> $total,
  288. 'balance'=> floatval($businessInfo['usdt_num']-$num),
  289. 'create_time'=> time(),
  290. 'update_time'=> time(),
  291. 'status'=> 1,
  292. 'mark'=>1,
  293. 'remark'=> '交易员卖出',
  294. ];
  295. if(!$this->capitalModel->edit($data)){
  296. $this->model->rollBack();
  297. $this->error = '3014';
  298. return false;
  299. }
  300. // 订单通知
  301. $data = [
  302. 'from_uid' => $userId,
  303. 'to_uid' => $businessInfo['id'],
  304. 'type' => 3,
  305. 'order_no' => $orderNo,
  306. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  307. 'message' => "您有来自客户购买广告订单:{$orderNo}的消息,请尽快回复!",
  308. 'message_type' => 1,
  309. 'data_type' => 2,
  310. 'create_time' => time(),
  311. 'update_time' => time(),
  312. 'status' => 1,
  313. 'mark' => 1,
  314. ];
  315. if (!ChatMessageService::make()->pushMessage($data)) {
  316. $this->model->rollBack();
  317. $this->error = '3031';
  318. return false;
  319. }
  320. $this->model->commit();
  321. return $order;
  322. }
  323. /**
  324. * 购买
  325. * @param $userId
  326. * @param $params
  327. * @return false|int|number
  328. */
  329. public function sell($userId, $params)
  330. {
  331. $id = isset($params['id']) ? intval($params['id']) : 0;
  332. $num = isset($params['num']) ? intval($params['num']) : 0;
  333. $paymentId = isset($params['payment_id']) ? intval($params['payment_id']) : 0;
  334. if ($id <= 0 || $num<=0 || $paymentId<=0) {
  335. $this->error = '1013';
  336. return false;
  337. }
  338. // 验证参数
  339. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  340. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  341. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_sell_price'] : 0;
  342. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  343. // 是否开启交易
  344. if ($tradeOpen != 1) {
  345. $this->error = '1013';
  346. return false;
  347. }
  348. $info = AdvertService::make()->getInfo($id);
  349. $tradeType = isset($info['type'])? $info['type'] : 0;
  350. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  351. $price = isset($info['price'])? $info['price'] : 0;
  352. $businessId = isset($info['user_id'])? $info['user_id'] : 0;
  353. if(empty($info) || $info['status'] != 1){
  354. $this->error = '4001';
  355. return false;
  356. }
  357. $expiredAt = isset($info['expired_at']) && $info['expired_at']? strtotime($info['expired_at']) : 0;
  358. if($expiredAt <= time()){
  359. $this->error = '4007';
  360. return false;
  361. }
  362. if($tradeType != 1){
  363. $this->error = '4004';
  364. return false;
  365. }
  366. if ($tradePrice <= 0 && $priceType == 2) {
  367. $this->error = '3002';
  368. return false;
  369. }
  370. // 验证数量或金额
  371. if ($priceType == 2) {
  372. $price = floatval($tradePrice + $price);
  373. }
  374. // 单笔限额
  375. $total = floatval($price * $num);
  376. if($total<=0){
  377. $this->error = '4002';
  378. return false;
  379. }
  380. if($total< $info['limit_min'] || $total> $info['limit_max']){
  381. $this->error = lang('4005',['min'=> $info['limit_min'],'max'=>$info['limit_max']]);
  382. return false;
  383. }
  384. // 用户信息
  385. $userInfo = MemberService::make()->getInfo($userId);
  386. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  387. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  388. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  389. if ($status != 1) {
  390. $this->error = '2009';
  391. return false;
  392. }
  393. if($idcardCheck != 1){
  394. $this->error = '2014';
  395. return false;
  396. }
  397. // 未处理订单
  398. $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
  399. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  400. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  401. return false;
  402. }
  403. // 交易商家
  404. $businessInfo = MemberService::make()->getInfo($businessId);
  405. if (empty($businessInfo)) {
  406. $this->error = '3004';
  407. return false;
  408. }
  409. $setting = MemberSettingService::make()->getInfo($businessId);
  410. $advertOnline = isset($setting['advert_online'])? $setting['advert_online'] : 0;
  411. if($advertOnline != 1){
  412. $this->error = '4006';
  413. return false;
  414. }
  415. // 购买者身份信息
  416. $idcardData = [
  417. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  418. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  419. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  420. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  421. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  422. ];
  423. // 收款方式
  424. $paymentInfo = MemberPaymentService::make()->getInfo($paymentId);
  425. if (empty($paymentInfo)) {
  426. $this->error = '3010';
  427. return false;
  428. }
  429. $paymentData = [
  430. 'payment_id' => $paymentInfo['id'],
  431. 'type' => $paymentInfo['type'],
  432. 'logo' => $paymentInfo['logo'] ? get_image_url($paymentInfo['logo']) : '',
  433. 'real_name' => $paymentInfo['real_name'],
  434. 'bank_name' => $paymentInfo['bank_name'],
  435. 'bank_card' => $paymentInfo['bank_card'],
  436. 'branch_name' => $paymentInfo['branch_name'],
  437. 'qrcode' => $paymentInfo['qrcode'] ? get_image_url($paymentInfo['qrcode']) : '',
  438. 'account' => $paymentInfo['account'],
  439. ];
  440. $this->model->startTrans();
  441. $orderNo = get_order_num('TA');
  442. $data = [
  443. 'user_id' => $userId,
  444. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  445. 'order_no' => $orderNo,
  446. 'type' => 2,
  447. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  448. 'price' => $price,
  449. 'num' => $num,
  450. 'total' => $total,
  451. 'payment_id' => $paymentId,
  452. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  453. 'payment_data' => $paymentData ? json_encode($paymentData, 256) : '',
  454. 'create_time' => time(),
  455. 'update_time' => time(),
  456. 'status' => 1,
  457. 'mark' => 1,
  458. ];
  459. if (!$order = $this->model->edit($data)) {
  460. $this->model->rollBack();
  461. $this->error = '3023';
  462. return false;
  463. }
  464. // 扣除出售用户的USDT
  465. if(!$this->memberModel->where(['id'=> $userId])->decrement('usdt_num', $num)){
  466. $this->model->rollBack();
  467. $this->error = '3020';
  468. return false;
  469. }
  470. $data = [
  471. 'order_no'=> $orderNo,
  472. 'user_id'=> $userId,
  473. 'type'=> 2,
  474. 'pay_type'=> 1,
  475. 'trade_type'=> 2,
  476. 'change_type'=> 2,
  477. 'num'=> $num,
  478. 'total'=> $total,
  479. 'balance'=> floatval($userInfo['usdt_num']-$num),
  480. 'create_time'=> time(),
  481. 'update_time'=> time(),
  482. 'status'=> 1,
  483. 'mark'=>1,
  484. 'remark'=> '客户卖出',
  485. ];
  486. if(!$this->capitalModel->edit($data)){
  487. $this->model->rollBack();
  488. $this->error = '3014';
  489. return false;
  490. }
  491. // 订单通知
  492. $data = [
  493. 'from_uid' => $userId,
  494. 'to_uid' => $businessInfo['id'],
  495. 'type' => 3,
  496. 'order_no' => $orderNo,
  497. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  498. 'message' => "您有来自客户出售广告订单:{$orderNo}的消息,请尽快回复!",
  499. 'message_type' => 1,
  500. 'data_type' => 3,
  501. 'create_time' => time(),
  502. 'update_time' => time(),
  503. 'status' => 1,
  504. 'mark' => 1,
  505. ];
  506. if (!ChatMessageService::make()->pushMessage($data)) {
  507. $this->model->rollBack();
  508. $this->error = '3031';
  509. return false;
  510. }
  511. $this->model->commit();
  512. return $order;
  513. }
  514. /**
  515. * 订单打款处理
  516. * @param $userId 用户ID
  517. * @param $params 打款参数
  518. * @return false
  519. */
  520. public function pay($userId, $params)
  521. {
  522. $orderId = isset($params['id']) ? $params['id'] : 0;
  523. if ($orderId <= 0) {
  524. $this->error = '1013';
  525. return false;
  526. }
  527. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 1])
  528. ->whereIn('status', [1, 2, 5, 7])
  529. ->select(['id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
  530. ->first();
  531. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  532. if (empty($orderInfo)) {
  533. $this->error = '3016';
  534. return false;
  535. }
  536. if ($tradeType != 1) {
  537. $this->error = '3024';
  538. return false;
  539. }
  540. // 用户信息
  541. $userInfo = MemberService::make()->getInfo($userId);
  542. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  543. if ($status != 1) {
  544. $this->error = '2009';
  545. return false;
  546. }
  547. // 交易密码
  548. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  549. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  550. if (empty($password)) {
  551. $this->error = '2015';
  552. return false;
  553. }
  554. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  555. $this->error = '2016';
  556. return false;
  557. }
  558. $data = [
  559. 'status' => 3,
  560. 'pay_type' => $params['pay_type'],
  561. 'pay_img' => $params['pay_img'],
  562. 'pay_remark' => $params['pay_remark'],
  563. 'pay_time' => time(),
  564. ];
  565. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($data)) {
  566. $this->error = '3018';
  567. return false;
  568. }
  569. return true;
  570. }
  571. /**
  572. * 订单确认处理
  573. * @param $userId 用户ID
  574. * @param $params 打款参数
  575. * @return false
  576. */
  577. public function collection($userId, $params)
  578. {
  579. $orderId = isset($params['id']) ? $params['id'] : 0;
  580. if ($orderId <= 0) {
  581. $this->error = '1013';
  582. return false;
  583. }
  584. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
  585. ->select(['id', 'user_id', 'order_no', 'business_id', 'payment_id', 'type', 'num', 'total', 'status'])
  586. ->first();
  587. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  588. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  589. if (empty($orderInfo) || empty($businessId)) {
  590. $this->error = '3016';
  591. return false;
  592. }
  593. if ($orderInfo['status'] != 3) {
  594. $this->error = '3026';
  595. return false;
  596. }
  597. if ($tradeType != 2) {
  598. $this->error = '3024';
  599. return false;
  600. }
  601. $this->model->startTrans();
  602. // 订单状态更新
  603. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update(['status' => 4, 'update_time' => time()])) {
  604. $this->model->rollBack();
  605. $this->error = '3023';
  606. return false;
  607. }
  608. // 交易处理
  609. if ($orderInfo['num'] > 0) {
  610. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username','usdt_num', 'user_type'])->first();
  611. if (empty($info)) {
  612. $this->model->rollBack();
  613. $this->error = '3019';
  614. return false;
  615. }
  616. // 商家进币
  617. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  618. $this->model->rollBack();
  619. $this->error = '3019';
  620. return false;
  621. }
  622. // 明细处理
  623. $data = [
  624. 'order_no' => $orderInfo['order_no'],
  625. 'user_id' => $businessId,
  626. 'type' => 1,
  627. 'trade_type' => 2,
  628. 'pay_type' => 1,
  629. 'change_type' => 1,
  630. 'num' => $orderInfo['num'],
  631. 'total' => $orderInfo['total'],
  632. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  633. 'create_time' => time(),
  634. 'remark' => '交易员购买广告',
  635. 'status' => 1,
  636. 'mark' => 1,
  637. ];
  638. if (!$this->capitalModel->edit($data)) {
  639. $this->error = '3014';
  640. $this->model->rollBack();
  641. return false;
  642. }
  643. }
  644. $this->model->commit();
  645. return true;
  646. }
  647. /**
  648. * 商家订单确认处理
  649. * @param $businessId 币商用户ID,平台处理时为0
  650. * @param $params 打款参数
  651. * @return false
  652. */
  653. public function businessCollection($businessId, $params)
  654. {
  655. $orderId = isset($params['id']) ? $params['id'] : 0;
  656. $catchUid = isset($params['catch_uid']) ? $params['catch_uid'] : 0;
  657. $type = isset($params['type']) ? $params['type'] : 0;
  658. if ($orderId <= 0) {
  659. $this->error = '1013';
  660. return false;
  661. }
  662. // 平台或币商处理
  663. $where = ['id' => $orderId, 'mark' => 1];
  664. if ($businessId) {
  665. $where['business_id'] = $businessId;
  666. }
  667. if ($type) {
  668. $where['type'] = $type;
  669. }
  670. $orderInfo = $this->model->where($where)
  671. ->select(['id', 'user_id', 'order_no','user_id', 'business_id', 'type', 'num', 'total','pay_time', 'status'])
  672. ->first();
  673. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  674. $orderType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  675. if (empty($orderInfo) || empty($userId)) {
  676. $this->error = '3016';
  677. return false;
  678. }
  679. $status = isset($params['status']) ? intval($params['status']) : 4;
  680. if ($status == 4 && $orderInfo['status'] != 3 && $businessId > 0) {
  681. $this->error = '3026';
  682. return false;
  683. }
  684. if ($status == 5 && $orderInfo['status'] == 8 && $businessId > 0) {
  685. $this->error = '3026';
  686. return false;
  687. }
  688. if ($status == 5 && $businessId <= 0 && $orderInfo['status'] == 4) {
  689. $this->error = '3026';
  690. return false;
  691. }
  692. if ($orderType != 1 && $status != 5) {
  693. $this->error = '3024';
  694. return false;
  695. }
  696. // 用户信息
  697. if ($businessId > 0) {
  698. $userInfo = MemberService::make()->getInfo($businessId);
  699. $userStatus = isset($userInfo['status']) ? $userInfo['status'] : 0;
  700. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  701. if ($userStatus != 1) {
  702. $this->error = '2009';
  703. return false;
  704. }
  705. // 交易密码
  706. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  707. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  708. if (empty($password)) {
  709. $this->error = '2015';
  710. return false;
  711. }
  712. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  713. $this->error = '2016';
  714. return false;
  715. }
  716. }
  717. $this->model->startTrans();
  718. // 订单状态更新,异常或确认
  719. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time()];
  720. // 异常处理数据
  721. if (isset($params['exception_type'])) {
  722. $updateData['exception_type'] = intval($params['exception_type']);
  723. }
  724. if (isset($params['exception_sub_type'])) {
  725. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  726. }
  727. if (isset($params['exception_img'])) {
  728. $updateData['exception_img'] = trim($params['exception_img']);
  729. if (strpos($updateData['exception_img'], "temp")) {
  730. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  731. } else {
  732. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  733. }
  734. }
  735. if (isset($params['exception_img1'])) {
  736. $updateData['exception_img'] = trim($params['exception_img1']);
  737. if (strpos($updateData['exception_img1'], "temp")) {
  738. $updateData['exception_img1'] = save_image($updateData['exception_img'], 'images');
  739. } else {
  740. $updateData['exception_img1'] = str_replace(IMG_URL, "", $updateData['exception_img1']);
  741. }
  742. }
  743. if (isset($params['exception_img2'])) {
  744. $updateData['exception_img2'] = trim($params['exception_img2']);
  745. if (strpos($updateData['exception_img2'], "temp")) {
  746. $updateData['exception_img2'] = save_image($updateData['exception_img2'], 'images');
  747. } else {
  748. $updateData['exception_img2'] = str_replace(IMG_URL, "", $updateData['exception_img2']);
  749. }
  750. }
  751. if (isset($params['exception_remark'])) {
  752. $updateData['exception_remark'] = trim($params['exception_remark']);
  753. }
  754. if (isset($params['exception_status'])) {
  755. $updateData['exception_status'] = intval($params['exception_status']);
  756. }
  757. if (!$this->model->where($where)->update($updateData)) {
  758. $this->model->rollBack();
  759. $this->error = '3023';
  760. return false;
  761. }
  762. // 交易处理,如果是确认状态
  763. if ($orderInfo['num'] > 0 && $status == 4 && $userId > 0 && $tradeType == 1) {
  764. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  765. if (empty($info)) {
  766. $this->model->rollBack();
  767. $this->error = '3019';
  768. return false;
  769. }
  770. // 客户币进账
  771. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  772. $this->model->rollBack();
  773. $this->error = '3019';
  774. return false;
  775. }
  776. // 明细处理
  777. $data = [
  778. 'order_no' => $orderInfo['order_no'],
  779. 'user_id' => $userId,
  780. 'type' => 1,
  781. 'pay_type' => 1,
  782. 'change_type' => 1,
  783. 'num' => $orderInfo['num'],
  784. 'total' => $orderInfo['total'],
  785. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  786. 'create_time' => time(),
  787. 'update_time' => time(),
  788. 'remark' => '客户买入',
  789. 'status' => 1,
  790. 'mark' => 1,
  791. ];
  792. if (!$this->capitalModel->edit($data)) {
  793. $this->error = '3014';
  794. $this->model->rollBack();
  795. return false;
  796. }
  797. // 更新收款地址数据
  798. $paymentId = isset($orderInfo['payment_id']) ? $orderInfo['payment_id'] : 0;
  799. $info = $this->paymentModel->where(['id' => $paymentId, 'mark' => 1])->select(['id', 'used_at'])->first();
  800. if ($paymentId && $info) {
  801. $usedAt = isset($info['used_at']) ? $info['used_at'] : '';
  802. if ($usedAt < date('Y-m-d')) {
  803. if (!$this->paymentModel->where(['id' => $paymentId])->update(['used_num' => 1, 'used_quota' => $orderInfo['total'], 'update_time' => time()])) {
  804. $this->model->rollBack();
  805. $this->error = '3018';
  806. return false;
  807. }
  808. } else {
  809. $res1 = $this->paymentModel->where(['id' => $paymentId])->increment('used_num', 1);
  810. $res2 = $this->paymentModel->where(['id' => $paymentId])->increment('used_quota', floatval($orderInfo['total']));
  811. if (!$res1 || !$res2) {
  812. $this->model->rollBack();
  813. $this->error = '3014';
  814. return false;
  815. }
  816. }
  817. }
  818. }
  819. // 外来平台需要出币和回调通知
  820. else if(in_array($tradeType, [2,3])){
  821. $usdtAddress = isset($orderInfo['usdt_address'])? $orderInfo['usdt_address'] : '';
  822. $contactType = isset($orderInfo['contact_type'])? $orderInfo['contact_type'] : '';
  823. $notifyUrl = isset($orderInfo['notify_url'])? $orderInfo['notify_url'] : '';
  824. if($usdtAddress && $contactType && $notifyUrl){
  825. // TRC 出币
  826. if($contactType == 1 && !$result = UsdtWalletService::make()->usdtTrcTransfer($usdtAddress,$orderInfo['num'])){
  827. $this->model->rollBack();
  828. $this->error = '3045';
  829. return false;
  830. }
  831. // ERC 出币
  832. else if($contactType == 2 && !$result = UsdtWalletService::make()->usdtErcTransfer($usdtAddress,$orderInfo['num'])){
  833. $this->model->rollBack();
  834. $this->error = '3045';
  835. return false;
  836. }
  837. // 更新出币数据和发送回调
  838. $txid = isset($result['txID'])? $result['txID'] : '';
  839. if($txid) {
  840. if (!$this->model->where($where)->update(['txid' => $txid, 'notify_num' => 1, 'notify_time' => date('Y-m-d H:i:s')])) {
  841. $this->model->rollBack();
  842. $this->error = '3045';
  843. return false;
  844. }
  845. // 发送回调通知
  846. $notifyData = [
  847. 'transaction_id' => $orderInfo['order_no'],
  848. 'order_no' => $orderInfo['pt_order_no'],
  849. 'usdt' => $orderInfo['num'],
  850. 'amount' => $orderInfo['total'],
  851. 'contact_type' => $orderInfo['contact_type'],
  852. 'usdt_address' => $orderInfo['usdt_address'],
  853. 'hash' => $txid,
  854. 'status'=> 1,
  855. 'pay_time'=> $orderInfo['pay_time']? $orderInfo['pay_time'] : time(),
  856. 'trade_time'=> time(),
  857. ];
  858. \App\Services\Oapi\TradeOrderService::make()->notify($notifyUrl, $notifyData);
  859. }
  860. }
  861. }
  862. $this->model->commit();
  863. $this->error = $status == 5 ? 3032 : 3021;
  864. return true;
  865. }
  866. /**
  867. * 取消订单
  868. * @param $userId
  869. * @param $params
  870. * @return false
  871. */
  872. public function cancel($userId, $params)
  873. {
  874. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  875. if ($orderId <= 0) {
  876. $this->error = '1013';
  877. return false;
  878. }
  879. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])
  880. ->select(['id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  881. ->first();
  882. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  883. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  884. if (empty($orderInfo) || $businessId<=0) {
  885. $this->error = '3016';
  886. return false;
  887. }
  888. if ($orderInfo['status'] == 3) {
  889. $this->error = '3027';
  890. return false;
  891. }
  892. if ($orderInfo['status'] == 4) {
  893. $this->error = '3028';
  894. return false;
  895. }
  896. if ($orderInfo['status'] == 7) {
  897. $this->error = '3030';
  898. return false;
  899. }
  900. if (!in_array($orderInfo['status'], [1, 2])) {
  901. $this->error = '3029';
  902. return false;
  903. }
  904. $this->model->startTrans();
  905. // 订单状态更新
  906. $updateData = ['status' => 8, 'update_time' => time(), 'exception_remark' => '客户取消'];
  907. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($updateData)) {
  908. $this->model->rollBack();
  909. $this->error = '3023';
  910. return false;
  911. }
  912. // 出售订单,USDT退回
  913. if ($tradeType == 2 && $orderInfo['num']>0) {
  914. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  915. if (empty($info)) {
  916. $this->model->rollBack();
  917. $this->error = '3019';
  918. return false;
  919. }
  920. // 退还币给客户
  921. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  922. $this->model->rollBack();
  923. $this->error = '3019';
  924. return false;
  925. }
  926. // 明细处理
  927. $data = [
  928. 'order_no' => $orderInfo['order_no'],
  929. 'user_id' => $userId,
  930. 'type' => 3,
  931. 'trade_type' => 2,
  932. 'pay_type' => 1,
  933. 'change_type' => 1,
  934. 'num' => $orderInfo['num'],
  935. 'total' => $orderInfo['total'],
  936. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  937. 'create_time' => time(),
  938. 'update_time' => time(),
  939. 'remark' => '客户取消退还',
  940. 'status' => 1,
  941. 'mark' => 1,
  942. ];
  943. if (!$this->capitalModel->edit($data)) {
  944. $this->error = '3014';
  945. $this->model->rollBack();
  946. return false;
  947. }
  948. }
  949. // 买入取消
  950. else if($tradeType == 1 && $orderInfo['num']>0){
  951. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  952. if (empty($info)) {
  953. $this->model->rollBack();
  954. $this->error = '3019';
  955. return false;
  956. }
  957. // 退还币给币商
  958. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  959. $this->model->rollBack();
  960. $this->error = '3019';
  961. return false;
  962. }
  963. // 明细处理
  964. $data = [
  965. 'order_no' => $orderInfo['order_no'],
  966. 'user_id' => $businessId,
  967. 'type' => 3,
  968. 'trade_type' => 2,
  969. 'pay_type' => 1,
  970. 'change_type' => 1,
  971. 'num' => $orderInfo['num'],
  972. 'total' => $orderInfo['total'],
  973. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  974. 'create_time' => time(),
  975. 'update_time' => time(),
  976. 'remark' => '客户取消退还',
  977. 'status' => 1,
  978. 'mark' => 1,
  979. ];
  980. if (!$this->capitalModel->edit($data)) {
  981. $this->error = '3014';
  982. $this->model->rollBack();
  983. return false;
  984. }
  985. }
  986. $this->model->commit();
  987. return true;
  988. }
  989. /**
  990. * 获取未支付或处理的订单数
  991. * @param $userId
  992. * @param int $type
  993. * @return mixed
  994. */
  995. public function checkOrderNoCatch($userId, $type = 1)
  996. {
  997. return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])
  998. ->whereIn('status', [1, 2, 5, 7])
  999. ->count('id');
  1000. }
  1001. /**
  1002. * 自动取消广告订单处理
  1003. * @return false
  1004. */
  1005. public function catchInvalidOrder(){
  1006. $cacheKey = "caches:adverts:cancels:";
  1007. if(RedisService::get($cacheKey.'lock')){
  1008. return false;
  1009. }
  1010. RedisService::set($cacheKey.'lock', 1, rand(3, 5));
  1011. $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  1012. $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
  1013. $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
  1014. $catchNum = $catchNum > 0 ? $catchNum : 200;
  1015. // 处理超时订单
  1016. if ($overtime > 0) {
  1017. $this->model->where(['mark' => 1])
  1018. ->where('status', '<=', 2)
  1019. ->where('create_time', '<=', time() - $overtime * 60)
  1020. ->update(['status' => 7, 'catch_at' => time()]);
  1021. }
  1022. if ($cancelTime <= 0) {
  1023. $this->error = '1023';
  1024. return false;
  1025. }
  1026. $fail = 0;
  1027. $success = 0;
  1028. $this->model->where(function ($query) use ($cancelTime) {
  1029. // 已更新为超时的订单
  1030. $query->where(['mark' => 1, 'status' => 7])
  1031. ->where('catch_at', '<=', time() - $cancelTime * 60);
  1032. })
  1033. ->orWhere(function ($query) use ($cancelTime, $overtime) {
  1034. $query->where('mark', '=', 1)
  1035. ->where('status', '<=', 2)
  1036. ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
  1037. })
  1038. ->select(['id', 'user_id', 'business_id','advert_id','order_no', 'type', 'num','total'])
  1039. ->take($catchNum)
  1040. ->get()
  1041. ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
  1042. // 客户卖出订单退还
  1043. $date = date('Y-m-d H:i:s');
  1044. $type = isset($item['type']) ? $item['type'] : 0;
  1045. if ($type == 2) {
  1046. if(!$this->orderReback($item['user_id'], $item)){
  1047. $fail++;
  1048. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1049. }else{
  1050. $success++;
  1051. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1052. }
  1053. }
  1054. else{
  1055. if(!$this->orderReback($item['business_id'], $item)){
  1056. $fail++;
  1057. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1058. }else{
  1059. $success++;
  1060. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1061. }
  1062. }
  1063. });
  1064. return ['success'=> $success,'fail'=> $fail];
  1065. }
  1066. /**
  1067. * 订单取消退还处理
  1068. * @param $userId
  1069. * @param $orderInfo
  1070. * @return bool
  1071. */
  1072. protected function orderReback($userId, $orderInfo){
  1073. try {
  1074. if($orderInfo['num']<=0){
  1075. return false;
  1076. }
  1077. $this->model->startTrans();
  1078. $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_status'=> 0,'exception_remark'=>'系统取消'];
  1079. if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
  1080. $this->model->rollBack();
  1081. $this->error = '3043';
  1082. return false;
  1083. }
  1084. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1085. if (empty($info)) {
  1086. $this->model->rollBack();
  1087. $this->error = '3019';
  1088. return false;
  1089. }
  1090. // 退还币给客户
  1091. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1092. $this->model->rollBack();
  1093. $this->error = '3019';
  1094. return false;
  1095. }
  1096. // 明细处理
  1097. $data = [
  1098. 'order_no' => $orderInfo['order_no'],
  1099. 'user_id' => $userId,
  1100. 'type' => 3,
  1101. 'pay_type' => 1,
  1102. 'trade_type' => 2,
  1103. 'change_type' => 1,
  1104. 'num' => $orderInfo['num'],
  1105. 'total' => $orderInfo['total'],
  1106. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1107. 'create_time' => time(),
  1108. 'update_time' => time(),
  1109. 'remark' => '系统自动取消退还',
  1110. 'status' => 1,
  1111. 'mark' => 1,
  1112. ];
  1113. if (!$this->capitalModel->edit($data)) {
  1114. $this->error = '3014';
  1115. $this->model->rollBack();
  1116. return false;
  1117. }
  1118. $this->model->commit();
  1119. $this->error = '3044';
  1120. return true;
  1121. } catch (\Exception $exception){
  1122. $this->error = $exception->getMessage();
  1123. return false;
  1124. }
  1125. }
  1126. }