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;
/**
*
* 社保方案适用人员表 服务实现类
*
*
* @author 芯导软件(江苏)有限公司
* @since 2026-03-05 11:50:17
*/
@Service
public class SchemeApplicableStaffServiceImpl extends ServiceImpl 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 schemeApplicableStaffLambdaQueryWrapper = new LambdaQueryWrapper<>();
if(schemeApplicableStaff != null){
if(StringUtils.isNotEmpty(schemeApplicableStaff.getTitle())){
schemeApplicableStaffLambdaQueryWrapper.like(SchemeApplicableStaff::getTitle, schemeApplicableStaff.getTitle());
}
}
Page page1 = schemeApplicableStaffMapper.selectPage(page, schemeApplicableStaffLambdaQueryWrapper);
List collect = page1.getRecords().stream().map(SchemeApplicableStaff::getId).collect(Collectors.toList());
if(CollectionUtils.isEmpty(collect)){
return AjaxResult.success(page1);
}
List schemeInsuranceDetails = schemeInsuranceDetailMapper
.selectList(new LambdaQueryWrapper()
.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 sysDepts = sysDeptMapper.selectList(new LambdaQueryWrapper()
.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 sysUserDepts = sysUserDeptMapper.selectList(new LambdaQueryWrapper()
.in(SysUserDept::getDeptId, schemeApplicableStaff.getDeptIds()));
if(CollectionUtils.isEmpty(sysUserDepts)){
throw new IllegalArgumentException("部门下无员工");
}
List 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()
.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 ids) {
if (CollectionUtils.isEmpty(ids)) {
return AjaxResult.error("参数错误");
}
int delete = schemeApplicableStaffMapper.deleteBatchIds(ids);
schemeInsuranceDetailMapper.delete(new LambdaQueryWrapper()
.in(SchemeInsuranceDetail::getSchemeId, ids));
return AjaxResult.success(delete);
}
}