cqp 1 ماه پیش
والد
کامیت
8f6f98ae90
5فایلهای تغییر یافته به همراه232 افزوده شده و 1 حذف شده
  1. 7 0
      app/Model/Priority.php
  2. 25 0
      app/Service/ExportFileService.php
  3. 138 0
      app/Service/ImportService.php
  4. 1 1
      app/Service/PriorityService.php
  5. 61 0
      config/excel/priority.php

+ 7 - 0
app/Model/Priority.php

@@ -22,4 +22,11 @@ class Priority extends DataScopeBaseModel
         self::TYPE_TWO => '节点',
         self::TYPE_THREE => '任务',
     ];
+
+    const IS_USE_ZERO = 0;
+    const IS_USE_ONE = 1;
+    const IS_USE = [
+        self::IS_USE_ZERO => '否',
+        self::IS_USE_ONE => '是',
+    ];
 }

+ 25 - 0
app/Service/ExportFileService.php

@@ -403,6 +403,31 @@ class ExportFileService extends Service
         return [true, $this->saveExportData($return, $header)];
     }
 
+    //优先级
+    public function priority($ergs, $user)
+    {
+        // 导出配置
+        $return = [];
+        $header_default = $user['e_header_default'];
+        $column = array_column($header_default, 'export');
+        $header = array_column($header_default, 'value');
+
+        $service = new PriorityService();
+        $model = $service->priorityCommon($ergs, $user);
+
+        $model->chunk(500, function ($data) use (&$return, $service, $column, $user) {
+            $data = $data->toArray();
+            $list['data'] = $data;
+
+            //订单数据
+            $list = $service->fillPriorityList($list, $user, true);
+            //返回数据
+            $this->fillData($list['data'], $column, $return);
+        });
+
+        return [true, $this->saveExportData($return, $header)];
+    }
+
     // 项目工资统计表
     public function exportEmployeeSalary($data, $user)
     {

+ 138 - 0
app/Service/ImportService.php

@@ -18,6 +18,7 @@ use App\Model\MonthlyDdOrder;
 use App\Model\MonthlyPsOrder;
 use App\Model\MonthlyPwOrder;
 use App\Model\PLeaveOverOrder;
+use App\Model\Priority;
 use App\Model\RuleSet;
 use App\Model\RuleSetDetails;
 use Illuminate\Support\Facades\DB;
@@ -45,6 +46,7 @@ class ImportService extends Service
         'overtimeOrder', // 加班单
         'feeOrder', // 费用项目单
         'RDOrder', // 研发支出辅助帐
+        'priority', // 优先级
     ];
 
     public function getTableTitleXls($data,$user){
@@ -3967,6 +3969,142 @@ class ImportService extends Service
         return [!empty($errors) ? implode('|', $errors) : "", $update_map, $maps];
     }
 
