Ver código fonte

增加领取保修卡,查询订单相关的保修卡

林海涛 1 ano atrás
pai
commit
2cb99b3740

+ 14 - 1
app/api/controller/ServiceOrderController.php

@@ -115,6 +115,19 @@ class ServiceOrderController extends BaseApiController
         if (false === $result) {
             return $this->fail(ServiceOrderLogic::getError());
         }
-        return $this->success('已确认服务完成', [], 1, 1);
+        return $this->success('获取保修卡成功',$result, 1, 1);
+    }
+
+    public function receiveEffective()
+    {
+        $params = (new ServiceOrderValidate())->post()->goCheck('receiveEffective', [
+            'user_id' => $this->userId,
+            'user_info' => $this->userInfo
+        ]);
+        $result = ServiceOrderLogic::receiveEffective($params);
+        if (false === $result) {
+            return $this->fail(ServiceOrderLogic::getError());
+        }
+        return $this->success('领取保修卡成功',[], 1, 1);
     }
 }

+ 86 - 2
app/api/logic/ServiceOrderLogic.php

@@ -7,8 +7,11 @@ use app\common\enum\PayEnum;
 use app\common\enum\WorkEnum;
 use app\common\logic\BaseLogic;
 use app\common\model\dict\DictData;
+use app\common\model\effective\EffectiveCategory;
+use app\common\model\effective\EffectiveRules;
 use app\common\model\goods\Goods;
 use app\common\model\master_worker\MasterWorker;
+use app\common\model\orders\OrderEffectiveLog;
 use app\common\model\orders\RechargeOrder;
 use app\common\model\performance\PerformanceRules;
 use app\common\model\recharge\OrderGoods;
