AdvertOrderService.php 56 KB

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