| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025 | 
							- <?php
 
- namespace App\Service;
 
- use App\Model\BasicType;
 
- use App\Model\Depart;
 
- use App\Model\Employee;
 
- use App\Model\Product;
 
- use App\Model\ProductActivity;
 
- use App\Model\ProductActivityPrice;
 
- use App\Model\ProductCategory;
 
- use App\Model\ProductInfo;
 
- use App\Model\ProductIntroduction;
 
- use App\Model\ProductPriceDetail;
 
- use App\Model\Role;
 
- use App\Model\RoleMenuButton;
 
- use App\Model\SeeRange;
 
- use Illuminate\Support\Facades\DB;
 
- /**
 
-  * 产品管理
 
-  */
 
- class ProductService extends Service
 
- {
 
-     public function setIsEditUnitPrice($data,$user){
 
-         if($this->isEmpty($data,'id')) return [false,'请选择分类!'];
 
-         if(! isset($data['is_edit_unit_price'])) return [false,'请选择受否允许修改采购单价!'];
 
-         $is_edit_unit_price = $data['is_edit_unit_price'] > 0 ? 1 : 0;
 
-         $all_id = $this->getProductCateGory($data['id']);
 
-         ProductCategory::whereIn('id',$all_id)->update([
 
-             'is_edit_unit_price' => $is_edit_unit_price
 
-         ]);
 
-         return [true, ''];
 
-     }
 
-     /**
 
-      * 产品分类编辑
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function productCategoryEdit($data,$user){
 
-         list($status,$msg) = $this->productCategoryRule($data,$user,false);
 
-         if(!$status) return [$status,$msg];
 
-         $update = $msg['data'][0];
 
-         $model = new ProductCategory();
 
-         $model->where('id',$data['id'])->update($update);
 
-         return [true,''];
 
-     }
 
-     /**
 
-      * 产品分类新增
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function productCategoryAdd($data,$user){
 
-         list($status,$msg) = $this->productCategoryRule($data,$user);
 
-         if(!$status) return [$status,$msg];
 
-         ProductCategory::insert($msg['data']);
 
-         return [true,''];
 
-     }
 
-     /**
 
-      * 产品分类删除
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function productCategoryDel($data){
 
-         if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
 
-         $bool = Product::where('del_time',0)
 
-             ->where('product_category_id',$data['id'])
 
-             ->exists();
 
-         if($bool) return [false,'产品分类下已添加产品,操作失败'];
 
-         try {
 
-             DB::beginTransaction();
 
-             ProductCategory::where('id',$data['id'])->update([
 
-                 'del_time' => time()
 
-             ]);
 
-             DB::commit();
 
-         }catch (\Exception $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true,''];
 
-     }
 
-     /**
 
-      * 产品分类列表
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function productCategoryList($data,$user){
 
-         $model = ProductCategory::TopClear($user,$data);
 
-         $model = $model->where('del_time',0)
 
-             ->select('title','id','parent_id','is_edit_unit_price')
 
-             ->orderby('id','asc');
 
-         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
 
-         if(isset($data['is_edit_unit_price'])) $model->where('is_edit_unit_price', $data['is_edit_unit_price']);
 
-         $list = $model->get()->toArray();
 
-         foreach ($list as $key => $value){
 
-             $list[$key]['is_edit_unit_price_title'] = ProductCategory::$is_edit_unit_price[$value['is_edit_unit_price']] ?? "";
 
-         }
 
-         $list_tree = $list;
 
-         if(! empty($list_tree)) {
 
-             $list_tree = $this->makeTree(0,$list_tree);
 
-             $list_tree = $this->set_sort_circle($list_tree);
 
-         }
 
-         return [200, ['data' => $list,'tree' => $list_tree]];
 
-     }
 
-     public function productCategoryList2($data,$user){
 
-         $head_top_depart_id = $user['head']['id'] ?? 0;
 
-         $now_top_depart_id = $this->getDepart($user);
 
-         //总社分类
 
-         $head = ProductCategory::where('del_time',0)
 
-             ->where('top_depart_id', $head_top_depart_id)
 
-             ->select('title','id','parent_id')
 
-             ->orderby('id','asc')
 
-             ->get()->toArray();
 
-         $head_tree = [];
 
-         if(! empty($head)) {
 
-             $head_tree = $this->makeTree(0,$head);
 
-             $head_tree = $this->set_sort_circle($head_tree);
 
-         }
 
-         //我的
 
-         $my_tree = [];
 
-         if($head_top_depart_id != $now_top_depart_id){
 
-             $model = ProductCategory::TopClear($user,$data);
 
-             $model = $model->where('del_time',0)
 
-                 ->select('title','id','parent_id')
 
-                 ->orderby('id','asc');
 
-             $list = $model->get()->toArray();
 
-             if(! empty($list)) {
 
-                 $my_tree = $this->makeTree(0,$list);
 
-                 $my_tree = $this->set_sort_circle($my_tree);
 
-             }
 
-         }
 
-         return [200, ['head_tree' => $head_tree, 'my_tree' => $my_tree]];
 
-     }
 
-     /**
 
-      * 产品分类参数规则
 
-      * @param $data
 
-      * @param $is_add
 
-      * @return array
 
-      */
 
-     public function productCategoryRule($data,$user, $is_add = true){
 
-         if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
 
-         //所属部门 以及  顶级部门
 
-         if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
 
-         $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
 
-         $title = array_column($data['data'],'title');
 
-         $title = array_map(function($val) {
 
-             return $val !== null ? $val : 0;
 
-         }, $title);
 
-         $title_count = array_count_values($title);
 
-         foreach ($title as $value){
 
-             if(empty($value)) return [false,'名称不能为空!'];
 
-             if($title_count[$value] > 1) return [false,'名称不能重复'];
 
-         }
 
-         foreach ($data['data'] as $key => $value){
 
-             $data['data'][$key]['upd_time'] = time();
 
-             if($is_add){
 
-                 $parent_id = 0;
 
-                 if(! empty($value['parent_id'])) {
 
-                     $bool = Product::where('del_time',0)
 
-                         ->where('product_category_id',$value['parent_id'])
 
-                         ->exists();
 
-                     if($bool) {
 
-                         $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value('title');
 
-                         return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类'];
 
-                     }
 
-                     $parent_id = $value['parent_id'];
 
-                 }
 
-                 $data['data'][$key]['parent_id'] = $parent_id;
 
-                 $data['data'][$key]['crt_time'] = time();
 
-                 $data['data'][$key]['depart_id'] = $data['depart_id'];
 
-                 $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];
 
-                 $bool = ProductCategory::where('title',$value['title'])
 
-                     ->where('top_depart_id',$data['top_depart_id'])
 
-                     ->where('del_time',0)
 
-                     ->exists();
 
-             }else{
 
-                 if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
 
-                 $top_depart_id = ProductCategory::where('id',$data['id'])->value('top_depart_id');
 
-                 $bool = ProductCategory::where('title',$value['title'])
 
-                     ->where('top_depart_id',$top_depart_id)
 
-                     ->where('id','<>',$data['id'])
 
-                     ->where('del_time',0)
 
-                     ->exists();
 
-             }
 
-             if($bool) return [false,'分类名称不能重复'];
 
-         }
 
-         return [true, $data];
 
-     }
 