@@ -318,7 +321,6 @@ class ServiceOrderLogic extends BaseLogic
 
     public static function queryEffective($params)
     {
-        Db::startTrans();
         try {
             $work_id = \app\common\model\recharge\RechargeOrder::where([
                 'order_type' => 0,
@@ -328,9 +330,91 @@ class ServiceOrderLogic extends BaseLogic
             if(empty($work_id)){
                 throw new Exception('订单不存在');
             }
+            //查询工单下所有已完结的订单
+            $orders = RechargeOrder::with('orderGoods')->where(['sn'=>$params['sn'],
+                'user_id'=>$params['user_id'],
+                'pay_status'=>1])->select()->toArray();
+            //查询订单领取的保修卡
+             $logs = OrderEffectiveLog::where(['sn'=>$params['sn'],'user_id'=>$params['user_id']])->select();
+             $data = [];
+             $queryData = [];
+             foreach($orders as $oV){
+                foreach($oV['orderGoods'] as $v){
+                    if($logs->where('sn',$oV['sn'])->where('goods_id',$v['goods_id'])->count() > 0){
+                        $logData = $logs->where('sn',$oV['sn'])->where('goods_id',$v['goods_id'])->toArray();
+                        foreach($logData as $k =>$lV){
+                            $logData[$k]['end_effective_time'] = date("Y-m-d H:i:s",$lV['end_effective_time']);
+                            $logData[$k]['is_receive'] = 1;
+                        }
+                        $data = array_merge($data,$logData);
+                        continue;
+                    }
+                    $queryData[] = ['work_id'=>$oV['work_id'],'sn' =>$oV['sn'],'goods_id' => $v['goods_id'],'goods_category_id' => $v['goods_category_id'],'pay_time'=>$oV['pay_time'] ?? $oV['update_time']];
+                }
+             }
+             $categoryIds = array_column($queryData,'goods_category_id');
+             if(!empty($categoryIds)){
+                 $effectiveRule = EffectiveCategory::with('effectiveRule')->whereIn('goods_category_id',$categoryIds)->select();
+                 foreach($queryData as $v){
+                     if(!$effectiveRule->where('goods_category_id',$v['goods_category_id'])->isEmpty()){
+                         $val = $v ;
+                         $result = $effectiveRule->where('goods_category_id',$v['goods_category_id'])->toArray()[0];
+                         $val = $val+[
+                             'id' => 0,
+                             'effective_id'=>$result['effectiveRule']['id'],
+                             'user_id'=>$params['user_id'],
+                             'effective_unit'=>$result['effectiveRule']['effective_unit'],
+                             'effective_num'=>$result['effectiveRule']['effective_num'],
+                             'remark'=>$result['effectiveRule']['remark'],
+                             'is_receive' => 0,
+                             'end_effective_time'=>  date("Y-m-d H:i:s",EffectiveRules::formatEndEffectiveTime($val['pay_time'],$result['effectiveRule']['effective_num'],$result['effectiveRule']['effective_unit'])),
+                             'create_time'=> date("Y-m-d H:i:s",time()),
+                             'update_time'=>  date("Y-m-d H:i:s",time()),
+                         ];
+                         $data[] = $val;
+                     }
+                 }
+             }
+             return $data;
+        }catch(\Exception $e){
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+    public static function  receiveEffective($params)
+    {
+        try{
+            $orderGoods = OrderGoods::where(['sn'=>$params['sn'],'goods_id' => $params['goods_id']])->findOrEmpty();
+            if($orderGoods->isEmpty()){
+                throw new  \Exception('订单不存在');
+            }
+            $log = OrderEffectiveLog::where(['sn'=>$params['sn'],'user_id'=>$params['user_id'],'goods_id' =>$params['goods_id'] ])->findOrEmpty();
+            if($log->isEmpty()){
+                $orderInfo = \app\common\model\recharge\RechargeOrder::where([
+                    'order_type' => 0,
+                    'user_id' => $params['user_id'],
+                    'sn'=>$params['sn'],
+                    'pay_status'=>1
+                ])->findOrEmpty();
+                if(empty($orderInfo)){
+                    throw new Exception('订单不存在');
+                }
+                $effective =  EffectiveCategory::with('effectiveRule')->where('goods_category_id',$orderGoods->goods_category_id)->findOrEmpty();
+                OrderEffectiveLog::create([
+                    'effective_id' => $effective->effectiveRule->id,
+                    'sn' => $params['sn'],
+                    'user_id' => $params['user_id'],
+                    'work_id' => $orderInfo->work_id,
+                    'goods_id' => $orderGoods->goods_id,
+                    'effective_unit' => $effective->effectiveRule->effective_unit,
+                    'effective_num' => $effective->effectiveRule->effective_num,
+                    'remark' => $effective->effectiveRule->remark,
+                    'end_effective_time' => EffectiveRules::formatEndEffectiveTime($orderInfo->pay_time?? $orderInfo->update_time ,$effective->effectiveRule->effective_num,$effective->effectiveRule->effective_unit),
+                ]);
+            }
             return true;
         }catch(\Exception $e){
-            Db::rollback();
             self::setError($e->getMessage());
             return false;
         }

+ 5 - 0
app/api/validate/ServiceOrderValidate.php

@@ -62,4 +62,9 @@ class ServiceOrderValidate extends BaseValidate
     {
         return $this->only(['sn']);
     }
+
+    public function sceneReceiveEffective()
+    {
+        return $this->only(['sn','goods_id']);
+    }
 }

+ 4 - 0
app/common/model/effective/EffectiveCategory.php

@@ -12,4 +12,8 @@ class EffectiveCategory extends Pivot
 {
     protected $name = 'effective_category';
     protected $autoWriteTimestamp = true;
+
+    public function effectiveRule(){
+        return $this->hasOne(EffectiveRules::class,'id','effective_id');
+    }
 }

+ 17 - 0
app/common/model/effective/EffectiveRules.php

@@ -34,4 +34,21 @@ class EffectiveRules extends BaseModel
     {
         return $this->belongsToMany(GoodsCategory::class,EffectiveCategory::class,'goods_category_id','effective_id');
     }
+
+    public static function formatEndEffectiveTime( $time,$num,$unit)
+    {
+        if(is_numeric($time)){
+            $time = date('Y-m-d H:i:s',$time);
+        }
+        $effectiveUnit = 'day';
+        switch ($unit){
+            case 1:
+                $effectiveUnit = 'day';
+                break;
+            case 2:
+                $effectiveUnit = 'hour';
+                break;
+        }
+        return  strtotime("$time +". $num.$effectiveUnit);
+    }
 }

+ 11 - 0
app/common/model/orders/OrderEffectiveLog.php

@@ -0,0 +1,11 @@
+<?php
+namespace app\common\model\orders;
+use app\common\model\BaseModel;
+/**
+ * @author 林海涛
+ * @date 2024/7/17 下午5:23
+ */
+class OrderEffectiveLog extends BaseModel
+{
+    protected $name = 'order_effective_log';
+}