+    // 优先级
+    public function priorityImport($array, $user, $other_param)
+    {
+        $upload = $array[0];
+        list($status, $msg) = $this->compareTableAndReturn($upload, $other_param);
+        if (!$status) return [false, $msg];
+        $table_config = $msg;
+
+        unset($array[0]);
+        if (empty($array)) return [false, '导入数据不能为空'];
+
+        // 基础格式校验
+        list($array, $error) = $this->checkCommon($array, $table_config);
+        if (!empty($error)) return [0, $error];
+
+        // 2. 业务逻辑校验
+        list($error, $update_map) = $this->priorityCheck($array, $user, $table_config);
+        if (!empty($error)) return [0, $error];
+
+        $time = time();
+        $insert = [];
+        $update = [];
+
+        foreach ($array as $key => $value) {
+            $main_tmp = [];
+            foreach ($value as $k => $val){
+                if(!empty($table_config[$k]['is_main'])){
+                    $main_tmp[$table_config[$k]['key']] = $val;
+                }
+            }
+
+            if (isset($update_map[$key])) {
+                // 更新逻辑
+                $update[] = array_merge($main_tmp, [
+                    'id' => $update_map[$key],
+                ]);
+            } else {
+                // 新增逻辑
+                $main_tmp['top_depart_id'] = $user['top_depart_id'];
+                $main_tmp['crt_time'] = $time;
+                $insert[] = $main_tmp;
+            }
+        }
+
+        DB::beginTransaction();
+        try {
+            if (!empty($insert)) {
+                foreach (array_chunk($insert, 500) as $chunk) {
+                    Priority::insert($chunk);
+                }
+            }
+
+            if (!empty($update)) {
+                foreach ($update as $item) {
+                    $uId = $item['id'];
+                    unset($item['id']);
+                    Priority::where('id', $uId)->update($item);
+                }
+            }
+
+            DB::commit();
+        } catch (\Exception $e) {
+            DB::rollBack();
+            return [false, "导入失败:" . $e->getMessage()];
+        }
+        return [true, ''];
+    }
+
+    private function priorityCheck(&$array, $user, $table_config)
+    {
+        $keys = array_column($table_config, 'key');
+        $codeIdx = array_search('code', $keys);
+        $sortIdx = array_search('sort', $keys);
+        $typeIdx = array_search('type', $keys);
+        $isUseIdx = array_search('is_use', $keys);
+
+        $type_map = array_flip(Priority::TYPE_TITLE);
+        $isUse_map = array_flip(Priority::IS_USE);
+
+        // 获取当前企业下已存在的优先级,用于判断是新增还是更新
+        $dbMap = Priority::where('del_time', 0)
+            ->where('top_depart_id', $user['top_depart_id'])
+            ->get()
+            ->keyBy('code')
+            ->toArray();
+
+        $errors = [];
+        $update = [];
+        $excelCodes = [];
+
+        foreach ($array as $rowIndex => $value) {
+            $displayLine = $rowIndex + 1;
+            $valCode = trim($value[$codeIdx] ?? '');
+
+            if ($valCode === '') {
+                $errors[] = "第{$displayLine}行:编码不能为空";
+                continue;
+            }
+
+            // 校验 Excel 内部是否重复
+            if (isset($excelCodes[$valCode])) {
+                $errors[] = "第{$displayLine}行:编码[{$valCode}]在文件中重复出现";
+                continue;
+            }
+            $excelCodes[$valCode] = true;
+
+            // 类型
+            $e_text = $value[$typeIdx] ?? '';
+            if (!isset($type_map[$e_text])) {
+                $errors[] = "第{$displayLine}行:类型[{$e_text}]无效";
+            } else {
+                $array[$rowIndex][$typeIdx] = $type_map[$e_text];
+            }
+
+            // 是否启用
+            $e_text = $value[$isUseIdx] ?? '';
+            if (!isset($isUse_map[$e_text])) {
+                $errors[] = "第{$displayLine}行:是否启用[{$e_text}]无效";
+            } else {
+                $array[$rowIndex][$isUseIdx] = $isUse_map[$e_text];
+            }
+
+            // 数字格式校验
+            $sort = $value[$sortIdx];
+            if (filter_var($sort, FILTER_VALIDATE_INT) === false) return [false, "第{$displayLine}行排序字段排序字段必须是整数且不能含有小数点"];
+
+            // 如果数据库已存在,记录为更新操作
+            if (isset($dbMap[$valCode])) {
+                $update[$rowIndex] = $dbMap[$valCode]['id'];
+            }
+        }
+
+        $error_string = !empty($errors) ? implode('|', $errors) : "";
+        return [$error_string, $update];
+    }
+
     /**
      * 解析并校验时间
      */

+ 1 - 1
app/Service/PriorityService.php

@@ -127,7 +127,7 @@ class PriorityService extends Service
             $data['data'][$key]['upd_time'] = time();
 
             if(! isset($value['sort'])) return [false, '排序字段sort不存在'];
-            if(! is_numeric($value['sort'])) return [false, '排序字段sort非合法数字'];
+            if (filter_var($value['sort'], FILTER_VALIDATE_INT) === false) return [false, '排序字段sort必须是整数且不能含有小数点'];
 
             if(! isset(Priority::TYPE_TITLE[$value['type']])) return [false, 'type错误'];
             if($is_check){

+ 61 - 0
config/excel/priority.php

@@ -0,0 +1,61 @@
+<?php
+return [
+    "name" => "优先级",
+    "array" => [
+        [
+            'key' =>'code',
+            'export' =>'code',
+            'value' => '编码',
+            'required' => true,
+            'is_main' => true,
+            'default' => "",
+            'unique' => false,
+            'enums' => [],
+            'comments' => '必填'
+        ],
+        [
+            'key' =>'title',
+            'export' =>'title',
+            'value' => '名称',
+            'required' => true,
+            'is_main' => true,
+            'default' => "",
+            'unique' => false,
+            'enums' => [],
+            'comments' => '必填'
+        ],
+        [
+            'key' =>'type',
+            'export' =>'type_title',
+            'value' => '类型',
+            'required' => true,
+            'is_main' => true,
+            'default' => 0,
+            'unique' => false,
+            'enums' => array_values(\App\Model\Priority::TYPE_TITLE),
+            'comments' => '必填'
+        ],
+        [
+            'key' =>'is_use',
+            'export' =>'is_use_title',
+            'value' => '是否启用',
+            'required' => true,
+            'is_main' => true,
+            'default' => 0,
+            'unique' => false,
+            'enums' => array_values(\App\Model\Priority::IS_USE),
+            'comments' => '必填'
+        ],
+        [
+            'key' =>'sort',
+            'export' =>'sort',
+            'value' => '排序字段',
+            'required' => true,
+            'is_main' => true,
+            'default' => 0,
+            'unique' => false,
+            'enums' => [],
+            'comments' => '必填'
+        ]
+    ]
+];