-     /**
 
-      * 产品编辑
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function productEdit($data,$user){
 
-         list($status,$msg) = $this->productRule($data, $user, false);
 
-         if(!$status) return [$status,$msg];
 
-         try {
 
-             DB::beginTransaction();
 
-             $model = Product::where('id',$data['id'])->first();
 
-             $model->product_category_id = $data['product_category_id'] ?? 0;
 
-             $model->product_category = $data['product_category'] ?? '';
 
-             $model->title = $data['title'];
 
-             $model->code = $data['code'] ?? '';
 
-             $model->warranty_time = $data['warranty_time'] ?? 0;
 
-             $model->install_time = $data['install_time'] ?? 0;
 
-             $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->build_fee = $data['build_fee'] ?? 0;
 
-             $model->mark = $data['mark'] ?? '';
 
-             $model->state = $data['state'] ?? 0;
 
-             $model->product_attribute = $data['product_attribute'] ?? 0;
 
-             $model->is_use = $data['is_use'] ?? Product::is_use_one;
 
-             $model->save();
 
-             $time = time();
 
-             ProductIntroduction::where('product_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();
 
-             }
 
-             $old = ProductInfo::where('del_time',0)
 
-                 ->where('product_id',$data['id'])
 
-                 ->select('file')
 
-                 ->get()->toArray();
 
-             $old = array_column($old,'file');
 
-             ProductInfo::where('del_time',0)
 
-                 ->where('product_id',$data['id'])
 
-                 ->update(['del_time' => $time]);
 
-             $new = [];
 
-             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,
 
-                     ];
 
-                     if(in_array($value['url'], $old)) {
 
-                         foreach ($old as $o_k => $o_v){
 
-                             if($o_v == $value['url']) unset($old[$o_k]);
 
-                         }
 
-                     }else{
 
-                         $new[] = $value['url'];
 
-                     }
 
-                 }
 
-                 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,
 
-                     ];
 
-                     if(in_array($value['url'], $old)) {
 
-                         foreach ($old as $o_k => $o_v){
 
-                             if($o_v == $value['url']) unset($old[$o_k]);
 
-                         }
 
-                     }else{
 
-                         $new[] = $value['url'];
 
-                     }
 
-                 }
 
-                 ProductInfo::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){
 
-                     $insert[] = [
 
-                         'product_id' => $model->id,
 
-                         'basic_type_id' => $value['basic_type_id'],
 
-                         'price' => $value['price'] ?? 0,
 
-                         'crt_time' => $time,
 
-                     ];
 
-                 }
 
-                 ProductPriceDetail::insert($insert);
 
-             }
 
-             DB::commit();
 
-         }catch (\Exception $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true, ['file' => ['new' => $new, 'old' => $old]]];
 
-     }
 
-     /**
 
-      * 产品新增
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function productAdd($data,$user){
 
-         list($status,$msg) = $this->productRule($data, $user);
 
-         if(!$status) return [$status,$msg];
 
-         try {
 
-             DB::beginTransaction();
 
-             $model = new Product();
 
-             $model->product_category_id = $data['product_category_id'] ?? 0;
 
-             $model->product_category = $data['product_category'] ?? '';
 
-             $model->title = $data['title'];
 
-             $model->code = $data['code'] ?? '';
 
-             $model->warranty_time = $data['warranty_time'] ?? 0;
 
-             $model->install_time = $data['install_time'] ?? 0;
 
-             $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->build_fee = $data['build_fee'] ?? 0;
 
-             $model->mark = $data['mark'] ?? '';
 
-             $model->state = $data['state'] ?? 0;
 
-             $model->product_attribute = $data['product_attribute'] ?? 0;
 
-             $model->crt_id = $user['id'];
 
-             $model->depart_id = $data['depart_id'] ?? 0;
 
-             $model->top_depart_id = $data['top_depart_id'] ?? 0;
 
-             $model->is_use = $data['is_use'] ?? Product::is_use_one;
 
-             $model->save();
 
-             $time = time();
 
-             if(! empty($data['introduction'])){
 
-                 $models = new ProductIntroduction();
 
-                 $models->product_id = $model->id;
 
-                 $models->introduction = $data['introduction'];
 
-                 $models->save();
 
-             }
 
-             $new = [];
 
-             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,
 
-                     ];
 
-                     if(! empty($value['url'])) $new[] = $value['url'];
 
-                 }
 
-                 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,
 
-                     ];
 
-                     if(! empty($value['url'])) $new[] = $value['url'];
 
-                 }
 
-                 ProductInfo::insert($insert);
 
-             }
 
-             if(! empty($data['product_price'])){
 
-                 $insert = [];
 
-                 foreach ($data['product_price'] as $value){
 
-                     $insert[] = [
 
-                         'product_id' => $model->id,
 
-                         'basic_type_id' => $value['basic_type_id'],
 
-                         'price' => $value['price'] ?? 0,
 
-                         'crt_time' => $time,
 
-                     ];
 
-                 }
 
-                 ProductPriceDetail::insert($insert);
 
-             }
 
-             DB::commit();
 
-         }catch (\Exception $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true, ['file' => ['new' => $new]]];
 
-     }
 
-     /**
 
-      * 产品删除
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function productDel($data){
 
-         if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
 
-         try {
 
-             DB::beginTransaction();
 
-             $time = time();
 
-             Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
 
-             ProductIntroduction::where('product_id',$data['id'])
 
-                 ->where('del_time',0)
 
-                 ->update(['del_time' => $time]);
 
-             $old = ProductInfo::where('del_time',0)
 
-                 ->where('product_id',$data['id'])
 
-                 ->select('file')
 
-                 ->get()->toArray();
 
-             $old = array_column($old,'file');
 
-             ProductInfo::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]);
 
-             (new RangeService())->RangeDelete($data['id'],SeeRange::type_four);
 
-             DB::commit();
 
-         }catch (\Exception $exception){
 
-             DB::rollBack();
 
-             return [false,$exception->getMessage()];
 
-         }
 
-         return [true, ['file' => ['old' => $old]]];
 
-     }
 
-     /**
 
-      * 产品详情
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function productDetail($data,$user){
 
-         if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
 
-         $customer = Product::where('del_time',0)
 
-             ->where('id',$data['id'])
 
-             ->first();
 
-         if(empty($customer)) return [false,'产品不存在或已被删除'];
 
-         $customer = $customer->toArray();
 
-         $customer['product_attribute_title'] = Product::$product_attribute[$customer['product_attribute']] ?? "";
 
-         $customer['is_use_title'] = Product::$is_use[$customer['is_use']] ?? "";
 
-         $customer['product_category'] = ! empty($customer['product_category']) ? json_decode($customer['product_category'],true): [];
 
-         $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;
 
-         $data['top_depart_id'] = $user['head']['id'] ?? 0;
 
-         $model = BasicType::TopClear($user,$data);
 
-         $basic = $model->where('del_time',0)
 
-             ->where('type',22)
 
-             ->select('title','id','type')
 
-             ->orderby('id', 'asc')->get()->toArray();
 
-         $detail = ProductPriceDetail::where('del_time',0)
 
-             ->where('product_id',$data['id'])
 
-             ->select('product_id','basic_type_id','price')
 
-             ->get()->toArray();
 
-         $title_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($detail,'basic_type_id'),array_column($basic,'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'] = [];
 
-         //特殊功能按钮
 
-         $special_button = $user['special_button'] ?? [];
 
-         //成本隐藏
 
-         $price = "******";
 
-         if(in_array(RoleMenuButton::special_two,$special_button)) $price = $customer['cost'];
 
-         $customer['cost_show'] = $price;
 
-         //所有金额
 
-         foreach ($basic as $value){
 
-             $show = 0;
 
-             if(in_array(RoleMenuButton::special_one,$special_button)){
 
-                 $show = 1;
 
-             }else{
 
-                 if($top_depart['basic_type_id'] == $value['id']) $show = 1;
 
-             }
 
-             $customer['product_price'][$value['id']] = [
 
-                 'basic_type_id' => $value['id'],
 
-                 'basic_type_title' => $title_map[$value['id']] ?? '',
 
-                 'price' => 0,
 
-                 'is_show' => $show,
 
-             ];
 
-         }
 
-         //展示金额
 
-         foreach ($detail as $value){
 
-             if(isset($customer['product_price'][$value['basic_type_id']])) $customer['product_price'][$value['basic_type_id']]['price'] = $value['price'];
 
-         }
 
-         $customer['product_price'] = array_values($customer['product_price']);
 
-         //单位
 
-         $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();
 
-         $fileUploadService = new FileUploadService();
 
-         foreach ($customer_info as $value){
 
-             $tmp = [
 
-                 'url' => $value['file'],
 
-                 'name' => $value['name'],
 
-                 'show_url' => $fileUploadService->getFileShow($value['file']),
 
-             ];
 
-             if($value['type'] == ProductInfo::type_one){
 
-                 $customer['img'][] = $tmp;
 
-             }elseif ($value['type'] == ProductInfo::type_two){
 
-                 $customer['file'][] = $tmp;
 
-             }
 
-         }
 
-         $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 = (new RangeService())->RangeDetail($data['id'],SeeRange::type_four);
 
-         $customer['depart'] = $return[0] ?? [];
 
-         $customer['employee'] = $return[1] ?? [];
 
-         return [true, $customer];
 
-     }
 
-     public function productListIndex($data,$user){
 
-         $model = Product::ProductClear($user,$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','install_time','product_attribute','is_use','build_fee')
 
-             ->orderby('product_attribute', 'desc')
 
-             ->orderby('id', 'desc');
 
-         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
 
-         if(isset($data['state'])) $model->where('state', $data['state']);
 
-         if(isset($data['is_use'])) $model->where('is_use', $data['is_use']);
 
-         if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
 
-         if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
 
-         if(! empty($data['category'])){
 
-             $id = $this->getProductCateGory($data['category']);
 
-             $model->whereIn('product_category_id', $id);
 
-         }
 
-         if(! empty($data['product_category'])) {
 
-             $product_category = ProductCategory::where('del_time',0)
 
-                 ->where('title', 'LIKE', '%'.$data['product_category'].'%')
 
-                 ->select('id')
 
-                 ->get()->toArray();
 
-             $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
 
-         }
 
-         if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
 
-         if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
 
-         if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
 
-         if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
 
-             $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
 
-             $model->where('crt_time','>=',$return[0]);
 
-             $model->where('crt_time','<=',$return[1]);
 
-         }
 
-         if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
 
-         $list = $this->limit($model,'',$data);
 
-         $list = $this->fillData($list,$user,$data);
 
-         return [true, $list];
 
-     }
 
-     /**
 
-      * 产品列表
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function productList($data,$user){
 
-         $model = Product::ProductClear($user,$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','install_time','product_attribute','is_use','build_fee')
 
-             ->where('is_use', Product::is_use_one)
 
-             ->orderby('product_attribute', 'desc')
 
-             ->orderby('id', 'desc');
 
-         if(! empty($data['title_t'])) {
 
-             $model->where('title', 'LIKE', '%'.$data['title_t'].'%')
 
-                 ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
 
-                 ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
 
-         }
 
-         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
 
-         if(isset($data['state'])) $model->where('state', $data['state']);
 
-         if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
 
-         if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
 
-         if(! empty($data['category'])){
 
-             $id = $this->getProductCateGory($data['category']);
 
-             $model->whereIn('product_category_id', $id);
 
-         }
 
-         if(! empty($data['product_category'])) {
 
-             $product_category = ProductCategory::where('del_time',0)
 
-                 ->where('title', 'LIKE', '%'.$data['product_category'].'%')
 
-                 ->select('id')
 
-                 ->get()->toArray();
 
-             $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
 
-         }
 
-         if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
 
-         if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
 
-         if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
 
-         if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
 
-             $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
 
-             $model->where('crt_time','>=',$return[0]);
 
-             $model->where('crt_time','<=',$return[1]);
 
-         }
 
-         if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
 
-         $list = $this->limit($model,'',$data);
 
-         $list = $this->fillData($list,$user,$data);
 
-         return [true, $list];
 
-     }
 
-     public function productList2($data,$user){
 
-         $model = ProductCategory::ProductClear($user,$data);
 
-         $model = $model->where('del_time',0)
 
-             ->select('title','id','parent_id')
 
-             ->orderby('id','desc');
 
-         if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
 
-         $list = $model->get()->toArray();
 
-         if(! empty($list)) {
 
-             $productList = Product::where('del_time',0)
 
-                 ->whereIn('product_category_id',array_column($list,'id'))
 
-                 ->select('id','product_category_id','title','code')
 
-                 ->get()->toArray();
 
-             $productMap = [];
 
-             foreach ($productList as $value){
 
-                 $productMap[$value['product_category_id']][] = $value;
 
-             }
 
-             foreach ($list as $key => $value){
 
-                 if(isset($productMap[$value['id']])) $list[$key]['product'] = $productMap[$value['id']];
 
-             }
 
-             $list = $this->makeTree(0,$list);
 
-             $list = $this->set_sort_circle($list);
 
-         }
 
-         return [200, $list];
 
-     }
 
-     /**
 
-      * 产品参数规则
 
-      * @param $data
 
-      * @param $is_add
 
-      * @return array
 
-      */
 
