basicTypeRule($data,false); if(!$status) return [$status,$msg]; $update = $msg['data'][0]; BasicType::where('id',$data['id'])->update($update); return [true,'']; } public function basicTypeAdd($data){ list($status,$msg) = $this->basicTypeRule($data); if(!$status) return [$status,$msg]; BasicType::insert($msg['data']); return [true,'']; } public function basicTypeDel($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; BasicType::whereIn('id',$data['id'])->update([ 'del_time'=>time() ]); return [true,'']; } 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]; } 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]; } 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; } }