| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713 | 
							- <?php
 
- namespace App\Service;
 
- use App\Jobs\ProcessDataJob;
 
- 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\ProductInventoryOfTop;
 
- use App\Model\ProductItemCodeMessage;
 
- use App\Model\ProductPriceDetail;
 
- use App\Model\Role;
 
- use App\Model\RoleMenuButton;
 
- use App\Model\SeeRange;
 
- use App\Model\Storehouse;
 
- use App\Model\U8Job;
 
- use Illuminate\Support\Arr;
 
- 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->leftJoin('product_category_orderby as a', function ($join) {
 
-                 $join->on('product_category.id', '=', 'a.category_id')
 
-                     ->where('a.del_time',0);
 
-             })
 
-             ->where('product_category.del_time',0)
 
-             ->select('product_category.title','product_category.id','product_category.parent_id','product_category.is_edit_unit_price')
 
-             ->orderByRaw('IF(a.sort IS NULL, product_category.id, a.sort) ASC'); // 排序逻辑:优先按 a.sort 排序,没有关联时按主表 id 排序
 
-         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::leftJoin('product_category_orderby as a', function ($join) {
 
-                 $join->on('product_category.id', '=', 'a.category_id')
 
-                     ->where('a.del_time',0);
 
-             })
 
-             ->where('product_category.del_time',0)
 
-             ->where('product_category.top_depart_id', $head_top_depart_id)
 
-             ->select('product_category.title','product_category.id','product_category.parent_id')
 
-             ->orderByRaw('IF(a.sort IS NULL, product_category.id, a.sort) ASC') // 排序逻辑:优先按 a.sort 排序,没有关联时按主表 id 排序
 
-             ->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->item_code = $data['item_code'] ?? "";
 
-             $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->item_code = $data['item_code'] ?? "";
 
-             $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();
 
-         $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'] ?? [];
 
-         //成本
 
-         $is_show_cost = 0;
 
-         if(in_array(RoleMenuButton::special_two,$special_button)) $is_show_cost = 1;
 
-         $customer['cost_show'] = $is_show_cost;
 
-         //所有金额
 
-         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 productCommon($data,$user, $field = []){
 
-         if(empty($field)){
 
-             $field = ['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','item_code'];
 
-         }
 
-         $model = Product::ProductClear($user,$data);
 
-         $model = $model->where('del_time',0)
 
-             ->select($field)
 
-             ->orderby('product_attribute', 'desc')
 
-             ->orderby('id', 'desc');
 
-         if(! empty($data['title_t'])) {
 
-             // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
 
-             $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
 
-             // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
 
-             $model->where(function ($query) use ($cleanTitle, $data) {
 
-                 $query->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
 
-                     ->orWhere('code', 'LIKE', '%' . $data['title_t'] . '%')
 
-                     ->orWhere('size', 'LIKE', '%' . $data['title_t'] . '%');
 
-             });
 
-         }
 
-         if(! empty($data['title'])) {
 
-             // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
 
-             $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
 
-             // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
 
-             $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
 
- //            $model->where('title', 'LIKE', '%'.$data['title'].'%');
 
-         }
 
-         if(! empty($data['product_item_code_title'])){
 
-             $item_code = ProductItemCodeMessage::where('del_time',0)
 
-                 ->where('title', 'LIKE', '%'.$data['product_item_code_title'].'%')
 
-                 ->select('item_code')
 
-                 ->get()->toArray();
 
-             $model->whereIn('item_code', array_column($item_code,'item_code'));
 
-         }
 
-         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['item_code'])) $model->where('item_code', 'LIKE', '%'.$data['item_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']);
 
-         return $model;
 
-     }
 
-     /**
 
-      * 产品列表
 
-      * @param $data
 
-      * @param $user
 
-      * @return array
 
-      */
 
-     public function productListIndex($data,$user){
 
-         $model = $this->productCommon($data, $user);
 
-         $list = $this->limit($model,'',$data);
 
-         $list = $this->fillData($list,$user,$data);
 
-         return [true, $list];
 
-     }
 
