|
@@ -0,0 +1,233 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace App\Service;
|
|
|
|
|
+
|
|
|
|
|
+use App\Model\CustomFieldDefinitions;
|
|
|
|
|
+use App\Model\SysMenu;
|
|
|
|
|
+use App\Model\SysModules;
|
|
|
|
|
+use App\Model\SysOssTasks;
|
|
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
+
|
|
|
|
|
+class CustomFieldSettingService extends Service
|
|
|
|
|
+{
|
|
|
|
|
+ public function add($data,$user){
|
|
|
|
|
+ list($status,$msg) = $this->fieldRule($data,$user);
|
|
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
|
+
|
|
|
|
|
+ CustomFieldDefinitions::insert($data['data']);
|
|
|
|
|
+
|
|
|
|
|
+ DB::commit();
|
|
|
|
|
+ }catch (\Exception $exception){
|
|
|
|
|
+ DB::rollBack();
|
|
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return [true,''];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function edit($data,$user){
|
|
|
|
|
+ list($status,$msg) = $this->fieldRule($data,$user, false);
|
|
|
|
|
+ if(!$status) return [$status,$msg];
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ DB::beginTransaction();
|
|
|
|
|
+
|
|
|
|
|
+ $update = $msg['data'][0];
|
|
|
|
|
+ $model = new CustomFieldDefinitions();
|
|
|
|
|
+ $model->where('id', $data['id'])->update($update);
|
|
|
|
|
+
|
|
|
|
|
+ DB::commit();
|
|
|
|
|
+ }catch (\Exception $exception){
|
|
|
|
|
+ DB::rollBack();
|
|
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return [true,''];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function del($data, $user){
|
|
|
|
|
+ if(empty($data['id']) || ! is_array($data['id'])) return [false,'ID不能为空或格式错误'];
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ CustomFieldDefinitions::whereIn('id', $data['id'])->update([
|
|
|
|
|
+ 'del_time' => time()
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ }catch (\Exception $exception){
|
|
|
|
|
+ DB::rollBack();
|
|
|
|
|
+ return [false,$exception->getMessage()];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return [true,''];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function fieldCommon($data,$user, $field = []){
|
|
|
|
|
+ if(empty($field)) $field = CustomFieldDefinitions::$field;
|
|
|
|
|
+
|
|
|
|
|
+ $model = CustomFieldDefinitions::TopClear($user,$data);
|
|
|
|
|
+ $model = $model->where('del_time',0)
|
|
|
|
|
+ ->select($field)
|
|
|
|
|
+ ->orderby('id', 'desc');
|
|
|
|
|
+
|
|
|
|
|
+ if(! empty($data['field_label'])) $model->where('field_label', 'LIKE', '%'.$data['field_label'].'%');
|
|
|
|
|
+
|
|
|
|
|
+ return $model;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function list($data, $user){
|
|
|
|
|
+ $model = $this->fieldCommon($data, $user);
|
|
|
|
|
+ $list = $this->limit($model,'',$data);
|
|
|
|
|
+ $list = $this->fillList($list);
|
|
|
|
|
+
|
|
|
|
|
+ return [true, $list];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function fillList($data){
|
|
|
|
|
+ if(empty($data['data'])) return $data;
|
|
|
|
|
+
|
|
|
|
|
+ $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
|
|
|
|
|
+ $menu_map = (new SysMenuService())->getMap(array_unique(array_column($data['data'],'menu_id')));
|
|
|
|
|
+ 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]['menu_title'] = $menu_map[$value['menu_id']] ?? '';
|
|
|
|
|
+ $data['data'][$key]['field_type_title'] = CustomFieldDefinitions::State_Type[$value['field_type']] ?? '';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $data;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function fieldRule(&$data,$user, $is_add = true){
|
|
|
|
|
+ if(empty($data['menu_id'])) return [false, '菜单不能为空'];
|
|
|
|
|
+ $data['top_depart_id'] = $user['top_depart_id'];
|
|
|
|
|
+
|
|
|
|
|
+ $title = SysMenu::where('id', $data['menu_id'])->value('title');
|
|
|
|
|
+ if(empty($title)) return [false, '菜单不存在或已被删除'];
|
|
|
|
|
+
|
|
|
|
|
+ $bool = SysModules::where('del_time',0)
|
|
|
|
|
+ ->where('top_depart_id', $data['top_depart_id'])
|
|
|
|
|
+ ->where('menu_id', $data['menu_id'])
|
|
|
|
|
+ ->exists();
|
|
|
|
|
+ if(! $bool) return [false, $title . '不允许增加自定义表头'];
|
|
|
|
|
+
|
|
|
|
|
+ if(empty($data['data'])) return [false,'自定义数据不能为空'];
|
|
|
|
|
+
|
|
|
|
|
+ $title = array_column($data['data'],'field_label');
|
|
|
|
|
+ $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){
|
|
|
|
|
+ if(empty($value['field_label'])) return [false, '自定义项名称不能为空'];
|
|
|
|
|
+ if(empty($value['field_type'])) return [false, '自定义项类型不能为空'];
|
|
|
|
|
+ if(! isset(CustomFieldDefinitions::State_Type[$value['field_type']])) return [false, '自定义项类型错误'];
|
|
|
|
|
+
|
|
|
|
|
+ $top_depart_id = $user['top_depart_id'];
|
|
|
|
|
+ $data['data'][$key]['menu_id'] = $data['menu_id'];
|
|
|
|
|
+ $data['data'][$key]['top_depart_id'] = $top_depart_id;
|
|
|
|
|
+ $data['data'][$key]['upd_time'] = time();
|
|
|
|
|
+
|
|
|
|
|
+ if($is_add){
|
|
|
|
|
+ $data['data'][$key]['crt_time'] = time();
|
|
|
|
|
+ $fieldKey = 'f_' . substr(md5(microtime() . $value['field_label']), 0, 8);
|
|
|
|
|
+ $data['data'][$key]['field_key'] = $fieldKey;
|
|
|
|
|
+ $bool = CustomFieldDefinitions::where("field_label", $value['field_label'])
|
|
|
|
|
+ ->where('top_depart_id', $top_depart_id)
|
|
|
|
|
+ ->where('menu_id', $data['menu_id'])
|
|
|
|
|
+ ->where('del_time',0)
|
|
|
|
|
+ ->exists();
|
|
|
|
|
+ }else{
|
|
|
|
|
+ if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
|
|
|
|
|
+ $bool = CustomFieldDefinitions::where("field_label", $value['field_label'])
|
|
|
|
|
+ ->where('top_depart_id', $top_depart_id)
|
|
|
|
|
+ ->where('menu_id', $data['menu_id'])
|
|
|
|
|
+ ->where('id','<>',$data['id'])
|
|
|
|
|
+ ->where('del_time',0)
|
|
|
|
|
+ ->exists();
|
|
|
|
|
+ if(isset($value['field_key'])) unset($data['data'][$key]['field_key']);
|
|
|
|
|
+ }
|
|
|
|
|
+ if($bool) return [false,'自定义设置已存在'];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return [true, ''];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function syncCustomFieldData($id, $data, $user)
|
|
|
|
|
+ {
|
|
|
|
|
+ $top_depart_id = $user['top_depart_id'];
|
|
|
|
|
+ if (empty($data['menu_id'])) return [false, '菜单ID不能为空'];
|
|
|
|
|
+ $menuId = $data['menu_id'];
|
|
|
|
|
+
|
|
|
|
|
+ $definitions = CustomFieldDefinitions::where('menu_id', $menuId)
|
|
|
|
|
+ ->where('top_depart_id', $top_depart_id)
|
|
|
|
|
+ ->get();
|
|
|
|
|
+
|
|
|
|
|
+ $pendingTasks = [];
|
|
|
|
|
+
|
|
|
|
|
+ $time = time();
|
|
|
|
|
+ foreach ($definitions as $df) {
|
|
|
|
|
+ $key = $df->field_key;
|
|
|
|
|
+ // 如果请求数据中没有这个自定义字段的 key,跳过(支持局部更新)
|
|
|
|
|
+ if (!array_key_exists($key, $data)) continue;
|
|
|
|
|
+
|
|
|
|
|
+ $newVal = (string)($data[$key] ?? '');
|
|
|
|
|
+
|
|
|
|
|
+ // --- 附件类型逻辑处理 ---
|
|
|
|
|
+ if ($df->field_type === 'file') {
|
|
|
|
|
+ // 查询数据库中已有的旧路径
|
|
|
|
|
+ $oldVal = DB::table('custom_field_values')
|
|
|
|
|
+ ->where('model_id', $id)
|
|
|
|
|
+ ->where('definition_id', $df->id)
|
|
|
|
|
+ ->value('field_value') ?? '';
|
|
|
|
|
+
|
|
|
|
|
+ if ($newVal !== $oldVal) {
|
|
|
|
|
+ //产生一个【删除任务】
|
|
|
|
|
+ if (!empty($oldVal)) {
|
|
|
|
|
+ $pendingTasks[] = [
|
|
|
|
|
+ 'type' => 1, // 删除旧文件
|
|
|
|
|
+ 'url' => $oldVal,
|
|
|
|
|
+ 'top_depart_id' => $top_depart_id,
|
|
|
|
|
+ 'crt_time' => $time,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //产生一个【新增同步任务】
|
|
|
|
|
+ if (!empty($newVal)) {
|
|
|
|
|
+ $pendingTasks[] = [
|
|
|
|
|
+ 'type' => 2, // 新增同步到 OSS
|
|
|
|
|
+ 'url' => $newVal,
|
|
|
|
|
+ 'top_depart_id' => $top_depart_id,
|
|
|
|
|
+ 'crt_time' => $time,
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // --- 统一更新业务值表 (文本或附件路径) ---
|
|
|
|
|
+ DB::table('custom_field_values')->updateOrInsert(
|
|
|
|
|
+ [
|
|
|
|
|
+ 'model_id' => $id,
|
|
|
|
|
+ 'definition_id' => $df->id,
|
|
|
|
|
+ 'top_depart_id' => $top_depart_id,
|
|
|
|
|
+ ],
|
|
|
|
|
+ [
|
|
|
|
|
+ 'field_value' => $newVal, // 如果是删除,这里存入空字符串
|
|
|
|
|
+ 'upd_time' => time()
|
|
|
|
|
+ ]
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //需要执行的oss的任务 调用这个方法的地方在 事务结束后触发下队列 todo
|
|
|
|
|
+ if(! empty($pendingTasks)) SysOssTasks::insert($pendingTasks);
|
|
|
|
|
+
|
|
|
|
|
+ return [true, ''];
|
|
|
|
|
+ }
|
|
|
|
|
+}
|