Quellcode durchsuchen

add - 物业分成收益

刘观赐 vor 1 Jahr
Ursprung
Commit
a11cbfbd0b

+ 5 - 0
app/api/controller/ServiceOrderController.php

@@ -116,6 +116,11 @@ class ServiceOrderController extends BaseApiController
         if (false === $result) {
             return $this->fail(ServiceOrderLogic::getError());
         }
+        // 服务完成 - 后续分成事件
+        $eventResult = event('PropertyCommission',$params);
+        if (true !== $eventResult) {
+            Log::write('PropertyCommission事件失败:'.$eventResult);
+        }
         return $this->success('已确认服务完成', [], 1, 1);
     }
 

+ 96 - 0
app/api/logic/PropertyCommissionLogic.php

@@ -0,0 +1,96 @@
+<?php
+
+namespace app\api\logic;
+
+use app\common\enum\GoodsEnum;
+use app\common\enum\PayEnum;
+use app\common\enum\WorkEnum;
+use app\common\logic\BaseLogic;
+use app\common\model\coupon\UserCoupon;
+use app\common\model\dict\DictData;
+use app\common\model\goods\Goods;
+use app\common\model\master_worker\MasterWorker;
+use app\common\model\orders\RechargeOrder;
+use app\common\model\property\PropertyCommission;
+use app\common\model\property\PropertyHead;
+use app\common\model\property\PropertyOrder;
+use app\common\model\property\PropertySurplusLog;
+use app\common\model\property\PropertyUser;
+use app\common\model\recharge\OrderGoods;
+use app\common\model\works\ServiceWork;
+use app\workerapi\logic\ServiceWorkLogLogic;
+use think\Exception;
+use think\facade\Db;
+
+
+/**
+ *  物业分成
+ * Class PropertyCommissionLogic
+ * @package app\api\logic
+ */
+class PropertyCommissionLogic extends BaseLogic
+{
+
+    /**
+     * 确认服务完成物业分成
+     * @param $params
+     * @return false|true
+     */
+    public static function commissionSurplus($params)
+    {
+        Db::startTrans();
+        try {
+            $work_id =  \app\common\model\recharge\RechargeOrder::where([
+                'order_type' => 0,
+                'user_id' => $params['user_id'],
+                'sn'=>$params['sn']
+            ])->value('work_id');
+            if(empty($work_id)){
+                throw new Exception('订单不存在');
+            }
+            // 判断工单是否用户已完结
+            $service_work = ServiceWork::find($work_id);
+            if($service_work->user_confirm_status!=5){
+                throw new Exception('未完结订单,无法完成分成');
+            }
+
+            // 以工单 结算金额 为基准,计算分成金额
+            $work_amount = $service_work->work_amount;
+            $propertyUserId = PropertyUser::where('user_id',$params['user_id'])->value('id');
+            $propertyOrderInfo = PropertyOrder::where(['property_user_id' => $propertyUserId,'order_status' => 1,'work_id'=>$work_id])->findOrEmpty()->toArray();
+            //计算分成金额
+            $ratio = PropertyHead::where('id',$propertyOrderInfo['property_head_id'])->value('ratio');
+            $commission_amount = number_format($work_amount * ($ratio/100),2,'.','');
+            // 添加分成记录
+            $propertyCommissionInfo = PropertyCommission::create([
+                    'property_head_id' => $propertyOrderInfo['property_head_id'],
+                    'property_user_id' => $propertyUserId,
+                    'property_order_id' => $propertyOrderInfo['id'],
+                    'work_id' => $work_id,
+                    'order_amount' => $work_amount,
+                    'ratio' => $ratio,
+                    'commission_amount' => $commission_amount,
+            ]);
+            // 更新物业收益/订单状态已完结
+            PropertyHead::where(['id' => $propertyOrderInfo['property_head_id']])->update([
+                'all_profit_amount' => Db::raw('all_profit_amount+'.$commission_amount),
+                'surplus_profit_amount' => Db::raw('surplus_profit_amount+'.$commission_amount)
+            ]);
+            PropertyOrder::where(['id' => $propertyOrderInfo['id']])->update(['order_status' => 3]);
+            // 进出账记录
+            PropertySurplusLog::create([
+                'in_out' => 1,
+                'property_commission_id' => $propertyCommissionInfo['id'],
+                'amount' => $commission_amount,
+                'status' => 1,
+                'remark' => '订单完成,分成金额入账'
+            ]);
+
+            Db::commit();
+            return true;
+        }catch (\Exception $e) {
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+}

+ 30 - 0
app/common/listener/PropertyCommissionListener.php

@@ -0,0 +1,30 @@
+<?php
+namespace app\common\listener;
+
+use app\api\logic\PropertyCommissionLogic;
+use think\facade\Log;
+
+/**
+ * 物业分成收益事件监听
+ * Class PropertyCommissionListener
+ * @package app\listener
+ */
+class PropertyCommissionListener
+{
+    public function handle($params)
+    {
+        try {
+            if (empty($params['user_id']) || empty($params['sn'])) {
+                throw new \Exception('用户订单号不能为空');
+            }
+            $result = PropertyCommissionLogic::commissionSurplus($params);
+            if (false === $result) {
+                throw new \Exception(PropertyCommissionLogic::getError());
+            }
+            return true;
+        } catch (\Exception $e) {
+            Log::write('物业收益分成失败:'.$e->getMessage());
+            return $e->getMessage();
+        }
+    }
+}

+ 34 - 0
app/common/model/property/PropertyCommission.php

@@ -0,0 +1,34 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\common\model\property;
+
+
+use app\common\model\BaseModel;
+
+
+
+/**
+ * PropertyCommission模型
+ * Class PropertyCommission
+ * @package app\common\model
+ */
+class PropertyCommission extends BaseModel
+{
+    
+    protected $name = 'property_commission';
+    
+
+    
+}

+ 34 - 0
app/common/model/property/PropertySurplusLog.php

@@ -0,0 +1,34 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\common\model\property;
+
+
+use app\common\model\BaseModel;
+
+
+
+/**
+ * PropertySurplusLog模型
+ * Class PropertySurplusLog
+ * @package app\common\model
+ */
+class PropertySurplusLog extends BaseModel
+{
+    
+    protected $name = 'property_surplus_log';
+    
+
+    
+}

+ 3 - 0
app/event.php

@@ -1,6 +1,7 @@
 <?php
 // 事件定义文件
 use app\common\listener\NoticeListener;
+use app\common\listener\PropertyCommissionListener;
 
 return [
     'bind'      => [
@@ -15,6 +16,8 @@ return [
 
         // 通知
         'Notice' => [NoticeListener::class],
+        // 物业分成
+        'PropertyCommission' => [PropertyCommissionListener::class],
     ],
 
     'subscribe' => [