cqpCow 1 år sedan
förälder
incheckning
71f17c4767

+ 1 - 0
app/Model/BasicType.php

@@ -13,6 +13,7 @@ class BasicType extends Model
     const UPDATED_AT = 'upd_time';
     protected $dateFormat = 'U';
 
+    const type_22 = 22;
     public static $type = [
         1 => '客户意向类型',
         2 => '客户来源类型',

+ 39 - 0
app/Model/ProductActivity.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Model;
+
+use App\Scopes\DepartmentScope;
+use Illuminate\Database\Eloquent\Model;
+
+class ProductActivity extends Model
+{
+//    protected $fillable = ['userData'];
+    protected $guarded = [];
+    protected $table = "product_activity"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+
+    public static $user = [];
+    public static $search = [];
+    public static $is_search = false;
+    const range_function = '';
+
+    public function __construct(array $attributes = [])
+    {
+        if(! empty($attributes['userData'])) {
+            self::$user = $attributes['userData'];
+            self::$user['range_function'] = self::range_function;
+            self::$search = $attributes['search'] ?? [];
+            self::$is_search = true;
+        }
+        parent::__construct($attributes);
+    }
+
+    protected static function boot(){
+        parent::boot();
+        if(self::$is_search){
+            static::addGlobalScope(new DepartmentScope(self::$user,self::$search));
+        }
+    }
+}

+ 13 - 0
app/Model/ProductActivityPrice.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class ProductActivityPrice extends Model
+{
+    protected $table = "product_activity_price"; //指定表
+    const CREATED_AT = 'crt_time';
+    const UPDATED_AT = 'upd_time';
+    protected $dateFormat = 'U';
+}

+ 3 - 1
app/Model/Role.php

@@ -20,11 +20,13 @@ class Role extends Model
     public static $user = [];
     public static $search = [];
     public static $is_search = false;
+    const range_function = '';
 
     public function __construct(array $attributes = [])
     {
         if(! empty($attributes['userData'])) {
             self::$user = $attributes['userData'];
+            self::$user['range_function'] = self::range_function;
             self::$search = $attributes['search'] ?? [];
             self::$is_search = true;
         }
@@ -34,7 +36,7 @@ class Role extends Model
     protected static function boot(){
         parent::boot();
         if(self::$is_search){
-            static::addGlobalScope(new DepartmentScope(self::$user, self::$search));
+            static::addGlobalScope(new DepartmentScope(self::$user,self::$search));
         }
     }
 }

+ 1 - 1
app/Service/EmployeeService.php

@@ -1115,7 +1115,7 @@ class EmployeeService extends Service
             $button_map[$value['menu_id']][] = $value['id'];
         }
         $object = [];//返回的模型