-     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(! isset($data['cost'])) return [false, '请填写成本'];
 
-         if(! isset($data['retail_price'])) return [false, '请填写零售价'];
 
-         $res = $this->checkNumber($data['cost']);
 
-         if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值'];
 
-         $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'))
 
-                 ->pluck('title','id')
 
-                 ->toArray();
 
-             foreach ($data['product_price'] as $value){
 
-                 if(! empty($value['price'])) {
 
-                     $tmp = $map[$value['basic_type_id']] ?? '';
 
-                     $res = $this->checkNumber($value['price']);
 
-                     if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
 
-                 }
 
-             }
 
-         }
 
-         if(! empty($data['product_category'])) $data['product_category'] = json_encode($data['product_category']);
 
-         //所属部门 以及 顶级部门
 
-         if(empty($data['depart_id'])) {
 
-             $data['depart_id'] = $this->getDepart($user);
 
-             $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
 
-         }
 
-         //总社id
 
-         $top_depart_id = $user['head'] ?? [];
 
-         $top_depart_id = $top_depart_id['id'] ?? 0;
 
-         if($is_add){
 
-             $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
 
-                 ->where('del_time',0)
 
-                 ->exists();
 
-         }else{
 
-             if(empty($data['id'])) return [false,'ID不能为空'];
 
-             $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
 
-                 ->where('id','<>',$data['id'])
 
-                 ->where('del_time',0)
 
-                 ->exists();
 
-         }
 
