AdvertOrderService.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\AdvertModel;
  13. use App\Models\AdvertOrderModel;
  14. use App\Models\CapitalLogModel;
  15. use App\Models\MemberModel;
  16. use App\Services\Api\MemberPaymentService;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use App\Services\ChatMessageService;
  20. use App\Services\RedisService;
  21. /**
  22. * 用户广告订单-服务类
  23. * Class AdvertOrderService
  24. * @package App\Services\Common
  25. */
  26. class AdvertOrderService extends BaseService
  27. {
  28. // 静态对象
  29. protected static $instance = null;
  30. /**
  31. * 构造函数
  32. * @since 2020/11/10
  33. * LoginService constructor.
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new AdvertOrderModel();
  38. $this->advertModel = new AdvertModel();
  39. $this->memberModel = new MemberModel();
  40. $this->capitalModel = new CapitalLogModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * 订单列表
  55. * @param $params
  56. * @param int $pageSize
  57. * @return array
  58. */
  59. public function getDataList($params, $pageSize = 15)
  60. {
  61. $list = $this->model->from('advert_order as a')
  62. ->leftJoin('member as b', 'b.id', '=', 'a.business_id')
  63. ->leftJoin('member as c', 'c.id', '=', 'a.user_id')
  64. ->where(function ($query) use ($params) {
  65. $query->where(['a.mark' => 1])->where('a.status', '>', 0);
  66. $orderNo = isset($params['order_no']) && $params['order_no'] ? trim($params['order_no']) : '';
  67. if ($orderNo) {
  68. $query->where('a.order_no', 'like', "%{$orderNo}%");
  69. }
  70. $type = isset($params['type']) ? intval($params['type']) : 0;
  71. if ($type > 0) {
  72. $query->where(['a.type' => $type]);
  73. }
  74. $businessId = isset($params['business_id']) ? $params['business_id'] : 0;
  75. $userType = isset($params['user_type']) ? intval($params['user_type']) : 0;
  76. if ($userType > 0 && $businessId<=0) {
  77. $query->where(['c.user_type' => $userType]);
  78. }
  79. // 日期
  80. $date = isset($params['date']) ? $params['date'] : [];
  81. $start = isset($date[0])? $date[0] : '';
  82. $end = isset($date[1])? $date[1] : '';
  83. $end = $start>=$end? '' : $end;
  84. if ($start) {
  85. $query->where('a.create_time','>=', strtotime($start));
  86. }
  87. if($end){
  88. $query->where('a.create_time','<', strtotime($end));
  89. }
  90. $exceptionStatus = isset($params['exception_status']) ? intval($params['exception_status']) : 0;
  91. if ($exceptionStatus > 0) {
  92. $query->where(['a.exception_status' => $exceptionStatus]);
  93. }
  94. $status = isset($params['status']) ? intval($params['status']) : 0;
  95. if ($status > 0) {
  96. $query->where(['a.status' => $status]);
  97. }
  98. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  99. if ($userId > 0) {
  100. $query->where('a.user_id', $userId);
  101. }
  102. if ($businessId > 0) {
  103. $query->where('a.business_id', $businessId);
  104. }
  105. })
  106. ->select(['a.*', 'b.username', 'c.username as c_username'])
  107. ->orderBy('a.create_time', 'desc')
  108. ->orderBy('a.id', 'desc')
  109. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  110. $list = $list ? $list->toArray() : [];
  111. if ($list) {
  112. $payTypes = [1 => '银行卡', 2 => '微信', 3 => '支付宝', 4 => '其他'];
  113. $overTime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  114. $overTime = $overTime ? $overTime : 0;
  115. foreach ($list['data'] as &$item) {
  116. $item['idcardData'] = $item['idcard_data'] ? json_decode($item['idcard_data'], true) : [];
  117. $item['paymentData'] = $item['payment_data'] ? json_decode($item['payment_data'], true) : [];
  118. $item['create_time_text'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  119. $item['update_time_text'] = $item['update_time'] ? datetime($item['update_time'], 'Y-m-d H:i:s') : '';
  120. $item['time_text'] = $item['create_time'] ? datetime($item['create_time'], 'H:i') : '';
  121. $item['utime_text'] = $item['update_time'] ? datetime($item['update_time'], 'H:i') : '';
  122. $item['pay_time_text'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i') : '';
  123. $item['username_text'] = $item['username'] ? format_account($item['username']) : '';
  124. $item['c_username_text'] = $item['c_username'] ? format_account($item['c_username']) : '';
  125. $item['exception_img'] = $item['exception_img'] ? get_image_url($item['exception_img']) : '';
  126. $item['pay_img'] = $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  127. $item['paymentData']['qrcode'] = isset($item['paymentData']['qrcode']) && $item['paymentData']['qrcode'] ? get_image_url($item['paymentData']['qrcode']) : '';
  128. $overTime = max(0, intval($item['create_time']) + $overTime * 60 - time());
  129. $item['overtime_text'] = in_array($item['status'], [1, 2]) && $overTime ? date('H:i', $overTime) : '';
  130. $payType = isset($item['pay_type']) ? $item['pay_type'] : 0;
  131. $item['pay_name'] = isset($payTypes[$payType]) ? $payTypes[$payType] : '其他';
  132. $item['chat_key'] = getChatKey($item['user_id'],$item['business_id']);
  133. }
  134. }
  135. return [
  136. 'pageSize' => $pageSize,
  137. 'total' => isset($list['total']) ? $list['total'] : 0,
  138. 'list' => isset($list['data']) ? $list['data'] : []
  139. ];
  140. }
  141. /**
  142. * 购买
  143. * @param $userId
  144. * @param $params
  145. * @return false|int|number
  146. */
  147. public function buy($userId, $params)
  148. {
  149. $id = isset($params['id']) ? intval($params['id']) : 0;
  150. $num = isset($params['num']) ? intval($params['num']) : 0;
  151. if ($id <= 0 || $num<=0) {
  152. $this->error = '1013';
  153. return false;
  154. }
  155. // 验证参数
  156. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  157. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  158. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_buy_price'] : 0;
  159. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  160. // 是否开启交易
  161. if ($tradeOpen != 1) {
  162. $this->error = '1013';
  163. return false;
  164. }
  165. $info = AdvertService::make()->getInfo($id);
  166. $tradeType = isset($info['type'])? $info['type'] : 0;
  167. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  168. $price = isset($info['price'])? $info['price'] : 0;
  169. $businessId = isset($info['user_id'])? $info['user_id'] : 0;
  170. if(empty($info) || $info['status'] != 1 || $businessId<=0){
  171. $this->error = '4001';
  172. return false;
  173. }
  174. if($tradeType != 2){
  175. $this->error = '4003';
  176. return false;
  177. }
  178. if ($tradePrice <= 0 && $priceType == 2) {
  179. $this->error = '3002';
  180. return false;
  181. }
  182. // 浮动价格计算
  183. if ($priceType == 2) {
  184. $price = floatval($tradePrice + $price);
  185. }
  186. // 总价
  187. $total = floatval($price * $num);
  188. if($total<=0){
  189. $this->error = '4002';
  190. return false;
  191. }
  192. // 单笔限额
  193. if($total< $info['limit_min'] || $total> $info['limit_max']){
  194. $this->error = lang('4005',['min'=> $info['limit_min'],'max'=>$info['limit_max']]);
  195. return false;
  196. }
  197. // 购买用户信息
  198. $userInfo = MemberService::make()->getInfo($userId);
  199. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  200. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  201. $username = isset($userInfo['username']) ? format_account($userInfo['username']) : '';
  202. if (empty($userInfo) || $status != 1) {
  203. $this->error = '2009';
  204. return false;
  205. }
  206. if($idcardCheck != 1){
  207. $this->error = '2014';
  208. return false;
  209. }
  210. // 未处理订单
  211. $noCatchOrder = $this->checkOrderNoCatch($userId, 1);
  212. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  213. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  214. return false;
  215. }
  216. // 交易商家
  217. $businessInfo = MemberService::make()->getInfo($businessId);
  218. if (empty($businessInfo)) {
  219. $this->error = '3004';
  220. return false;
  221. }
  222. // 购买者身份信息
  223. $idcardData = [
  224. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  225. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  226. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  227. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  228. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  229. ];
  230. // 收款方式
  231. $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
  232. if (empty($payment)) {
  233. $this->error = '3015';
  234. return false;
  235. }
  236. $this->model->startTrans();
  237. $orderNo = get_order_num('TA');
  238. $data = [
  239. 'user_id' => $userId,
  240. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  241. 'order_no' => $orderNo,
  242. 'type' => 1,
  243. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  244. 'price' => $price,
  245. 'num' => $num,
  246. 'total' => $total,
  247. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  248. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  249. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  250. 'create_time' => time(),
  251. 'update_time' => time(),
  252. 'status' => 1,
  253. 'mark' => 1,
  254. ];
  255. if (!$order = $this->model->edit($data)) {
  256. $this->model->rollBack();
  257. $this->error = '3023';
  258. return false;
  259. }
  260. if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
  261. $this->model->rollBack();
  262. $this->error = '3020';
  263. return false;
  264. }
  265. $data = [
  266. 'order_no'=> $orderNo,
  267. 'user_id'=> $businessInfo['id'],
  268. 'type'=> 2,
  269. 'pay_type'=> 1,
  270. 'trade_type'=> 2,
  271. 'change_type'=> 2,
  272. 'num'=> $num,
  273. 'total'=> $total,
  274. 'balance'=> floatval($businessInfo['usdt_num']-$num),
  275. 'create_time'=> time(),
  276. 'update_time'=> time(),
  277. 'status'=> 1,
  278. 'mark'=>1,
  279. 'remark'=> '交易员卖出',
  280. ];
  281. if(!$this->capitalModel->edit($data)){
  282. $this->model->rollBack();
  283. $this->error = '3014';
  284. return false;
  285. }
  286. // 订单通知
  287. $data = [
  288. 'from_uid' => $userId,
  289. 'to_uid' => $businessInfo['id'],
  290. 'type' => 3,
  291. 'order_no' => $orderNo,
  292. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  293. 'message' => "您有来自客户购买广告订单:{$orderNo}的消息,请尽快回复!",
  294. 'message_type' => 1,
  295. 'data_type' => 2,
  296. 'create_time' => time(),
  297. 'update_time' => time(),
  298. 'status' => 1,
  299. 'mark' => 1,
  300. ];
  301. if (!ChatMessageService::make()->pushMessage($data)) {
  302. $this->model->rollBack();
  303. $this->error = '3031';
  304. return false;
  305. }
  306. $this->model->commit();
  307. return $order;
  308. }
  309. /**
  310. * 购买
  311. * @param $userId
  312. * @param $params
  313. * @return false|int|number
  314. */
  315. public function sell($userId, $params)
  316. {
  317. $id = isset($params['id']) ? intval($params['id']) : 0;
  318. $num = isset($params['num']) ? intval($params['num']) : 0;
  319. $paymentId = isset($params['payment_id']) ? intval($params['payment_id']) : 0;
  320. if ($id <= 0 || $num<=0 || $paymentId<=0) {
  321. $this->error = '1013';
  322. return false;
  323. }
  324. // 验证参数
  325. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  326. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  327. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_sell_price'] : 0;
  328. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  329. // 是否开启交易
  330. if ($tradeOpen != 1) {
  331. $this->error = '1013';
  332. return false;
  333. }
  334. $info = AdvertService::make()->getInfo($id);
  335. $tradeType = isset($info['type'])? $info['type'] : 0;
  336. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  337. $price = isset($info['price'])? $info['price'] : 0;
  338. $businessId = isset($info['user_id'])? $info['user_id'] : 0;
  339. if(empty($info) || $info['status'] != 1){
  340. $this->error = '4001';
  341. return false;
  342. }
  343. if($tradeType != 1){
  344. $this->error = '4004';
  345. return false;
  346. }
  347. if ($tradePrice <= 0 && $priceType == 2) {
  348. $this->error = '3002';
  349. return false;
  350. }
  351. // 验证数量或金额
  352. if ($priceType == 2) {
  353. $price = floatval($tradePrice + $price);
  354. }
  355. // 单笔限额
  356. $total = floatval($price * $num);
  357. if($total<=0){
  358. $this->error = '4002';
  359. return false;
  360. }
  361. if($total< $info['limit_min'] || $total> $info['limit_max']){
  362. $this->error = lang('4005',['min'=> $info['limit_min'],'max'=>$info['limit_max']]);
  363. return false;
  364. }
  365. // 用户信息
  366. $userInfo = MemberService::make()->getInfo($userId);
  367. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  368. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  369. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  370. if ($status != 1) {
  371. $this->error = '2009';
  372. return false;
  373. }
  374. if($idcardCheck != 1){
  375. $this->error = '2014';
  376. return false;
  377. }
  378. // 未处理订单
  379. $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
  380. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  381. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  382. return false;
  383. }
  384. // 交易商家
  385. $businessInfo = MemberService::make()->getInfo($businessId);
  386. if (empty($businessInfo)) {
  387. $this->error = '3004';
  388. return false;
  389. }
  390. // 购买者身份信息
  391. $idcardData = [
  392. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  393. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  394. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  395. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  396. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  397. ];
  398. // 收款方式
  399. $paymentInfo = MemberPaymentService::make()->getInfo($paymentId);
  400. if (empty($paymentInfo)) {
  401. $this->error = '3010';
  402. return false;
  403. }
  404. $paymentData = [
  405. 'payment_id' => $paymentInfo['id'],
  406. 'type' => $paymentInfo['type'],
  407. 'logo' => $paymentInfo['logo'] ? get_image_url($paymentInfo['logo']) : '',
  408. 'real_name' => $paymentInfo['real_name'],
  409. 'bank_name' => $paymentInfo['bank_name'],
  410. 'bank_card' => $paymentInfo['bank_card'],
  411. 'branch_name' => $paymentInfo['branch_name'],
  412. 'qrcode' => $paymentInfo['qrcode'] ? get_image_url($paymentInfo['qrcode']) : '',
  413. 'account' => $paymentInfo['account'],
  414. ];
  415. $this->model->startTrans();
  416. $orderNo = get_order_num('TA');
  417. $data = [
  418. 'user_id' => $userId,
  419. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  420. 'order_no' => $orderNo,
  421. 'type' => 2,
  422. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  423. 'price' => $price,
  424. 'num' => $num,
  425. 'total' => $total,
  426. 'payment_id' => $paymentId,
  427. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  428. 'payment_data' => $paymentData ? json_encode($paymentData, 256) : '',
  429. 'create_time' => time(),
  430. 'update_time' => time(),
  431. 'status' => 1,
  432. 'mark' => 1,
  433. ];
  434. if (!$order = $this->model->edit($data)) {
  435. $this->model->rollBack();
  436. $this->error = '3023';
  437. return false;
  438. }
  439. // 扣除出售用户的USDT
  440. if(!$this->memberModel->where(['id'=> $userId])->decrement('usdt_num', $num)){
  441. $this->model->rollBack();
  442. $this->error = '3020';
  443. return false;
  444. }
  445. $data = [
  446. 'order_no'=> $orderNo,
  447. 'user_id'=> $userId,
  448. 'type'=> 2,
  449. 'pay_type'=> 1,
  450. 'trade_type'=> 2,
  451. 'change_type'=> 2,
  452. 'num'=> $num,
  453. 'total'=> $total,
  454. 'balance'=> floatval($userInfo['usdt_num']-$num),
  455. 'create_time'=> time(),
  456. 'update_time'=> time(),
  457. 'status'=> 1,
  458. 'mark'=>1,
  459. 'remark'=> '客户卖出',
  460. ];
  461. if(!$this->capitalModel->edit($data)){
  462. $this->model->rollBack();
  463. $this->error = '3014';
  464. return false;
  465. }
  466. // 订单通知
  467. $data = [
  468. 'from_uid' => $userId,
  469. 'to_uid' => $businessInfo['id'],
  470. 'type' => 3,
  471. 'order_no' => $orderNo,
  472. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  473. 'message' => "您有来自客户出售广告订单:{$orderNo}的消息,请尽快回复!",
  474. 'message_type' => 1,
  475. 'data_type' => 3,
  476. 'create_time' => time(),
  477. 'update_time' => time(),
  478. 'status' => 1,
  479. 'mark' => 1,
  480. ];
  481. if (!ChatMessageService::make()->pushMessage($data)) {
  482. $this->model->rollBack();
  483. $this->error = '3031';
  484. return false;
  485. }
  486. $this->model->commit();
  487. return $order;
  488. }
  489. /**
  490. * 订单打款处理
  491. * @param $userId 用户ID
  492. * @param $params 打款参数
  493. * @return false
  494. */
  495. public function pay($userId, $params)
  496. {
  497. $orderId = isset($params['id']) ? $params['id'] : 0;
  498. if ($orderId <= 0) {
  499. $this->error = '1013';
  500. return false;
  501. }
  502. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 1])
  503. ->whereIn('status', [1, 2, 5, 7])
  504. ->select(['id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
  505. ->first();
  506. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  507. if (empty($orderInfo)) {
  508. $this->error = '3016';
  509. return false;
  510. }
  511. if ($tradeType != 1) {
  512. $this->error = '3024';
  513. return false;
  514. }
  515. // 用户信息
  516. $userInfo = MemberService::make()->getInfo($userId);
  517. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  518. if ($status != 1) {
  519. $this->error = '2009';
  520. return false;
  521. }
  522. // 交易密码
  523. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  524. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  525. if (empty($password)) {
  526. $this->error = '2015';
  527. return false;
  528. }
  529. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  530. $this->error = '2016';
  531. return false;
  532. }
  533. $data = [
  534. 'status' => 3,
  535. 'pay_type' => $params['pay_type'],
  536. 'pay_img' => $params['pay_img'],
  537. 'pay_remark' => $params['pay_remark'],
  538. 'pay_time' => time(),
  539. ];
  540. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($data)) {
  541. $this->error = '3018';
  542. return false;
  543. }
  544. return true;
  545. }
  546. /**
  547. * 订单确认处理
  548. * @param $userId 用户ID
  549. * @param $params 打款参数
  550. * @return false
  551. */
  552. public function collection($userId, $params)
  553. {
  554. $orderId = isset($params['id']) ? $params['id'] : 0;
  555. if ($orderId <= 0) {
  556. $this->error = '1013';
  557. return false;
  558. }
  559. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
  560. ->select(['id', 'user_id', 'order_no', 'business_id', 'payment_id', 'type', 'num', 'total', 'status'])
  561. ->first();
  562. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  563. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  564. if (empty($orderInfo) || empty($businessId)) {
  565. $this->error = '3016';
  566. return false;
  567. }
  568. if ($orderInfo['status'] != 3) {
  569. $this->error = '3026';
  570. return false;
  571. }
  572. if ($tradeType != 2) {
  573. $this->error = '3024';
  574. return false;
  575. }
  576. $this->model->startTrans();
  577. // 订单状态更新
  578. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update(['status' => 4, 'update_time' => time()])) {
  579. $this->model->rollBack();
  580. $this->error = '3023';
  581. return false;
  582. }
  583. // 交易处理
  584. if ($orderInfo['num'] > 0) {
  585. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username','usdt_num', 'user_type'])->first();
  586. if (empty($info)) {
  587. $this->model->rollBack();
  588. $this->error = '3019';
  589. return false;
  590. }
  591. // 商家进币
  592. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  593. $this->model->rollBack();
  594. $this->error = '3019';
  595. return false;
  596. }
  597. // 明细处理
  598. $data = [
  599. 'order_no' => $orderInfo['order_no'],
  600. 'user_id' => $businessId,
  601. 'type' => 1,
  602. 'trade_type' => 2,
  603. 'pay_type' => 1,
  604. 'change_type' => 1,
  605. 'num' => $orderInfo['num'],
  606. 'total' => $orderInfo['total'],
  607. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  608. 'create_time' => time(),
  609. 'remark' => '交易员购买广告',
  610. 'status' => 1,
  611. 'mark' => 1,
  612. ];
  613. if (!$this->capitalModel->edit($data)) {
  614. $this->error = '3014';
  615. $this->model->rollBack();
  616. return false;
  617. }
  618. }
  619. $this->model->commit();
  620. return true;
  621. }
  622. /**
  623. * 取消订单
  624. * @param $userId
  625. * @param $params
  626. * @return false
  627. */
  628. public function cancel($userId, $params)
  629. {
  630. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  631. if ($orderId <= 0) {
  632. $this->error = '1013';
  633. return false;
  634. }
  635. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])
  636. ->select(['id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  637. ->first();
  638. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  639. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  640. if (empty($orderInfo) || $businessId<=0) {
  641. $this->error = '3016';
  642. return false;
  643. }
  644. if ($orderInfo['status'] == 3) {
  645. $this->error = '3027';
  646. return false;
  647. }
  648. if ($orderInfo['status'] == 4) {
  649. $this->error = '3028';
  650. return false;
  651. }
  652. if ($orderInfo['status'] == 7) {
  653. $this->error = '3030';
  654. return false;
  655. }
  656. if (!in_array($orderInfo['status'], [1, 2])) {
  657. $this->error = '3029';
  658. return false;
  659. }
  660. $this->model->startTrans();
  661. // 订单状态更新
  662. $updateData = ['status' => 8, 'update_time' => time(), 'exception_remark' => '客户取消'];
  663. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($updateData)) {
  664. $this->model->rollBack();
  665. $this->error = '3023';
  666. return false;
  667. }
  668. // 出售订单,USDT退回
  669. if ($tradeType == 2 && $orderInfo['num']>0) {
  670. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  671. if (empty($info)) {
  672. $this->model->rollBack();
  673. $this->error = '3019';
  674. return false;
  675. }
  676. // 退还币给客户
  677. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  678. $this->model->rollBack();
  679. $this->error = '3019';
  680. return false;
  681. }
  682. // 明细处理
  683. $data = [
  684. 'order_no' => $orderInfo['order_no'],
  685. 'user_id' => $userId,
  686. 'type' => 3,
  687. 'trade_type' => 2,
  688. 'pay_type' => 1,
  689. 'change_type' => 1,
  690. 'num' => $orderInfo['num'],
  691. 'total' => $orderInfo['total'],
  692. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  693. 'create_time' => time(),
  694. 'update_time' => time(),
  695. 'remark' => '客户取消退还',
  696. 'status' => 1,
  697. 'mark' => 1,
  698. ];
  699. if (!$this->capitalModel->edit($data)) {
  700. $this->error = '3014';
  701. $this->model->rollBack();
  702. return false;
  703. }
  704. }
  705. // 买入取消
  706. else if($tradeType == 1 && $orderInfo['num']>0){
  707. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  708. if (empty($info)) {
  709. $this->model->rollBack();
  710. $this->error = '3019';
  711. return false;
  712. }
  713. // 退还币给币商
  714. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  715. $this->model->rollBack();
  716. $this->error = '3019';
  717. return false;
  718. }
  719. // 明细处理
  720. $data = [
  721. 'order_no' => $orderInfo['order_no'],
  722. 'user_id' => $businessId,
  723. 'type' => 3,
  724. 'trade_type' => 2,
  725. 'pay_type' => 1,
  726. 'change_type' => 1,
  727. 'num' => $orderInfo['num'],
  728. 'total' => $orderInfo['total'],
  729. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  730. 'create_time' => time(),
  731. 'update_time' => time(),
  732. 'remark' => '客户取消退还',
  733. 'status' => 1,
  734. 'mark' => 1,
  735. ];
  736. if (!$this->capitalModel->edit($data)) {
  737. $this->error = '3014';
  738. $this->model->rollBack();
  739. return false;
  740. }
  741. }
  742. $this->model->commit();
  743. return true;
  744. }
  745. /**
  746. * 获取未支付或处理的订单数
  747. * @param $userId
  748. * @param int $type
  749. * @return mixed
  750. */
  751. public function checkOrderNoCatch($userId, $type = 1)
  752. {
  753. return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])
  754. ->whereIn('status', [1, 2, 5, 7])
  755. ->count('id');
  756. }
  757. /**
  758. * 自动取消广告订单处理
  759. * @return false
  760. */
  761. public function catchInvalidOrder(){
  762. $cacheKey = "caches:adverts:cancels:";
  763. if(RedisService::get($cacheKey.'lock')){
  764. return false;
  765. }
  766. RedisService::set($cacheKey.'lock', 1, rand(3, 5));
  767. $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  768. $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
  769. $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
  770. $catchNum = $catchNum > 0 ? $catchNum : 200;
  771. // 处理超时订单
  772. if ($overtime > 0) {
  773. $this->model->where(['mark' => 1])
  774. ->where('status', '<=', 2)
  775. ->where('create_time', '<=', time() - $overtime * 60)
  776. ->update(['status' => 7, 'catch_at' => time()]);
  777. }
  778. if ($cancelTime <= 0) {
  779. $this->error = '1023';
  780. return false;
  781. }
  782. $fail = 0;
  783. $success = 0;
  784. $this->model->where(function ($query) use ($cancelTime) {
  785. // 已更新为超时的订单
  786. $query->where(['mark' => 1, 'status' => 7])
  787. ->where('catch_at', '<=', time() - $cancelTime * 60);
  788. })
  789. ->orWhere(function ($query) use ($cancelTime, $overtime) {
  790. $query->where('mark', '=', 1)
  791. ->where('status', '<=', 2)
  792. ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
  793. })
  794. ->select(['id', 'user_id', 'business_id','advert_id','order_no', 'type', 'num','total'])
  795. ->take($catchNum)
  796. ->get()
  797. ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
  798. // 客户卖出订单退还
  799. $date = date('Y-m-d H:i:s');
  800. $type = isset($item['type']) ? $item['type'] : 0;
  801. if ($type == 2) {
  802. if(!$this->orderReback($item['user_id'], $item)){
  803. $fail++;
  804. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  805. }else{
  806. $success++;
  807. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  808. }
  809. }
  810. else{
  811. if(!$this->orderReback($item['business_id'], $item)){
  812. $fail++;
  813. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  814. }else{
  815. $success++;
  816. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  817. }
  818. }
  819. });
  820. return ['success'=> $success,'fail'=> $fail];
  821. }
  822. /**
  823. * 订单取消退还处理
  824. * @param $userId
  825. * @param $orderInfo
  826. * @return bool
  827. */
  828. protected function orderReback($userId, $orderInfo){
  829. try {
  830. if($orderInfo['num']<=0){
  831. return false;
  832. }
  833. $this->model->startTrans();
  834. $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_status'=> 0,'exception_remark'=>'系统取消'];
  835. if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
  836. $this->model->rollBack();
  837. $this->error = '3043';
  838. return false;
  839. }
  840. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  841. if (empty($info)) {
  842. $this->model->rollBack();
  843. $this->error = '3019';
  844. return false;
  845. }
  846. // 退还币给客户
  847. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  848. $this->model->rollBack();
  849. $this->error = '3019';
  850. return false;
  851. }
  852. // 明细处理
  853. $data = [
  854. 'order_no' => $orderInfo['order_no'],
  855. 'user_id' => $userId,
  856. 'type' => 3,
  857. 'pay_type' => 1,
  858. 'trade_type' => 2,
  859. 'change_type' => 1,
  860. 'num' => $orderInfo['num'],
  861. 'total' => $orderInfo['total'],
  862. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  863. 'create_time' => time(),
  864. 'update_time' => time(),
  865. 'remark' => '系统自动取消退还',
  866. 'status' => 1,
  867. 'mark' => 1,
  868. ];
  869. if (!$this->capitalModel->edit($data)) {
  870. $this->error = '3014';
  871. $this->model->rollBack();
  872. return false;
  873. }
  874. $this->model->commit();
  875. $this->error = '3044';
  876. return true;
  877. } catch (\Exception $exception){
  878. $this->error = $exception->getMessage();
  879. return false;
  880. }
  881. }
  882. }