productCategoryRule($data,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); 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::where('del_time',0) ->select('title','id','parent_id') ->orderby('id','desc'); if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); $list = $this->limit($model,'',$data); return [true, $list]; } /** * 产品分类参数规则 * @param $data * @param $is_add * @return array */ public function productCategoryRule($data, $is_add = true){ if($this->isEmpty($data,'data')) return [false,'数据不能为空!']; $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(); $bool = ProductCategory::where('title',$value['title']) ->where('del_time',0) ->exists(); }else{ if($this->isEmpty($data,'id')) return [false,'id不能为空!']; $bool = ProductCategory::where('title',$value['title']) ->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,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->title = $data['title']; $model->code = $data['code'] ?? ''; $model->size = $data['size'] ?? ''; $model->unit = $data['unit'] ?? 0; $model->bar_code = $data['bar_code'] ?? ''; $model->cost = $data['cost'] ?? 0; $model->depart_price = $data['depart_price'] ?? 0; $model->retail_price = $data['retail_price'] ?? 0; $model->mark = $data['mark'] ?? ''; $model->state = $data['state'] ?? 0; $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(); } ProductInfo::where('del_time',0) ->where('product_id',$data['id']) ->update(['del_time' => $time]); if(! empty($data['img'])){ $insert = []; foreach ($data['img'] as $value){ $insert[] = [ 'product_id' => $model->id, 'file' => $value['url'], 'type' => ProductInfo::type_one, 'name' => $value['name'], 'crt_time' => $time, ]; } ProductInfo::insert($insert); } if(! empty($data['file'])){ $insert = []; foreach ($data['file'] as $value){ $insert[] = [ 'product_id' => $model->id, 'file' => $value['url'], 'type' => ProductInfo::type_two, 'name' => $value['name'], 'crt_time' => $time, ]; } ProductInfo::insert($insert); } ProductRange::where('del_time',0) ->where('product_id',$data['id']) ->update(['del_time' => $time]); if(! empty($data['depart'])){ $insert = []; foreach ($data['depart'] as $value){ $insert[] = [ 'product_id' => $model->id, 'depart_id' => $value, 'type' => ProductRange::type_one, 'crt_time' => $time, ]; } ProductRange::insert($insert); } if(! empty($data['employee'])){ $insert = []; foreach ($data['employee'] as $value){ $insert[] = [ 'product_id' => $model->id, 'employee_id' => $value, 'type' => ProductRange::type_two, 'crt_time' => $time, ]; } ProductRange::insert($insert); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } /** * 产品新增 * @param $data * @param $user * @return array */ public function productAdd($data,$user){ list($status,$msg) = $this->productRule($data); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new Product(); $model->product_category_id = $data['product_category_id'] ?? 0; $model->title = $data['title']; $model->code = $data['code'] ?? ''; $model->size = $data['size'] ?? ''; $model->unit = $data['unit'] ?? 0; $model->bar_code = $data['bar_code'] ?? ''; $model->cost = $data['cost'] ?? 0; $model->depart_price = $data['depart_price'] ?? 0; $model->retail_price = $data['retail_price'] ?? 0; $model->mark = $data['mark'] ?? ''; $model->state = $data['state'] ?? 0; $model->crt_id = $user['id']; $model->save(); $time = time(); if(! empty($data['introduction'])){ $models = new ProductIntroduction(); $models->product_id = $model->id; $models->introduction = $data['introduction']; $models->save(); } if(! empty($data['img'])){ $insert = []; foreach ($data['img'] as $value){ $insert[] = [ 'product_id' => $model->id, 'file' => $value['url'], 'type' => ProductInfo::type_one, 'name' => $value['name'], 'crt_time' => $time, ]; } ProductInfo::insert($insert); } if(! empty($data['file'])){ $insert = []; foreach ($data['file'] as $value){ $insert[] = [ 'product_id' => $model->id, 'file' => $value['url'], 'type' => ProductInfo::type_two, 'name' => $value['name'], 'crt_time' => $time, ]; } ProductInfo::insert($insert); } if(! empty($data['depart'])){ $insert = []; foreach ($data['depart'] as $value){ $insert[] = [ 'product_id' => $model->id, 'depart_id' => $value, 'type' => ProductRange::type_one, 'crt_time' => $time, ]; } ProductRange::insert($insert); } if(! empty($data['employee'])){ $insert = []; foreach ($data['employee'] as $value){ $insert[] = [ 'product_id' => $model->id, 'employee_id' => $value, 'type' => ProductRange::type_two, 'crt_time' => $time, ]; } ProductRange::insert($insert); } DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } /** * 产品删除 * @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]); ProductInfo::where('del_time',0) ->where('product_id',$data['id']) ->update(['del_time' => $time]); ProductRange::where('del_time',0) ->where('product_id',$data['id']) ->update(['del_time' => $time]); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true,'']; } /** * 产品详情 * @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(); $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($in) $customer['introduction'] = $in->introduction; //单位 $title = BasicType::where('id',$customer['unit'])->value('title'); $customer['unit_name'] = $title; //库存 $customer['product_inventory'] = (new ProductInventoryService())->getRealStock([$data['id']]); $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = []; $customer_info = ProductInfo::where('del_time',0) ->where('product_id',$customer['id']) ->select('id','product_id','file','type','name') ->get()->toArray(); foreach ($customer_info as $value){ $tmp = [ 'url' => $value['file'], 'name' => $value['name'], ]; if($value['type'] == ProductInfo::type_one){ $customer['img'][] = $tmp; }elseif ($value['type'] == ProductInfo::type_two){ $customer['file'][] = $tmp; } } $customer_range = ProductRange::where('del_time',0) ->where('product_id',$customer['id']) ->select('id','product_id','depart_id','type','employee_id') ->get()->toArray(); $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$customer['crt_id']],array_column($customer_range,'employee_id')))) ->pluck('emp_name','id') ->toArray(); $depart_map = Depart::whereIn('id',array_unique(array_column($customer_range,'depart_id'))) ->pluck('title','id') ->toArray(); foreach ($customer_range as $value){ if($value['type'] == ProductRange::type_one){ $tmp = [ 'id' => $value['depart_id'], 'name' => $depart_map[$value['depart_id']], ]; $customer['depart'][] = $tmp; }elseif ($value['type'] == ProductRange::type_two){ $tmp = [ 'id' => $value['employee_id'], 'name' => $emp_map[$value['employee_id']] ?? '', ]; $customer['employee'][] = $tmp; } } $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? ''; $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): ''; return [true, $customer]; } /** * 产品列表 * @param $data * @param $user * @return array */ public function productList($data,$user){ $model = Product::where('del_time',0) ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','cost','depart_price','state','crt_id','crt_time','mark') ->orderby('id', 'desc'); //getALL传入后无视设置范围 if(empty($data['getAll']) && $user['id'] != Employee::SPECIAL_ADMIN) { $user_id = $user['id']; $depart_id = $user['depart_range']; $product_id = ProductRange::where('del_time',0) ->where(function ($query) use($user_id, $depart_id) { $query->where('employee_id',$user_id) ->orWhereIn('depart_id', $depart_id); })->select('product_id')->get() ->toArray(); $model->whereIn('id',array_unique(array_column($product_id,'product_id'))); } if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); if(isset($data['state'])) $model->where('state', $data['state']); $list = $this->limit($model,'',$data); $list = $this->fillData($list); return [true, $list]; } /** * 产品参数规则 * @param $data * @param $is_add * @return array */ public function productRule($data, $is_add = true){ if(empty($data['title'])) return [false,'产品名称不能为空']; if(empty($data['product_category_id'])) return [false,'产品分类不能为空']; if(empty($data['code'])) return [false,'产品编码不能为空']; if(empty($data['cost'])) return [false,'成本不能为空']; $res = $this->checkNumber($data['cost']); if(! $res) return [false,'成本请输入不超过两位小数并且大于0的数值']; if(empty($data['depart_price'])) return [false,'分社价格不能为空']; $res = $this->checkNumber($data['depart_price']); if(! $res) return [false,'分社价格请输入不超过两位小数并且大于0的数值']; if(empty($data['retail_price'])) return [false,'零售价不能为空']; $res = $this->checkNumber($data['retail_price']); if(! $res) return [false,'零售价格请输入不超过两位小数并且大于0的数值']; if($is_add){ $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')") ->where('del_time',0) ->exists(); }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')") ->where('id','<>',$data['id']) ->where('del_time',0) ->exists(); } if($bool) return [false,'产品名称或编码不能重复']; return [true, $data]; } /** * 拼接数据 * @param $data * @return array */ public function fillData($data){ if(empty($data['data'])) return $data; $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id'))) ->pluck('emp_name','id') ->toArray(); $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id'))) ->pluck('title','id') ->toArray(); $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit'))) ->pluck('title','id') ->toArray(); foreach ($data['data'] as $key => $value){ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : ''; $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? ''; $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? ''; $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? ''; $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? ''; } return $data; } }