-         if($bool) return [false,'产品编码不能重复'];
 
-         return [true, $data];
 
-     }
 
-     /**
 
-      * 拼接数据
 
-      * @param $data
 
-      * @return array
 
-      */
 
-     public function fillData($data, $user, $search){
 
-         if(empty($data['data'])) return $data;
 
-         $type = $search['type'] ?? 0;
 
-         $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')))
 
-             ->select('title','id','is_edit_unit_price')
 
-             ->get()
 
-             ->toArray();
 
-         $category = array_column($category,null,'id');
 
-         $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
 
-             ->orWhere('type',BasicType::type_22)
 
-             ->pluck('title','id')
 
-             ->toArray();
 
-         $depart_map = Depart::whereIn('id',array_unique(array_column($data['data'],'top_depart_id')))->pluck('title','id')->toArray();
 
-         //产品使用价格
 
-         $product_id = array_column($data['data'],'id');
 
-         $detail_map = $this->getProductPrice($product_id, $type);
 
-         //获取产品图片
 
-         $img = $this->getProductImg($product_id);
 
-         //当前门店
 
-         $top_depart = $user['depart_top'][0] ?? [];
 
-         //特殊功能按钮
 
-         $special_button = $user['special_button'] ?? [];
 
-         foreach ($data['data'] as $key => $value){
 
-             $tmp = [];
 
-             if(isset($detail_map[$value['id']])){
 
-                 $d = $detail_map[$value['id']];
 
-                 foreach ($d as $v){
 
-                     $is_show = 0;
 
-                     if(in_array(RoleMenuButton::special_one,$special_button)) $is_show = 1;
 
-                     $is_use = 0;
 
-                     if($top_depart['basic_type_id'] == $v['basic_type_id']) $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]['is_use_title'] = Product::$is_use[$value['is_use']] ?? "";
 
-             $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
 
-             $category_tmp = $category[$value['product_category_id']] ?? [];
 
-             $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
 
-             $data['data'][$key]['is_edit_unit_price'] = $category_tmp['is_edit_unit_price'] ?? 0;
 
-             $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
 
-             $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
 
-             $data['data'][$key]['belong_to'] = $depart_map[$value['top_depart_id']] ?? '';
 
-             $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
 
-             $data['data'][$key]['color'] = Product::$product_attribute_color[$value['product_attribute']] ?? [];
 
-             $data['data'][$key]['img'] = $img[$value['id']] ?? "";
 
-             //成本隐藏
 
-             $price = "******";
 
-             if(in_array(RoleMenuButton::special_two,$special_button)) $price = $value['cost'];
 
-             $data['data'][$key]['cost_show'] = $price;
 
-         }
 
-         return $data;
 
-     }
 
-     public function getProductCateGory($category){
 
-         $product_category = ProductCategory::where('del_time',0)
 
-             ->select('id','parent_id')
 
-             ->get()->toArray();
 
-         $result = array_merge($this->getAllDescendants($product_category,$category),[$category]);
 
-         return $result;
 
-     }
 
-     public function getProductImg($product_id = []){
 
-         if(empty($product_id)) return [];
 
-         $img = [];
 
-         $customer_info = ProductInfo::where('del_time',0)
 
-             ->whereIn('product_id',$product_id)
 
-             ->where('type',ProductInfo::type_one)
 
-             ->select('product_id','file','type','name')
 
-             ->get()->toArray();
 
-         $fileUploadService = new FileUploadService();
 
-         foreach ($customer_info as $value){
 
-             if(isset($img[$value['product_id']])) continue;
 
-             $url = "";
 
-             if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
 
-             $img[$value['product_id']] = $url;
 
-         }
 
-         return $img;
 
-     }
 
-     public function batchUploadImg($data){
 
-         if(empty($data['product_id'])) return [false, '请选择产品'];
 
-         $time = time();
 
-         try {
 
-             DB::beginTransaction();
 
-             $old = ProductInfo::where('del_time',0)
 
-                 ->where('type',ProductInfo::type_one)
 
-                 ->whereIn('product_id',$data['product_id'])
 
-                 ->select('file')
 
-                 ->get()->toArray();
 
-             $old = array_column($old,'file');
 
-             ProductInfo::where('del_time',0)
 
-                 ->where('type',ProductInfo::type_one)
 
-                 ->whereIn('product_id',$data['product_id'])
 
-                 ->update(['del_time' => $time]);
 
-             $new['origin'] = $data['img_url'] ?? "";
 
-             $new['img_list'] = [];
 
-             $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $data['img_url']);
 
-             $img = explode('.', $img);
 
-             $insert = [];
 
-             if(! empty($data['img_url'])){
 
-                 foreach ($data['product_id'] as $key => $value){
 
-                     $copy = $img[0] . 'C' . $key . '.' . $img[1];
 
-                     $copy = FileUploadService::string . FileUploadService::string2 . $copy;
 
-                     $insert[] = [
 
-                         'product_id' => $value,
 
-                         'file' => $copy,
 
-                         'type' => ProductInfo::type_one,
 
-                         'name' => $data['img_name'] ?? "",
 
-                         'crt_time' => $time,
 
-                     ];
 
-                     $new['img_list'][] = $copy;
 
-                 }
 
-                 if(! empty($insert)) ProductInfo::insert($insert);
 
-             }
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getMessage()];
 
-         }
 
