Repository: TencentBlueKing/bk-user

Issue:
【BUG】ldap/mad目录配置拉取周期不生效
**用文字描述你遇到的问题**
ldap/mad目录配置拉取周期，Saas页面保存成功，配置不生效。原本为拉取周期为0的目录，配置为60的目录。对应表PeriodTask 中无相应定时任务注册


**重现方法**

配置拉取周期为0的ldap/mad目录，然后更新拉取周期。观察PeriodTask是否更新对应目录的同步任务记录。

**预期行为**

同步周期变更

**版本**
- 2.5.2

**如果是 SaaS 页面问题，请提供使用的操作系统和浏览器信息**
 - OS: [e.g. iOS]
 - Browser [e.g. chrome, safari]
 - Version [e.g. 22]

**额外信息**

任何你觉得有助于问题解决的内容



Discussion:
https://github.com/TencentBlueKing/bk-user/blob/705515df50c5d233f182d642d0344cc6b6093eae/src/api/bkuser_core/api/web/category/views.py#L187
![image](https://user-images.githubusercontent.com/49228807/230857451-a5110f7b-5e6d-43ad-88ec-98588cb2dcd7.png)

上面del ns_settings[key] 逻辑，导致ns_settings为{}，下面逻辑无法执行，导致信号机制无法触发=>同步任务无法更新
https://github.com/TencentBlueKing/bk-user/blob/705515df50c5d233f182d642d0344cc6b6093eae/src/api/bkuser_core/api/web/category/views.py#L199

![image](https://user-images.githubusercontent.com/49228807/230857760-b7ea1e3d-593e-471d-9a3e-5bde45fc4527.png)


<img width="983" alt="image" src="https://user-images.githubusercontent.com/17491932/231107163-d9cd6b3f-e64e-43d2-8f83-b9a5b529a49a.png">


最后return需要查询所有的，因为可能新建的，所以要再次查询
```python
if ns_settings:
    db_settings = Setting.objects.filter(meta__in=metas, category_id=category_id).all()
return Response(self.get_serializer_class()(db_settings, many=True).data)
```

Resolving patch:
diff --git a/src/api/bkuser_core/api/web/category/views.py b/src/api/bkuser_core/api/web/category/views.py
--- a/src/api/bkuser_core/api/web/category/views.py
+++ b/src/api/bkuser_core/api/web/category/views.py
@@ -184,23 +184,32 @@ def put(self, request, *args, **kwargs):
         metas = list_setting_metas(category.type, None, namespace)
         db_settings = Setting.objects.filter(meta__in=metas, category_id=category_id).all()
 
+        # 更新已存在
         for s in db_settings:
             key = s.meta.key
-            if key in ns_settings:
-                in_value = ns_settings[key]
+            if key not in ns_settings:
+                continue
+            in_value = ns_settings[key]
+            # 值变换才更新
+            if s.value != in_value["value"] or s.enabled != in_value["enabled"]:
                 s.value = in_value["value"]
                 s.enabled = in_value["enabled"]
                 s.save()
-
-                del ns_settings[key]
+                post_setting_update.send(
+                    sender=self,
+                    instance=s,
+                    operator=request.operator,
+                    extra_values={"request": request},
+                )
+            del ns_settings[key]
 
         metas_map = {m.key: m for m in metas}
-        # the left settings are new settings
+        # the left ns_settings are new settings
         for key, setting in ns_settings.items():
             if key not in metas_map:
                 continue
             meta = metas_map[key]
-            s, created = Setting.objects.update_or_create(
+            s, _ = Setting.objects.update_or_create(
                 meta=meta,
                 category_id=category_id,
                 defaults={
@@ -209,23 +218,17 @@ def put(self, request, *args, **kwargs):
                 },
             )
 
-            if created:
-                post_setting_create.send(
-                    sender=self,
-                    instance=s,
-                    operator=request.operator,
-                    extra_values={"request": request},
-                )
-            else:
-                # 仅当更新成功时才发送信号
-                post_setting_update.send(
-                    sender=self,
-                    instance=s,
-                    operator=request.operator,
-                    extra_values={"request": request},
-                )
+            post_setting_create.send(
+                sender=self,
+                instance=s,
+                operator=request.operator,
+                extra_values={"request": request},
+            )
+
+        if ns_settings:
+            # 有新建的setting内容，刷新db_settings
+            db_settings = Setting.objects.filter(meta__in=metas, category_id=category_id).all()
 
-        # Setting.objects.filter(meta__in=metas, category_id=category_id).all()
         return Response(self.get_serializer_class()(db_settings, many=True).data)
 
 


Test patch: