123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Service;
- use App\Model\BasicType;
- /**
- * 基础分类设置相关
- */
- class BasicTypeService extends Service
- {
- /**
- * 基础类型编辑
- * @param $data
- * @return array
- */
- public function basicTypeEdit($data){
- list($status,$msg) = $this->basicTypeRule($data,false);
- if(!$status) return [$status,$msg];
- $update = $msg['data'][0];
- BasicType::where('id',$data['id'])->update($update);
- return [true,''];
- }
- /**
- * 基础类型新增
- * @param $data
- * @return array
- */
- public function basicTypeAdd($data){
- list($status,$msg) = $this->basicTypeRule($data);
- if(!$status) return [$status,$msg];
- BasicType::insert($msg['data']);
- return [true,''];
- }
- /**
- * 基础类型删除
- * @param $data
- * @return array
- */
- public function basicTypeDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- BasicType::whereIn('id',$data['id'])->update([
- 'del_time'=>time()
- ]);
- return [true,''];
- }
- /**
- * 基础类型列表
- * @param $data
- * @return array
- */
- public function basicTypeList($data){
- $model = BasicType::where('del_time',0)
- ->select('title','id','type')
- ->orderby('id', 'asc');
- if(! empty($data['type'])) $model->where('type',$data['type']);
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list);
- return [true, $list];
- }
- /**
- * 基础类型参数规则
- * @param $data
- * @param $is_check
- * @return array
- */
- public function basicTypeRule($data,$is_check = true){
- if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
- $type = array_column($data['data'],'type');
- $type = array_map(function($val) {
- return $val !== null ? $val : 0;
- }, $type);
- foreach ($type as $value){
- if(empty($value)) return [false,'类型不能为空!'];
- if(! isset(BasicType::$type[$value])) return [false,'类型不存在!'];
- }
- $map = [];
- foreach ($data['data'] as $value){
- if(! isset($map[$value['title']])){
- $map[$value['title']][] = $value['type'];
- }else{
- if(! in_array($value['type'],$map[$value['title']])){
- $map[$value['title']][] = $value['type'];
- }
- }
- }
- $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 && count($map[$value]) != $title_count[$value]) return [false,'同一归属类别下,名称不能重复'];
- }
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['type'] = $value['type'];
- $data['data'][$key]['upd_time'] = time();
- if($is_check){
- $bool = BasicType::where('title',$value['title'])
- ->where('type',$value['type'])
- ->where('del_time',0)
- ->exists();
- $data['data'][$key]['crt_time'] = time();
- }else{
- if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
- $bool = BasicType::where('title',$value['title'])
- ->where('type',$value['type'])
- ->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;
- foreach ($data['data'] as $key => $value){
- $data['data'][$key]['type_name'] = BasicType::$type[$value['type']] ?? '';
- }
- return $data;
- }
- }
|