-        if($user['id'] == Employee::SPECIAL_ADMIN || $user['is_manager']){
+        if($user['id'] == Employee::SPECIAL_ADMIN){
             //超级管理员
             foreach ($menu as $value){
                 $object[] = [

+ 62 - 325
app/Service/ProductActivityService.php

@@ -3,17 +3,12 @@
 namespace App\Service;
 
 use App\Model\BasicType;
-use App\Model\Depart;
 use App\Model\Employee;
 use App\Model\Product;
-use App\Model\ProductCategory;
-use App\Model\ProductInfo;
-use App\Model\ProductIntroduction;
-use App\Model\ProductPriceDetail;
-use App\Model\ProductRange;
+use App\Model\ProductActivity;
+use App\Model\ProductActivityPrice;
 use Illuminate\Support\Facades\DB;
 
-
 class ProductActivityService extends Service
 {
     public function productEdit($data,$user){
@@ -23,108 +18,33 @@ class ProductActivityService extends Service
         try {
             DB::beginTransaction();
 
-            $model = Product::where('id',$data['id'])->first();
-            $model->product_category_id = $data['product_category_id'] ?? 0;
+            $model = ProductActivity::where('id',$data['id'])->first();
             $model->title = $data['title'];
-            $model->code = $data['code'] ?? '';
-            $model->size = $data['size'] ?? '';
-            $model->unit = $data['unit'] ?? 0;
-            $model->bar_code = $data['bar_code'] ?? '';
-            $model->cost = $data['cost'] ?? 0;
-            $model->retail_price = $data['retail_price'] ?? 0;
+            $model->start_time = $data['start_time'] ?? 0;
+            $model->end_time = $data['end_time'] ?? 0;
             $model->mark = $data['mark'] ?? '';
-            $model->state = $data['state'] ?? 0;
             $model->save();
 
             $time = time();
 
-            ProductIntroduction::where('product_id',$data['id'])
+            ProductActivityPrice::where('product_activity_id',$data['id'])
                 ->where('del_time',0)
                 ->update(['del_time' => $time]);
-            if(! empty($data['introduction'])){
-                $models = new ProductIntroduction();
-                $models->product_id = $model->id;
-                $models->introduction = $data['introduction'];
-                $models->save();
-            }
-
-            ProductInfo::where('del_time',0)
-                ->where('product_id',$data['id'])
-                ->update(['del_time' => $time]);
-
-            if(! empty($data['img'])){
-                $insert = [];
-                foreach ($data['img'] as $value){
-                    $insert[] = [
-                        'product_id' => $model->id,
-                        'file' => $value['url'],
-                        'type' => ProductInfo::type_one,
-                        'name' => $value['name'],
-                        'crt_time' => $time,
-                    ];
-                }
-                ProductInfo::insert($insert);
-            }
-
-            if(! empty($data['file'])){
-                $insert = [];
-                foreach ($data['file'] as $value){
-                    $insert[] = [
-                        'product_id' => $model->id,
-                        'file' => $value['url'],
-                        'type' => ProductInfo::type_two,
-                        'name' => $value['name'],
-                        'crt_time' => $time,
-                    ];
-                }
-                ProductInfo::insert($insert);
-            }
-
-            ProductRange::where('del_time',0)
-                ->where('product_id',$data['id'])
-                ->update(['del_time' => $time]);
-
-            if(! empty($data['depart'])){
-                $insert = [];
-                foreach ($data['depart'] as $value){
-                    $insert[] = [
-                        'product_id' => $model->id,
-                        'depart_id' => $value,
-                        'type' => ProductRange::type_one,
-                        'crt_time' => $time,
-                    ];
-                }
-                ProductRange::insert($insert);
-            }
 
-            if(! empty($data['employee'])){
+            if(! empty($data['product'])){
                 $insert = [];
-                foreach ($data['employee'] as $value){
-                    $insert[] = [
-                        'product_id' => $model->id,
-                        'employee_id' => $value,
-                        'type' => ProductRange::type_two,
-                        'crt_time' => $time,
-                    ];
-                }
-                ProductRange::insert($insert);
-            }
-
-            ProductPriceDetail::where('del_time',0)
-                ->where('product_id',$data['id'])
-                ->update(['del_time' => $time]);
-
-            if(! empty($data['product_price'])){
-                $insert = [];
-                foreach ($data['product_price'] as $value){
+                foreach ($data['product'] as $value){
                     $insert[] = [
+                        'product_activity_id' => $model->id,
                         'product_id' => $model->id,
                         'basic_type_id' => $value['basic_type_id'],
                         'price' => $value['price'],
                         'crt_time' => $time,
+                        'start_time' => $data['start_time'] ?? 0,
+                        'end_time' => $data['end_time'] ?? 0,
                     ];
                 }
-                ProductPriceDetail::insert($insert);
+                ProductActivityPrice::insert($insert);
             }
 
             DB::commit();
@@ -143,17 +63,12 @@ class ProductActivityService extends Service
         try {
             DB::beginTransaction();
 
-            $model = new Product();
+            $model = new ProductActivity();
             $model->product_category_id = $data['product_category_id'] ?? 0;
             $model->title = $data['title'];
-            $model->code = $data['code'] ?? '';
-            $model->size = $data['size'] ?? '';
-            $model->unit = $data['unit'] ?? 0;
-            $model->bar_code = $data['bar_code'] ?? '';
-            $model->cost = $data['cost'] ?? 0;
-            $model->retail_price = $data['retail_price'] ?? 0;
+            $model->start_time = $data['start_time'] ?? 0;
+            $model->end_time = $data['end_time'] ?? 0;
             $model->mark = $data['mark'] ?? '';
-            $model->state = $data['state'] ?? 0;
             $model->crt_id = $user['id'];
             $model->depart_id = $data['depart_id'] ?? 0;
             $model->top_depart_id = $data['top_depart_id'] ?? 0;
@@ -161,78 +76,20 @@ class ProductActivityService extends Service
 
             $time = time();
 
-            if(! empty($data['introduction'])){
-                $models = new ProductIntroduction();
-                $models->product_id = $model->id;
-                $models->introduction = $data['introduction'];
-                $models->save();
-            }
-
-            if(! empty($data['img'])){
-                $insert = [];
-                foreach ($data['img'] as $value){
-                    $insert[] = [
-                        'product_id' => $model->id,
-                        'file' => $value['url'],
-                        'type' => ProductInfo::type_one,
-                        'name' => $value['name'],
-                        'crt_time' => $time,
-                    ];
-                }
-                ProductInfo::insert($insert);
-            }
-
-            if(! empty($data['file'])){
+            if(! empty($data['product'])){
                 $insert = [];
-                foreach ($data['file'] as $value){
-                    $insert[] = [
-                        'product_id' => $model->id,
-                        'file' => $value['url'],
-                        'type' => ProductInfo::type_two,
-                        'name' => $value['name'],
-                        'crt_time' => $time,
-                    ];
-                }
-                ProductInfo::insert($insert);
-            }
-
-            if(! empty($data['depart'])){
-                $insert = [];
-                foreach ($data['depart'] as $value){
-                    $insert[] = [
-                        'product_id' => $model->id,
-                        'depart_id' => $value,
-                        'type' => ProductRange::type_one,
-                        'crt_time' => $time,
-                    ];
-                }
-                ProductRange::insert($insert);
-            }
-
-            if(! empty($data['employee'])){
-                $insert = [];
-                foreach ($data['employee'] as $value){
-                    $insert[] = [
-                        'product_id' => $model->id,
-                        'employee_id' => $value,
-                        'type' => ProductRange::type_two,
-                        'crt_time' => $time,
-                    ];
-                }
-                ProductRange::insert($insert);
-            }
-
-            if(! empty($data['product_price'])){
-                $insert = [];
-                foreach ($data['product_price'] as $value){
+                foreach ($data['product'] as $value){
                     $insert[] = [
+                        'product_activity_id' => $model->id,
                         'product_id' => $model->id,
                         'basic_type_id' => $value['basic_type_id'],
                         'price' => $value['price'],
                         'crt_time' => $time,
+                        'start_time' => $data['start_time'] ?? 0,
+                        'end_time' => $data['end_time'] ?? 0,
                     ];
                 }
-                ProductPriceDetail::insert($insert);
+                ProductActivityPrice::insert($insert);
             }
 
             DB::commit();
@@ -251,24 +108,12 @@ class ProductActivityService extends Service
             DB::beginTransaction();
             $time = time();
 
-            Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
+            ProductActivity::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
 
-            ProductIntroduction::where('product_id',$data['id'])
+            ProductActivityPrice::where('product_activity_id',$data['id'])
                 ->where('del_time',0)
                 ->update(['del_time' => $time]);
 
-            ProductInfo::where('del_time',0)
-                ->where('product_id',$data['id'])
-                ->update(['del_time' => $time]);
-
-            ProductRange::where('del_time',0)
-                ->where('product_id',$data['id'])
-                ->update(['del_time' => $time]);
-
-            ProductPriceDetail::where('del_time',0)
-                ->where('product_id',$data['id'])
-                ->update(['del_time' => $time]);
-
             DB::commit();
         }catch (\Exception $exception){
             DB::rollBack();
@@ -280,107 +125,39 @@ class ProductActivityService extends Service
 
     public function productDetail($data,$user){
         if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
-        $customer = Product::where('del_time',0)
+        $customer = ProductActivity::where('del_time',0)
             ->where('id',$data['id'])
             ->first();
-        if(empty($customer)) return [false,'产品不存在或已被删除'];
+        if(empty($customer)) return [false,'活动不存在或已被删除'];
         $customer = $customer->toArray();
-        $category = ProductCategory::where('id',$customer['product_category_id'])
-            ->value('title');
-        $customer['product_category_title'] = $category;
-        $customer['introduction'] = "";
-        $in = ProductIntroduction::where('del_time',0)
-            ->where('product_id',$data['id'])
-            ->first();
-        if(! empty($in)) $customer['introduction'] = $in->introduction;
 
-        $detail = ProductPriceDetail::where('del_time',0)
-            ->where('product_id',$data['id'])
-            ->select('product_id','basic_type_id','price')
+        $detail = ProductActivityPrice::where('del_time',0)
+            ->where('product_activity_id',$data['id'])
             ->get()->toArray();
         $title_map = BasicType::whereIn('id',array_column($detail,'basic_type_id'))
             ->pluck('title','id')
             ->toArray();
-        //是否总公司
-        $is_main = $user['is_all_depart'];
-        $top_depart = $user['depart_top'][0] ?? [];
-        $customer['is_edit'] = $customer['top_depart_id'] == $top_depart['depart_id'] ? 1 : 0;
-        $customer['product_price'] = [];
-        //展示金额
+        $customer['product'] = [];
         foreach ($detail as $value){
-            if(! $is_main && ($top_depart['basic_type_id'] != $value['basic_type_id'])) {
-                $is_show = 0;
-            }else{
-                $is_show = 1;
-            }
-            $customer['product_price'][] = [
+            $customer['product'][] = [
                 'basic_type_id' => $value['basic_type_id'],
                 'basic_type_title' => $title_map[$value['basic_type_id']] ?? '',
                 'price' => $value['price'],
-                'is_show' => $is_show,
             ];
         }
 
-        //单位
-        $title = BasicType::where('id',$customer['unit'])->value('title');
-        $customer['unit_name'] = $title;
-
-        $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
-        $customer_info = ProductInfo::where('del_time',0)
-            ->where('product_id',$customer['id'])
-            ->select('id','product_id','file','type','name')
-            ->get()->toArray();
-        foreach ($customer_info as $value){
-            $tmp = [
-                'url' => $value['file'],
-                'name' => $value['name'],
-            ];
-            if($value['type'] == ProductInfo::type_one){
-                $customer['img'][] = $tmp;
-            }elseif ($value['type'] == ProductInfo::type_two){
-                $customer['file'][] = $tmp;
-            }
-        }
-
-        $customer_range = ProductRange::where('del_time',0)
-            ->where('product_id',$customer['id'])
-            ->select('id','product_id','depart_id','type','employee_id')
-            ->get()->toArray();
-        $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$customer['crt_id']],array_column($customer_range,'employee_id'))))
-            ->pluck('emp_name','id')
-            ->toArray();
-        $depart_map = Depart::whereIn('id',array_unique(array_column($customer_range,'depart_id')))
-            ->pluck('title','id')
-            ->toArray();
-        foreach ($customer_range as $value){
-            if($value['type'] == ProductRange::type_one){
-                $tmp = [
-                    'id' => $value['depart_id'],
-                    'name' => $depart_map[$value['depart_id']],
-                ];
-                $customer['depart'][] = $tmp;
-            }elseif ($value['type'] == ProductRange::type_two){
-                $tmp = [
-                    'id' => $value['employee_id'],
-                    'name' => $emp_map[$value['employee_id']] ?? '',
-                ];
-                $customer['employee'][] = $tmp;
-            }
-        }
-        $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
+        $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
         $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
 
         return [true, $customer];
     }
 
     public function productList($data,$user){
-        $model = new Product(['userData' => $user, 'search' => $data]);
+        $model = new ProductActivity(['userData' => $user, 'search' => $data]);
         $model = $model->where('del_time',0)
-            ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','cost','state','crt_id','crt_time','mark','depart_id','top_depart_id')
             ->orderby('id', 'desc');
 
         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
-        if(isset($data['state'])) $model->where('state', $data['state']);
 
         $list = $this->limit($model,'',$data);
         $list = $this->fillData($list,$user);
@@ -389,20 +166,39 @@ class ProductActivityService extends Service
     }
 
     public function productRule(&$data, $user, $is_add = true){
-        if(empty($data['title'])) return [false,'产品名称不能为空'];
-        if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
-        if(empty($data['code'])) return [false,'产品编码不能为空'];
-        if(empty($data['cost'])) return [false,'成本不能为空'];
-        $res = $this->checkNumber($data['cost']);
-        if(! $res) return [false,'成本请输入不超过两位小数并且大于0的数值'];
-        if(empty($data['retail_price'])) return [false,'零售价不能为空'];
-        $res = $this->checkNumber($data['retail_price']);
-        if(! $res) return [false,'零售价格请输入不超过两位小数并且大于0的数值'];
-        if(! empty($data['product_price'])){
-            $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
+        if(empty($data['title'])) return [false,'活动名称名称不能为空'];
+        if(empty($data['activity_time'][0]) || empty($data['activity_time'][1])) return [false,'请填写活动时间范围'];
+        $data['start_time'] = $this->changeDateToDateMin($data['activity_time'][0]);
+        $data['end_time'] = $this->changeDateToDateMin($data['activity_time'][1]);
+        if($is_add){
+            $product = ProductActivityPrice::where('del_time',0)
+                ->where('start', '<=', $data['end_time'])
+                ->where('end', '>=', $data['start_time'])
+                ->select('product_id')
+                ->get()->toArray();
+            $product = array_column($product,'product_id');
+        }else{
+            if(empty($data['id'])) return [false,'ID不能为空'];
+            $product = ProductActivityPrice::where('del_time',0)
+                ->where('id','<>',$data['id'])
+                ->where('start', '<=', $data['end_time'])
+                ->where('end', '>=', $data['start_time'])
+                ->select('product_id')
+                ->get()->toArray();
+            $product = array_column($product,'product_id');
+        }
+        if(! empty($data['product'])){
+            $start_time = date("Y-m-d H:i",$data['start_time']);
+            $end_time = date("Y-m-d H:i",$data['end_time']);
+            $map = BasicType::whereIn('id',array_column($data['product'],'basic_type_id'))
+                ->pluck('title','id')
+                ->toArray();
+            $map2 = Product::whereIn('id',array_column($data['product'],'product_id'))
                 ->pluck('title','id')
                 ->toArray();
-            foreach ($data['product_price'] as $value){
+            foreach ($data['product'] as $value){
+                $pro = $map2[$value['product_id']] ?? "";
+                if(in_array($value['product_id'], $product)) return [false,'产品:' . $pro . '在' . $start_time . '——' . $end_time . '已设置活动价格'];
                 if(! empty($value['price'])) {
                     $tmp = $map[$value['basic_type_id']] ?? '';
                     $res = $this->checkNumber($value['price']);
@@ -415,21 +211,6 @@ class ProductActivityService extends Service
         if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
         $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
 
-        $top_depart = Depart::where('del_time',0)->where('parent_id',0)->where('is_main',1)->value('id');
-        if($is_add){
-            $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}') AND top_depart_id = {$data['top_depart_id']}")
-                ->where('del_time',0)
-                ->exists();
-        }else{
-            if(empty($data['id'])) return [false,'ID不能为空'];
-            $top_depart_id = Product::where('id',$data['id'])->value('top_depart_id');
-            $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}') AND top_depart_id = {$top_depart_id}")
-                ->where('id','<>',$data['id'])
-                ->where('del_time',0)
-                ->exists();
-        }
-        if($bool) return [false,'产品名称或编码不能重复'];
-
         return [true, $data];
     }
 
@@ -439,54 +220,10 @@ class ProductActivityService extends Service
         $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
             ->pluck('emp_name','id')
             ->toArray();
-        $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
-            ->pluck('title','id')
-            ->toArray();
-        $detail = ProductPriceDetail::where('del_time',0)
-            ->whereIn('product_id',array_unique(array_column($data['data'],'id')))
-            ->select('product_id','basic_type_id','price')
-            ->get()->toArray();
-        $basic_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'unit'),array_column($detail,'basic_type_id'))))
-            ->pluck('title','id')
-            ->toArray();
-        $detail_map = [];
-        foreach ($detail as $value){
-            $detail_map[$value['product_id']][] = $value;
-        }
 
-        //是否总公司
-        $is_main = $user['is_all_depart'];
-        $top_depart = $user['depart_top'][0] ?? [];
         foreach ($data['data'] as $key => $value){
-            $tmp = [];
-            if(isset($detail_map[$value['id']])){
-                $d = $detail_map[$value['id']];
-                foreach ($d as $v){
-                    if(! $is_main && ($top_depart['basic_type_id'] != $v['basic_type_id'])) {
-                        $is_show = 0;
-                    }else{
-                        $is_show = 1;
-                    }
-                    if($top_depart['basic_type_id'] != $v['basic_type_id']) {
-                        $is_use = 0;
-                    }else{
-                        $is_use = 1;
-                    }
-                    $tmp[] = [
-                        'basic_type_id' => $v['basic_type_id'],
-                        'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
-                        'price' => $v['price'],
-                        'is_show' => $is_show,
-                        'is_use' => $is_use
-                    ];
-                }
-            }
-            $data['data'][$key]['product_price'] = $tmp;
             $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
             $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
-            $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
-            $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
-            $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
         }
 
         return $data;

+ 52 - 12
app/Service/ProductService.php

@@ -6,6 +6,7 @@ use App\Model\BasicType;
 use App\Model\Depart;
 use App\Model\Employee;
 use App\Model\Product;
+use App\Model\ProductActivityPrice;
 use App\Model\ProductCategory;
 use App\Model\ProductInfo;
 use App\Model\ProductIntroduction;
@@ -272,7 +273,7 @@ class ProductService extends Service
                     $insert[] = [
                         'product_id' => $model->id,
                         'basic_type_id' => $value['basic_type_id'],
-                        'price' => $value['price'],
+                        'price' => $value['price'] ?? 0,
                         'crt_time' => $time,
                     ];
                 }
@@ -386,7 +387,7 @@ class ProductService extends Service
                     $insert[] = [
                         'product_id' => $model->id,
                         'basic_type_id' => $value['basic_type_id'],
-                        'price' => $value['price'],
+                        'price' => $value['price'] ?? 0,
                         'crt_time' => $time,
                     ];
                 }
@@ -596,7 +597,6 @@ class ProductService extends Service
         if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
         $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
 
-        $top_depart = Depart::where('del_time',0)->where('parent_id',0)->where('is_main',1)->value('id');
         if($is_add){
             $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}') AND top_depart_id = {$data['top_depart_id']}")
                 ->where('del_time',0)
@@ -628,17 +628,13 @@ class ProductService extends Service
         $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
             ->pluck('title','id')
             ->toArray();
-        $detail = ProductPriceDetail::where('del_time',0)
-            ->whereIn('product_id',array_unique(array_column($data['data'],'id')))
-            ->select('product_id','basic_type_id','price')
-            ->get()->toArray();
-        $basic_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'unit'),array_column($detail,'basic_type_id'))))
+        $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
+            ->orWhere('type',BasicType::type_22)
             ->pluck('title','id')
             ->toArray();
-        $detail_map = [];
-        foreach ($detail as $value){
-            $detail_map[$value['product_id']][] = $value;
-        }
+
+        //产品使用价格
+        $detail_map = $this->getProductPrice(array_column($data['data'],'id'));
 
         //是否总公司
         $is_main = $user['is_all_depart'];
@@ -684,4 +680,48 @@ class ProductService extends Service
         $pro = Product::whereIn('id', $product_id)->get()->toArray();
         return array_column($pro,null,'id');
     }
+
+    //获取产品使用价格
+    public function getProductPrice($product_id = []){
+        //产品里设置的价格
+        $detail = ProductPriceDetail::where('del_time',0)
+            ->whereIn('product_id',$product_id)
+            ->select('product_id','basic_type_id','price')
+            ->get()->toArray();
+        $detail_map = [];
+        foreach ($detail as $value){
+            $detail_map[$value['product_id']][] = $value;
+        }
+        $return = $detail_map;
+
+        //活动价格
+        $time = time();
+        $activity = ProductActivityPrice::where('del_time',0)
+            ->whereIn('product_id',$product_id)
+            ->where('start_time','<=',$time)
+            ->where('end_time','>=',$time)
+            ->select('product_id','basic_type_id','price')
+            ->get()->toArray();
+        if(! empty($activity)){
+            foreach ($activity as $value){
+                if(! isset($detail_map[$value['product_id']])) {
+                    $return[$value['product_id']][] = $value;
+                }else{
+                    $basic_type = array_column($detail_map[$value['product_id']],'basic_type_id');
+                    if(! in_array($value['basic_type_id'], $basic_type)){
+                        $return[$value['product_id']][] = $value;
+                        continue;
+                    }
+                    foreach ($detail_map[$value['product_id']] as $key => $val){
+                        if($value['basic_type_id'] == $val['basic_type_id']) {
+                            //活动价格替换
+                            $return[$value['product_id']][$key]['price'] = $value['price'];
+                        }
+                    }
+                }
+            }
+        }unset($detail_map);
+
+        return $return;
+    }
 }

+ 7 - 0
routes/api.php

@@ -178,6 +178,13 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('sportsBagAdd', 'Api\SportsBagController@add');
     $route->any('sportsBagDel', 'Api\SportsBagController@del');
 
+    //活动价列表
+    $route->any('productActivityList', 'Api\ProductActivityController@productList');
+    $route->any('productActivityEdit', 'Api\ProductActivityController@productEdit');
+    $route->any('productActivityAdd', 'Api\ProductActivityController@productAdd');
+    $route->any('productActivityDel', 'Api\ProductActivityController@productDel');
+    $route->any('productActivityDetail', 'Api\ProductActivityController@productDetail');
+
     //现存量
     $route->any('productInventoryList', 'Api\ProductInventoryController@productInventoryList');
     //收发存汇总