@ -5,12 +5,18 @@ import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONException ;
import cn.hutool.json.JSONUtil ;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper ;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper ;
import com.fastbee.common.constant.ProjectLevelConstant ;
import com.fastbee.common.core.domain.entity.SysDept ;
import com.fastbee.common.core.domain.entity.SysMenu ;
import com.fastbee.common.core.domain.entity.SysRole ;
import com.fastbee.common.core.domain.entity.SysUser ;
import com.fastbee.common.exception.ServiceException ;
import com.fastbee.common.holder.ProjectHolder ;
import com.fastbee.common.utils.DateUtils ;
import com.fastbee.iot.model.RegisterUserInput ;
import com.fastbee.iot.model.RegisterUserOutput ;
import com.fastbee.iot.service.IToolService ;
import com.fastbee.project.domain.Project ;
import com.fastbee.project.mapper.ProjectMapper ;
import com.fastbee.project.service.IProjectService ;
@ -19,8 +25,12 @@ import com.fastbee.system.domain.SysDistrict;
import com.fastbee.system.mapper.SysDeptMapper ;
import com.fastbee.system.mapper.SysDistrictMapper ;
import com.fastbee.system.mapper.SysUserMapper ;
import com.fastbee.system.service.ISysDeptService ;
import com.fastbee.system.service.ISysMenuService ;
import com.fastbee.system.service.ISysRoleService ;
import org.apache.commons.lang3.StringUtils ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import java.util.* ;
import java.util.stream.Collectors ;
@ -40,13 +50,25 @@ public class ProjectServiceImpl implements IProjectService
private final SysDistrictMapper sysDistrictMapper ;
public ProjectServiceImpl ( ProjectMapper projectMapper , SysDeptMapper sysDeptMapper , SysUserMapper sysUserMapper , SysDistrictMapper sysDistrictMapper ) {
private final I SysMenuService sysMenuService ;
private final ISysRoleService sysRoleService ;
private final ISysDeptService deptService ;
private final IToolService toolService ;
public ProjectServiceImpl ( ProjectMapper projectMapper , SysDeptMapper sysDeptMapper , SysUserMapper sysUserMapper , SysDistrictMapper sysDistrictMapper , ISysMenuService sysMenuService , ISysRoleService sysRoleService , ISysDeptService deptService , IToolService toolService ) {
this . projectMapper = projectMapper ;
this . sysDeptMapper = sysDeptMapper ;
this . sysUserMapper = sysUserMapper ;
this . sysDistrictMapper = sysDistrictMapper ;
this . sysMenuService = sysMenuService ;
this . sysRoleService = sysRoleService ;
this . deptService = deptService ;
this . toolService = toolService ;
}
/**
* 根据主键查询项目
*
@ -88,21 +110,99 @@ public class ProjectServiceImpl implements IProjectService
* 新增项目
*/
@Override
@Transactional ( rollbackFor = Exception . class )
public int insertProject ( Project project )
{
//判断选择的机构下是否已经绑定项目
List< Project> projectList = new LambdaQueryChainWrapper< > ( projectMapper )
. select( Project: : getId , Project : : getProjectName )
. eq ( Project : : getDeptId, project. getDeptId( ) ) . list ( ) ;
if ( ! projectList. isEmpty( ) ) {
throw new ServiceException( " 该机构下已经存在项目" ) ;
}
//处理项目行政区划
// List< Project> projectList = new LambdaQueryChainWrapper<>( projectMapper)
// . select( Project::getId, Project:: getProjectName)
// .eq(Project:: getDeptId, project. getDeptId()).list() ;
// if(! projectList. isEmpty()) {
// throw new ServiceException(" 该机构下已经存在项目") ;
// }
//TODO 参数校验
//获取当前时间
Date nowDate = DateUtils . getNowDate ( ) ;
//处理项目行政区划
parseAdministrativeDivisionInfo ( project ) ;
//给项目分配一个机构--------------------------------------------------------
SysDept newSysDept = new SysDept ( ) ;
//设置上级机构id
newSysDept . setParentId ( 100L ) ;
//设置祖级列表
newSysDept . setAncestors ( " 0,100 " ) ;
//设置创建时间
newSysDept . setCreateTime ( nowDate ) ;
//设置默认机构名称
newSysDept . setDeptName ( project . getProjectName ( ) ) ;
newSysDept . setLeader ( " admin " ) ;
//设置机构型行政区域信息
newSysDept . setAdministrativeAreaCode ( Objects . nonNull ( project . getAdministrativeAreaCode ( ) ) ? project . getAdministrativeAreaCode ( ) : null ) ;
newSysDept . setAdministrativeArea ( Objects . nonNull ( project . getAdministrativeArea ( ) ) ? project . getAdministrativeArea ( ) : null ) ;
newSysDept . setProvinceCode ( Objects . nonNull ( project . getProvinceCode ( ) ) ? project . getProvinceCode ( ) : null ) ;
newSysDept . setCityCode ( Objects . nonNull ( project . getCityCode ( ) ) ? project . getCityCode ( ) : null ) ;
newSysDept . setCountyCode ( Objects . nonNull ( project . getCountyCode ( ) ) ? project . getCountyCode ( ) : null ) ;
newSysDept . setTownCode ( Objects . nonNull ( project . getTownCode ( ) ) ? project . getTownCode ( ) : null ) ;
newSysDept . setVillageCode ( Objects . nonNull ( project . getVillageCode ( ) ) ? project . getVillageCode ( ) : null ) ;
if ( sysDeptMapper . insertDept ( newSysDept ) < 1 ) {
throw new ServiceException ( " 创建默认机构失败! " ) ;
}
// 新增机构关联系统账号
// 添加管理员角色,给所有权限
SysDept sysDept = deptService . selectDeptById ( 100L ) ;
List < SysMenu > sysMenuList = sysMenuService . selectMenuList ( new SysMenu ( ) , sysDept . getDeptUserId ( ) ) ;
Long [ ] menuIdList = sysMenuList . stream ( ) . map ( SysMenu : : getMenuId ) . toArray ( Long [ ] : : new ) ;
SysRole sysRole = new SysRole ( ) ;
sysRole . setRoleName ( " 管理员 " ) ;
sysRole . setRoleKey ( " manager " ) ;
sysRole . setRoleSort ( 1 ) ;
sysRole . setStatus ( " 0 " ) ;
sysRole . setDeptId ( newSysDept . getDeptId ( ) ) ;
sysRole . setMenuIds ( menuIdList ) ;
sysRoleService . insertRole ( sysRole ) ;
// 注册机构管理员用户
RegisterUserInput registerUserInput = new RegisterUserInput ( ) ;
registerUserInput . setUsername ( project . getUserName ( ) ) ;
registerUserInput . setPassword ( project . getPassword ( ) ) ;
registerUserInput . setPhonenumber ( " 15666617898 " ) ;
registerUserInput . setDeptId ( newSysDept . getDeptId ( ) ) ;
registerUserInput . setRoleIds ( new Long [ ] { sysRole . getRoleId ( ) } ) ;
RegisterUserOutput registerUserOutput = toolService . registerNoCaptcha ( registerUserInput ) ;
if ( com . fastbee . common . utils . StringUtils . isNotEmpty ( registerUserOutput . getMsg ( ) ) ) {
deptService . deleteDeptById ( newSysDept . getDeptId ( ) ) ;
sysRoleService . deleteRoleById ( sysRole . getRoleId ( ) ) ;
throw new ServiceException ( registerUserOutput . getMsg ( ) ) ;
}
// 更新机构管理员角色绑定信息
deptService . updateDeptUserId ( newSysDept . getDeptId ( ) , registerUserOutput . getSysUserId ( ) ) ;
// //创建该机构的登录账号数据
// SysUser newSysUser = new SysUser();
// newSysUser.setUserName(project.getUserName());
// newSysUser.setPassword(project.getPassword());
// newSysUser.setNickName(project.getProjectName()+"管理员");
// newSysUser.setDeptId(newSysDept.getDeptId());
// newSysUser.setCreateTime(nowDate);
// newSysUser.setCreateBy(project.getCreateBy());
//
// if(sysUserMapper.insertUser(newSysUser)<1){
// throw new ServiceException("创建默认机构管理员账号失败!");
// }
project . setDeptId ( newSysDept . getDeptId ( ) ) ;
project . setCreateTime ( DateUtils . getNowDate ( ) ) ;
return projectMapper . insertProject ( project ) ;
}
/**
* 解析项目行政区划信息
* @param project 项目
*/
private void parseAdministrativeDivisionInfo ( Project project ) {
//项目行政区域处理
//拆解项目行政区域代码信息
@ -131,19 +231,25 @@ public class ProjectServiceImpl implements IProjectService
//单个行政区划代码列表
JSONArray administrativeArea = JSONUtil . parseArray ( item ) ;
//拆解出每个行政区划代码里面的镇或村
if ( administrativeArea. size ( ) ! = 5 ) {
throw new ServiceException( " 项目行政区域代码多选列表的元素长度必须为5, 到达村级别! " ) ;
}
// if( administrativeArea.size()!=5) {
// throw new ServiceException(" 项目行政区域代码多选列表的元素长度必须为5, 到达村级别! ") ;
// }
//保存项目省代码列表
provincialCodeList . add ( administrativeArea . get ( 0 ) . toString ( ) ) ;
//保存项目市代码列表
cityCodeList . add ( administrativeArea . get ( 1 ) . toString ( ) ) ;
//保存项目县代码列表
countyCodeList . add ( administrativeArea . get ( 2 ) . toString ( ) ) ;
//保存项目镇代码列表
townCodeList . add ( administrativeArea . get ( 3 ) . toString ( ) ) ;
//保存项目村 代码列表
village CodeList . add ( administrativeArea . get ( 4 ) . toString ( ) ) ;
if ( administrativeArea . size ( ) > = 4 ) {
//保存项目镇 代码列表
town CodeList . add ( administrativeArea . get ( 3 ) . toString ( ) ) ;
}
if ( administrativeArea . size ( ) > = 5 ) {
//保存项目村代码列表
villageCodeList . add ( administrativeArea . get ( 4 ) . toString ( ) ) ;
}
} ) ;
//设置项目省、市、县、镇、村行政区划代码列表
@ -170,6 +276,7 @@ public class ProjectServiceImpl implements IProjectService
* 修改项目
*/
@Override
@Transactional ( rollbackFor = Exception . class )
public int updateProject ( Project project )
{
//判断选择的机构下是否已经绑定项目
@ -187,7 +294,24 @@ public class ProjectServiceImpl implements IProjectService
project . setUpdateTime ( DateUtils . getNowDate ( ) ) ;
//处理项目行政区划
parseAdministrativeDivisionInfo ( project ) ;
return projectMapper . updateProject ( project ) ;
//同步更新项目所属最顶级部门行政区划数据
boolean updateDept = new LambdaUpdateChainWrapper < > ( sysDeptMapper )
. set ( SysDept : : getAdministrativeAreaCode , project . getAdministrativeAreaCode ( ) )
. set ( SysDept : : getProvinceCode , project . getProvinceCode ( ) )
. set ( SysDept : : getCityCode , project . getCityCode ( ) )
. set ( SysDept : : getCountyCode , project . getCountyCode ( ) )
. set ( SysDept : : getTownCode , project . getTownCode ( ) )
. set ( SysDept : : getVillageCode , project . getVillageCode ( ) )
. eq ( SysDept : : getDeptId , project . getDeptId ( ) )
. update ( ) ;
if ( ! updateDept ) {
throw new ServiceException ( " 同步更新项目所属最顶级部门行政区划数据失败! " ) ;
}
int updateProject = projectMapper . updateProject ( project ) ;
if ( updateProject < 1 ) {
throw new ServiceException ( " 修改项目失败! " ) ;
}
return 1 ;
}
/**
@ -211,7 +335,6 @@ public class ProjectServiceImpl implements IProjectService
. select ( SysUser : : getUserId , SysUser : : getDeptId )
. eq ( SysUser : : getUserId , userId )
. one ( ) ;
//查询该管理员所管理的项目id以及子项目id列表
Project project = new LambdaQueryChainWrapper < > ( projectMapper )
. select ( Project : : getId , Project : : getProjectName , Project : : getLevel , Project : : getDeptId
@ -311,24 +434,30 @@ public class ProjectServiceImpl implements IProjectService
* 查询项目的行政区划树状列表
*/
@Override
public List < SysDistrict > getProjectAreaTree ( Long projec tId, Integer startLevel ) {
public List < SysDistrict > getProjectAreaTree ( Long dep tId, Integer startLevel ) {
//参数校验
if ( startLevel > 5 | | startLevel < 1 ) {
throw new ServiceException ( " 超出行政级别查询范围(1~5级)! " ) ;
}
//查询项目的行政区划信息
Project project = new LambdaQueryChainWrapper< > ( projectMapper )
. select( Project: : getProvinceCode, Project : : getCityCode , Project : : getCountyCode ,
Project: : getTownCode, Project : : getVillageCode )
. eq ( Project : : getId , projectId )
// Project project = new LambdaQueryChainWrapper<>( projectMapper)
// . select( Project:: getProvinceCode, Project::getCityCode, Project:: getCountyCode,
// Project:: getTownCode, Project:: getVillageCode)
// .eq(Project::getId, projectId)
// .one();
SysDept sysDept = new LambdaQueryChainWrapper < > ( sysDeptMapper )
. select ( SysDept : : getDeptId , SysDept : : getProvinceCode , SysDept : : getCityCode , SysDept : : getCountyCode ,
SysDept : : getTownCode , SysDept : : getVillageCode )
. eq ( SysDept : : getDeptId , deptId )
. one ( ) ;
if ( Objects . isNull ( projec t) ) {
throw new ServiceException( " 项目不存在 " ) ;
if ( Objects . isNull ( sysDep t) ) {
// throw new ServiceException("项目不存在") ;
return null ;
}
//判断项目的省、市、县、镇、村行政区划列表数据不为null
isProvinceCodeBlank ( projec t) ;
isProvinceCodeBlank ( sysDep t) ;
List < Object > administrativeCodeList = getTownAndvillageCodeList ( projec t, startLevel ) ;
List < Object > administrativeCodeList = getTownAndvillageCodeList ( sysDep t, startLevel ) ;
//查询项目所属县下面的所有镇、村行政区划列表
List < SysDistrict > projectDistrictList ;
@ -339,7 +468,7 @@ public class ProjectServiceImpl implements IProjectService
//限制行政区划级别
projectDistrictList = sysDistrictLambdaQueryChainWrapper
. in ( SysDistrict : : getAdcode , administrativeCodeList )
. in ( ! administrativeCodeList . isEmpty ( ) , SysDistrict : : getAdcode , administrativeCodeList )
. list ( ) ;
// System.err.println("项目所属县下面的所有镇、村行政区划列表");
@ -348,12 +477,15 @@ public class ProjectServiceImpl implements IProjectService
return buildTree ( projectDistrictList ) ;
}
private static List < Object > getTownAndvillageCodeList ( Project projec t, Integer startLevel ) {
private static List < Object > getTownAndvillageCodeList ( SysDept sysDep t, Integer startLevel ) {
// if(sysDept.getDeptId().toString().equals("100")){
// return new ArrayList<>();
// }
//查询项目的省、市、县、镇、村行政区划列表数据
//目前项目下面的镇或者村都可以多选划分成一个项目
//找到项目行政区划开始多选的级别
//判断省是否多选
JSONArray provinceCodeList = JSONUtil . parseArray ( projec t. getProvinceCode ( ) ) ;
JSONArray provinceCodeList = JSONUtil . parseArray ( sysDep t. getProvinceCode ( ) ) ;
//判断集合中的元素值都相等则说明省未多选
if ( ! provinceCodeList . stream ( ) . allMatch ( provinceCode - > provinceCode . equals ( provinceCodeList . get ( 0 ) ) ) ) {
throw new ServiceException ( " 项目省级行政区划只能包含一个省! " ) ;
@ -362,7 +494,7 @@ public class ProjectServiceImpl implements IProjectService
List < Object > distinctProvinceCodeList = provinceCodeList . stream ( ) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
//判断市是否多选
JSONArray cityCodeList = JSONUtil . parseArray ( projec t. getCityCode ( ) ) ;
JSONArray cityCodeList = JSONUtil . parseArray ( sysDep t. getCityCode ( ) ) ;
//判断集合中的元素值都相等则说明市未多选
if ( ! cityCodeList . stream ( ) . allMatch ( cityCode - > cityCode . equals ( cityCodeList . get ( 0 ) ) ) ) {
throw new ServiceException ( " 项目市级行政区划只能包含一个市! " ) ;
@ -371,7 +503,7 @@ public class ProjectServiceImpl implements IProjectService
List < Object > distinctCityCodeList = cityCodeList . stream ( ) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
//判断县是否多选
JSONArray countyCodeList = JSONUtil . parseArray ( projec t. getCountyCode ( ) ) ;
JSONArray countyCodeList = JSONUtil . parseArray ( sysDep t. getCountyCode ( ) ) ;
//判断集合中的元素值都相等则说明县未多选
if ( ! countyCodeList . stream ( ) . allMatch ( countyCode - > countyCode . equals ( countyCodeList . get ( 0 ) ) ) ) {
throw new ServiceException ( " 项目县级行政区划只能包含一个县! " ) ;
@ -380,12 +512,12 @@ public class ProjectServiceImpl implements IProjectService
List < Object > distinctcCountyCodeList = countyCodeList . stream ( ) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
//判断镇是否多选
JSONArray townCodeList = JSONUtil . parseArray ( projec t. getTownCode ( ) ) ;
JSONArray townCodeList = JSONUtil . parseArray ( sysDep t. getTownCode ( ) ) ;
//判断集合中的元素值都相等则说明镇未多选
List < Object > distinctTownCodeList ;
distinctTownCodeList = townCodeList . stream ( ) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
JSONArray villageCodeList = JSONUtil . parseArray ( projec t. getVillageCode ( ) ) ;
JSONArray villageCodeList = JSONUtil . parseArray ( sysDep t. getVillageCode ( ) ) ;
// System.err.println("镇行政区划代码");
// System.err.println(distinctTownCodeList);
@ -409,20 +541,23 @@ public class ProjectServiceImpl implements IProjectService
return administrativeCodeList ;
}
private static void isProvinceCodeBlank ( Project projec t) {
if ( StringUtils . isBlank ( project . getProvinceCode ( ) ) ) {
private void isProvinceCodeBlank ( SysDept sysDep t) {
// if(sysDept.getDeptId().toString().equals("100")) {
// return ;
// }
if ( StringUtils . isBlank ( sysDept . getProvinceCode ( ) ) ) {
throw new ServiceException ( " 项目省级行政区划信息缺失! " ) ;
}
if ( StringUtils . isBlank ( projec t. getCityCode ( ) ) ) {
if ( StringUtils . isBlank ( sysDep t. getCityCode ( ) ) ) {
throw new ServiceException ( " 项目市级行政区划信息缺失! " ) ;
}
if ( StringUtils . isBlank ( projec t. getCountyCode ( ) ) ) {
if ( StringUtils . isBlank ( sysDep t. getCountyCode ( ) ) ) {
throw new ServiceException ( " 项目县级行政区划信息缺失! " ) ;
}
if ( StringUtils . isBlank ( projec t. getTownCode ( ) ) ) {
if ( StringUtils . isBlank ( sysDep t. getTownCode ( ) ) ) {
throw new ServiceException ( " 项目镇级行政区划信息缺失! " ) ;
}
if ( StringUtils . isBlank ( projec t. getVillageCode ( ) ) ) {
if ( StringUtils . isBlank ( sysDep t. getVillageCode ( ) ) ) {
throw new ServiceException ( " 项目村级行政区划信息缺失! " ) ;
}
}