| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\AdvertModel;
- use App\Models\AdvertOrderModel;
- use App\Services\Api\MemberPaymentService;
- use App\Services\BaseService;
- /**
- * 用户广告订单-服务类
- * Class AdvertOrderService
- * @package App\Services\Common
- */
- class AdvertOrderService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @since 2020/11/10
- * LoginService constructor.
- */
- public function __construct()
- {
- $this->model = new AdvertOrderModel();
- $this->advertModel = new AdvertModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 购买
- * @param $userId
- * @param $params
- * @return false|int|number
- */
- public function buy($userId, $params)
- {
- $id = isset($params['id']) ? intval($params['id']) : 0;
- $num = isset($params['num']) ? intval($params['num']) : 0;
- if ($id <= 0 || $num<=0) {
- $this->error = '1013';
- return false;
- }
- // 验证参数
- $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
- $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
- $tradeMinNum = isset($config['trade_min_num']) ? $config['trade_min_num'] : 0;
- $tradeMaxNum = isset($config['trade_max_num']) ? $config['trade_max_num'] : 0;
- $trademinMoney = isset($config['trade_min_money']) ? $config['trade_min_money'] : 0;
- $tradeMaxMoney = isset($config['trade_max_money']) ? $config['trade_max_money'] : 0;
- $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_buy_price'] : 0;
- $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
- // 是否开启交易
- if ($tradeOpen != 1) {
- $this->error = '1013';
- return false;
- }
- $info = AdvertService::make()->getInfo($id);
- $tradeType = isset($info['type'])? $info['type'] : 0;
- $priceType = isset($info['price_type'])? $info['price_type'] : 0;
- $price = isset($info['price'])? $info['price'] : 0;
- $businessId = isset($info['business_id'])? $info['business_id'] : 0;
- if(empty($info) || $info['status'] != 1){
- $this->error = '4001';
- return false;
- }
- if ($tradePrice <= 0 && $priceType == 2) {
- $this->error = '3002';
- return false;
- }
- // 验证数量或金额
- if ($priceType == 2) {
- $price = floatval($tradePrice + $price);
- }
- $total = floatval($price * $num);
- // 用户信息
- $userInfo = MemberService::make()->getInfo($userId);
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
- $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
- if ($status != 1) {
- $this->error = '2009';
- return false;
- }
- // 未处理订单
- $noCatchOrder = $this->checkOrderNoCatch($userId, 1);
- if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
- $this->error = lang(3005, ['num' => $tradeLimitNum]);
- return false;
- }
- // 交易商家
- $businessInfo = MemberService::make()->getInfo($businessId);
- if (empty($businessInfo)) {
- $this->error = '3004';
- return false;
- }
- // 购买者身份信息
- $idcardData = [
- 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
- 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
- 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
- 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
- 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
- ];
- // 收款方式
- $payment = MemberPaymentService::make()->getPayment($businessInfo['id']);
- if (empty($payment)) {
- $this->error = '3015';
- return false;
- }
- $this->model->startTrans();
- $orderNo = get_order_num('OT');
- $data = [
- 'user_id' => $userId,
- 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
- 'order_no' => $orderNo,
- 'type' => 1,
- 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
- 'price' => $price,
- 'num' => $num,
- 'total' => $total,
- 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
- 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
- 'payment_data' => $payment ? json_encode($payment, 256) : '',
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$order = $this->model->edit($data)) {
- $this->model->rollBack();
- $this->error = '3023';
- return false;
- }
- if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
- $this->model->rollBack();
- $this->error = '3020';
- return false;
- }
- $data = [
- 'order_no'=> $orderNo,
- 'user_id'=> $businessInfo['id'],
- 'type'=> 2,
- 'pay_type'=> 1,
- 'trade_type'=> 2,
- 'change_type'=> 2,
- 'num'=> $num,
- 'total'=> $total,
- 'balance'=> floatval($businessInfo['usdt_num']-$num),
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'status'=> 1,
- 'mark'=>1,
- 'remark'=> '交易员卖出',
- ];
- if(!$this->capitalModel->edit($data)){
- $this->model->rollBack();
- $this->error = '3014';
- return false;
- }
- // 订单通知
- $data = [
- 'from_uid' => $userId,
- 'to_uid' => $businessInfo['id'],
- 'type' => 2,
- 'order_no' => $orderNo,
- 'chat_key' => ChatMessageService::make()->getChatKey($userId, $businessInfo['id']),
- 'message' => "来自用户{$username}的购买订单,金额{$total},单号{$orderNo},时间" . date('Y-m-d H:i:s'),
- 'message_type' => 1,
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!ChatMessageService::make()->pushMessage($data)) {
- $this->model->rollBack();
- $this->error = '3031';
- return false;
- }
- $this->model->commit();
- return $order;
- }
- /**
- * 购买
- * @param $userId
- * @param $params
- * @return false|int|number
- */
- public function sell($userId, $params)
- {
- $id = isset($params['id']) ? intval($params['id']) : 0;
- $num = isset($params['num']) ? intval($params['num']) : 0;
- if ($id <= 0 || $num<=0) {
- $this->error = '1013';
- return false;
- }
- // 验证参数
- $config = \App\Services\ConfigService::make()->getConfigOptionByGroup(5);
- $tradeOpen = isset($config['trade_usdt_open']) ? $config['trade_usdt_open'] : 0;
- $tradeMinNum = isset($config['trade_min_num']) ? $config['trade_min_num'] : 0;
- $tradeMaxNum = isset($config['trade_max_num']) ? $config['trade_max_num'] : 0;
- $trademinMoney = isset($config['trade_min_money']) ? $config['trade_min_money'] : 0;
- $tradeMaxMoney = isset($config['trade_max_money']) ? $config['trade_max_money'] : 0;
- $tradePrice = isset($config['usdt_buy_price']) ? $config['usdt_sell_price'] : 0;
- $tradeLimitNum = isset($config['trade_no_catch']) ? $config['trade_no_catch'] : 0;
- // 是否开启交易
- if ($tradeOpen != 1) {
- $this->error = '1013';
- return false;
- }
- $info = AdvertService::make()->getInfo($id);
- $tradeType = isset($info['type'])? $info['type'] : 0;
- $priceType = isset($info['price_type'])? $info['price_type'] : 0;
- $price = isset($info['price'])? $info['price'] : 0;
- $businessId = isset($info['business_id'])? $info['business_id'] : 0;
- if(empty($info) || $info['status'] != 1){
- $this->error = '4001';
- return false;
- }
- if ($tradePrice <= 0 && $priceType == 2) {
- $this->error = '3002';
- return false;
- }
- // 验证数量或金额
- if ($priceType == 2) {
- $price = floatval($tradePrice + $price);
- }
- $total = floatval($price * $num);
- // 用户信息
- $userInfo = MemberService::make()->getInfo($userId);
- $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
- $idcardCheck = isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0;
- $username = isset($userInfo['username']) && $userInfo['username'] ? format_account($userInfo['username']) : '';
- if ($status != 1) {
- $this->error = '2009';
- return false;
- }
- // 未处理订单
- $noCatchOrder = $this->checkOrderNoCatch($userId, 2);
- if ($tradeLimitNum > 0 && $noCatchOrder >= $tradeLimitNum) {
- $this->error = lang(3005, ['num' => $tradeLimitNum]);
- return false;
- }
- // 交易商家
- $businessInfo = MemberService::make()->getInfo($businessId);
- if (empty($businessInfo)) {
- $this->error = '3004';
- return false;
- }
- // 购买者身份信息
- $idcardData = [
- 'idcard' => isset($userInfo['idcard']) ? $userInfo['idcard'] : '',
- 'idcard_check' => isset($userInfo['idcard_check']) ? $userInfo['idcard_check'] : 0,
- 'idcard_front_img' => isset($userInfo['idcard_front_img']) ? $userInfo['idcard_front_img'] : '',
- 'idcard_back_img' => isset($userInfo['idcard_back_img']) ? $userInfo['idcard_back_img'] : '',
- 'idcard_hand_img' => isset($userInfo['idcard_hand_img']) ? $userInfo['idcard_hand_img'] : '',
- ];
- // 收款方式
- $payment = MemberPaymentService::make()->getPayment($userId);
- if (empty($payment)) {
- $this->error = '3015';
- return false;
- }
- $this->model->startTrans();
- $orderNo = get_order_num('OT');
- $data = [
- 'user_id' => $userId,
- 'business_id' => isset($businessInfo['id']) ? $businessInfo['id'] : 0,
- 'order_no' => $orderNo,
- 'type' => 1,
- 'pay_type' => isset($params['pay_type']) ? floatval($params['pay_type']) : 1,
- 'price' => $price,
- 'num' => $num,
- 'total' => $total,
- 'payment_id' => isset($payment['id']) ? intval($payment['id']) : 0,
- 'idcard_data' => $idcardData ? json_encode($idcardData, 256) : '',
- 'payment_data' => $payment ? json_encode($payment, 256) : '',
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!$order = $this->model->edit($data)) {
- $this->model->rollBack();
- $this->error = '3023';
- return false;
- }
- if(!$this->memberModel->where(['id'=> $businessInfo['id']])->decrement('usdt_num', $num)){
- $this->model->rollBack();
- $this->error = '3020';
- return false;
- }
- $data = [
- 'order_no'=> $orderNo,
- 'user_id'=> $businessInfo['id'],
- 'type'=> 2,
- 'pay_type'=> 1,
- 'trade_type'=> 2,
- 'change_type'=> 2,
- 'num'=> $num,
- 'total'=> $total,
- 'balance'=> floatval($businessInfo['usdt_num']-$num),
- 'create_time'=> time(),
- 'update_time'=> time(),
- 'status'=> 1,
- 'mark'=>1,
- 'remark'=> '交易员卖出',
- ];
- if(!$this->capitalModel->edit($data)){
- $this->model->rollBack();
- $this->error = '3014';
- return false;
- }
- // 订单通知
- $data = [
- 'from_uid' => $userId,
- 'to_uid' => $businessInfo['id'],
- 'type' => 2,
- 'order_no' => $orderNo,
- 'chat_key' => ChatMessageService::make()->getChatKey($userId, $businessInfo['id']),
- 'message' => "来自用户{$username}的购买订单,金额{$total},单号{$orderNo},时间" . date('Y-m-d H:i:s'),
- 'message_type' => 1,
- 'create_time' => time(),
- 'update_time' => time(),
- 'status' => 1,
- 'mark' => 1,
- ];
- if (!ChatMessageService::make()->pushMessage($data)) {
- $this->model->rollBack();
- $this->error = '3031';
- return false;
- }
- $this->model->commit();
- return $order;
- }
- /**
- * 获取未支付或处理的订单数
- * @param $userId
- * @param int $type
- * @return mixed
- */
- public function checkOrderNoCatch($userId, $type = 1)
- {
- return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])->whereIn('status', [1, 2, 7])->count('id');
- }
- }
|