AdvertOrderService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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\Services\Api\MemberPaymentService;
  15. use App\Services\BaseService;
  16. /**
  17. * 用户广告订单-服务类
  18. * Class AdvertOrderService
  19. * @package App\Services\Common
  20. */
  21. class AdvertOrderService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @since 2020/11/10
  28. * LoginService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new AdvertOrderModel();
  33. $this->advertModel = new AdvertModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 购买
  48. * @param $userId
  49. * @param $params
  50. * @return false|int|number
  51. */
  52. public function buy($userId, $params)
  53. {
  54. $id = isset($params['id']) ? intval($params['id']) : 0;
  55. $num = isset($params['num']) ? intval($params['num']) : 0;
  56. if ($id <= 0 || $num<=0) {
  57. $this->error = '1013';
  58. return false;
  59. }
  60. // 验证参数
  61. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  62. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  63. $tradeMinNum = isset($config['trade_min_num']) ? $config['trade_min_num'] : 0;
  64. $tradeMaxNum = isset($config['trade_max_num']) ? $config['trade_max_num'] : 0;
  65. $trademinMoney = isset($config['trade_min_money']) ? $config['trade_min_money'] : 0;
  66. $tradeMaxMoney = isset($config['trade_max_money']) ? $config['trade_max_money'] : 0;
  67. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_buy_price'] : 0;
  68. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  69. // 是否开启交易
  70. if ($tradeOpen != 1) {
  71. $this->error = '1013';
  72. return false;
  73. }
  74. $info = AdvertService::make()->getInfo($id);
  75. $tradeType = isset($info['type'])? $info['type'] : 0;
  76. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  77. $price = isset($info['price'])? $info['price'] : 0;
  78. $businessId = isset($info['business_id'])? $info['business_id'] : 0;
  79. if(empty($info) || $info['status'] != 1){
  80. $this->error = '4001';
  81. return false;
  82. }
  83. if ($tradePrice <= 0 && $priceType == 2) {
  84. $this->error = '3002';
  85. return false;
  86. }
  87. // 验证数量或金额
  88. if ($priceType == 2) {
  89. $price = floatval($tradePrice + $price);
  90. }
  91. $total = floatval($price * $num);
  92. // 用户信息
  93. $userInfo = MemberService::make()->getInfo($userId);
  94. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  95. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  96. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  97. if ($status != 1) {
  98. $this->error = '2009';
  99. return false;
  100. }
  101. // 未处理订单
  102. $noCatchOrder = $this->checkOrderNoCatch($userId, 1);
  103. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  104. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  105. return false;
  106. }
  107. // 交易商家
  108. $businessInfo = MemberService::make()->getInfo($businessId);
  109. if (empty($businessInfo)) {
  110. $this->error = '3004';
  111. return false;
  112. }
  113. // 购买者身份信息
  114. $idcardData = [
  115. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  116. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  117. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  118. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  119. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  120. ];
  121. // 收款方式
  122. $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
  123. if (empty($payment)) {
  124. $this->error = '3015';
  125. return false;
  126. }
  127. $this->model->startTrans();
  128. $orderNo = get_order_num('OT');
  129. $data = [
  130. 'user_id' => $userId,
  131. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  132. 'order_no' => $orderNo,
  133. 'type' => 1,
  134. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  135. 'price' => $price,
  136. 'num' => $num,
  137. 'total' => $total,
  138. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  139. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  140. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  141. 'create_time' => time(),
  142. 'update_time' => time(),
  143. 'status' => 1,
  144. 'mark' => 1,
  145. ];
  146. if (!$order = $this->model->edit($data)) {
  147. $this->model->rollBack();
  148. $this->error = '3023';
  149. return false;
  150. }
  151. if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
  152. $this->model->rollBack();
  153. $this->error = '3020';
  154. return false;
  155. }
  156. $data = [
  157. 'order_no'=> $orderNo,
  158. 'user_id'=> $businessInfo['id'],
  159. 'type'=> 2,
  160. 'pay_type'=> 1,
  161. 'trade_type'=> 2,
  162. 'change_type'=> 2,
  163. 'num'=> $num,
  164. 'total'=> $total,
  165. 'balance'=> floatval($businessInfo['usdt_num']-$num),
  166. 'create_time'=> time(),
  167. 'update_time'=> time(),
  168. 'status'=> 1,
  169. 'mark'=>1,
  170. 'remark'=> '交易员卖出',
  171. ];
  172. if(!$this->capitalModel->edit($data)){
  173. $this->model->rollBack();
  174. $this->error = '3014';
  175. return false;
  176. }
  177. // 订单通知
  178. $data = [
  179. 'from_uid' => $userId,
  180. 'to_uid' => $businessInfo['id'],
  181. 'type' => 2,
  182. 'order_no' => $orderNo,
  183. 'chat_key' => ChatMessageService::make()->getChatKey($userId, $businessInfo['id']),
  184. 'message' => "来自用户{$username}的购买订单,金额{$total},单号{$orderNo},时间" . date('Y-m-d H:i:s'),
  185. 'message_type' => 1,
  186. 'create_time' => time(),
  187. 'update_time' => time(),
  188. 'status' => 1,
  189. 'mark' => 1,
  190. ];
  191. if (!ChatMessageService::make()->pushMessage($data)) {
  192. $this->model->rollBack();
  193. $this->error = '3031';
  194. return false;
  195. }
  196. $this->model->commit();
  197. return $order;
  198. }
  199. /**
  200. * 购买
  201. * @param $userId
  202. * @param $params
  203. * @return false|int|number
  204. */
  205. public function sell($userId, $params)
  206. {
  207. $id = isset($params['id']) ? intval($params['id']) : 0;
  208. $num = isset($params['num']) ? intval($params['num']) : 0;
  209. if ($id <= 0 || $num<=0) {
  210. $this->error = '1013';
  211. return false;
  212. }
  213. // 验证参数
  214. $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
  215. $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
  216. $tradeMinNum = isset($config['trade_min_num']) ? $config['trade_min_num'] : 0;
  217. $tradeMaxNum = isset($config['trade_max_num']) ? $config['trade_max_num'] : 0;
  218. $trademinMoney = isset($config['trade_min_money']) ? $config['trade_min_money'] : 0;
  219. $tradeMaxMoney = isset($config['trade_max_money']) ? $config['trade_max_money'] : 0;
  220. $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_sell_price'] : 0;
  221. $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
  222. // 是否开启交易
  223. if ($tradeOpen != 1) {
  224. $this->error = '1013';
  225. return false;
  226. }
  227. $info = AdvertService::make()->getInfo($id);
  228. $tradeType = isset($info['type'])? $info['type'] : 0;
  229. $priceType = isset($info['price_type'])? $info['price_type'] : 0;
  230. $price = isset($info['price'])? $info['price'] : 0;
  231. $businessId = isset($info['business_id'])? $info['business_id'] : 0;
  232. if(empty($info) || $info['status'] != 1){
  233. $this->error = '4001';
  234. return false;
  235. }
  236. if ($tradePrice <= 0 && $priceType == 2) {
  237. $this->error = '3002';
  238. return false;
  239. }
  240. // 验证数量或金额
  241. if ($priceType == 2) {
  242. $price = floatval($tradePrice + $price);
  243. }
  244. $total = floatval($price * $num);
  245. // 用户信息
  246. $userInfo = MemberService::make()->getInfo($userId);
  247. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  248. $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
  249. $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
  250. if ($status != 1) {
  251. $this->error = '2009';
  252. return false;
  253. }
  254. // 未处理订单
  255. $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
  256. if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
  257. $this->error = lang(3005, ['num' => $tradeLimitNum]);
  258. return false;
  259. }
  260. // 交易商家
  261. $businessInfo = MemberService::make()->getInfo($businessId);
  262. if (empty($businessInfo)) {
  263. $this->error = '3004';
  264. return false;
  265. }
  266. // 购买者身份信息
  267. $idcardData = [
  268. 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
  269. 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
  270. 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
  271. 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
  272. 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
  273. ];
  274. // 收款方式
  275. $payment = MemberPaymentService::make()->getPayment($userId);
  276. if (empty($payment)) {
  277. $this->error = '3015';
  278. return false;
  279. }
  280. $this->model->startTrans();
  281. $orderNo = get_order_num('OT');
  282. $data = [
  283. 'user_id' => $userId,
  284. 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
  285. 'order_no' => $orderNo,
  286. 'type' => 1,
  287. 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
  288. 'price' => $price,
  289. 'num' => $num,
  290. 'total' => $total,
  291. 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
  292. 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
  293. 'payment_data' => $payment ? json_encode($payment, 256) : '',
  294. 'create_time' => time(),
  295. 'update_time' => time(),
  296. 'status' => 1,
  297. 'mark' => 1,
  298. ];
  299. if (!$order = $this->model->edit($data)) {
  300. $this->model->rollBack();
  301. $this->error = '3023';
  302. return false;
  303. }
  304. if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
  305. $this->model->rollBack();
  306. $this->error = '3020';
  307. return false;
  308. }
  309. $data = [
  310. 'order_no'=> $orderNo,
  311. 'user_id'=> $businessInfo['id'],
  312. 'type'=> 2,
  313. 'pay_type'=> 1,
  314. 'trade_type'=> 2,
  315. 'change_type'=> 2,
  316. 'num'=> $num,
  317. 'total'=> $total,
  318. 'balance'=> floatval($businessInfo['usdt_num']-$num),
  319. 'create_time'=> time(),
  320. 'update_time'=> time(),
  321. 'status'=> 1,
  322. 'mark'=>1,
  323. 'remark'=> '交易员卖出',
  324. ];
  325. if(!$this->capitalModel->edit($data)){
  326. $this->model->rollBack();
  327. $this->error = '3014';
  328. return false;
  329. }
  330. // 订单通知
  331. $data = [
  332. 'from_uid' => $userId,
  333. 'to_uid' => $businessInfo['id'],
  334. 'type' => 2,
  335. 'order_no' => $orderNo,
  336. 'chat_key' => ChatMessageService::make()->getChatKey($userId, $businessInfo['id']),
  337. 'message' => "来自用户{$username}的购买订单,金额{$total},单号{$orderNo},时间" . date('Y-m-d H:i:s'),
  338. 'message_type' => 1,
  339. 'create_time' => time(),
  340. 'update_time' => time(),
  341. 'status' => 1,
  342. 'mark' => 1,
  343. ];
  344. if (!ChatMessageService::make()->pushMessage($data)) {
  345. $this->model->rollBack();
  346. $this->error = '3031';
  347. return false;
  348. }
  349. $this->model->commit();
  350. return $order;
  351. }
  352. /**
  353. * 获取未支付或处理的订单数
  354. * @param $userId
  355. * @param int $type
  356. * @return mixed
  357. */
  358. public function checkOrderNoCatch($userId, $type = 1)
  359. {
  360. return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])->whereIn('status', [1, 2, 7])->count('id');
  361. }
  362. }