AdvertOrderService.php 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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','b.realname', 'c.username as c_username','c.realname as c_realname'])
  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'], 'm-d 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. $usdtNum = isset($businessInfo['usdt_num'])? $businessInfo['usdt_num'] : 0;
  255. if (empty($businessInfo)) {
  256. $this->error = '3004';
  257. return false;
  258. }
  259. if($num > $usdtNum){
  260. $this->error = '4009';
  261. return false;
  262. }
  263. $setting = MemberSettingService::make()->getInfo($businessId);
  264. $advertOnline = isset($setting['advert_online'])? $setting['advert_online'] : 0;
  265. if($advertOnline != 1){
  266. $this->error = '4006';
  267. return false;
  268. }
  269. // 购买者身份信息
  270. $idcardData = [
  271. 'realname' => isset($userInfo['realname']) ? $userInfo['realname'] : '',
  272. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  273. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  274. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  275. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  276. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  277. ];
  278. // 收款方式
  279. $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
  280. if (empty($payment)) {
  281. $this->error = '3015';
  282. return false;
  283. }
  284. $this->model->startTrans();
  285. $orderNo = get_order_num('TA');
  286. $data = [
  287. 'user_id' => $userId,
  288. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  289. 'order_no' => $orderNo,
  290. 'type' => 1,
  291. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  292. 'price' => $price,
  293. 'num' => $num,
  294. 'total' => $total,
  295. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  296. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  297. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  298. 'create_time' => time(),
  299. 'update_time' => time(),
  300. 'status' => 1,
  301. 'mark' => 1,
  302. ];
  303. if (!$order = $this->model->edit($data)) {
  304. $this->model->rollBack();
  305. $this->error = '3023';
  306. return false;
  307. }
  308. if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
  309. $this->model->rollBack();
  310. $this->error = '3020';
  311. return false;
  312. }
  313. $data = [
  314. 'order_no'=> $orderNo,
  315. 'user_id'=> $businessInfo['id'],
  316. 'type'=> 2,
  317. 'pay_type'=> 1,
  318. 'trade_type'=> 2,
  319. 'change_type'=> 2,
  320. 'num'=> $num,
  321. 'total'=> $total,
  322. 'balance'=> floatval($businessInfo['usdt_num']-$num),
  323. 'create_time'=> time(),
  324. 'update_time'=> time(),
  325. 'status'=> 1,
  326. 'mark'=>1,
  327. 'remark'=> '交易员卖出',
  328. ];
  329. if(!$this->capitalModel->edit($data)){
  330. $this->model->rollBack();
  331. $this->error = '3014';
  332. return false;
  333. }
  334. // 订单通知
  335. $data = [
  336. 'from_uid' => $userId,
  337. 'to_uid' => $businessInfo['id'],
  338. 'type' => 3,
  339. 'order_no' => $orderNo,
  340. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  341. 'message' => "您有来自客户购买广告订单:{$orderNo}的消息,请尽快回复!",
  342. 'message_type' => 1,
  343. 'data_type' => 2,
  344. 'create_time' => time(),
  345. 'update_time' => time(),
  346. 'status' => 1,
  347. 'mark' => 1,
  348. ];
  349. if (!ChatMessageService::make()->pushMessage($data)) {
  350. $this->model->rollBack();
  351. $this->error = '3031';
  352. return false;
  353. }
  354. $this->model->commit();
  355. $this->error = '1002';
  356. return $order;
  357. }
  358. /**
  359. * 购买
  360. * @param $userId
  361. * @param $params
  362. * @return false|int|number
  363. */
  364. public function sell($userId, $params)
  365. {
  366. $id = isset($params['id']) ? intval($params['id']) : 0;
  367. $num = isset($params['num']) ? intval($params['num']) : 0;
  368. $paymentId = isset($params['payment_id']) ? intval($params['payment_id']) : 0;
  369. if ($id <= 0 || $num<=0 || $paymentId<=0) {
  370. $this->error = '1013';
  371. return false;
  372. }
  373. // 验证参数
  374. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  375. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  376. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_sell_price'] : 0;
  377. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  378. // 是否开启交易
  379. if ($tradeOpen != 1) {
  380. $this->error = '1013';
  381. return false;
  382. }
  383. $info = AdvertService::make()->getInfo($id);
  384. $tradeType = isset($info['type'])? $info['type'] : 0;
  385. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  386. $price = isset($info['price'])? $info['price'] : 0;
  387. $businessId = isset($info['user_id'])? $info['user_id'] : 0;
  388. if(empty($info) || $info['status'] != 1){
  389. $this->error = '4001';
  390. return false;
  391. }
  392. if($userId == $businessId){
  393. $this->error = '4008';
  394. return false;
  395. }
  396. $expiredAt = isset($info['expired_at']) && $info['expired_at']? strtotime($info['expired_at']) : 0;
  397. if($expiredAt <= time()){
  398. $this->error = '4007';
  399. return false;
  400. }
  401. if($tradeType != 1){
  402. $this->error = '4004';
  403. return false;
  404. }
  405. if ($tradePrice <= 0 && $priceType == 2) {
  406. $this->error = '3002';
  407. return false;
  408. }
  409. // 验证数量或金额
  410. if ($priceType == 2) {
  411. $price = floatval($tradePrice + $price);
  412. }
  413. // 单笔限额
  414. $total = floatval($price * $num);
  415. if($total<=0){
  416. $this->error = '4002';
  417. return false;
  418. }
  419. if($total< $info['limit_min'] || $total> $info['limit_max']){
  420. $this->error = lang('4005',['min'=> $info['limit_min'],'max'=>$info['limit_max']]);
  421. return false;
  422. }
  423. // 用户信息
  424. $userInfo = MemberService::make()->getInfo($userId);
  425. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  426. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  427. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  428. if ($status != 1) {
  429. $this->error = '2009';
  430. return false;
  431. }
  432. if($idcardCheck != 1){
  433. $this->error = '2014';
  434. return false;
  435. }
  436. $usdtNum = isset($userInfo['usdt_num'])? $userInfo['usdt_num'] : 0;
  437. if($num > $usdtNum){
  438. $this->error = '4010';
  439. return false;
  440. }
  441. // 未处理订单
  442. $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
  443. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  444. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  445. return false;
  446. }
  447. // 交易商家
  448. $businessInfo = MemberService::make()->getInfo($businessId);
  449. if (empty($businessInfo)) {
  450. $this->error = '3004';
  451. return false;
  452. }
  453. $setting = MemberSettingService::make()->getInfo($businessId);
  454. $advertOnline = isset($setting['advert_online'])? $setting['advert_online'] : 0;
  455. if($advertOnline != 1){
  456. $this->error = '4006';
  457. return false;
  458. }
  459. // 购买者身份信息
  460. $idcardData = [
  461. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  462. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  463. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  464. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  465. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  466. ];
  467. // 收款方式
  468. $paymentInfo = MemberPaymentService::make()->getInfo($paymentId);
  469. if (empty($paymentInfo)) {
  470. $this->error = '3010';
  471. return false;
  472. }
  473. $paymentData = [
  474. 'payment_id' => $paymentInfo['id'],
  475. 'type' => $paymentInfo['type'],
  476. 'logo' => $paymentInfo['logo'] ? get_image_url($paymentInfo['logo']) : '',
  477. 'real_name' => $paymentInfo['real_name'],
  478. 'bank_name' => $paymentInfo['bank_name'],
  479. 'bank_card' => $paymentInfo['bank_card'],
  480. 'branch_name' => $paymentInfo['branch_name'],
  481. 'qrcode' => $paymentInfo['qrcode'] ? get_image_url($paymentInfo['qrcode']) : '',
  482. 'account' => $paymentInfo['account'],
  483. ];
  484. $this->model->startTrans();
  485. $orderNo = get_order_num('TA');
  486. $data = [
  487. 'user_id' => $userId,
  488. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  489. 'order_no' => $orderNo,
  490. 'type' => 2,
  491. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  492. 'price' => $price,
  493. 'num' => $num,
  494. 'total' => $total,
  495. 'payment_id' => $paymentId,
  496. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  497. 'payment_data' => $paymentData ? json_encode($paymentData, 256) : '',
  498. 'create_time' => time(),
  499. 'update_time' => time(),
  500. 'status' => 1,
  501. 'mark' => 1,
  502. ];
  503. if (!$order = $this->model->edit($data)) {
  504. $this->model->rollBack();
  505. $this->error = '3023';
  506. return false;
  507. }
  508. // 扣除出售用户的USDT
  509. if(!$this->memberModel->where(['id'=> $userId])->decrement('usdt_num', $num)){
  510. $this->model->rollBack();
  511. $this->error = '3020';
  512. return false;
  513. }
  514. $data = [
  515. 'order_no'=> $orderNo,
  516. 'user_id'=> $userId,
  517. 'type'=> 2,
  518. 'pay_type'=> 1,
  519. 'trade_type'=> 2,
  520. 'change_type'=> 2,
  521. 'num'=> $num,
  522. 'total'=> $total,
  523. 'balance'=> floatval($userInfo['usdt_num']-$num),
  524. 'create_time'=> time(),
  525. 'update_time'=> time(),
  526. 'status'=> 1,
  527. 'mark'=>1,
  528. 'remark'=> '客户卖出',
  529. ];
  530. if(!$this->capitalModel->edit($data)){
  531. $this->model->rollBack();
  532. $this->error = '3014';
  533. return false;
  534. }
  535. // 订单通知
  536. $data = [
  537. 'from_uid' => $userId,
  538. 'to_uid' => $businessInfo['id'],
  539. 'type' => 3,
  540. 'order_no' => $orderNo,
  541. 'chat_key' => getChatKey($userId, $businessInfo['id']),
  542. 'message' => "您有来自客户出售广告订单:{$orderNo}的消息,请尽快回复!",
  543. 'message_type' => 1,
  544. 'data_type' => 3,
  545. 'create_time' => time(),
  546. 'update_time' => time(),
  547. 'status' => 1,
  548. 'mark' => 1,
  549. ];
  550. if (!ChatMessageService::make()->pushMessage($data)) {
  551. $this->model->rollBack();
  552. $this->error = '3031';
  553. return false;
  554. }
  555. $this->model->commit();
  556. $this->error = '1002';
  557. return $order;
  558. }
  559. /**
  560. * 订单打款处理
  561. * @param $userId 用户ID
  562. * @param $params 打款参数
  563. * @return false
  564. */
  565. public function pay($userId, $params)
  566. {
  567. $orderId = isset($params['id']) ? $params['id'] : 0;
  568. if ($orderId <= 0) {
  569. $this->error = '1013';
  570. return false;
  571. }
  572. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1, 'type' => 1])
  573. ->whereIn('status', [1, 2, 5, 7])
  574. ->select(['id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
  575. ->first();
  576. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  577. if (empty($orderInfo)) {
  578. $this->error = '3016';
  579. return false;
  580. }
  581. if ($tradeType != 1) {
  582. $this->error = '3024';
  583. return false;
  584. }
  585. // 用户信息
  586. $userInfo = MemberService::make()->getInfo($userId);
  587. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  588. if ($status != 1) {
  589. $this->error = '2009';
  590. return false;
  591. }
  592. // 交易密码
  593. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  594. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  595. if (empty($password)) {
  596. $this->error = '2015';
  597. return false;
  598. }
  599. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  600. $this->error = '2016';
  601. return false;
  602. }
  603. $data = [
  604. 'status' => 3,
  605. 'pay_type' => $params['pay_type'],
  606. 'pay_img' => $params['pay_img'],
  607. 'pay_remark' => $params['pay_remark'],
  608. 'pay_time' => time(),
  609. ];
  610. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($data)) {
  611. $this->error = '3018';
  612. return false;
  613. }
  614. return true;
  615. }
  616. /**
  617. * 商家订单打款处理
  618. * @param $userId 用户ID
  619. * @param $params 打款参数
  620. * @return false
  621. */
  622. public function businessPay($businessId, $params)
  623. {
  624. $orderId = isset($params['id']) ? $params['id'] : 0;
  625. if ($orderId <= 0) {
  626. $this->error = '1013';
  627. return false;
  628. }
  629. $orderInfo = $this->model->where(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1, 'type' => 2])
  630. ->whereIn('status', [1, 2, 5, 7])
  631. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'payment_id', 'num', 'total', 'status'])
  632. ->first();
  633. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  634. var_dump($orderInfo);
  635. if (empty($orderInfo)) {
  636. $this->error = '3016';
  637. return false;
  638. }
  639. if ($tradeType != 2) {
  640. $this->error = '3025';
  641. return false;
  642. }
  643. // 用户信息
  644. $userInfo = MemberService::make()->getInfo($businessId);
  645. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  646. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  647. if ($status != 1) {
  648. $this->error = '2009';
  649. return false;
  650. }
  651. // 交易密码
  652. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  653. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  654. if (empty($password)) {
  655. $this->error = '2015';
  656. return false;
  657. }
  658. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  659. $this->error = '2016';
  660. return false;
  661. }
  662. if (strpos($params['pay_img'], "temp")) {
  663. $params['pay_img'] = save_image($params['pay_img'], 'images');
  664. } else {
  665. $params['pay_img'] = str_replace(IMG_URL, "", $params['pay_img']);
  666. }
  667. $data = [
  668. 'status' => 3,
  669. 'pay_type' => $params['pay_type'],
  670. 'pay_img' => $params['pay_img'],
  671. 'pay_remark' => $params['pay_remark'],
  672. 'pay_time' => time(),
  673. ];
  674. if (!$this->model->where(['business_id' => $businessId, 'id' => $orderId, 'mark' => 1])->update($data)) {
  675. $this->error = '3018';
  676. return false;
  677. }
  678. $this->error = '3017';
  679. return true;
  680. }
  681. /**
  682. * 订单确认处理
  683. * @param $userId 用户ID
  684. * @param $params 打款参数
  685. * @return false
  686. */
  687. public function collection($userId, $params)
  688. {
  689. $orderId = isset($params['id']) ? $params['id'] : 0;
  690. $catchUid = isset($params['catch_uid']) ? $params['catch_uid'] : 0;
  691. if ($orderId <= 0) {
  692. $this->error = '1013';
  693. return false;
  694. }
  695. $status = isset($params['status']) ? intval($params['status']) : 4;
  696. $where = ['user_id' => $userId, 'id' => $orderId, 'mark' => 1];
  697. if($status==4){
  698. $where['type'] = 2;
  699. }
  700. $orderInfo = $this->model->where($where)
  701. ->select(['id', 'user_id', 'order_no', 'business_id', 'payment_id', 'type', 'num', 'total', 'status'])
  702. ->first();
  703. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  704. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  705. if (empty($orderInfo) || empty($businessId)) {
  706. $this->error = '3016';
  707. return false;
  708. }
  709. if ($orderInfo['status'] != 3) {
  710. $this->error = '3026';
  711. return false;
  712. }
  713. if ($status == 2 && $tradeType != 2) {
  714. $this->error = '3024';
  715. return false;
  716. }
  717. // 订单状态更新,异常或确认
  718. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time()];
  719. // 异常处理数据
  720. if (isset($params['exception_type'])) {
  721. $updateData['exception_type'] = intval($params['exception_type']);
  722. }
  723. if (isset($params['exception_sub_type'])) {
  724. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  725. }
  726. if (isset($params['exception_img'])) {
  727. $updateData['exception_img'] = trim($params['exception_img']);
  728. if (strpos($updateData['exception_img'], "temp")) {
  729. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  730. } else {
  731. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  732. }
  733. }
  734. if (isset($params['exception_img1'])) {
  735. $updateData['exception_img1'] = trim($params['exception_img1']);
  736. if (strpos($updateData['exception_img1'], "temp")) {
  737. $updateData['exception_img1'] = save_image($updateData['exception_img'], 'images');
  738. } else {
  739. $updateData['exception_img1'] = str_replace(IMG_URL, "", $updateData['exception_img1']);
  740. }
  741. }
  742. if (isset($params['exception_img2'])) {
  743. $updateData['exception_img2'] = trim($params['exception_img2']);
  744. if (strpos($updateData['exception_img2'], "temp")) {
  745. $updateData['exception_img2'] = save_image($updateData['exception_img2'], 'images');
  746. } else {
  747. $updateData['exception_img2'] = str_replace(IMG_URL, "", $updateData['exception_img2']);
  748. }
  749. }
  750. if (isset($params['exception_remark'])) {
  751. $updateData['exception_remark'] = trim($params['exception_remark']);
  752. }
  753. if (isset($params['exception_status'])) {
  754. $updateData['exception_status'] = intval($params['exception_status']);
  755. }
  756. $this->model->startTrans();
  757. // 订单状态更新
  758. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($updateData)) {
  759. $this->model->rollBack();
  760. $this->error = '3023';
  761. return false;
  762. }
  763. // 交易处理
  764. if ($orderInfo['num'] > 0 && $status == 4 && $userId > 0) {
  765. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username','usdt_num', 'user_type'])->first();
  766. if (empty($info)) {
  767. $this->model->rollBack();
  768. $this->error = '3019';
  769. return false;
  770. }
  771. // 商家进币
  772. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  773. $this->model->rollBack();
  774. $this->error = '3019';
  775. return false;
  776. }
  777. // 明细处理
  778. $data = [
  779. 'order_no' => $orderInfo['order_no'],
  780. 'user_id' => $businessId,
  781. 'type' => 1,
  782. 'trade_type' => 2,
  783. 'pay_type' => 1,
  784. 'change_type' => 1,
  785. 'num' => $orderInfo['num'],
  786. 'total' => $orderInfo['total'],
  787. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  788. 'create_time' => time(),
  789. 'remark' => '交易员购买广告',
  790. 'status' => 1,
  791. 'mark' => 1,
  792. ];
  793. if (!$this->capitalModel->edit($data)) {
  794. $this->error = '3014';
  795. $this->model->rollBack();
  796. return false;
  797. }
  798. }
  799. $this->model->commit();
  800. $this->error = $status == 5? 3032: '1002';
  801. return true;
  802. }
  803. /**
  804. * 商家订单确认处理
  805. * @param $businessId 币商用户ID,平台处理时为0
  806. * @param $params 打款参数
  807. * @return false
  808. */
  809. public function businessCollection($businessId, $params)
  810. {
  811. $orderId = isset($params['id']) ? $params['id'] : 0;
  812. $catchUid = isset($params['catch_uid']) ? $params['catch_uid'] : 0;
  813. $type = isset($params['type']) ? $params['type'] : 0;
  814. if ($orderId <= 0) {
  815. $this->error = '1013';
  816. return false;
  817. }
  818. // 平台或币商处理
  819. $where = ['id' => $orderId, 'mark' => 1];
  820. if ($businessId) {
  821. $where['business_id'] = $businessId;
  822. }
  823. if ($type) {
  824. $where['type'] = $type;
  825. }
  826. $orderInfo = $this->model->where($where)
  827. ->select(['id', 'user_id', 'order_no','user_id', 'business_id', 'type', 'num', 'total','pay_time', 'status'])
  828. ->first();
  829. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  830. $orderType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  831. if (empty($orderInfo) || empty($userId)) {
  832. $this->error = '3016';
  833. return false;
  834. }
  835. $status = isset($params['status']) ? intval($params['status']) : 4;
  836. if ($status == 4 && $orderInfo['status'] != 3 && $businessId > 0) {
  837. $this->error = '3026';
  838. return false;
  839. }
  840. if ($status == 5 && $orderInfo['status'] == 8 && $businessId > 0) {
  841. $this->error = '3026';
  842. return false;
  843. }
  844. if ($status == 5 && $businessId <= 0 && $orderInfo['status'] == 4) {
  845. $this->error = '3026';
  846. return false;
  847. }
  848. if ($orderType != 1 && $status != 5) {
  849. $this->error = '3024';
  850. return false;
  851. }
  852. // 用户信息
  853. if ($businessId > 0) {
  854. $userInfo = MemberService::make()->getInfo($businessId);
  855. $userStatus = isset($userInfo['status']) ? $userInfo['status'] : 0;
  856. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  857. if ($userStatus != 1) {
  858. $this->error = '2009';
  859. return false;
  860. }
  861. // 交易密码
  862. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  863. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  864. if (empty($password)) {
  865. $this->error = '2015';
  866. return false;
  867. }
  868. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  869. $this->error = '2016';
  870. return false;
  871. }
  872. }
  873. $this->model->startTrans();
  874. // 订单状态更新,异常或确认
  875. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time()];
  876. // 异常处理数据
  877. if (isset($params['exception_type'])) {
  878. $updateData['exception_type'] = intval($params['exception_type']);
  879. }
  880. if (isset($params['exception_sub_type'])) {
  881. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  882. }
  883. if (isset($params['exception_img'])) {
  884. $updateData['exception_img'] = trim($params['exception_img']);
  885. if (strpos($updateData['exception_img'], "temp")) {
  886. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  887. } else {
  888. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  889. }
  890. }
  891. if (isset($params['exception_img1'])) {
  892. $updateData['exception_img1'] = trim($params['exception_img1']);
  893. if (strpos($updateData['exception_img1'], "temp")) {
  894. $updateData['exception_img1'] = save_image($updateData['exception_img'], 'images');
  895. } else {
  896. $updateData['exception_img1'] = str_replace(IMG_URL, "", $updateData['exception_img1']);
  897. }
  898. }
  899. if (isset($params['exception_img2'])) {
  900. $updateData['exception_img2'] = trim($params['exception_img2']);
  901. if (strpos($updateData['exception_img2'], "temp")) {
  902. $updateData['exception_img2'] = save_image($updateData['exception_img2'], 'images');
  903. } else {
  904. $updateData['exception_img2'] = str_replace(IMG_URL, "", $updateData['exception_img2']);
  905. }
  906. }
  907. if (isset($params['exception_remark'])) {
  908. $updateData['exception_remark'] = trim($params['exception_remark']);
  909. }
  910. if (isset($params['exception_status'])) {
  911. $updateData['exception_status'] = intval($params['exception_status']);
  912. }
  913. if (!$this->model->where($where)->update($updateData)) {
  914. $this->model->rollBack();
  915. $this->error = '3023';
  916. return false;
  917. }
  918. // 交易处理,如果是确认状态
  919. if ($orderInfo['num'] > 0 && $status == 4 && $userId > 0) {
  920. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  921. if (empty($info)) {
  922. $this->model->rollBack();
  923. $this->error = '3019';
  924. return false;
  925. }
  926. // 客户币进账
  927. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  928. $this->model->rollBack();
  929. $this->error = '3019';
  930. return false;
  931. }
  932. // 明细处理
  933. $data = [
  934. 'order_no' => $orderInfo['order_no'],
  935. 'user_id' => $userId,
  936. 'type' => 1,
  937. 'pay_type' => 1,
  938. 'trade_type' => 2,
  939. 'change_type' => 1,
  940. 'num' => $orderInfo['num'],
  941. 'total' => $orderInfo['total'],
  942. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  943. 'create_time' => time(),
  944. 'update_time' => time(),
  945. 'remark' => '客户买入',
  946. 'status' => 1,
  947. 'mark' => 1,
  948. ];
  949. if (!$this->capitalModel->edit($data)) {
  950. $this->error = '3014';
  951. $this->model->rollBack();
  952. return false;
  953. }
  954. // 更新收款地址数据
  955. $paymentId = isset($orderInfo['payment_id']) ? $orderInfo['payment_id'] : 0;
  956. $info = $this->paymentModel->where(['id' => $paymentId, 'mark' => 1])->select(['id', 'used_at'])->first();
  957. if ($paymentId && $info) {
  958. $usedAt = isset($info['used_at']) ? $info['used_at'] : '';
  959. if ($usedAt < date('Y-m-d')) {
  960. if (!$this->paymentModel->where(['id' => $paymentId])->update(['used_num' => 1, 'used_quota' => $orderInfo['total'], 'update_time' => time()])) {
  961. $this->model->rollBack();
  962. $this->error = '3018';
  963. return false;
  964. }
  965. } else {
  966. $res1 = $this->paymentModel->where(['id' => $paymentId])->increment('used_num', 1);
  967. $res2 = $this->paymentModel->where(['id' => $paymentId])->increment('used_quota', floatval($orderInfo['total']));
  968. if (!$res1 || !$res2) {
  969. $this->model->rollBack();
  970. $this->error = '3014';
  971. return false;
  972. }
  973. }
  974. }
  975. }
  976. $this->model->commit();
  977. $this->error = $status == 5 ? 3032 : 3021;
  978. return true;
  979. }
  980. /**
  981. * 取消订单
  982. * @param $userId
  983. * @param $params
  984. * @return false
  985. */
  986. public function cancel($userId, $params)
  987. {
  988. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  989. if ($orderId <= 0) {
  990. $this->error = '1013';
  991. return false;
  992. }
  993. $orderInfo = $this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])
  994. ->select(['id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  995. ->first();
  996. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  997. $businessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  998. if (empty($orderInfo) || $businessId<=0) {
  999. $this->error = '3016';
  1000. return false;
  1001. }
  1002. if ($orderInfo['status'] == 3) {
  1003. $this->error = '3027';
  1004. return false;
  1005. }
  1006. if ($orderInfo['status'] == 4) {
  1007. $this->error = '3028';
  1008. return false;
  1009. }
  1010. if ($orderInfo['status'] == 7) {
  1011. $this->error = '3030';
  1012. return false;
  1013. }
  1014. if (!in_array($orderInfo['status'], [1, 2])) {
  1015. $this->error = '3029';
  1016. return false;
  1017. }
  1018. $this->model->startTrans();
  1019. // 订单状态更新
  1020. $updateData = ['status' => 8, 'update_time' => time(), 'exception_remark' => '客户取消'];
  1021. if (!$this->model->where(['user_id' => $userId, 'id' => $orderId, 'mark' => 1])->update($updateData)) {
  1022. $this->model->rollBack();
  1023. $this->error = '3023';
  1024. return false;
  1025. }
  1026. // 出售订单,USDT退回
  1027. if ($tradeType == 2 && $orderInfo['num']>0) {
  1028. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1029. if (empty($info)) {
  1030. $this->model->rollBack();
  1031. $this->error = '3019';
  1032. return false;
  1033. }
  1034. // 退还币给客户
  1035. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1036. $this->model->rollBack();
  1037. $this->error = '3019';
  1038. return false;
  1039. }
  1040. // 明细处理
  1041. $data = [
  1042. 'order_no' => $orderInfo['order_no'],
  1043. 'user_id' => $userId,
  1044. 'type' => 3,
  1045. 'trade_type' => 2,
  1046. 'pay_type' => 1,
  1047. 'change_type' => 1,
  1048. 'num' => $orderInfo['num'],
  1049. 'total' => $orderInfo['total'],
  1050. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1051. 'create_time' => time(),
  1052. 'update_time' => time(),
  1053. 'remark' => '客户取消退还',
  1054. 'status' => 1,
  1055. 'mark' => 1,
  1056. ];
  1057. if (!$this->capitalModel->edit($data)) {
  1058. $this->error = '3014';
  1059. $this->model->rollBack();
  1060. return false;
  1061. }
  1062. }
  1063. // 买入取消
  1064. else if($tradeType == 1 && $orderInfo['num']>0){
  1065. $info = $this->memberModel->where(['id' => $businessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1066. if (empty($info)) {
  1067. $this->model->rollBack();
  1068. $this->error = '3019';
  1069. return false;
  1070. }
  1071. // 退还币给币商
  1072. if (!$this->memberModel->where(['id' => $businessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1073. $this->model->rollBack();
  1074. $this->error = '3019';
  1075. return false;
  1076. }
  1077. // 明细处理
  1078. $data = [
  1079. 'order_no' => $orderInfo['order_no'],
  1080. 'user_id' => $businessId,
  1081. 'type' => 3,
  1082. 'trade_type' => 2,
  1083. 'pay_type' => 1,
  1084. 'change_type' => 1,
  1085. 'num' => $orderInfo['num'],
  1086. 'total' => $orderInfo['total'],
  1087. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1088. 'create_time' => time(),
  1089. 'update_time' => time(),
  1090. 'remark' => '客户取消退还',
  1091. 'status' => 1,
  1092. 'mark' => 1,
  1093. ];
  1094. if (!$this->capitalModel->edit($data)) {
  1095. $this->error = '3014';
  1096. $this->model->rollBack();
  1097. return false;
  1098. }
  1099. }
  1100. $this->model->commit();
  1101. $this->error = '1002';
  1102. return true;
  1103. }
  1104. /**
  1105. * 取消订单
  1106. * @param $businessId 币商用户,平台为0
  1107. * @param $params
  1108. * @return false
  1109. */
  1110. public function businessCancel($businessId, $params)
  1111. {
  1112. $orderId = isset($params['id']) ? intval($params['id']) : 0;
  1113. $catchUid = isset($params['catch_uid']) ? intval($params['catch_uid']) : 0;
  1114. if ($orderId <= 0) {
  1115. $this->error = '1013';
  1116. return false;
  1117. }
  1118. $where = ['id' => $orderId, 'mark' => 1];
  1119. if ($businessId) {
  1120. $where['business_id'] = $businessId;
  1121. }
  1122. $orderInfo = $this->model->where($where)
  1123. ->select(['id', 'user_id', 'order_no', 'business_id', 'type', 'num', 'total', 'status'])
  1124. ->first();
  1125. $tradeType = isset($orderInfo['type']) ? $orderInfo['type'] : 0;
  1126. $userId = isset($orderInfo['user_id']) ? $orderInfo['user_id'] : 0;
  1127. $orderBusinessId = isset($orderInfo['business_id']) ? $orderInfo['business_id'] : 0;
  1128. if (empty($orderInfo) || $orderBusinessId <= 0) {
  1129. $this->error = '3016';
  1130. return false;
  1131. }
  1132. if (empty($userId)) {
  1133. $this->error = '3019';
  1134. return false;
  1135. }
  1136. if ($orderInfo['status'] == 4) {
  1137. $this->error = '3028';
  1138. return false;
  1139. }
  1140. if (!in_array($orderInfo['status'], [1, 2, 3, 5, 7])) {
  1141. $this->error = '3029';
  1142. return false;
  1143. }
  1144. // 用户信息
  1145. $status = isset($params['status']) ? $params['status'] : 8;
  1146. if ($businessId > 0 && $status != 8) {
  1147. $userInfo = MemberService::make()->getInfo($businessId);
  1148. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  1149. $username = isset($userInfo['username']) ? $userInfo['username'] : 0;
  1150. if ($status != 1) {
  1151. $this->error = '2009';
  1152. return false;
  1153. }
  1154. // 交易密码
  1155. $tradePassword = isset($params['trade_password']) ? trim($params['trade_password']) : '';
  1156. $password = isset($userInfo['trade_password']) ? trim($userInfo['trade_password']) : '';
  1157. if (empty($password)) {
  1158. $this->error = '2015';
  1159. return false;
  1160. }
  1161. if (!$tradePassword || get_password($tradePassword . md5($tradePassword . 'otc')) != $password) {
  1162. $this->error = '2016';
  1163. return false;
  1164. }
  1165. }
  1166. $this->model->startTrans();
  1167. // 订单状态更新,或异常处理
  1168. $updateData = ['status' => $status, 'exception_catch_user' => $catchUid, 'update_time' => time(), 'exception_remark' => '交易员取消'];
  1169. // 异常处理数据
  1170. if (isset($params['exception_type'])) {
  1171. $updateData['exception_type'] = intval($params['exception_type']);
  1172. }
  1173. if (isset($params['exception_sub_type'])) {
  1174. $updateData['exception_sub_type'] = intval($params['exception_sub_type']);
  1175. }
  1176. if (isset($params['exception_img'])) {
  1177. $updateData['exception_img'] = trim($params['exception_img']);
  1178. if (strpos($updateData['exception_img'], "temp")) {
  1179. $updateData['exception_img'] = save_image($updateData['exception_img'], 'images');
  1180. } else {
  1181. $updateData['exception_img'] = str_replace(IMG_URL, "", $updateData['exception_img']);
  1182. }
  1183. }
  1184. if (isset($params['exception_img1'])) {
  1185. $updateData['exception_img'] = trim($params['exception_img1']);
  1186. if (strpos($updateData['exception_img1'], "temp")) {
  1187. $updateData['exception_img1'] = save_image($updateData['exception_img'], 'images');
  1188. } else {
  1189. $updateData['exception_img1'] = str_replace(IMG_URL, "", $updateData['exception_img1']);
  1190. }
  1191. }
  1192. if (isset($params['exception_img2'])) {
  1193. $updateData['exception_img2'] = trim($params['exception_img2']);
  1194. if (strpos($updateData['exception_img2'], "temp")) {
  1195. $updateData['exception_img2'] = save_image($updateData['exception_img2'], 'images');
  1196. } else {
  1197. $updateData['exception_img2'] = str_replace(IMG_URL, "", $updateData['exception_img2']);
  1198. }
  1199. }
  1200. if (isset($params['exception_remark'])) {
  1201. $updateData['exception_remark'] = trim($params['exception_remark']);
  1202. }
  1203. if (isset($params['exception_status'])) {
  1204. $updateData['exception_status'] = intval($params['exception_status']);
  1205. }
  1206. if (isset($params['refund_status'])) {
  1207. $updateData['refund_status'] = intval($params['refund_status']);
  1208. }
  1209. if (isset($params['refund_money'])) {
  1210. $updateData['refund_money'] = intval($params['refund_money']);
  1211. }
  1212. if (!$this->model->where($where)->update($updateData)) {
  1213. $this->model->rollBack();
  1214. $this->error = '3023';
  1215. return false;
  1216. }
  1217. // 出售订单,状态已取消,USDT退回
  1218. if ($tradeType == 2 && $status == 8 && $orderInfo['num'] > 0) {
  1219. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1220. if (empty($info)) {
  1221. $this->model->rollBack();
  1222. $this->error = '3019';
  1223. return false;
  1224. }
  1225. // 退还币给客户
  1226. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1227. $this->model->rollBack();
  1228. $this->error = '3019';
  1229. return false;
  1230. }
  1231. // 明细处理
  1232. $data = [
  1233. 'order_no' => $orderInfo['order_no'],
  1234. 'user_id' => $userId,
  1235. 'type' => 3,
  1236. 'pay_type' => 1,
  1237. 'trade_type' => 2,
  1238. 'change_type' => 1,
  1239. 'num' => $orderInfo['num'],
  1240. 'total' => $orderInfo['total'],
  1241. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1242. 'create_time' => time(),
  1243. 'update_time' => time(),
  1244. 'remark' => '交易员取消买入退还',
  1245. 'status' => 1,
  1246. 'mark' => 1,
  1247. ];
  1248. if (!$this->capitalModel->edit($data)) {
  1249. $this->error = '3014';
  1250. $this->model->rollBack();
  1251. return false;
  1252. }
  1253. } // 客户买入订单取消
  1254. else if ($tradeType == 1 && $status == 8 && $orderInfo['num'] > 0) {
  1255. $info = $this->memberModel->where(['id' => $orderBusinessId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1256. if (empty($info)) {
  1257. $this->model->rollBack();
  1258. $this->error = '3019';
  1259. return false;
  1260. }
  1261. // 退还币给客户
  1262. if (!$this->memberModel->where(['id' => $orderBusinessId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1263. $this->model->rollBack();
  1264. $this->error = '3019';
  1265. return false;
  1266. }
  1267. // 明细处理
  1268. $data = [
  1269. 'order_no' => $orderInfo['order_no'],
  1270. 'user_id' => $orderBusinessId,
  1271. 'type' => 3,
  1272. 'trade_type' => 2,
  1273. 'pay_type' => 1,
  1274. 'change_type' => 1,
  1275. 'num' => $orderInfo['num'],
  1276. 'total' => $orderInfo['total'],
  1277. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1278. 'create_time' => time(),
  1279. 'update_time' => time(),
  1280. 'remark' => '交易员取消卖出退还',
  1281. 'status' => 1,
  1282. 'mark' => 1,
  1283. ];
  1284. if (!$this->capitalModel->edit($data)) {
  1285. $this->error = '3014';
  1286. $this->model->rollBack();
  1287. return false;
  1288. }
  1289. }
  1290. $this->model->commit();
  1291. $this->error = $status == 5 ? 3032 : 3034;
  1292. return true;
  1293. }
  1294. /**
  1295. * 获取未支付或处理的订单数
  1296. * @param $userId
  1297. * @param int $type
  1298. * @return mixed
  1299. */
  1300. public function checkOrderNoCatch($userId, $type = 1)
  1301. {
  1302. return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])
  1303. ->whereIn('status', [1, 2, 5, 7])
  1304. ->count('id');
  1305. }
  1306. /**
  1307. * 自动取消广告订单处理
  1308. * @return false
  1309. */
  1310. public function catchInvalidOrder(){
  1311. $cacheKey = "caches:adverts:cancels:";
  1312. if(RedisService::get($cacheKey.'lock')){
  1313. return false;
  1314. }
  1315. RedisService::set($cacheKey.'lock', 1, rand(3, 5));
  1316. $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
  1317. $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
  1318. $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
  1319. $catchNum = $catchNum > 0 ? $catchNum : 200;
  1320. // 处理超时订单
  1321. if ($overtime > 0) {
  1322. $this->model->where(['mark' => 1])
  1323. ->where('status', '<=', 2)
  1324. ->where('create_time', '<=', time() - $overtime * 60)
  1325. ->update(['status' => 7, 'catch_at' => time()]);
  1326. }
  1327. if ($cancelTime <= 0) {
  1328. $this->error = '1023';
  1329. return false;
  1330. }
  1331. $fail = 0;
  1332. $success = 0;
  1333. $this->model->where(function ($query) use ($cancelTime) {
  1334. // 已更新为超时的订单
  1335. $query->where(['mark' => 1, 'status' => 7])
  1336. ->where('catch_at', '<=', time() - $cancelTime * 60);
  1337. })
  1338. ->orWhere(function ($query) use ($cancelTime, $overtime) {
  1339. $query->where('mark', '=', 1)
  1340. ->where('status', '<=', 2)
  1341. ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
  1342. })
  1343. ->select(['id', 'user_id', 'business_id','advert_id','order_no', 'type', 'num','total'])
  1344. ->take($catchNum)
  1345. ->get()
  1346. ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
  1347. // 客户卖出订单退还
  1348. $date = date('Y-m-d H:i:s');
  1349. $type = isset($item['type']) ? $item['type'] : 0;
  1350. if ($type == 2) {
  1351. if(!$this->orderReback($item['user_id'], $item)){
  1352. $fail++;
  1353. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1354. }else{
  1355. $success++;
  1356. RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1357. }
  1358. }
  1359. else{
  1360. if(!$this->orderReback($item['business_id'], $item)){
  1361. $fail++;
  1362. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1363. }else{
  1364. $success++;
  1365. RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
  1366. }
  1367. }
  1368. });
  1369. return ['success'=> $success,'fail'=> $fail];
  1370. }
  1371. /**
  1372. * 订单取消退还处理
  1373. * @param $userId
  1374. * @param $orderInfo
  1375. * @return bool
  1376. */
  1377. protected function orderReback($userId, $orderInfo){
  1378. try {
  1379. if($orderInfo['num']<=0){
  1380. return false;
  1381. }
  1382. $this->model->startTrans();
  1383. $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_status'=> 0,'exception_remark'=>'系统取消'];
  1384. if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
  1385. $this->model->rollBack();
  1386. $this->error = '3043';
  1387. return false;
  1388. }
  1389. $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
  1390. if (empty($info)) {
  1391. $this->model->rollBack();
  1392. $this->error = '3019';
  1393. return false;
  1394. }
  1395. // 退还币给客户
  1396. if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
  1397. $this->model->rollBack();
  1398. $this->error = '3019';
  1399. return false;
  1400. }
  1401. // 明细处理
  1402. $data = [
  1403. 'order_no' => $orderInfo['order_no'],
  1404. 'user_id' => $userId,
  1405. 'type' => 3,
  1406. 'pay_type' => 1,
  1407. 'trade_type' => 2,
  1408. 'change_type' => 1,
  1409. 'num' => $orderInfo['num'],
  1410. 'total' => $orderInfo['total'],
  1411. 'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
  1412. 'create_time' => time(),
  1413. 'update_time' => time(),
  1414. 'remark' => '系统自动取消退还',
  1415. 'status' => 1,
  1416. 'mark' => 1,
  1417. ];
  1418. if (!$this->capitalModel->edit($data)) {
  1419. $this->error = '3014';
  1420. $this->model->rollBack();
  1421. return false;
  1422. }
  1423. $this->model->commit();
  1424. $this->error = '3044';
  1425. return true;
  1426. } catch (\Exception $exception){
  1427. $this->error = $exception->getMessage();
  1428. return false;
  1429. }
  1430. }
  1431. }