AdvertOrderService.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  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 businessPay($businessId, $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(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
  585. ->whereIn('status', [1, 2, 5, 7])
  586. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
  587. ->first();
  588. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  589. if (empty($orderInfo)) {
  590. $this->error = '3016';
  591. return false;
  592. }
  593. if ($tradeType != 2) {
  594. $this->error = '3025';
  595. return false;
  596. }
  597. // 用户信息
  598. $userInfo = MemberService::make()->getInfo($businessId);
  599. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  600. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  601. if ($status != 1) {
  602. $this->error = '2009';
  603. return false;
  604. }
  605. // 交易密码
  606. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  607. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  608. if (empty($password)) {
  609. $this->error = '2015';
  610. return false;
  611. }
  612. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  613. $this->error = '2016';
  614. return false;
  615. }
  616. if (strpos($params['pay_img'], "temp")) {
  617. $params['pay_img'] = save_image($params['pay_img'], 'images');
  618. } else {
  619. $params['pay_img'] = str_replace(IMG_URL, "", $params['pay_img']);
  620. }
  621. $data = [
  622. 'status' => 3,
  623. 'pay_type' => $params['pay_type'],
  624. 'pay_img' => $params['pay_img'],
  625. 'pay_remark' => $params['pay_remark'],
  626. 'pay_time' => time(),
  627. ];
  628. if (!$this->model->where(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1])->update($data)) {
  629. $this->error = '3018';
  630. return false;
  631. }
  632. $this->error = '3017';
  633. return true;
  634. }
  635. /**
  636. * 订单确认处理
  637. * @param $userId 用户ID
  638. * @param $params 打款参数
  639. * @return false
  640. */
  641. public function collection($userId, $params)
  642. {
  643. $orderId = isset($params['id']) ? $params['id'] : 0;
  644. if ($orderId <= 0) {
  645. $this->error = '1013';
  646. return false;
  647. }
  648. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
  649. ->select(['id', 'user_id', 'order_no', 'business_id', 'payment_id', 'type', 'num', 'total', 'status'])
  650. ->first();
  651. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  652. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  653. if (empty($orderInfo) || empty($businessId)) {
  654. $this->error = '3016';
  655. return false;
  656. }
  657. if ($orderInfo['status'] != 3) {
  658. $this->error = '3026';
  659. return false;
  660. }
  661. if ($tradeType != 2) {
  662. $this->error = '3024';
  663. return false;
  664. }
  665. $this->model->startTrans();
  666. // 订单状态更新
  667. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update(['status' => 4, 'update_time' => time()])) {
  668. $this->model->rollBack();
  669. $this->error = '3023';
  670. return false;
  671. }
  672. // 交易处理
  673. if ($orderInfo['num'] > 0) {
  674. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username','usdt_num', 'user_type'])->first();
  675. if (empty($info)) {
  676. $this->model->rollBack();
  677. $this->error = '3019';
  678. return false;
  679. }
  680. // 商家进币
  681. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  682. $this->model->rollBack();
  683. $this->error = '3019';
  684. return false;
  685. }
  686. // 明细处理
  687. $data = [
  688. 'order_no' => $orderInfo['order_no'],
  689. 'user_id' => $businessId,
  690. 'type' => 1,
  691. 'trade_type' => 2,
  692. 'pay_type' => 1,
  693. 'change_type' => 1,
  694. 'num' => $orderInfo['num'],
  695. 'total' => $orderInfo['total'],
  696. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  697. 'create_time' => time(),
  698. 'remark' => '交易员购买广告',
  699. 'status' => 1,
  700. 'mark' => 1,
  701. ];
  702. if (!$this->capitalModel->edit($data)) {
  703. $this->error = '3014';
  704. $this->model->rollBack();
  705. return false;
  706. }
  707. }
  708. $this->model->commit();
  709. return true;
  710. }
  711. /**
  712. * 商家订单确认处理
  713. * @param $businessId 币商用户ID,平台处理时为0
  714. * @param $params 打款参数
  715. * @return false
  716. */
  717. public function businessCollection($businessId, $params)
  718. {
  719. $orderId = isset($params['id']) ? $params['id'] : 0;
  720. $catchUid = isset($params['catch_uid']) ? $params['catch_uid'] : 0;
  721. $type = isset($params['type']) ? $params['type'] : 0;
  722. if ($orderId <= 0) {
  723. $this->error = '1013';
  724. return false;
  725. }
  726. // 平台或币商处理
  727. $where = ['id' => $orderId, 'mark' => 1];
  728. if ($businessId) {
  729. $where['business_id'] = $businessId;
  730. }
  731. if ($type) {
  732. $where['type'] = $type;
  733. }
  734. $orderInfo = $this->model->where($where)
  735. ->select(['id', 'user_id', 'order_no','user_id', 'business_id', 'type', 'num', 'total','pay_time', 'status'])
  736. ->first();
  737. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  738. $orderType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  739. if (empty($orderInfo) || empty($userId)) {
  740. $this->error = '3016';
  741. return false;
  742. }
  743. $status = isset($params['status']) ? intval($params['status']) : 4;
  744. if ($status == 4 && $orderInfo['status'] != 3 && $businessId > 0) {
  745. $this->error = '3026';
  746. return false;
  747. }
  748. if ($status == 5 && $orderInfo['status'] == 8 && $businessId > 0) {
  749. $this->error = '3026';
  750. return false;
  751. }
  752. if ($status == 5 && $businessId <= 0 && $orderInfo['status'] == 4) {
  753. $this->error = '3026';
  754. return false;
  755. }
  756. if ($orderType != 1 && $status != 5) {
  757. $this->error = '3024';
  758. return false;
  759. }
  760. // 用户信息
  761. if ($businessId > 0) {
  762. $userInfo = MemberService::make()->getInfo($businessId);
  763. $userStatus = isset($userInfo['status']) ? $userInfo['status'] : 0;
  764. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  765. if ($userStatus != 1) {
  766. $this->error = '2009';
  767. return false;
  768. }
  769. // 交易密码
  770. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  771. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  772. if (empty($password)) {
  773. $this->error = '2015';
  774. return false;
  775. }
  776. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  777. $this->error = '2016';
  778. return false;
  779. }
  780. }
  781. $this->model->startTrans();
  782. // 订单状态更新,异常或确认
  783. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time()];
  784. // 异常处理数据
  785. if (isset($params['exception_type'])) {
  786. $updateData['exception_type'] = intval($params['exception_type']);
  787. }
  788. if (isset($params['exception_sub_type'])) {
  789. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  790. }
  791. if (isset($params['exception_img'])) {
  792. $updateData['exception_img'] = trim($params['exception_img']);
  793. if (strpos($updateData['exception_img'], "temp")) {
  794. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  795. } else {
  796. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  797. }
  798. }
  799. if (isset($params['exception_img1'])) {
  800. $updateData['exception_img'] = trim($params['exception_img1']);
  801. if (strpos($updateData['exception_img1'], "temp")) {
  802. $updateData['exception_img1'] = save_image($updateData['exception_img'], 'images');
  803. } else {
  804. $updateData['exception_img1'] = str_replace(IMG_URL, "", $updateData['exception_img1']);
  805. }
  806. }
  807. if (isset($params['exception_img2'])) {
  808. $updateData['exception_img2'] = trim($params['exception_img2']);
  809. if (strpos($updateData['exception_img2'], "temp")) {
  810. $updateData['exception_img2'] = save_image($updateData['exception_img2'], 'images');
  811. } else {
  812. $updateData['exception_img2'] = str_replace(IMG_URL, "", $updateData['exception_img2']);
  813. }
  814. }
  815. if (isset($params['exception_remark'])) {
  816. $updateData['exception_remark'] = trim($params['exception_remark']);
  817. }
  818. if (isset($params['exception_status'])) {
  819. $updateData['exception_status'] = intval($params['exception_status']);
  820. }
  821. if (!$this->model->where($where)->update($updateData)) {
  822. $this->model->rollBack();
  823. $this->error = '3023';
  824. return false;
  825. }
  826. // 交易处理,如果是确认状态
  827. if ($orderInfo['num'] > 0 && $status == 4 && $userId > 0) {
  828. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  829. if (empty($info)) {
  830. $this->model->rollBack();
  831. $this->error = '3019';
  832. return false;
  833. }
  834. // 客户币进账
  835. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  836. $this->model->rollBack();
  837. $this->error = '3019';
  838. return false;
  839. }
  840. // 明细处理
  841. $data = [
  842. 'order_no' => $orderInfo['order_no'],
  843. 'user_id' => $userId,
  844. 'type' => 1,
  845. 'pay_type' => 1,
  846. 'trade_type' => 2,
  847. 'change_type' => 1,
  848. 'num' => $orderInfo['num'],
  849. 'total' => $orderInfo['total'],
  850. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  851. 'create_time' => time(),
  852. 'update_time' => time(),
  853. 'remark' => '客户买入',
  854. 'status' => 1,
  855. 'mark' => 1,
  856. ];
  857. if (!$this->capitalModel->edit($data)) {
  858. $this->error = '3014';
  859. $this->model->rollBack();
  860. return false;
  861. }
  862. // 更新收款地址数据
  863. $paymentId = isset($orderInfo['payment_id']) ? $orderInfo['payment_id'] : 0;
  864. $info = $this->paymentModel->where(['id' => $paymentId, 'mark' => 1])->select(['id', 'used_at'])->first();
  865. if ($paymentId && $info) {
  866. $usedAt = isset($info['used_at']) ? $info['used_at'] : '';
  867. if ($usedAt < date('Y-m-d')) {
  868. if (!$this->paymentModel->where(['id' => $paymentId])->update(['used_num' => 1, 'used_quota' => $orderInfo['total'], 'update_time' => time()])) {
  869. $this->model->rollBack();
  870. $this->error = '3018';
  871. return false;
  872. }
  873. } else {
  874. $res1 = $this->paymentModel->where(['id' => $paymentId])->increment('used_num', 1);
  875. $res2 = $this->paymentModel->where(['id' => $paymentId])->increment('used_quota', floatval($orderInfo['total']));
  876. if (!$res1 || !$res2) {
  877. $this->model->rollBack();
  878. $this->error = '3014';
  879. return false;
  880. }
  881. }
  882. }
  883. }
  884. $this->model->commit();
  885. $this->error = $status == 5 ? 3032 : 3021;
  886. return true;
  887. }
  888. /**
  889. * 取消订单
  890. * @param $userId
  891. * @param $params
  892. * @return false
  893. */
  894. public function cancel($userId, $params)
  895. {
  896. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  897. if ($orderId <= 0) {
  898. $this->error = '1013';
  899. return false;
  900. }
  901. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])
  902. ->select(['id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  903. ->first();
  904. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  905. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  906. if (empty($orderInfo) || $businessId<=0) {
  907. $this->error = '3016';
  908. return false;
  909. }
  910. if ($orderInfo['status'] == 3) {
  911. $this->error = '3027';
  912. return false;
  913. }
  914. if ($orderInfo['status'] == 4) {
  915. $this->error = '3028';
  916. return false;
  917. }
  918. if ($orderInfo['status'] == 7) {
  919. $this->error = '3030';
  920. return false;
  921. }
  922. if (!in_array($orderInfo['status'], [1, 2])) {
  923. $this->error = '3029';
  924. return false;
  925. }
  926. $this->model->startTrans();
  927. // 订单状态更新
  928. $updateData = ['status' => 8, 'update_time' => time(), 'exception_remark' => '客户取消'];
  929. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($updateData)) {
  930. $this->model->rollBack();
  931. $this->error = '3023';
  932. return false;
  933. }
  934. // 出售订单,USDT退回
  935. if ($tradeType == 2 && $orderInfo['num']>0) {
  936. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  937. if (empty($info)) {
  938. $this->model->rollBack();
  939. $this->error = '3019';
  940. return false;
  941. }
  942. // 退还币给客户
  943. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  944. $this->model->rollBack();
  945. $this->error = '3019';
  946. return false;
  947. }
  948. // 明细处理
  949. $data = [
  950. 'order_no' => $orderInfo['order_no'],
  951. 'user_id' => $userId,
  952. 'type' => 3,
  953. 'trade_type' => 2,
  954. 'pay_type' => 1,
  955. 'change_type' => 1,
  956. 'num' => $orderInfo['num'],
  957. 'total' => $orderInfo['total'],
  958. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  959. 'create_time' => time(),
  960. 'update_time' => time(),
  961. 'remark' => '客户取消退还',
  962. 'status' => 1,
  963. 'mark' => 1,
  964. ];
  965. if (!$this->capitalModel->edit($data)) {
  966. $this->error = '3014';
  967. $this->model->rollBack();
  968. return false;
  969. }
  970. }
  971. // 买入取消
  972. else if($tradeType == 1 && $orderInfo['num']>0){
  973. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  974. if (empty($info)) {
  975. $this->model->rollBack();
  976. $this->error = '3019';
  977. return false;
  978. }
  979. // 退还币给币商
  980. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  981. $this->model->rollBack();
  982. $this->error = '3019';
  983. return false;
  984. }
  985. // 明细处理
  986. $data = [
  987. 'order_no' => $orderInfo['order_no'],
  988. 'user_id' => $businessId,
  989. 'type' => 3,
  990. 'trade_type' => 2,
  991. 'pay_type' => 1,
  992. 'change_type' => 1,
  993. 'num' => $orderInfo['num'],
  994. 'total' => $orderInfo['total'],
  995. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  996. 'create_time' => time(),
  997. 'update_time' => time(),
  998. 'remark' => '客户取消退还',
  999. 'status' => 1,
  1000. 'mark' => 1,
  1001. ];
  1002. if (!$this->capitalModel->edit($data)) {
  1003. $this->error = '3014';
  1004. $this->model->rollBack();
  1005. return false;
  1006. }
  1007. }
  1008. $this->model->commit();
  1009. return true;
  1010. }
  1011. /**
  1012. * 取消订单
  1013. * @param $businessId 币商用户,平台为0
  1014. * @param $params
  1015. * @return false
  1016. */
  1017. public function businessCancel($businessId, $params)
  1018. {
  1019. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  1020. $catchUid = isset($params['catch_uid']) ? intval($params['catch_uid']) : 0;
  1021. if ($orderId <= 0) {
  1022. $this->error = '1013';
  1023. return false;
  1024. }
  1025. $where = ['id' => $orderId, 'mark' => 1];
  1026. if ($businessId) {
  1027. $where['business_id'] = $businessId;
  1028. }
  1029. $orderInfo = $this->model->where($where)
  1030. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  1031. ->first();
  1032. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1033. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  1034. $orderBusinessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  1035. if (empty($orderInfo) || $orderBusinessId <= 0) {
  1036. $this->error = '3016';
  1037. return false;
  1038. }
  1039. if (empty($userId)) {
  1040. $this->error = '3019';
  1041. return false;
  1042. }
  1043. if ($orderInfo['status'] == 4) {
  1044. $this->error = '3028';
  1045. return false;
  1046. }
  1047. if (!in_array($orderInfo['status'], [1, 2, 3, 5, 7])) {
  1048. $this->error = '3029';
  1049. return false;
  1050. }
  1051. // 用户信息
  1052. $status = isset($params['status']) ? $params['status'] : 8;
  1053. if ($businessId > 0 && $status != 8) {
  1054. $userInfo = MemberService::make()->getInfo($businessId);
  1055. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  1056. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  1057. if ($status != 1) {
  1058. $this->error = '2009';
  1059. return false;
  1060. }
  1061. // 交易密码
  1062. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  1063. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  1064. if (empty($password)) {
  1065. $this->error = '2015';
  1066. return false;
  1067. }
  1068. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  1069. $this->error = '2016';
  1070. return false;
  1071. }
  1072. }
  1073. $this->model->startTrans();
  1074. // 订单状态更新,或异常处理
  1075. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time(), 'exception_remark' => '交易员取消'];
  1076. // 异常处理数据
  1077. if (isset($params['exception_type'])) {
  1078. $updateData['exception_type'] = intval($params['exception_type']);
  1079. }
  1080. if (isset($params['exception_sub_type'])) {
  1081. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  1082. }
  1083. if (isset($params['exception_img'])) {
  1084. $updateData['exception_img'] = trim($params['exception_img']);
  1085. if (strpos($updateData['exception_img'], "temp")) {
  1086. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  1087. } else {
  1088. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  1089. }
  1090. }
  1091. if (isset($params['exception_img1'])) {
  1092. $updateData['exception_img'] = trim($params['exception_img1']);
  1093. if (strpos($updateData['exception_img1'], "temp")) {
  1094. $updateData['exception_img1'] = save_image($updateData['exception_img'], 'images');
  1095. } else {
  1096. $updateData['exception_img1'] = str_replace(IMG_URL, "", $updateData['exception_img1']);
  1097. }
  1098. }
  1099. if (isset($params['exception_img2'])) {
  1100. $updateData['exception_img2'] = trim($params['exception_img2']);
  1101. if (strpos($updateData['exception_img2'], "temp")) {
  1102. $updateData['exception_img2'] = save_image($updateData['exception_img2'], 'images');
  1103. } else {
  1104. $updateData['exception_img2'] = str_replace(IMG_URL, "", $updateData['exception_img2']);
  1105. }
  1106. }
  1107. if (isset($params['exception_remark'])) {
  1108. $updateData['exception_remark'] = trim($params['exception_remark']);
  1109. }
  1110. if (isset($params['exception_status'])) {
  1111. $updateData['exception_status'] = intval($params['exception_status']);
  1112. }
  1113. if (isset($params['refund_status'])) {
  1114. $updateData['refund_status'] = intval($params['refund_status']);
  1115. }
  1116. if (isset($params['refund_money'])) {
  1117. $updateData['refund_money'] = intval($params['refund_money']);
  1118. }
  1119. if (!$this->model->where($where)->update($updateData)) {
  1120. $this->model->rollBack();
  1121. $this->error = '3023';
  1122. return false;
  1123. }
  1124. // 出售订单,状态已取消,USDT退回
  1125. if ($tradeType == 2 && $status == 8 && $orderInfo['num'] > 0) {
  1126. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1127. if (empty($info)) {
  1128. $this->model->rollBack();
  1129. $this->error = '3019';
  1130. return false;
  1131. }
  1132. // 退还币给客户
  1133. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1134. $this->model->rollBack();
  1135. $this->error = '3019';
  1136. return false;
  1137. }
  1138. // 明细处理
  1139. $data = [
  1140. 'order_no' => $orderInfo['order_no'],
  1141. 'user_id' => $userId,
  1142. 'type' => 3,
  1143. 'pay_type' => 1,
  1144. 'trade_type' => 2,
  1145. 'change_type' => 1,
  1146. 'num' => $orderInfo['num'],
  1147. 'total' => $orderInfo['total'],
  1148. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1149. 'create_time' => time(),
  1150. 'update_time' => time(),
  1151. 'remark' => '交易员取消买入退还',
  1152. 'status' => 1,
  1153. 'mark' => 1,
  1154. ];
  1155. if (!$this->capitalModel->edit($data)) {
  1156. $this->error = '3014';
  1157. $this->model->rollBack();
  1158. return false;
  1159. }
  1160. } // 客户买入订单取消
  1161. else if ($tradeType == 1 && $status == 8 && $orderInfo['num'] > 0) {
  1162. $info = $this->memberModel->where(['id' => $orderBusinessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1163. if (empty($info)) {
  1164. $this->model->rollBack();
  1165. $this->error = '3019';
  1166. return false;
  1167. }
  1168. // 退还币给客户
  1169. if (!$this->memberModel->where(['id' => $orderBusinessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1170. $this->model->rollBack();
  1171. $this->error = '3019';
  1172. return false;
  1173. }
  1174. // 明细处理
  1175. $data = [
  1176. 'order_no' => $orderInfo['order_no'],
  1177. 'user_id' => $orderBusinessId,
  1178. 'type' => 3,
  1179. 'trade_type' => 2,
  1180. 'pay_type' => 1,
  1181. 'change_type' => 1,
  1182. 'num' => $orderInfo['num'],
  1183. 'total' => $orderInfo['total'],
  1184. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1185. 'create_time' => time(),
  1186. 'update_time' => time(),
  1187. 'remark' => '交易员取消卖出退还',
  1188. 'status' => 1,
  1189. 'mark' => 1,
  1190. ];
  1191. if (!$this->capitalModel->edit($data)) {
  1192. $this->error = '3014';
  1193. $this->model->rollBack();
  1194. return false;
  1195. }
  1196. }
  1197. $this->model->commit();
  1198. $this->error = $status == 5 ? 3032 : 3034;
  1199. return true;
  1200. }
  1201. /**
  1202. * 获取未支付或处理的订单数
  1203. * @param $userId
  1204. * @param int $type
  1205. * @return mixed
  1206. */
  1207. public function checkOrderNoCatch($userId, $type = 1)
  1208. {
  1209. return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])
  1210. ->whereIn('status', [1, 2, 5, 7])
  1211. ->count('id');
  1212. }
  1213. /**
  1214. * 自动取消广告订单处理
  1215. * @return false
  1216. */
  1217. public function catchInvalidOrder(){
  1218. $cacheKey = "caches:adverts:cancels:";
  1219. if(RedisService::get($cacheKey.'lock')){
  1220. return false;
  1221. }
  1222. RedisService::set($cacheKey.'lock', 1, rand(3, 5));
  1223. $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  1224. $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
  1225. $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
  1226. $catchNum = $catchNum > 0 ? $catchNum : 200;
  1227. // 处理超时订单
  1228. if ($overtime > 0) {
  1229. $this->model->where(['mark' => 1])
  1230. ->where('status', '<=', 2)
  1231. ->where('create_time', '<=', time() - $overtime * 60)
  1232. ->update(['status' => 7, 'catch_at' => time()]);
  1233. }
  1234. if ($cancelTime <= 0) {
  1235. $this->error = '1023';
  1236. return false;
  1237. }
  1238. $fail = 0;
  1239. $success = 0;
  1240. $this->model->where(function ($query) use ($cancelTime) {
  1241. // 已更新为超时的订单
  1242. $query->where(['mark' => 1, 'status' => 7])
  1243. ->where('catch_at', '<=', time() - $cancelTime * 60);
  1244. })
  1245. ->orWhere(function ($query) use ($cancelTime, $overtime) {
  1246. $query->where('mark', '=', 1)
  1247. ->where('status', '<=', 2)
  1248. ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
  1249. })
  1250. ->select(['id', 'user_id', 'business_id','advert_id','order_no', 'type', 'num','total'])
  1251. ->take($catchNum)
  1252. ->get()
  1253. ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
  1254. // 客户卖出订单退还
  1255. $date = date('Y-m-d H:i:s');
  1256. $type = isset($item['type']) ? $item['type'] : 0;
  1257. if ($type == 2) {
  1258. if(!$this->orderReback($item['user_id'], $item)){
  1259. $fail++;
  1260. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1261. }else{
  1262. $success++;
  1263. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1264. }
  1265. }
  1266. else{
  1267. if(!$this->orderReback($item['business_id'], $item)){
  1268. $fail++;
  1269. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1270. }else{
  1271. $success++;
  1272. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1273. }
  1274. }
  1275. });
  1276. return ['success'=> $success,'fail'=> $fail];
  1277. }
  1278. /**
  1279. * 订单取消退还处理
  1280. * @param $userId
  1281. * @param $orderInfo
  1282. * @return bool
  1283. */
  1284. protected function orderReback($userId, $orderInfo){
  1285. try {
  1286. if($orderInfo['num']<=0){
  1287. return false;
  1288. }
  1289. $this->model->startTrans();
  1290. $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_status'=> 0,'exception_remark'=>'系统取消'];
  1291. if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
  1292. $this->model->rollBack();
  1293. $this->error = '3043';
  1294. return false;
  1295. }
  1296. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1297. if (empty($info)) {
  1298. $this->model->rollBack();
  1299. $this->error = '3019';
  1300. return false;
  1301. }
  1302. // 退还币给客户
  1303. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1304. $this->model->rollBack();
  1305. $this->error = '3019';
  1306. return false;
  1307. }
  1308. // 明细处理
  1309. $data = [
  1310. 'order_no' => $orderInfo['order_no'],
  1311. 'user_id' => $userId,
  1312. 'type' => 3,
  1313. 'pay_type' => 1,
  1314. 'trade_type' => 2,
  1315. 'change_type' => 1,
  1316. 'num' => $orderInfo['num'],
  1317. 'total' => $orderInfo['total'],
  1318. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1319. 'create_time' => time(),
  1320. 'update_time' => time(),
  1321. 'remark' => '系统自动取消退还',
  1322. 'status' => 1,
  1323. 'mark' => 1,
  1324. ];
  1325. if (!$this->capitalModel->edit($data)) {
  1326. $this->error = '3014';
  1327. $this->model->rollBack();
  1328. return false;
  1329. }
  1330. $this->model->commit();
  1331. $this->error = '3044';
  1332. return true;
  1333. } catch (\Exception $exception){
  1334. $this->error = $exception->getMessage();
  1335. return false;
  1336. }
  1337. }
  1338. }