Przeglądaj źródła

增加我的保修券列表

林海涛 1 rok temu
rodzic
commit
e5562d5d08

+ 13 - 0
app/api/controller/OrderEffectiveController.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace app\api\controller;
+
+use app\api\lists\OrderEffectiveLogLists;
+
+class OrderEffectiveController extends BaseApiController
+{
+    public function lists()
+    {
+        return $this->dataLists(new OrderEffectiveLogLists());
+    }
+}

+ 34 - 0
app/api/lists/OrderEffectiveLogLists.php

@@ -0,0 +1,34 @@
+<?php
+namespace app\api\lists;
+use app\common\model\orders\OrderEffectiveLog;
+
+/**
+ * @author 林海涛
+ * @date 2024/7/24 下午3:29
+ */
+class OrderEffectiveLogLists  extends BaseApiDataLists
+{
+   public function queryWhere()
+   {
+       $where = [];
+       $where[] = ['user_id','=',$this->userId];
+       $where[] = ['end_effective_time','>=',time()];
+       return $where;
+   }
+
+   public function  lists():array
+   {
+       return OrderEffectiveLog::where($this->queryWhere())
+           ->limit($this->limitOffset, $this->limitLength)
+           ->field(['id','goods_id','sn','effective_unit','effective_num','remark','end_effective_time'])
+           ->append(['effective_unit_text'])
+           ->order('create_time desc')
+           ->select()
+           ->toArray();
+   }
+
+    public function count(): int
+    {
+        return OrderEffectiveLog::where($this->queryWhere())->count();
+    }
+}

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

@@ -8,8 +8,11 @@ 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\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;
@@ -404,9 +407,9 @@ class ServiceOrderLogic extends BaseLogic
     }
 
 
+
     public static function queryEffective($params)
     {
-        Db::startTrans();
         try {
             $work_id = \app\common\model\recharge\RechargeOrder::where([
                 'order_type' => 0,
@@ -416,9 +419,90 @@ 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]['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'],'user_id',$params['user_id'],'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' =>  date("Y-m-d H:i:s",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;
         }

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

@@ -8,4 +8,16 @@ use app\common\model\BaseModel;
 class OrderEffectiveLog extends BaseModel
 {
     protected $name = 'order_effective_log';
+
+
+    public function getEndEffectiveTimeAttr($value,$data)
+    {
+        return !empty($data['end_effective_time'])?date('Y-m-d H:i:s',$data['end_effective_time']):'';
+    }
+
+    public static function getEffectiveUnitTextAttr($value,$data)
+    {
+       $type = [1 => '天',2=> '小时'];
+       return $type[$data['effective_unit']];
+    }
 }