-     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','item_code')
 
-             ->where('is_use', Product::is_use_one)
 
-             ->orderby('product_attribute', 'desc')
 
-             ->orderby('id', 'desc');
 
-         if(! empty($data['title_t'])) {
 
-             // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
 
-             $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
 
-             // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
 
-             $model->where(function ($query) use ($cleanTitle, $data) {
 
-                 $query->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
 
-                     ->orWhere('code', 'LIKE', '%' . $data['title_t'] . '%')
 
-                     ->orWhere('size', 'LIKE', '%' . $data['title_t'] . '%');
 
-             });
 
-         }
 
-         if(! empty($data['title'])){
 
-             // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
 
-             $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
 
-             // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
 
-             $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
 
- //            $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']);
 
-         if(isset($data['is_head'])){
 
-             if($data['is_head'] == 1){
 
-                 $head = $user['head']['id'] ?? 0;
 
-                 $model->where('top_depart_id', $head);
 
-             }else{
 
-                 $top_depart_id = $user['depart_top'][0] ?? [];
 
-                 $top_depart_id = $top_depart_id['depart_id'] ?? 0;
 
-                 $model->where('top_depart_id', $top_depart_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['product_category'])) return [false,'产品分类树结构不能为空'];
 
-         $data['product_category'] = json_encode($data['product_category']);
 
-         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['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'] ?? [];
 
-         //库存
 
-         $stock_map = [];
 
-         if(! empty($search['storehouse_id'])){
 
-             $stock = ProductInventoryService::getRealStock($product_id, $search['storehouse_id']);
 
-             foreach ($stock as $value){
 
-                 $stock_map[$value['product_id']] = $value;
 
-             }unset($stock);
 
-         }
 
-         //分社价格
 
-         $product_fs = [];
 
-         if(! empty($search['fs_price_get'])) $product_fs = $this->getProductPriceDetail(['id' => $product_id], $user);
 
-         //商品编码关联信息
 
-         $item_code_map = ProductItemCodeMessage::where('del_time',0)
 
-             ->whereIn('item_code', array_column($data['data'],'item_code'))
 
-             ->pluck('title','item_code')
 
-             ->toArray();
 
-         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;
 
-             $price_tmp = $product_fs[$value['id']] ?? [];
 
-             foreach ($price_tmp as $t_k => $t){
 
-                 $data['data'][$key][$t_k] = $t;
 
-             }
 
-             $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']] ?? "";
 
-             //库存
 
-             $data['data'][$key]['stock'] = $stock_map[$value['id']] ?? (object)[];
 
-             //成本
 
-             $data['data'][$key]['cost_show'] = $value['cost'];
 
-             $data['data'][$key]['product_item_code_title'] = $item_code_map[$value['item_code']] ?? "";
 
-         }
 
-         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 getProductImgGroup($product_id = [],$search){
 
-         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(empty($search['from_wechat_program'])){
 
-                 if(isset($img[$value['product_id']])) continue;
 
-                 $url = "";
 
-                 if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
 
-                 $img[$value['product_id']][] = $url;
 
-             }else{
 
-                 $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 batchBuildFee($data){
 
-         if(empty($data['product_id'])) return [false, '请选择产品'];
 
-         if(! isset($data['build_fee'])) return [false, '请填写安装费'];
 
-         $res = $this->checkNumber($data['build_fee']);
 
-         if(! $res) return [false,'安装费请输入不超过两位小数并且大于等于0的数值'];
 
-         try {
 
-             DB::beginTransaction();
 
-             Product::where('del_time',0)
 
-                 ->whereIn('id', $data['product_id'])
 
-                 ->update([
 
-                     'build_fee' => $data['build_fee']
 
-                 ]);
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getMessage()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     //获取产品字典
 
-     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, $basic_type_id_for = 0){
 
-         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)
 
-                 ->when(! empty($basic_type_id_for), function ($query) use ($basic_type_id_for) {
 
-                     return $query->where('a.basic_type_id',$basic_type_id_for);
 
-                 })
 
-                 ->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)
 
-                 ->when(! empty($basic_type_id_for), function ($query) use ($basic_type_id_for) {
 
-                     return $query->where('basic_type_id',$basic_type_id_for);
 
-                 })
 
-                 ->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;
 
-     }
 
-     //获取产品分社价格
 
-     public function getProductPriceDetail($data, $user){
 
-         $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)
 
-             ->whereIn('product_id',$data['id'])
 
-             ->select('product_id','basic_type_id','price')
 
-             ->get()->toArray();
 
-         $product_price = [];
 
-         //所有金额
 
-         foreach ($basic as $value){
 
-             $product_price[$value['id']] = "0";
 
-         }
 
-         $return = [];
 
-         foreach ($data['id'] as $value){
 
-             $return[$value] = $product_price;
 
-         }
 
-         //展示金额
 
-         foreach ($detail as $value){
 
-             if(isset($return[$value['product_id']][$value['basic_type_id']])) $return[$value['product_id']][$value['basic_type_id']] = $value['price'];
 
-         }
 
-         return $return;
 
-     }
 
-     public function productGroupByList($data,$user){
 
-         $model = Product::ProductClear($user,$data);
 
-         $model = $model->where('del_time',0)
 
-             ->where('item_code','<>',"")
 
-             ->select('title','id','product_category_id','product_category','retail_price','product_attribute','item_code','unit','size','code','bar_code')
 
-             ->where('is_use', Product::is_use_one)
 
-             ->groupBy('item_code')
 
-             ->orderByDesc('product_attribute')
 
-             ->orderByDesc('item_code');
 
- //        if(isset($data['from_wechat_program'])){
 
- //            //先根据大类分类排序 然后再根据子类分类排序 然后再根据产品属性排序
 
- //            $model = $model->orderByRaw("CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(product_category, ',', 1), '[', -1) AS UNSIGNED)")
 
- //                ->orderByRaw("CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(product_category, ',', 2), ',', -1) AS UNSIGNED)")
 
- //                ->orderByDesc('product_attribute')
 
- //                ->orderByDesc('item_code');
 
- //        }else{
 
- //            $model = $model->orderByDesc('product_attribute')
 
- //                ->orderByDesc('item_code');
 
- //        }
 
-         if(! empty($data['title_t'])) {
 
-             // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
 
-             $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
 
-             // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
 
-             $model->where(function ($query) use ($cleanTitle, $data) {
 
-                 $query->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
 
-                     ->orWhere('code', 'LIKE', '%' . $data['title_t'] . '%')
 
-                     ->orWhere('size', 'LIKE', '%' . $data['title_t'] . '%');
 
-             });
 
-         }
 
-         if(! empty($data['title'])){
 
-             // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
 
-             $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
 
-             // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
 
-             $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
 
- //            $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['product_category_array_id'])) $model->whereIn('product_category_id', $data['product_category_array_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->productGroupByListData($list,$user,$data);
 
-         return [true, $list];
 
-     }
 
-     public function productGroupByListData($data, $user, $search){
 
-         if(empty($data['data'])) return $data;
 
-         $type = $search['type'] ?? 1;
 
-         $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
 
-             ->select('title','id')
 
-             ->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();
 
-         //产品使用价格
 
-         $product_id = array_column($data['data'],'id');
 
-         $detail_map = $this->getProductPrice($product_id, $type);
 
-         //获取产品图片
 
-         $img = $this->getProductImgGroup($product_id,$search);
 
-         //当前门店
 
-         $top_depart = $user['depart_top'][0] ?? [];
 
-         //库存
 
-         $stock = ProductInventoryOfTop::where('del_time',0)
 
-             ->whereIn('product_id',$product_id)
 
-             ->pluck('number','product_id')
 
-             ->toArray();
 
-         //商品编码关联信息
 
-         $item_code_map = ProductItemCodeMessage::where('del_time',0)
 
-             ->whereIn('item_code', array_column($data['data'],'item_code'))
 
-             ->pluck('title','item_code')
 
-             ->toArray();
 
-         foreach ($data['data'] as $key => $value){
 
-             $arr = json_decode($value['product_category'], true);
 
-             $arr = array_flip($arr);
 
-             if (isset($arr[112])) {
 
-                 $data['data'][$key]['is_roll'] = true;
 
-                 $is_roll = true;
 
-             }else{
 
-                 $data['data'][$key]['is_roll'] = false;
 
-                 $is_roll = false;
 
-             }
 
-             $tmp = $img[$value['id']] ?? [];
 
-             $data['data'][$key]['img'] = $tmp[0] ?? "";
 
-             if(isset($tmp[0])) unset($tmp[0]);
 
-             $data['data'][$key]['img_detail'] = array_values($tmp);
 
-             $tmp = $this->fillProductPrice($detail_map, $value, $top_depart,$basic_map);
 
-             $data['data'][$key]['product_price'] = $tmp;
 
-             $category_tmp = $category[$value['product_category_id']] ?? [];
 
-             $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
 
-             $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
 
-             $data['data'][$key]['stock'] = $this->returnStock($stock, $value);
 
-             $data['data'][$key]['unit_title'] = $basic_map[$value['unit']] ?? "";
 
-             $title = $value['title'];
 
-             if(! empty($item_code_map[$value['item_code']])) $title = $item_code_map[$value['item_code']];
 
-             $data['data'][$key]['title'] = $title;
 
-             //通用名称
 
-             $common_title = $item_code_map[$value['item_code']] ?? "";
 
-             //is_roll false 展示title  true product_category_name
 
-             if($is_roll){
 
-                 $category_tmp = $category[$value['product_category_id']] ?? [];
 
-                 $category_title = $category_tmp['title'] ?? '';
 
-                 if(! empty($common_title)) $category_title = $common_title;
 
-                 $data['data'][$key]['product_category_name'] = $category_title;
 
-             }else{
 
-                 $title = $value['title'];
 
-                 if(! empty($common_title)) $title = $common_title;
 
-                 $data['data'][$key]['title'] = $title;
 
-             }
 
-         }
 
-         return $data;
 
-     }
 
-     public function productGroupByListDetail($data, $user){
 
-         if(empty($data['item_code'])) return [false, '商品编码不能为空'];
 
-         $type = $data['type'] ?? 1;
 
-         //获取相同itemCode的产品
 
-         $product = $this->getProductByItemCode([$data['item_code']], $user);
 
-         $category = ProductCategory::whereIn('id',array_unique(array_column($product,'product_category_id')))
 
-             ->pluck('title','id')
 
-             ->toArray();
 
-         $basic_map = BasicType::whereIn('id',array_unique(array_column($product, 'unit')))
 
-             ->OrWhere('type',BasicType::type_22)
 
-             ->pluck('title','id')
 
-             ->toArray();
 
-         //产品使用价格
 
-         $product_id = array_column($product,'id');
 
-         $detail_map = $this->getProductPrice($product_id, $type);
 
-         //获取产品图片
 
-         $img = $this->getProductImg($product_id);
 
-         //当前门店
 
-         $top_depart = $user['depart_top'][0] ?? [];
 
-         $stock = ProductInventoryOfTop::where('del_time',0)
 
-             ->whereIn('product_id',$product_id)
 
-             ->pluck('number','product_id')
 
-             ->toArray();
 
-         foreach ($product as $key => $value){
 
-             $tmp = $this->fillProductPrice($detail_map, $value, $top_depart,$basic_map);
 
-             $product[$key]['product_price'] = $tmp;
 
-             $product[$key]['img'] = $img[$value['id']] ?? "";
 
-             $product[$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
 
-             $product[$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
 
-             $product[$key]['stock'] = $this->returnStock($stock, $value);
 
-             $product[$key]['unit_title'] = $basic_map[$value['unit']] ?? "";
 
-             $arr = json_decode($value['product_category'], true);
 
-             $arr = array_flip($arr);
 
-             if (isset($arr[112])) {
 
-                 $product[$key]['is_roll'] = true;
 
-             }else{
 
-                 $product[$key]['is_roll'] = false;
 
-             }
 
-         }
 
-         return [true, $product];
 
-     }
 
-     public function productUpdateProductBasicInfo($data){
 
-         $time = time();
 
-         list($status, $msg) = $this->productUpdateProductBasicInfoRule($data,$time);
 
-         if(! $status) return [false, $msg];
 
-         //产品id 主表更新信息 子表插入信息 子表是否参与更新
 
-         list($product_id,$update,$insert_detail,$update_detail_bool) = $msg;
 
- //        dd($product_id,$update,$insert_detail,$update_detail_bool);
 
-         try {
 
-             DB::beginTransaction();
 
-             if(! empty($update)){
 
-                 foreach ($product_id as $p_id){
 
-                     Product::where('id',$p_id)
 
-                         ->update($update);
 
-                 }
 
-             }
 
-             if($update_detail_bool){
 
-                 ProductPriceDetail::where('del_time',0)
 
-                     ->whereIn('product_id',$product_id)
 
-                     ->update(['del_time' => $time]);
 
-                 if(! empty($insert_detail)) ProductPriceDetail::insert($insert_detail);
 
-             }
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getMessage()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     public function productUpdateProductBasicInfoRule($data,$time){
 
-         if(empty($data['product_id'])) return [false, '产品ID不能为空'];
 
-         $product = Product::where('del_time',0)
 
-             ->whereIn('id',$data['product_id'])
 
-             ->select('id')
 
-             ->get()->toArray();
 
-         if(empty($product)) return [false, '产品不存在或已被删除'];
 
-         $product = array_column($product,'id');
 
-         // 产品分类   单位   成本   各分社价   商品编码   产品属性
 
-         $update = $update_detail = [];
 
-         if(! empty($data['product_category_id'])){
 
-             $update['product_category_id'] = $data['product_category_id'];
 
-             if(empty($data['product_category'])) return [false,'产品分类树结构不能为空'];
 
-             $product_category = json_encode($data['product_category']);
 
-             $update['product_category'] = $product_category;
 
-         }
 
-         if(Arr::has($data,'unit')) $update['unit'] = $data['unit'] ?? 0;
 
-         if(Arr::has($data,'cost')){
 
-             $cost = $data['cost'] ?? 0;
 
-             $res = $this->checkNumber($cost);
 
-             if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值'];
 
-             $update['cost'] = $cost;
 
-         }
 
-         if(Arr::has($data,'item_code')) $update['item_code'] = $data['item_code'] ?? "";
 
-         if(Arr::has($data,'product_attribute')) $update['product_attribute'] = $data['product_attribute'] ?? 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){
 
-                 $price = $value['price'] ?? 0;
 
-                 $tmp = $map[$value['basic_type_id']] ?? '';
 
-                 if(! $tmp) return [false, '分社价格错误'];
 
-                 $res = $this->checkNumber($price);
 
-                 if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
 
-                 $update_detail[] = [
 
-                     'product_id' => 0,
 
-                     'basic_type_id' => $value['basic_type_id'],
 
-                     'price' => $price,
 
-                     'crt_time' => $time,
 
-                 ];
 
-             }
 
-         }
 
-         if(empty($update) && empty($update_detail)) return [false, '暂无更新'];
 
-         $update_detail_bool = false;
 
-         //详情信息
 
-         $insert_detail = [];
 
-         if(! empty($update_detail)){
 
-             $update_detail_bool = true;
 
-             foreach ($product as $p_id){
 
-                 foreach ($update_detail as $detail){
 
-                     $detail['product_id'] = $p_id;
 
-                     $insert_detail[] = $detail;
 
-                 }
 
-             }
 
-         }
 
-         return [true, [$product, $update, $insert_detail, $update_detail_bool]];
 
-     }
 
-     public function productUpdateByItemCode($data){
 
-         if(empty($data['item_code'])) return [false, "商品编码不能为空"];
 
-         $title = $data['title'] ?? '';
 
-         try {
 
-             DB::beginTransaction();
 
-             $time = time();
 
-             ProductItemCodeMessage::where('del_time',0)
 
-                 ->where('item_code', $data['item_code'])
 
-                 ->update(['del_time' => $time]);
 
-             ProductItemCodeMessage::insert([
 
-                 'item_code' => $data['item_code'],
 
-                 'title' => $title,
 
-             ]);
 
-             DB::commit();
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             return [false, $exception->getMessage()];
 
-         }
 
-         return [true, ''];
 
-     }
 
-     public function getProductByItemCode($item_code,$user){
 
-         if(empty($item_code)) return [];
 
-         $model = Product::ProductClear($user,[]);
 
-         $product = $model->where('del_time',0)
 
-             ->whereIn('item_code', $item_code)
 
-             ->where('is_use', Product::is_use_one)
 
-             ->select('title','id','product_category_id','retail_price','product_attribute','item_code','size','unit','code','bar_code','product_category')
 
-             ->orderby('product_attribute', 'desc')
 
-             ->get()->toArray();
 
-         return $product;
 
-     }
 
-     public function fillProductPrice($detail_map, $value, $top_depart,$basic_map = []){
 
-         $tmp = [];
 
-         if(isset($detail_map[$value['id']])){
 
-             $d = $detail_map[$value['id']];
 
-             foreach ($d as $v){
 
-                 if($top_depart['basic_type_id'] != $v['basic_type_id']) continue;
 
-                 $tmp = [
 
-                     'basic_type_id' => $v['basic_type_id'],
 
-                     'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
 
-                     'price' => $v['price']
 
-                 ];
 
-             }
 
-         }
 
-         if(empty($tmp)) $tmp = (object)$tmp;
 
-         return $tmp;
 
-     }
 
-     public function updateTopStock($data, $user){
 
-         $limit_key = "updateTopStockFromYy";
 
- //        list($status, $msg) = $this->limitingSendRequestBackg($limit_key);
 
- //        if(! $status) return [false, '正在获取T9杭州总仓库存货库存,请勿重复操作'];
 
-         //总店
 
-         $head = $user['head']['id'] ?? 0;
 
-         $storehouse_id = Storehouse::where('top_depart_id', $head)->where('del_time',0)->value('id') ?? 0;
 
-         $time = time();
 
-         DB::table('product')
 
-             ->where('del_time', 0)
 
-             ->where('top_depart_id', $head)
 
-             ->select('id','code')
 
-             ->orderBy('id')
 
-             ->chunk(200, function ($data) use($head,$storehouse_id,$time){
 
-                 $data = Collect($data)->map(function ($object) {
 
-                     return (array)$object;
 
-                 })->toArray();
 
-                 $map = array_column($data,'id','code');
 
-                 $id = array_unique(array_column($data,'id'));
 
-                 $code = array_unique(array_column($data,'code'));
 
-                 $post = [
 
-                     'urlFromT9' => 'getStock',
 
-                     'code' => $code,
 
-                     'warehouse' => '001',
 
-                 ];
 
-                 $post = json_encode($post);
 
-                 $header = ['Content-Type:application/json'];
 
-                 list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
 
-                 if($msg['code'] != 200) return [false, $msg['msg']];
 
-                 $msg = $msg['data'] ?? [];
 
-                 $insert = [];
 
-                 foreach ($msg as $value){
 
-                     $insert[] = [
 
-                         'product_id' => $map[$value['product_no']] ?? 0,
 
-                         'storehouse_id' => $storehouse_id,
 
-                         'top_depart_id' => $head,
 
-                         'number' => $value['number'],
 
-                         'crt_time' => $time,
 
-                     ];
 
-                 }
 
-                 ProductInventoryOfTop::where('del_time',0)
 
-                     ->whereIn('product_id',$id)
 
-                     ->update(['del_time' => $time]);
 
-                 if(! empty($insert)) ProductInventoryOfTop::insert($insert);
 
-             });
 
-         return [true, ''];
 
-         try{
 
-             $job = ProcessDataJob::dispatch(['head' => $head, 'storehouse_id' => $storehouse_id, 'type' => U8Job::three,'limit_key' => $limit_key])->onQueue(U8Job::job3);
 
-             if(! $job) {
 
-                 $this->dellimitingSendRequestBackg($limit_key);
 
-                 return [false,'T9杭州总仓库存货库存任务没有进入队列!'];
 
-             }
 
-         }catch (\Throwable $e){
 
-             $this->dellimitingSendRequestBackg($limit_key);
 
-             return [false,$e->getMessage()];
 
-         }
 
-         return [true, 'T9杭州总仓库存货库存任务正在后台执行,请等待'];
 
-     }
 
-     private function returnStock($stock, $value){
 
-         if(isset($stock[$value['id']])){
 
-             $number = $stock[$value['id']];
 
-             if($number <= 0){
 
-                 $stock = "售罄";
 
-             } elseif($number <= 15){
 
-                 $stock = " 库存紧张";
 
-             }else{
 
-                 $stock = " 库存充足";
 
-             }
 
-         }else{
 
-             $stock = "联系客服";
 
-         }
 
-         return $stock;
 
-     }
 
-     public function writeStock($data){
 
-         try {
 
-             $head = $data['head'];
 
-             $storehouse_id = $data['storehouse_id'];
 
-             $limit_key = $data['limit_key'];
 
-             DB::enableQueryLog();
 
-             $service = new U8ServerService(true);
 
-             if(! empty($service->error)) return [false, $service->error];
 
-             $time = time();
 
-             DB::table('product')
 
-                 ->where('del_time', 0)
 
-                 ->where('top_depart_id', $head)
 
-                 ->select('id','code')
 
-                 ->orderBy('id')
 
-                 ->chunk(200, function ($data) use($head,$storehouse_id,$time){
 
-                     $data = Collect($data)->map(function ($object) {
 
-                         return (array)$object;
 
-                     })->toArray();
 
-                     $map = array_column($data,'id','code');
 
-                     $id = array_unique(array_column($data,'id'));
 
-                     $code = array_unique(array_column($data,'code'));
 
-                     $post = [
 
-                         'urlFromT9' => 'getStock',
 
-                         'code' => $code,
 
-                         'warehouse' => '001',
 
-                     ];
 
-                     $post = json_encode($post);
 
-                     $header = ['Content-Type:application/json'];
 
-                     list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
 
-                     if($msg['code'] != 200) return [false, $msg['msg']];
 
-                     $msg = $msg['data'] ?? [];
 
-                     $insert = [];
 
-                     foreach ($msg as $value){
 
-                         $insert[] = [
 
-                             'product_id' => $map[$value['product_no']] ?? 0,
 
-                             'storehouse_id' => $storehouse_id,
 
-                             'top_depart_id' => $head,
 
-                             'number' => $value['number'],
 
-                             'crt_time' => $time,
 
-                         ];
 
-                     }
 
-                     ProductInventoryOfTop::where('del_time',0)
 
-                         ->whereIn('product_id',$id)
 
-                         ->update(['del_time' => $time]);
 
-                     if(! empty($insert)) ProductInventoryOfTop::insert($insert);
 
-                 });
 
-             DB::commit();
 
-             $this->dellimitingSendRequestBackg($limit_key);
 
-         }catch (\Throwable $exception){
 
-             DB::rollBack();
 
-             $this->dellimitingSendRequestBackg($limit_key);
 
-             return [false, $exception->getMessage()];
 
-         }
 
-         return [true, ''];
 
-     }
 
- }
 
 
  |