AdvertOrderService.php 60 KB

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