AdvertOrderService.php 59 KB

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