zqy
2025-07-15 fd29ca6cef3074b7d43fc0885ffe5bfe730bfd42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
package com.ruoyi.web.controller.system;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.entity.sysUserInfo;
import com.ruoyi.domain.*;
import com.ruoyi.domain.dto.MarryInfoDto;
import com.ruoyi.service.*;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
 
/**
 * 用户信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController
{
    @Autowired
    private ISysUserService userService;
 
    @Autowired
    private ISysRoleService roleService;
 
    @Autowired
    private ISysDeptService deptService;
 
    @Autowired
    private ISysPostService postService;
    @Resource
    private ZInfoUserService zInfoUserService;
    @Resource
    private ZfFamilyService zfFamilyService;
 
    @Resource
    private ZfAncestorService zfAncestorService;
 
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
 
    MarrySelfService marrySelfService;
    @Resource
    MarryUserService marryUserService;
 
    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUser user)
    {
        startPage();
        System.out.println(user.getClanId());
        System.out.println("----------------++++++++++++==0------------");
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }
    @GetMapping("/listCommonUser")
    public TableDataInfo listCommonUser(SysUser user)
    {
        startPage();
    //    System.out.println(user.getClanId());
        System.out.println("----------------++++++++++++==0------------");
        List<SysUser> list = userService.selectCommonUserList(user);
        return getDataTable(list);
    }
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/exportCommonUser")
    public void exportCommonUser(HttpServletResponse response, SysUser user)
    {
        List<SysUser> list = userService.selectCommonUserList(user);
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        util.exportExcel(response, list, "用户数据");
    }
 
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUser user)
    {
        List<SysUser> list = userService.selectUserList(user);
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        util.exportExcel(response, list, "用户数据");
    }
 
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        List<SysUser> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = userService.importUser(userList, updateSupport, operName);
        return success(message);
    }
 
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        util.importTemplateExcel(response, "用户数据");
    }
 
    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
      //  userService.checkUserDataScope(userId);
        AjaxResult ajax = AjaxResult.success();
        List<SysRole> roles = roleService.selectRoleAll();
        //如果是admin则直接返回所有的角色,否则就返回不是管理员的角色信息
        ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        ajax.put("posts", postService.selectPostAll());
        if (StringUtils.isNotNull(userId))
        {
            SysUser sysUser = userService.selectUserById(userId);
            ajax.put(AjaxResult.DATA_TAG, sysUser);
            ajax.put("postIds", postService.selectPostListByUserId(userId));
//            ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
        }
        return ajax;
    }
 
    /**
     * 更新用户信息及关联的家庭/婚姻数据
     *
     * 核心流程:
     * 1. 更新基础用户信息
     * 2. 家庭成员专属处理(角色ID=102)
     *   2.1 构建家庭成员扩展信息
     *   2.2 处理家族继承关系(父母/配偶)
     *   2.3 更新配偶数据
     *   2.4 同步婚姻状态(含离婚特殊逻辑)
     *
     * @param user 前端传入的用户数据对象(含基础信息+家庭关系)
     * @return AjaxResult 统一响应格式(成功/错误信息)
     * @throws IllegalArgumentException 关键数据校验失败时抛出
     * @apiNote 仅角色ID=102(家庭成员)执行完整更新流程
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateUserInfo")
    public AjaxResult editUser(@Validated @RequestBody sysUserAndUserInfo user) {
        // ===================== 1. 更新基础用户信息 =====================
        SysUser us = new SysUser();
        us.setUserId(user.getSysId());       // 系统唯一ID(必填)
        us.setUserName(user.getUserName());  // 登录名(账号标识)
        us.setNickName(user.getNickName());  // 显示昵称
        us.setRoleId(user.getRoleId());      // 角色ID(102=家庭成员)
        us.setSex(user.getSex());            // 性别(0男/1女)
        us.setStatus(user.getStatus());      // 账户状态(启用/禁用)
        us.setRemark(user.getRemark());      // 备注信息
        userService.updateUser(us);          // 执行SysUser表更新
 
        // ===== 2. 家庭成员专属处理(仅角色ID=102时执行)=====
        if (user.getRoleId() != null && user.getRoleId().equals(102L)) {
            // ----- 2.1 构建家庭成员扩展信息对象 -----
            ZInfoUser infoUser = new ZInfoUser();
            infoUser.setUserId(user.getUserId());         // 用户业务ID
            infoUser.setOldName(user.getUserName());      // 历史名称(保留记录)
            infoUser.setNickName(user.getNickName());      // 当前昵称
            infoUser.setIsMyFamily(user.getIsMyFamily()); // 家庭成员标记(0/1)
            infoUser.setMomId(user.getMid());             // 母亲ID(可为空)
            infoUser.setFatherId(user.getFid());           // 父亲ID(可为空)
            // 婚姻状态空值保护(0未婚/1已婚/2离婚)
            infoUser.setMaritalStatus(user.getMaritalStatus() == null ? null : user.getMaritalStatus());
            // 性别格式转换(String → Integer)
            if (user.getSex() != null) infoUser.setSex(Integer.valueOf(user.getSex()));
            infoUser.setSysId(user.getSysId());           // 关联系统ID(外键)
            // 配偶ID设置(非必填)
            if(user.getSpouseId() != null) {
                infoUser.setSpouseId(user.getSpouseId());
            }
 
            // ----- 2.2 家族关系继承逻辑 -----
            // 优先获取当前数据库记录
            ZInfoUser byId = zInfoUserService.getById(user);
            Long myFamily = byId.getFamilyId();
            // 保留原家庭ID(确保数据连续性)
            if (myFamily != null) infoUser.setFamilyId(myFamily);
 
            /* 家族继承优先级:父亲 > 母亲 > 配偶 */
            // 2.2.1 继承父亲家族
            if (user.getFid() != null) {
                ZInfoUser father = zInfoUserService.getById(user.getFid());
                if (father != null) {
                    infoUser.setClanId(father.getClanId()); // 父系家族ID
                }
            }
            // 2.2.2 继承母亲家族(覆盖父亲ID)
            if(user.getMid() != null) {
                ZInfoUser mother = zInfoUserService.getById(user.getMid());
                if (mother != null) {
                    infoUser.setClanId(mother.getClanId()); // 母系家族ID
                }
            }
            // 2.2.3 配偶家族继承(最高优先级)
            if (user.getSpouseId() != null ){
                //将修改的对应用户原本id清空
                zInfoUserService.setSpouseOnly(user.getUserId());
                // 仅当用户首次成为家庭成员时继承配偶家族
                if (zInfoUserService.getById(user.getUserId()) == null ||
                        zInfoUserService.getById(user.getUserId()).getIsMyFamily() == 0) {
                    ZInfoUser spouse = zInfoUserService.getById(user.getSpouseId());
                    infoUser.setFamilyId(spouse.getFamilyId()); // 配偶家庭ID
                    // 继承配偶父母信息(建立姻亲关系)
                    if(spouse.getFatherId() != null || spouse.getMomId() != null) {
                        infoUser.setFatherId(spouse.getFatherId());
                        infoUser.setMomId(spouse.getMomId());
                    }
                    infoUser.setClanId(spouse.getClanId()); // 配偶家族ID
                }
            }
 
            // ----- 2.3 保存家庭信息 -----
            // 关键校验:家庭ID为空说明不满足家庭成员条件
            if(infoUser.getFamilyId() != null ) {
                zInfoUserService.saveOrUpdate(infoUser); // 持久化到ZInfoUser表
            } else {
                // ⚠️ 家庭ID缺失阻断操作
                return AjaxResult.error("家庭成员不能修改为普通用户");
            }
 
            // ----- 2.4 更新配偶信息 -----
            ZInfoUser byId2 = zInfoUserService.getById(user.getUserId()); // 刷新数据
            ZInfoUser zu = new ZInfoUser();
            // 仅当原记录存在配偶时更新
            if(byId2.getSpouseId() != null) {
                zu.setUserId(byId2.getSpouseId()); // 配偶ID
                // 同步婚姻状态(空值保护)
                zu.setMaritalStatus(user.getMaritalStatus() == null ? null : user.getMaritalStatus());
                // 建立双向关联
                if(user.getSpouseId() != null) {
                    zu.setSpouseId(infoUser.getUserId()); // 绑定当前用户
                    zu.setFatherId(infoUser.getFatherId()); // 同步父亲
                    Long spouseMoM = zInfoUserService.getById(user.getSpouseId()).getMomId();
                    Long spouseDad = zInfoUserService.getById(user.getSpouseId()).getFatherId();
                    zu.setMomId(spouseMoM);
                    zu.setFatherId(spouseDad);
                    zu.setMomId(infoUser.getMomId());      // 同步母亲
                }
                zInfoUserService.updateById(zu); // 更新配偶记录
            }
 
            // ===== 3. 婚姻状态专项处理 =====
                // 3.1 构建本人婚姻信息
                MarryInfoDto marrySelf = new MarryInfoDto();
                marrySelf.setUid(user.getSysId()); // 系统ID
                marrySelf.setName(user.getUserName()); // 当前用户名
                marrySelf.setSex(us.getSex().equals("0") ? "男" : "女"); // 性别转换
 
                // 3.2 配偶婚姻信息同步
                if(byId2.getSpouseId() != null) {
                    // 自动设置已婚状态(默认值逻辑)
                    infoUser.setMaritalStatus(infoUser.getMaritalStatus() == null ? 1 : infoUser.getMaritalStatus());
 
                    // 获取配偶最新数据
                    ZInfoUser byId1 = zInfoUserService.getById(byId2.getSpouseId());
                    // 更新本人婚姻档案
                    marrySelf.setSpouseName(byId1.getOldName()); // 配偶原名
                    marrySelf.setMarryStatus(infoUser.getMaritalStatus()); // 婚姻状态
                    marrySelf.setSpouseSex(byId1.getSex().equals(0) ? "男" : "女"); // 配偶性别
                    marrySelfService.updateData(marrySelf); // 持久化
 
                    // 3.3 反向更新配偶婚姻档案
                    MarryInfoDto spouse = new MarryInfoDto();
                    spouse.setUid(byId1.getSysId()); // 配偶系统ID
                    spouse.setSpouseName(byId2.getOldName()); // 本人历史名
                    spouse.setMarryStatus(infoUser.getMaritalStatus()); // 同步状态
                    spouse.setSpouseSex(marrySelf.getSex()); // 本人性别
                    marrySelfService.updateData(spouse); // 持久化
                }
 
                // 3.4 离婚特殊处理(状态码=2)
                if(user.getMaritalStatus() != null && user.getMaritalStatus() == 2) {
                    // 3.4.1 获取配偶信息
                    Long spouseId = zInfoUserService.getById(user.getUserId()).getSpouseId();
                    String spouseName = null;
                    Long spouseSysId = 0L;
                    if (spouseId != null) {
                        ZInfoUser spouseInfo = zInfoUserService.getById(spouseId);
                        spouseName = spouseInfo.getOldName();
                        spouseSysId = spouseInfo.getSysId();
 
                        // 3.4.2 生成本人离婚记录
                        MarryUser marryUser = new MarryUser();
                        marryUser.setUid(user.getSysId()); // 本人ID
                        marryUser.setName(spouseName);     // 配偶名称
                        marryUser.setSex(user.getSex().equals("0") ? "男" : "女"); // 本人性别
                        marryUser.setMarryStatus(2);       // 离婚状态
                        marryUserService.save(marryUser);  // 写入历史表
                    }
 
                    // 3.4.3 生成配偶离婚记录
                    MarryUser exSpouseRecord = new MarryUser();
                    exSpouseRecord.setUid(spouseSysId);        // 配偶ID
                    exSpouseRecord.setName(infoUser.getOldName()); // 本人历史名
                    exSpouseRecord.setSex(us.getSex().equals("0") ? "男" : "女"); // 本人性别
                    exSpouseRecord.setMarryStatus(2);          // 离婚状态
                    marryUserService.save(exSpouseRecord);     // 写入历史表
                }
 
            return AjaxResult.success(); // 操作成功
        } else {
            // ⚠️ 非家庭成员直接阻断
            return AjaxResult.error("家庭成员不能修改为普通用户");
        }
    }
 
    /**
     * 新增用户接口
     *
     * 核心功能:
     * 1. 创建基础用户信息
     * 2. 校验用户唯一性(登录名/手机号/邮箱)
     * 3. 分配用户角色
     * 4. 家庭成员特殊处理(角色ID=102)
     *   4.1 创建家庭扩展信息
     *   4.2 建立家族关系(父母/配偶)
     *   4.3 初始化婚姻信息
     *
     * @param userAll 前端传入的用户数据对象(包含基础信息+家庭关系)
     * @return AjaxResult 统一响应格式(成功/错误信息)
     * @apiNote 需具备'system:user:add'权限才能访问[1,4](@ref)
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')") // 权限校验注解
    @Log(title = "用户管理", businessType = BusinessType.INSERT) // 操作日志记录
    @PostMapping
    public AjaxResult add(@Validated @RequestBody sysUserAndUserInfo userAll) {
        // ===================== 1. 初始化用户基础信息 =====================
        SysUser user = new SysUser();
        user.setAvatar(userAll.getAvatar());        // 用户头像
        user.setPassword("123456");                // 默认密码(需加密)
        user.setNickName(userAll.getNickName());    // 用户昵称
        user.setUserName(userAll.getUserName());    // 登录用户名
        user.setPhonenumber(userAll.getPhonenumber()); // 手机号
        user.setRoleId(userAll.getRoleId());        // 主角色ID
        user.setRoleIds(userAll.getRoleIds());      // 多角色ID集合
        user.setStatus(userAll.getStatus());        // 账户状态(0正常/1停用)
        user.setSex(userAll.getSex());              // 性别(0男/1女)
        user.setRemark(userAll.getRemark());        // 备注信息
        user.setUpdateBy(userAll.getUpdateBy());    // 更新人
        user.setClanId(userAll.getClanId());        // 家族ID
 
        // ===================== 2. 唯一性校验(登录名/手机号/邮箱) =====================
        // 2.1 登录名校验
        if (!userService.checkNickNameUnique(user)) {
            return error("新增用户'" + user.getNickName() + "'失败,登录账号已存在");
        }
        // 2.2 手机号校验(非空时验证)
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user)) {
            return error("新增用户'" + user.getUserName() + "'失败,手机号码已存在");
        }
        // 2.3 邮箱校验(非空时验证)
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user)) {
            return error("新增用户'" + user.getUserName() + "'失败,邮箱账号已存在");
        }
 
        // ===================== 3. 保存核心用户数据 =====================
        user.setCreateBy(getUsername()); // 设置创建人
        // 密码加密处理(使用系统安全工具)[5](@ref)
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        userService.insertUser(user); // 写入sys_user主表
 
        // ===================== 4. 用户角色关联处理 =====================
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(user.getUserId()); // 新用户ID
        sysUserRole.setRoleId(user.getRoleId()); // 主角色ID
        List<SysUserRole> sysUserRoles = new ArrayList<>();
        sysUserRoles.add(sysUserRole);
        // 批量写入用户-角色关系表
        sysUserRoleMapper.batchUserRole(sysUserRoles);
 
        // ===================== 5. 家庭成员特殊处理(角色ID=102) =====================
        if (userAll.getRoleId() == 102) {
            // ----- 5.1 初始化家庭成员扩展信息 -----
            ZInfoUser infoUser = new ZInfoUser();
            infoUser.setSysId(user.getUserId());        // 关联系统用户ID
            infoUser.setNickName(userAll.getNickName()); // 当前昵称
            infoUser.setOldName(userAll.getUserName());  // 历史名称(保留记录)
            infoUser.setPhoneNumber(userAll.getPhonenumber()); // 联系方式
            // 性别格式转换(String → Integer)
            infoUser.setSex(userAll.getSex().equals("0") ? 0 : 1);
            infoUser.setSpouseId(userAll.getSpouseId()); // 配偶ID
            infoUser.setFatherId(userAll.getFid());      // 父亲ID
            infoUser.setMomId(userAll.getMid());          // 母亲ID
            infoUser.setIsMyFamily(userAll.getIsMyFamily()); // 家庭成员标记(0/1)
 
            // ----- 5.2 创建新家庭(当用户标记为本家时) -----
            if (userAll.getIsMyFamily() == 1) {
                ZfFamily zfFamily = new ZfFamily();
                zfFamily.setName(userAll.getNickName() + "的家庭"); // 家庭名称
                zfFamilyService.addFamily(zfFamily);     // 创建家庭记录
                infoUser.setFamilyId(zfFamily.getId());  // 绑定家庭ID
            }
 
            // ----- 5.3 家族关系继承逻辑 -----
            /* 继承优先级:父亲家族 → 母亲家族 → 配偶家族 */
            if (userAll.getFid() != null && userAll.getMid() != null) {
                // 5.3.1 获取父母信息
                ZInfoUser father = zInfoUserService.getById(userAll.getFid());
                ZInfoUser mother = zInfoUserService.getById(userAll.getMid());
 
                // 5.3.2 优先继承父亲家族(当父亲是本家)
                if (father.getIsMyFamily() == 1) {
                    infoUser.setClanId(father.getClanId());
                }
                // 5.3.3 其次继承母亲家族(当母亲是本家)
                else if (mother.getIsMyFamily() == 1) {
                    infoUser.setClanId(mother.getClanId());
                }
            } else {
                // 5.3.4 无父母信息时继承配偶家族
                ZInfoUser spouse = zInfoUserService.getById(userAll.getSpouseId());
                infoUser.setFamilyId(spouse.getFamilyId());
                infoUser.setClanId(spouse.getClanId());
            }
 
            // ----- 5.4 婚姻信息处理 -----
            // 5.4.1 初始化婚姻档案
            MarrySelf marrySelf = new MarrySelf();
            marrySelf.setUid(user.getUserId()); // 用户ID
            marrySelf.setName(userAll.getUserName()); // 姓名
            marrySelf.setSex(userAll.getSex().equals("0") ? "男" : "女"); // 性别转换
            // 婚姻状态(空值保护:0未婚)
            marrySelf.setMarryStatus(userAll.getMaritalStatus() == null ? 0 : userAll.getMaritalStatus());
 
            // 5.4.2 配偶存在时的特殊处理
            if (userAll.getSpouseId() != null) {
                // 自动设置已婚状态(默认值逻辑)
                infoUser.setMaritalStatus(userAll.getMaritalStatus() == null ? 1 : userAll.getMaritalStatus());
 
                // 获取配偶信息
                ZInfoUser byId = zInfoUserService.getById(userAll.getSpouseId());
 
                // 更新本人婚姻档案
                marrySelf.setSpouseName(byId.getOldName()); // 配偶原名
                marrySelf.setMarryStatus(userAll.getMaritalStatus() == null ? 1 : userAll.getMaritalStatus());
                marrySelf.setSpouseSex(byId.getSex().equals(0) ? "男" : "女"); // 配偶性别
 
                // 5.4.3 更新配偶婚姻档案
                MarryInfoDto spouseDto = new MarryInfoDto();
                spouseDto.setUid(byId.getSysId()); // 配偶系统ID
                spouseDto.setSpouseName(userAll.getUserName()); // 当前用户名称
                spouseDto.setMarryStatus(userAll.getMaritalStatus() == null ? 1 : userAll.getMaritalStatus());
                spouseDto.setSpouseSex(marrySelf.getSex()); // 当前用户性别
                marrySelfService.updateData(spouseDto); // 持久化
            }
            marrySelfService.save(marrySelf); // 保存本人婚姻记录
 
            // ----- 5.5 家庭成员信息保存 -----
            if (userAll.getRoleId().equals(102L)) {
                infoUser.setRoleId(3L); // 设置家庭角色ID
                zInfoUserService.save(infoUser); // 保存家庭扩展信息
 
                // 5.5.1 更新配偶关联信息
                if (userAll.getSpouseId() != null) {
                    ZInfoUser zu = new ZInfoUser();
                    zu.setUserId(userAll.getSpouseId()); // 配偶ID
                    zu.setSpouseId(infoUser.getUserId()); // 绑定当前用户
                    Long spouseMoM = zInfoUserService.getById(userAll.getSpouseId()).getMomId();
                    Long spouseDad = zInfoUserService.getById(userAll.getSpouseId()).getFatherId();
                    zu.setMomId(spouseMoM);
                    zu.setFatherId(spouseDad);
                    // 同步婚姻状态(空值保护:1已婚)
                    zu.setMaritalStatus(userAll.getMaritalStatus() == null ? 1 : userAll.getMaritalStatus());
                    zInfoUserService.updateById(zu); // 更新配偶记录
                }
            }
        }
 
        return AjaxResult.success(); // 操作成功响应
    }
 
    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysUser user)
    {
        userService.checkUserAllowed(user);
        userService.checkUserDataScope(user.getUserId());
        if (!userService.checkNickNameUnique(user))
        {
            return error("修改用户'" + user.getNickName() + "'失败,登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败,手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
        }
        user.setUpdateBy(getUsername());
        return toAjax(userService.updateUser(user));
    }
 
    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        if (ArrayUtils.contains(userIds, getUserId()))
        {
            return error("当前用户不能删除");
        }
//        for(Long uid: userIds) {
//            zInfoUserService.getInfoBysysId();
//        }
       // zInfoUserService.removeByUserName( userService.findUserNames(userIds));
        return toAjax(userService.deleteUserByIds(userIds));
    }
 
    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('system:user:resetPwd')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody SysUser user)
    {
  //      userService.checkUserAllowed(user);
//        userService.checkUserDataScope(user.getUserId());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setUpdateBy(getUsername());
        return toAjax(userService.resetPwd(user));
    }
 
    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysUser user)
    {
        userService.checkUserAllowed(user);
        userService.checkUserDataScope(user.getUserId());
        user.setUpdateBy(getUsername());
        return toAjax(userService.updateUserStatus(user));
    }
 
    /**
     * 根据用户编号获取授权角色
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping("/authRole/{userId}")
    public AjaxResult authRole(@PathVariable("userId") Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        SysUser user = userService.selectUserById(userId);
        List<SysRole> roles = roleService.selectRolesByUserId(userId);
        ajax.put("user", user);
        ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        return ajax;
    }
 
    /**
     * 用户授权角色
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.GRANT)
    @PutMapping("/authRole")
    public AjaxResult insertAuthRole(Long userId, Long[] roleIds)
    {
        userService.checkUserDataScope(userId);
        userService.insertUserAuth(userId, roleIds);
        return success();
    }
 
    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/deptTree")
    public AjaxResult deptTree(SysDept dept)
    {
        return success(deptService.selectDeptTreeList(dept));
    }
 
}