Prechádzať zdrojové kódy

wesmiler 报恩寺项目提交

wesmiler 4 rokov pred
rodič
commit
2ad9092ef7

+ 1 - 0
app/Services/CardService.php

@@ -176,6 +176,7 @@ class CardService extends BaseService
             return message("用户卡额度更新失败", false);
         }
 
+        // 卡入款
         $params = [
             'CommandType'=> 3, // 执行命令方式:3-充值,4-扣款
             'data'=> $cardInfo->code.','.$num

+ 106 - 17
app/Services/GongdengOrderService.php

@@ -11,6 +11,8 @@
 
 namespace App\Services;
 
+use App\Models\CardModel;
+use App\Models\DynamicNoticeModel;
 use App\Models\GongdengFoxiangModel;
 use App\Models\GongdengOrderModel;
 use App\Models\LampMealsModel;
@@ -345,7 +347,7 @@ class GongdengOrderService extends BaseService
 
         // 创建订单
         $expireTime = ($mealInfo->num * 24 * 3600) + time();
-        $data = [
+        $order = [
             'source_id' => $fid,
             'user_id' => $userId,
             'pay_type' => $payType,
@@ -368,33 +370,120 @@ class GongdengOrderService extends BaseService
         }
 
         // 订单
-        if (!$oid = GongdengOrderModel::insertGetId($data)) {
+        if (!$oid = GongdengOrderModel::insertGetId($order)) {
             return message('供灯订单创建失败', false);
         }
 
-        // 卡支付调用
-        $data = [
-            'CommandType'=> 'OpenLed', // 执行命令方式:OpenLed-开灯,CloseLed-关灯,QuitStatus-查询状态
-            'OrderNo'=> $data['order_sn'],
-            'total'=> $data['total'],
-            'body'=> '供灯订单',
-            'notify_url'=> url()->formatRoot('http://').'/api/notify/pay/cardGd'
+        $cardInfo = CardModel::where(['user_id'=> $userId,'mark'=> 1,'status'=> 1])->first();
+        if(!$cardInfo) {
+            return message('卡状态无法操作', false);
+        }
+
+        if($cardInfo->quota < $order['total']){
+            return message('当前卡额度不足,请先到开卡管理菜单处充值', false);
+        }
+
+        $params = [
+            'CommandType'=> 4, // 执行命令方式:3-充值,4-扣款
+            'data'=> $cardInfo->code.','.$order['total']
         ];
         $dataStr = [];
-        foreach($data as $k => $v){
+        foreach($params as $k => $v){
             $dataStr[] = $k.'='.$v;
         }
 
-        // 卡支付供灯调用
-        $errorKey = "caches:gongdeng:{$userId}_{$fid}_{$mid}";
+        // 调用亮灯接口处理
+        $errorKey = "caches:cards:payGongdeng:{$userId}";
         $dataStr = implode('&', $dataStr);
         $headers = ["Content-Type:application/x-www-form-urlencoded; charset=UTF-8"];
-        $apiUrl = ConfigService::make()->getConfigByCode('gd_api_url').'/LEDControl.ashx';
-        RedisService::set($errorKey.':request', ['url'=> $apiUrl,'data'=> $data], 3600);
-        $result = httpHeaderRequest($apiUrl, $dataStr,'post',$headers,'text', 5);
-        RedisService::set($errorKey.':result', $result, 3600);
+        $apiUrl = ConfigService::make()->getConfigByCode('gd_api_url').'/ICcontrol.ashx';
+        RedisService::set($errorKey.':request', ['url'=> $apiUrl,'data'=> $params], 3600);
+        $result = httpHeaderRequest($apiUrl, $dataStr,'post',$headers,'text',3);
+        RedisService::set($errorKey.':result', ['result'=>$result], 3600);
+        if($result !== 0){
+            return message('卡扣款失败', false);
+        }
 
-        return message('支付处理失败', false);
+        \DB::beginTransaction();
+        if(!CardModel::where(['user_id'=> $userId,'mark'=> 1])->decrement('quota',$order['total'])){
+            \DB::rollBack();
+            return message('卡扣款处理失败', false);
+        }
+
+        // 处理支付明细
+        $data = [
+            'user_id'=> $userId,
+            'type'=> 1,
+            'coin_type'=> 5,
+            'pay_type'=> 4,
+            'money'=> $order['total'],
+            'change_type'=> 2,
+            'balance'=> $cardInfo->quota,
+            'create_time'=> time(),
+            'remark'=> '供灯订单卡支付',
+            'status'=> 1
+        ];
+
+        if(!TradeModel::insertGetId($data)){
+            \DB::rollBack();
+            return message('处理交易明细失败', false);
+        }
+
+        // 项目完成
+        $giveGd = ConfigService::make()->getConfigByCode('gongdeng_give_gd');
+        $giveGd = $giveGd? $giveGd : 0;
+        if($giveGd>0){
+            $memberInfo = MemberModel::where(['id' => $userId, 'mark' => 1, 'status' => 1])
+                ->select('id', 'openid', 'nickname','merits_num')
+                ->first();
+
+            if(!MemberModel::where(['id'=> $userId])->increment('merits_num', $giveGd)){
+                \DB::rollBack();
+                return message('奖励功德失败', false);
+            }
+
+            $data = [
+                'user_id'=> $userId,
+                'type'=> 3,
+                'coin_type'=> 4,
+                'pay_type'=> 4,
+                'money'=> $giveGd,
+                'change_type'=> 1,
+                'balance'=> $memberInfo->merits_num? $memberInfo->merits_num : 0,
+                'create_time'=> time(),
+                'remark'=> '供灯奖励',
+                'status'=> 1
+            ];
+            if(!TradeModel::insertGetId($data)){
+                \DB::rollBack();
+                return message('功德奖励处理失败', false);
+            }
+
+            // 完成供灯项目
+            PracticesService::make()->saveLog($userId, 4,'供灯祈福');
+        }
+
+        \DB::commit();
+
+        // 记录
+        $data = [
+            'user_id'=> $userId,
+            'remark'=> "供灯祈福,福报绵延。",
+            'create_time'=> time(),
+            'update_time'=> time(),
+            'status'=> 1,
+        ];
+        RedisService::set("caches:dynamic:notice_{$userId}", $data, 300);
+        DynamicNoticeModel::insertGetId($data);
+
+
+        // 开灯处理,续费不处理
+        if($buyType == 1){
+            $order['id'] = $oid;
+            DevicesService::make()->catchLamp($order[''], $order);
+        }
+
+        return message('供灯订单支付成功', false);
     }