|
@@ -0,0 +1,494 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Service;
|
|
|
+
|
|
|
+use App\Model\BasicType;
|
|
|
+use App\Model\Depart;
|
|
|
+use App\Model\Employee;
|
|
|
+use App\Model\Product;
|
|
|
+use App\Model\ProductCategory;
|
|
|
+use App\Model\ProductInfo;
|
|
|
+use App\Model\ProductIntroduction;
|
|
|
+use App\Model\ProductPriceDetail;
|
|
|
+use App\Model\ProductRange;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+
|
|
|
+
|
|
|
+class ProductActivityService extends Service
|
|
|
+{
|
|
|
+ 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->title = $data['title'];
|
|
|
+ $model->code = $data['code'] ?? '';
|
|
|
+ $model->size = $data['size'] ?? '';
|
|
|
+ $model->unit = $data['unit'] ?? 0;
|
|
|
+ $model->bar_code = $data['bar_code'] ?? '';
|
|
|
+ $model->cost = $data['cost'] ?? 0;
|
|
|
+ $model->retail_price = $data['retail_price'] ?? 0;
|
|
|
+ $model->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);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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'],
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ ProductPriceDetail::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ 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->title = $data['title'];
|
|
|
+ $model->code = $data['code'] ?? '';
|
|
|
+ $model->size = $data['size'] ?? '';
|
|
|
+ $model->unit = $data['unit'] ?? 0;
|
|
|
+ $model->bar_code = $data['bar_code'] ?? '';
|
|
|
+ $model->cost = $data['cost'] ?? 0;
|
|
|
+ $model->retail_price = $data['retail_price'] ?? 0;
|
|
|
+ $model->mark = $data['mark'] ?? '';
|
|
|
+ $model->state = $data['state'] ?? 0;
|
|
|
+ $model->crt_id = $user['id'];
|
|
|
+ $model->depart_id = $data['depart_id'] ?? 0;
|
|
|
+ $model->top_depart_id = $data['top_depart_id'] ?? 0;
|
|
|
+ $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);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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'],
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ ProductPriceDetail::insert($insert);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ DB::rollBack();
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ 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]);
|
|
|
+
|
|
|
+ ProductPriceDetail::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,''];
|
|
|
+ }
|
|
|
+
|
|
|
+ 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(! empty($in)) $customer['introduction'] = $in->introduction;
|
|
|
+
|
|
|
+ $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_column($detail,'basic_type_id'))
|
|
|
+ ->pluck('title','id')
|
|
|
+ ->toArray();
|
|
|
+ //是否总公司
|
|
|
+ $is_main = $user['is_all_depart'];
|
|
|
+ $top_depart = $user['depart_top'][0] ?? [];
|
|
|
+ $customer['is_edit'] = $customer['top_depart_id'] == $top_depart['depart_id'] ? 1 : 0;
|
|
|
+ $customer['product_price'] = [];
|
|
|
+ //展示金额
|
|
|
+ foreach ($detail as $value){
|
|
|
+ if(! $is_main && ($top_depart['basic_type_id'] != $value['basic_type_id'])) {
|
|
|
+ $is_show = 0;
|
|
|
+ }else{
|
|
|
+ $is_show = 1;
|
|
|
+ }
|
|
|
+ $customer['product_price'][] = [
|
|
|
+ 'basic_type_id' => $value['basic_type_id'],
|
|
|
+ 'basic_type_title' => $title_map[$value['basic_type_id']] ?? '',
|
|
|
+ 'price' => $value['price'],
|
|
|
+ 'is_show' => $is_show,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ //单位
|
|
|
+ $title = BasicType::where('id',$customer['unit'])->value('title');
|
|
|
+ $customer['unit_name'] = $title;
|
|
|
+
|
|
|
+ $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
|
|
|
+ $customer_info = ProductInfo::where('del_time',0)
|
|
|
+ ->where('product_id',$customer['id'])
|
|
|
+ ->select('id','product_id','file','type','name')
|
|
|
+ ->get()->toArray();
|
|
|
+ foreach ($customer_info as $value){
|
|
|
+ $tmp = [
|
|
|
+ 'url' => $value['file'],
|
|
|
+ 'name' => $value['name'],
|
|
|
+ ];
|
|
|
+ if($value['type'] == ProductInfo::type_one){
|
|
|
+ $customer['img'][] = $tmp;
|
|
|
+ }elseif ($value['type'] == ProductInfo::type_two){
|
|
|
+ $customer['file'][] = $tmp;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $customer_range = ProductRange::where('del_time',0)
|
|
|
+ ->where('product_id',$customer['id'])
|
|
|
+ ->select('id','product_id','depart_id','type','employee_id')
|
|
|
+ ->get()->toArray();
|
|
|
+ $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$customer['crt_id']],array_column($customer_range,'employee_id'))))
|
|
|
+ ->pluck('emp_name','id')
|
|
|
+ ->toArray();
|
|
|
+ $depart_map = Depart::whereIn('id',array_unique(array_column($customer_range,'depart_id')))
|
|
|
+ ->pluck('title','id')
|
|
|
+ ->toArray();
|
|
|
+ foreach ($customer_range as $value){
|
|
|
+ if($value['type'] == ProductRange::type_one){
|
|
|
+ $tmp = [
|
|
|
+ 'id' => $value['depart_id'],
|
|
|
+ 'name' => $depart_map[$value['depart_id']],
|
|
|
+ ];
|
|
|
+ $customer['depart'][] = $tmp;
|
|
|
+ }elseif ($value['type'] == ProductRange::type_two){
|
|
|
+ $tmp = [
|
|
|
+ 'id' => $value['employee_id'],
|
|
|
+ 'name' => $emp_map[$value['employee_id']] ?? '',
|
|
|
+ ];
|
|
|
+ $customer['employee'][] = $tmp;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
|
|
|
+ $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
|
|
|
+
|
|
|
+ return [true, $customer];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function productList($data,$user){
|
|
|
+ $model = new Product(['userData' => $user, 'search' => $data]);
|
|
|
+ $model = $model->where('del_time',0)
|
|
|
+ ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','cost','state','crt_id','crt_time','mark','depart_id','top_depart_id')
|
|
|
+ ->orderby('id', 'desc');
|
|
|
+
|
|
|
+ if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
|
|
|
+ if(isset($data['state'])) $model->where('state', $data['state']);
|
|
|
+
|
|
|
+ $list = $this->limit($model,'',$data);
|
|
|
+ $list = $this->fillData($list,$user);
|
|
|
+
|
|
|
+ return [true, $list];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function productRule(&$data, $user, $is_add = true){
|
|
|
+ if(empty($data['title'])) return [false,'产品名称不能为空'];
|
|
|
+ if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
|
|
|
+ if(empty($data['code'])) return [false,'产品编码不能为空'];
|
|
|
+ if(empty($data['cost'])) return [false,'成本不能为空'];
|
|
|
+ $res = $this->checkNumber($data['cost']);
|
|
|
+ if(! $res) return [false,'成本请输入不超过两位小数并且大于0的数值'];
|
|
|
+ if(empty($data['retail_price'])) return [false,'零售价不能为空'];
|
|
|
+ $res = $this->checkNumber($data['retail_price']);
|
|
|
+ if(! $res) return [false,'零售价格请输入不超过两位小数并且大于0的数值'];
|
|
|
+ if(! empty($data['product_price'])){
|
|
|
+ $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
|
|
|
+ ->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;
|
|
|
+
|
|
|
+ $top_depart = Depart::where('del_time',0)->where('parent_id',0)->where('is_main',1)->value('id');
|
|
|
+ if($is_add){
|
|
|
+ $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}') AND top_depart_id = {$data['top_depart_id']}")
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ }else{
|
|
|
+ if(empty($data['id'])) return [false,'ID不能为空'];
|
|
|
+ $top_depart_id = Product::where('id',$data['id'])->value('top_depart_id');
|
|
|
+ $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}') AND top_depart_id = {$top_depart_id}")
|
|
|
+ ->where('id','<>',$data['id'])
|
|
|
+ ->where('del_time',0)
|
|
|
+ ->exists();
|
|
|
+ }
|
|
|
+ if($bool) return [false,'产品名称或编码不能重复'];
|
|
|
+
|
|
|
+ return [true, $data];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function fillData($data, $user){
|
|
|
+ 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();
|
|
|
+ $detail = ProductPriceDetail::where('del_time',0)
|
|
|
+ ->whereIn('product_id',array_unique(array_column($data['data'],'id')))
|
|
|
+ ->select('product_id','basic_type_id','price')
|
|
|
+ ->get()->toArray();
|
|
|
+ $basic_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'unit'),array_column($detail,'basic_type_id'))))
|
|
|
+ ->pluck('title','id')
|
|
|
+ ->toArray();
|
|
|
+ $detail_map = [];
|
|
|
+ foreach ($detail as $value){
|
|
|
+ $detail_map[$value['product_id']][] = $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ //是否总公司
|
|
|
+ $is_main = $user['is_all_depart'];
|
|
|
+ $top_depart = $user['depart_top'][0] ?? [];
|
|
|
+ foreach ($data['data'] as $key => $value){
|
|
|
+ $tmp = [];
|
|
|
+ if(isset($detail_map[$value['id']])){
|
|
|
+ $d = $detail_map[$value['id']];
|
|
|
+ foreach ($d as $v){
|
|
|
+ if(! $is_main && ($top_depart['basic_type_id'] != $v['basic_type_id'])) {
|
|
|
+ $is_show = 0;
|
|
|
+ }else{
|
|
|
+ $is_show = 1;
|
|
|
+ }
|
|
|
+ if($top_depart['basic_type_id'] != $v['basic_type_id']) {
|
|
|
+ $is_use = 0;
|
|
|
+ }else{
|
|
|
+ $is_use = 1;
|
|
|
+ }
|
|
|
+ $tmp[] = [
|
|
|
+ 'basic_type_id' => $v['basic_type_id'],
|
|
|
+ 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
|
|
|
+ 'price' => $v['price'],
|
|
|
+ 'is_show' => $is_show,
|
|
|
+ 'is_use' => $is_use
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $data['data'][$key]['product_price'] = $tmp;
|
|
|
+ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
|
|
|
+ $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
|
|
|
+ $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
|
|
|
+ $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
|
|
|
+ $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
|
|
|
+ }
|
|
|
+
|
|
|
+ return $data;
|
|
|
+ }
|
|
|
+}
|