123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- <?php
- namespace App\Service;
- use App\Model\BasicType;
- use App\Model\Employee;
- use App\Model\Product;
- use App\Model\ProductActivityPrice;
- use App\Model\ProductCategory;
- use App\Model\ProductInfo;
- use App\Model\ProductIntroduction;
- use App\Model\ProductPriceDetail;
- use App\Model\SeeRange;
- use Illuminate\Support\Facades\DB;
- /**
- * 产品管理
- */
- class ProductService extends Service
- {
- /**
- * 产品分类编辑
- * @param $data
- * @param $user
- * @return array
- */
- public function productCategoryEdit($data,$user){
- list($status,$msg) = $this->productCategoryRule($data,$user,false);
- if(!$status) return [$status,$msg];
- $update = $msg['data'][0];
- $model = new ProductCategory();
- $model->where('id',$data['id'])->update($update);
- return [true,''];
- }
- /**
- * 产品分类新增
- * @param $data
- * @param $user
- * @return array
- */
- public function productCategoryAdd($data,$user){
- list($status,$msg) = $this->productCategoryRule($data,$user);
- if(!$status) return [$status,$msg];
- ProductCategory::insert($msg['data']);
- return [true,''];
- }
- /**
- * 产品分类删除
- * @param $data
- * @return array
- */
- public function productCategoryDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $bool = Product::where('del_time',0)
- ->where('product_category_id',$data['id'])
- ->exists();
- if($bool) return [false,'产品分类下已添加产品,操作失败'];
- try {
- DB::beginTransaction();
- ProductCategory::where('id',$data['id'])->update([
- 'del_time' => time()
- ]);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true,''];
- }
- /**
- * 产品分类列表
- * @param $data
- * @param $user
- * @return array
- */
- public function productCategoryList($data,$user){
- $model = ProductCategory::TopClear($user,$data);
- $model = $model->where('del_time',0)
- ->select('title','id','parent_id')
- ->orderby('id','desc');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- $list = $model->get()->toArray();
- $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]];
- }
- /**
- * 产品分类参数规则
- * @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->title = $data['title'];
- $model->code = $data['code'] ?? '';
- $model->sn_code = $data['sn_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);
- }
- 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,''];
- }
- /**
- * 产品新增
- * @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->title = $data['title'];
- $model->code = $data['code'] ?? '';
- $model->sn_code = $data['sn_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['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,''];
- }
- /**
- * 产品删除
- * @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]);
- 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,''];
- }
- /**
- * 产品详情
- * @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(! 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['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];
- }
- /**
- * 产品列表
- * @param $data
- * @param $user
- * @return array
- */
- public function productList($data,$user){
- $model = Product::ProductClear($user,$data);
- $model = $model->where('del_time',0)
- ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','cost','state','crt_id','crt_time','mark','depart_id','top_depart_id','sn_code')
- ->orderby('id', 'desc');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(isset($data['state'])) $model->where('state', $data['state']);
- if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list,$user,$data);
- return [true, $list];
- }
- /**
- * 产品参数规则
- * @param $data
- * @param $is_add
- * @return array
- */
- public function productRule(&$data, $user, $is_add = true){
- if(empty($data['title'])) return [false,'产品名称不能为空'];
- if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
- if(empty($data['code'])) return [false,'产品编码不能为空'];
- if(empty($data['sn_code'])) return [false,'产品SN编码不能为空'];
- 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;
- }
- //总社id
- $top_depart_id = $user['head'] ?? [];
- $top_depart_id = $top_depart_id['id'] ?? 0;
- if($is_add){
- $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}' OR sn_code = '{$data['sn_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']}' OR title = '{$data['title']}' OR sn_code = '{$data['sn_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,'产品名称、编码与SN编码不能重复'];
- 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')))
- ->pluck('title','id')
- ->toArray();
- $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
- ->orWhere('type',BasicType::type_22)
- ->pluck('title','id')
- ->toArray();
- //产品使用价格
- $detail_map = $this->getProductPrice(array_column($data['data'],'id'), $type);
- //是否总公司
- $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;
- }
- //获取产品字典
- public function getProductDetail($product_id = []){
- if(empty($product_id)) return [];
- $pro = Product::whereIn('id', $product_id)->get()->toArray();
- return array_column($pro,null,'id');
- }
- //获取产品使用价格
- public function getProductPrice($product_id = [], $type = 1){
- //type 1 采购 2 合同
- $detail_map = [];
- if($type == 1) {
- //分社价
- $detail = ProductPriceDetail::where('del_time',0)
- ->whereIn('product_id',$product_id)
- ->select('product_id','basic_type_id','price')
- ->get()->toArray();
- foreach ($detail as $value){
- $detail_map[$value['product_id']][] = $value;
- }
- }else{
- //活动价格
- $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();
- 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;
- }
- }
|