maven
2 天以前 9af5090f5d4597fbcfdf53a71189175892d29109
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
package com.ruoyi.staff.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.SysUserDept;
import com.ruoyi.project.system.mapper.SysDeptMapper;
import com.ruoyi.project.system.mapper.SysUserDeptMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.staff.mapper.SchemeInsuranceDetailMapper;
import com.ruoyi.staff.pojo.SchemeApplicableStaff;
import com.ruoyi.staff.mapper.SchemeApplicableStaffMapper;
import com.ruoyi.staff.pojo.SchemeInsuranceDetail;
import com.ruoyi.staff.service.SchemeApplicableStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 社保方案适用人员表 服务实现类
 * </p>
 *
 * @author 芯导软件(江苏)有限公司
 * @since 2026-03-05 11:50:17
 */
@Service
public class SchemeApplicableStaffServiceImpl extends ServiceImpl<SchemeApplicableStaffMapper, SchemeApplicableStaff> implements SchemeApplicableStaffService {
 
    @Autowired
    private SchemeApplicableStaffMapper schemeApplicableStaffMapper;
 
    @Autowired
    private SchemeInsuranceDetailMapper schemeInsuranceDetailMapper;
 
    @Autowired
    private SysUserDeptMapper sysUserDeptMapper;
 
    @Autowired
    private SysUserMapper sysUserMapper;
 
    @Autowired
    private SysDeptMapper sysDeptMapper;
 
 
    @Override
    public AjaxResult listPage(Page page, SchemeApplicableStaff schemeApplicableStaff) {
        LambdaQueryWrapper<SchemeApplicableStaff> schemeApplicableStaffLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(schemeApplicableStaff != null){
            if(StringUtils.isNotEmpty(schemeApplicableStaff.getTitle())){
                schemeApplicableStaffLambdaQueryWrapper.like(SchemeApplicableStaff::getTitle, schemeApplicableStaff.getTitle());
            }
        }
        Page<SchemeApplicableStaff> page1 = schemeApplicableStaffMapper.selectPage(page, schemeApplicableStaffLambdaQueryWrapper);
        List<Long> collect = page1.getRecords().stream().map(SchemeApplicableStaff::getId).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(collect)){
            return AjaxResult.success(page1);
        }
        List<SchemeInsuranceDetail> schemeInsuranceDetails = schemeInsuranceDetailMapper
                .selectList(new LambdaQueryWrapper<SchemeInsuranceDetail>()
                        .in(SchemeInsuranceDetail::getSchemeId, collect));
        page1.getRecords().forEach(item -> {
            item.setSchemeInsuranceDetailList(schemeInsuranceDetails
                    .stream()
                    .filter(detail -> detail.getSchemeId().equals(item.getId()))
                    .collect(Collectors.toList()));
            SysUser sysUser = sysUserMapper.selectUserById(item.getCreateUser().longValue());
            item.setCreateUserName(sysUser == null ? "未知" : sysUser.getNickName());
            // 获取部门信息
            String[] split = item.getDeptIds().split(",");
            List<SysDept> sysDepts = sysDeptMapper.selectList(new LambdaQueryWrapper<SysDept>()
                    .in(SysDept::getDeptId, Arrays.stream(split).map(Long::valueOf).collect(Collectors.toList())));
            if(!CollectionUtils.isEmpty(sysDepts)){
                item.setDeptNames(sysDepts.stream().map(SysDept::getDeptName).collect(Collectors.joining(",")));
            }
        });
        return AjaxResult.success(page1);
    }
 
    public void setSchemeApplicableStaffUserInfo(SchemeApplicableStaff schemeApplicableStaff) {
        // 通过部门获取人员id
        List<SysUserDept> sysUserDepts = sysUserDeptMapper.selectList(new LambdaQueryWrapper<SysUserDept>()
                .in(SysUserDept::getDeptId, schemeApplicableStaff.getDeptIds()));
        if(CollectionUtils.isEmpty(sysUserDepts)){
            throw new IllegalArgumentException("部门下无员工");
        }
        List<SysUser> sysUsers = sysUserMapper.selectUserByIds(sysUserDepts.stream().map(SysUserDept::getUserId).collect(Collectors.toList()));
        if(CollectionUtils.isEmpty(sysUsers)){
            throw new IllegalArgumentException("部门下无员工");
        }
        schemeApplicableStaff.setStaffIds(sysUsers
                .stream()
                .map(SysUser::getUserId)
                .filter(Objects::nonNull)         // 过滤掉 null 值
                .map(String::valueOf)
                .collect(Collectors.joining( ",")));
        schemeApplicableStaff.setStaffNames(sysUsers.stream().map(SysUser::getNickName).collect(Collectors.joining(",")));
    }
 
    @Override
    public AjaxResult add(SchemeApplicableStaff schemeApplicableStaff) {
        if(schemeApplicableStaff == null){
            return AjaxResult.error("参数错误");
        }
        if(CollectionUtils.isEmpty(schemeApplicableStaff.getSchemeInsuranceDetailList())){
            return AjaxResult.error("请选择方案明细");
        }
        setSchemeApplicableStaffUserInfo(schemeApplicableStaff); //根据部门设置用户信息
        int insert = schemeApplicableStaffMapper.insert(schemeApplicableStaff);
        schemeApplicableStaff.getSchemeInsuranceDetailList().forEach(item -> {
            item.setSchemeId(schemeApplicableStaff.getId());
            schemeInsuranceDetailMapper.insert(item);
        });
        return AjaxResult.success(insert);
    }
 
    @Override
    public AjaxResult updateSchemeApplicableStaff(SchemeApplicableStaff schemeApplicableStaff) {
        if(schemeApplicableStaff == null){
            return AjaxResult.error("参数错误");
        }
        setSchemeApplicableStaffUserInfo(schemeApplicableStaff); //根据部门设置用户信息
        int update = schemeApplicableStaffMapper.updateById(schemeApplicableStaff);
        // 先删,重新绑定
        schemeInsuranceDetailMapper.delete(new LambdaQueryWrapper<SchemeInsuranceDetail>()
                .eq(SchemeInsuranceDetail::getSchemeId, schemeApplicableStaff.getId()));
        schemeApplicableStaff.getSchemeInsuranceDetailList().forEach(item -> {
            item.setSchemeId(schemeApplicableStaff.getId());
            schemeInsuranceDetailMapper.insert(item);
        });
        return AjaxResult.success(update);
    }
 
    @Override
    public AjaxResult delete(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return AjaxResult.error("参数错误");
        }
        int delete = schemeApplicableStaffMapper.deleteBatchIds(ids);
        schemeInsuranceDetailMapper.delete(new LambdaQueryWrapper<SchemeInsuranceDetail>()
                .in(SchemeInsuranceDetail::getSchemeId, ids));
        return AjaxResult.success(delete);
    }
}