-         return [true, ['is_batch' => true, 'file' => ['new' => $new, 'old' => $old]]];
 
-     }
 
-     //获取产品字典
 
-     public function getProductDetail($product_id = []){
 
-         if(empty($product_id)) return [];
 
-         $pro = Product::whereIn('id', $product_id)->get()->toArray();
 
-         $category = ProductCategory::whereIn('id',array_unique(array_column($pro,'product_category_id')))
 
-             ->pluck('is_edit_unit_price','id')
 
-             ->toArray();
 
-         foreach ($pro as $key => $value){
 
-             $pro[$key]['is_edit_unit_price'] = $category[$value['product_category_id']] ?? 0;
 
-         }
 
-         return array_column($pro,null,'id');
 
-     }
 
-     //获取产品使用价格
 
-     public function getProductPrice($product_id = [], $type = 1){
 
-         if(! is_array($product_id)) $product_id = [$product_id];
 
-         //type 1 采购    2 合同 和 活动包
 
-         $detail_map = [];
 
-         $time = time();
 
-         if($type == 1) {
 
-             //供应商活动价格
 
-             $activity = ProductActivityPrice::from('product_activity_price as a')
 
-                 ->join('product_activity as b','b.id','a.product_activity_id')
 
-                 ->where('a.del_time',0)
 
-                 ->whereIn('a.product_id',$product_id)
 
-                 ->where('a.start_time','<=',$time)
 
-                 ->where('a.end_time','>=',$time)
 
-                 ->where('b.type',ProductActivity::type_two)
 
-                 ->select('a.product_id','a.basic_type_id','a.price')
 
-                 ->get()->toArray();
 
-             foreach ($activity as $value){
 
-                 $detail_map[$value['product_id']][] = $value;
 
-             }
 
-             //分社价 没有供应商活动价格使用分社价格
 
-             $detail = ProductPriceDetail::where('del_time',0)
 
-                 ->whereIn('product_id',$product_id)
 
-                 ->select('product_id','basic_type_id','price')
 
-                 ->get()->toArray();
 
-             foreach ($detail as $value){
 
-                 if(! isset($detail_map[$value['product_id']][$value['basic_type_id']])){
 
-                     $detail_map[$value['product_id']][$value['basic_type_id']] = $value;
 
-                 }
 
-             }
 
-         }else{
 
-             //零售活动价格
 
-             $activity = ProductActivityPrice::from('product_activity_price as a')
 
-                 ->join('product_activity as b','b.id','a.product_activity_id')
 
-                 ->where('a.del_time',0)
 
-                 ->whereIn('a.product_id',$product_id)
 
-                 ->where('a.start_time','<=',$time)
 
-                 ->where('a.end_time','>=',$time)
 
-                 ->where('b.type',ProductActivity::type_one)
 
-                 ->select('a.product_id','a.basic_type_id','a.price')
 
-                 ->get()->toArray();
 
-             foreach ($activity as $value){
 
-                 $detail_map[$value['product_id']][] = $value;
 
-             }
 
-         }
 
-         return $detail_map;
 
-     }
 
-     public function getProductPriceTmp($product_id = []){
 
-         //type 0 采购  1 合同
 
-         //分社价
 
-         $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;
 
-     }
 
- }
 
 
  |