|
|
@@ -420,35 +420,56 @@ class ItemService extends Service
|
|
|
->toArray();
|
|
|
}
|
|
|
|
|
|
- private function checkDelete($data, $type = 1){
|
|
|
- $id = $data['id'];
|
|
|
- if($type == 1){
|
|
|
- $item = Item::where('del_time',0)
|
|
|
- ->where('id', $id)
|
|
|
- ->first();
|
|
|
- if($item->state == Item::TYPE_THREE) return [false, '项目已完成'];
|
|
|
- $bool = ItemNode::where('del_time',0)
|
|
|
- ->where('item_id', $id)
|
|
|
- ->exists();
|
|
|
- if($bool) return [false, '项目下已存在节点'];
|
|
|
- $bool = ItemNodeMission::where('del_time',0)
|
|
|
- ->where('item_id', $id)
|
|
|
- ->exists();
|
|
|
- if($bool) return [false, '项目下已存在任务'];
|
|
|
- }elseif ($type == 2){
|
|
|
- $item = ItemNode::where('del_time',0)
|
|
|
- ->where('id', $id)
|
|
|
- ->first();
|
|
|
- if($item->state == ItemNode::TYPE_THREE) return [false, '节点已完成'];
|
|
|
- $bool = ItemNodeMission::where('del_time',0)
|
|
|
- ->where('item_node_id', $id)
|
|
|
+ private function checkDelete($data, $type = 1)
|
|
|
+ {
|
|
|
+ $id = $data['id'] ?? null;
|
|
|
+ if (!$id) return [false, '参数错误'];
|
|
|
+
|
|
|
+ // 1. 定义配置数组,将不同类型的校验逻辑抽离,消除 if-else 臃肿
|
|
|
+ $config = [
|
|
|
+ 1 => [
|
|
|
+ 'model' => Item::class,
|
|
|
+ 'name' => '项目',
|
|
|
+ 'relations' => [
|
|
|
+ ['model' => ItemNode::class, 'key' => 'item_id', 'msg' => '项目下已存在节点'],
|
|
|
+ ['model' => ItemNodeMission::class, 'key' => 'item_id', 'msg' => '项目下已存在任务'],
|
|
|
+ ]
|
|
|
+ ],
|
|
|
+ 2 => [
|
|
|
+ 'model' => ItemNode::class,
|
|
|
+ 'name' => '节点',
|
|
|
+ 'relations' => [
|
|
|
+ ['model' => ItemNodeMission::class, 'key' => 'item_node_id', 'msg' => '节点下已存在任务'],
|
|
|
+ ]
|
|
|
+ ],
|
|
|
+ 3 => [
|
|
|
+ 'model' => ItemNodeMission::class,
|
|
|
+ 'name' => '任务',
|
|
|
+ 'relations' => []
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+
|
|
|
+ $cfg = $config[$type] ?? null;
|
|
|
+ if (!$cfg) return [false, '无效的类型'];
|
|
|
+
|
|
|
+ // 2. 统一查询主体对象
|
|
|
+ $target = $cfg['model']::where('del_time', 0)->find($id);
|
|
|
+
|
|
|
+ // 3. 健壮性检查:防止操作不存在的记录
|
|
|
+ if (!$target) return [false, "未找到该{$cfg['name']}"];
|
|
|
+
|
|
|
+ // 4. 状态校验:统一判断是否已完成
|
|
|
+ // 建议在 Model 中统一定义 TYPE_THREE 的常量名或方法
|
|
|
+ if ($target->state == $cfg['model']::TYPE_THREE) {
|
|
|
+ return [false, "{$cfg['name']}已完成"];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 5. 关联依赖校验
|
|
|
+ foreach ($cfg['relations'] as $rel) {
|
|
|
+ $exists = $rel['model']::where('del_time', 0)
|
|
|
+ ->where($rel['key'], $id)
|
|
|
->exists();
|
|
|
- if($bool) return [false, '节点下已存在任务'];
|
|
|
- }else{
|
|
|
- $item_node_mission = ItemNodeMission::where('del_time',0)
|
|
|
- ->where('id', $id)
|
|
|
- ->first();
|
|
|
- if($item_node_mission->state == ItemNodeMission::TYPE_THREE) return [false, '任务已完成'];
|
|
|
+ if ($exists) return [false, $rel['msg']];
|
|
|
}
|
|
|
|
|
|
return [true, ''];
|
|
|
@@ -457,6 +478,9 @@ class ItemService extends Service
|
|
|
public function itemDel($data){
|
|
|
if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
|
|
|
+ list($status, $msg) = $this->checkDelete($data);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
try {
|
|
|
DB::beginTransaction();
|
|
|
$time = time();
|
|
|
@@ -1056,6 +1080,9 @@ class ItemService extends Service
|
|
|
public function itemNodeDel($data){
|
|
|
if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
|
|
|
+ list($status, $msg) = $this->checkDelete($data,2);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
+
|
|
|
try {
|
|
|
DB::beginTransaction();
|
|
|
$time = time();
|
|
|
@@ -1561,6 +1588,8 @@ class ItemService extends Service
|
|
|
public function itemNodeMissionDel($data){
|
|
|
if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
|
|
|
|
|
|
+ list($status, $msg) = $this->checkDelete($data,3);
|
|
|
+ if(! $status) return [false, $msg];
|
|
|
try {
|
|
|
DB::beginTransaction();
|
|
|
$time = time();
|
|
|
@@ -1662,8 +1691,8 @@ class ItemService extends Service
|
|
|
if(empty($value['data_id'])) return [false, '人员ID不能为空'];
|
|
|
$empId = $value['data_id'] ?? 0;
|
|
|
$empName = $empDisplayMap[$empId] ?? "ID:{$empId}";
|
|
|
- if(empty($data['order_time'])) return [false, '提交日期不能为空'];
|
|
|
- $order_time = $this->changeDateToDate($data['order_time']);
|
|
|
+ if(empty($value['order_time'])) return [false, '提交日期不能为空'];
|
|
|
+ $order_time = $this->changeDateToDate($value['order_time']);
|
|
|
if($order_time > $customer['end_time'] || $order_time < $customer['start_time']) return [false, $t . "提交日期必须在任务周期内"];
|
|
|
$data['data'][$key]['order_time'] = $order_time;
|
|
|
|