Java源码示例:com.jeesuite.common.util.BeanUtils
示例1
@ApiOperation(value = "保存")
@RequestMapping(value = "save", method = RequestMethod.POST)
public @ResponseBody WrapperResponse<String> addDepartment(@RequestBody DepartmentParam param) {
if(param.getCompanyId() == null || param.getCompanyId() == 0){
throw new JeesuiteBaseException(ExceptionCode.REQUEST_PARAM_REQUIRED.code, "请先选择公司");
}
DepartmentEntity entity = BeanUtils.copy(param, DepartmentEntity.class);
if (param.getId() == null || param.getId() == 0) {
entity.setCreatedAt(new Date());
entity.setCreatedBy(LoginContext.getIntFormatUserId());
departmentService.addDepartment(entity);
} else {
entity.setUpdatedAt(new Date());
entity.setUpdatedBy(LoginContext.getIntFormatUserId());
departmentService.updateDepartment(entity);
}
return new WrapperResponse<>();
}
示例2
@ApiOperation(value = "新增")
@RequestMapping(value = "add", method = RequestMethod.POST)
public @ResponseBody WrapperResponse<String> addEmployee(@RequestBody EmployeeParam param) {
if(param.getDepartmentId() == null || param.getDepartmentId() == 0){
throw new JeesuiteBaseException(ExceptionCode.REQUEST_PARAM_REQUIRED.code, "请先选择所在部门");
}
if(param.getPositionId() == null || param.getPositionId() == 0){
throw new JeesuiteBaseException(ExceptionCode.REQUEST_PARAM_REQUIRED.code, "请先选择职位");
}
EmployeeEntity entity = BeanUtils.copy(param, EmployeeEntity.class);
entity.setCreatedAt(new Date());
entity.setCreatedBy(LoginContext.getIntFormatUserId());
employeeService.addEmployee(entity,param.getDepartmentId(),param.getPositionId());
return new WrapperResponse<>();
}
示例3
@ApiOperation(value = "保存总公司信息")
@RequestMapping(value = "save", method = RequestMethod.POST)
public @ResponseBody WrapperResponse<String> addCompany(@RequestBody CompanyParam param) {
if(param.getBranch()){
AssertUtil.notNull(companyService.findHeadCompany(),"请先保存总公司信息");
}
CompanyEntity entity = BeanUtils.copy(param, CompanyEntity.class);
if(param.getId() == null || param.getId() == 0){
entity.setCreatedAt(new Date());
entity.setCreatedBy(LoginContext.getIntFormatUserId());
companyService.addCompany(entity);
}else{
entity.setUpdatedAt(new Date());
entity.setUpdatedBy(LoginContext.getIntFormatUserId());
companyService.updateCompany(entity);
}
return new WrapperResponse<>();
}
示例4
public void addCompany(CompanyEntity entity) {
CompanyEntity headCompany = companyMapper.findHeadCompany();
if(headCompany == null){
entity.setIsBranch(false);
}else{
entity.setIsBranch(true);
CompanyEntity sameNameCompany = companyMapper.findByName(entity.getName());
if(sameNameCompany != null){
if(sameNameCompany.getInActive())throw new JeesuiteBaseException(ExceptionCode.RECORD_EXISTED.code,"该子公司已存在");
BeanUtils.copy(entity, sameNameCompany);
sameNameCompany.setInActive(true);
companyMapper.updateByPrimaryKeySelective(sameNameCompany);
return ;
}
}
companyMapper.insertSelective(entity);
}
示例5
@Transactional
public void addAccount(int operUserId, AccountParam param) {
AccountEntity existEntity = accountMapper.findByLoginName(param.getMobile());
AssertUtil.isNull(existEntity, "手机号码已存在");
existEntity = accountMapper.findByLoginName(param.getEmail());
AssertUtil.isNull(existEntity, "邮箱已存在");
existEntity = accountMapper.findByLoginName(param.getUsername());
AssertUtil.isNull(existEntity, "用户名已存在");
AccountEntity entity = BeanUtils.copy(param, AccountEntity.class);
entity.setEnabled(true);
entity.setCreatedAt(new Date());
entity.setCreatedBy(operUserId);
entity.setPassword(AccountEntity.encryptPassword(param.getMobile().substring(5)));
accountMapper.insertSelective(entity);
}
示例6
@Transactional
public void updateUserGroup(Integer currentUserId, UpdateUserGroupParam param) {
//TODO 判重(平台、公司、部门维度)
UserGroupEntity entity = userGroupMapper.selectByPrimaryKey(param.getId());
AssertUtil.notNull(entity);
BeanUtils.copy(param, entity);
entity.setUpdatedBy(currentUserId);
entity.setUpdatedAt(new Date());
userGroupMapper.updateByPrimaryKeySelective(entity);
//权限组
permissionInternalService.updateGrantRelations(param.getId(), param.getPermGroupIds(), param.getPlatformType(), GrantRelationType.PermGroupToUserGroup);
//额外权限
List<Integer> extSourceIds = new ArrayList<>();
if(param.getExtrMenuIds() != null)extSourceIds.addAll(param.getExtrMenuIds());
if(param.getExtrApiIds() != null)extSourceIds.addAll(param.getExtrApiIds());
permissionInternalService.updateGrantRelations(param.getId(), param.getPermGroupIds(), param.getPlatformType(), GrantRelationType.PermToUserGroup);
}
示例7
@Transactional
public void updateResource(Integer currentUserId, UpdateResourceParam param) {
//TODO 判重
PermissionResourceEntity entity = permissionResourceMapper.selectByPrimaryKey(param.getId());
AssertUtil.notNull(entity);
BeanUtils.copy(param, entity);
ModuleEntity module = moduleMapper.selectByPrimaryKey(entity.getId());
entity.buildPermssionCode(module);
entity.setUpdatedBy(currentUserId);
entity.setUpdatedAt(new Date());
permissionResourceMapper.updateByPrimaryKey(entity);
//
if(param.getApiIds() != null && !param.getApiIds().isEmpty()){
permissionInternalService.updateSubordinateRelationsByParent(entity.getId(), param.getApiIds(), entity.getPlatformType(), SubRelationType.ApiToMenu);
}
}
示例8
@RequestMapping(value = "add", method = RequestMethod.POST)
public ResponseEntity<WrapperResponseEntity> addApp(@RequestBody AddOrEditAppRequest addAppRequest){
SecurityUtil.requireSuperAdmin();
if(addAppRequest.getMasterUid() == null || addAppRequest.getMasterUid() == 0){
throw new JeesuiteBaseException(1002, "请选择项目负责人");
}
if(appMapper.findByName(addAppRequest.getName()) != null){
throw new JeesuiteBaseException(1002, "应用["+addAppRequest.getName()+"]已存在");
}
AppEntity appEntity = BeanUtils.copy(addAppRequest, AppEntity.class);
//
UserEntity master = userMapper.selectByPrimaryKey(addAppRequest.getMasterUid());
appEntity.setMaster(master.getName());
appMapper.insertSelective(appEntity);
return new ResponseEntity<WrapperResponseEntity>(new WrapperResponseEntity(true),HttpStatus.OK);
}
示例9
@RequestMapping(value = "update", method = RequestMethod.POST)
public ResponseEntity<WrapperResponseEntity> updateApp(@RequestBody AddOrEditAppRequest addAppRequest){
SecurityUtil.requireSuperAdmin();
AppEntity app = appMapper.selectByPrimaryKey(addAppRequest.getId());
if(app == null){
throw new JeesuiteBaseException(1002, "应用不存在");
}
AppEntity appEntity = BeanUtils.copy(addAppRequest, AppEntity.class);
if(addAppRequest.getMasterUid() != null && addAppRequest.getMasterUid() > 0
&& !addAppRequest.getMasterUid().equals(app.getMasterUid())){
UserEntity master = userMapper.selectByPrimaryKey(addAppRequest.getMasterUid());
appEntity.setMaster(master.getName());
}
appMapper.updateByPrimaryKeySelective(appEntity);
return new ResponseEntity<WrapperResponseEntity>(new WrapperResponseEntity(true),HttpStatus.OK);
}
示例10
@ApiOperation(value = "新增用户")
@RequestMapping(value = "add", method = RequestMethod.POST)
public @ResponseBody WrapperResponse<String> addUserInfo(@RequestBody UserInfoParam param) {
UserInfoEntity entity = BeanUtils.copy(param, UserInfoEntity.class);
//entity.setCreatedAt(new Date());
//entity.setCreatedBy(LoginContext.getIntFormatUserId());
userInfoService.addUserInfo(entity);
return new WrapperResponse<>();
}
示例11
@ApiOperation(value = "更新用户")
@RequestMapping(value = "update", method = RequestMethod.POST)
public @ResponseBody WrapperResponse<String> updateUserInfo(@RequestBody UserInfoParam param) {
UserInfoEntity entity = BeanUtils.copy(param, UserInfoEntity.class);
//entity.setUpdatedAt(new Date());
//entity.setUpdatedBy(LoginContext.getIntFormatUserId());
userInfoService.updateUserInfo(entity);
return new WrapperResponse<>();
}
示例12
@ApiOperation(value = "更新")
@RequestMapping(value = "update", method = RequestMethod.POST)
public @ResponseBody WrapperResponse<String> updateEmployee(@RequestBody EmployeeParam param) {
EmployeeEntity entity = BeanUtils.copy(param, EmployeeEntity.class);
entity.setUpdatedAt(new Date());
entity.setUpdatedBy(LoginContext.getIntFormatUserId());
employeeService.updateEmployee(entity);
return new WrapperResponse<>();
}
示例13
public void updateCompany(CompanyEntity entity) {
CompanyEntity originEntity = companyMapper.selectByPrimaryKey(entity.getId());
AssertUtil.notNull(originEntity);
BeanUtils.copy(entity, originEntity);
originEntity.setIsBranch(false);
companyMapper.updateByPrimaryKey(originEntity);
}
示例14
@ApiOperation(value = "新增文章")
@RequestMapping(value = "add", method = RequestMethod.POST)
@ApiPermOptions(perms = PermissionType.Authorized)
public @ResponseBody WrapperResponse<String> addCmsArticle(@RequestBody ArticleParam param) {
ArticleEntity entity = BeanUtils.copy(param, ArticleEntity.class);
entity.setCreatedAt(new Date());
entity.setCreatedBy(LoginContext.getIntFormatUserId());
cmsArticleService.addCmsArticle(entity);
return new WrapperResponse<>();
}
示例15
@ApiOperation(value = "更新文章")
@RequestMapping(value = "update", method = RequestMethod.POST)
@ApiPermOptions(perms = PermissionType.Authorized)
public @ResponseBody WrapperResponse<String> updateCmsArticle(@RequestBody ArticleParam param) {
ArticleEntity entity = BeanUtils.copy(param, ArticleEntity.class);
entity.setUpdatedAt(new Date());
entity.setUpdatedBy(LoginContext.getIntFormatUserId());
cmsArticleService.updateCmsArticle(entity);
return new WrapperResponse<>();
}
示例16
@Transactional
public void addResource(Integer currentUserId, AddResourceParam param) {
//TODO 判重
PermissionResourceEntity entity = BeanUtils.copy(param, PermissionResourceEntity.class);
ModuleEntity module = moduleMapper.selectByPrimaryKey(entity.getId());
entity.buildPermssionCode(module);
entity.setCreatedAt(new Date());
entity.setCreatedBy(currentUserId);
permissionResourceMapper.insertSelective(entity);
//
if(param.getApiIds() != null && !param.getApiIds().isEmpty()){
permissionInternalService.updateSubordinateRelationsByParent(entity.getId(), param.getApiIds(), param.getPlatformType(), SubRelationType.ApiToMenu);
}
}
示例17
@Transactional
public void updatePermGroup(Integer currentUserId, UpdatePermGroupParam param){
PermissionGroupEntity entity = permissionGroupMapper.selectByPrimaryKey(param.getId());
AssertUtil.notNull(entity);
BeanUtils.copy(param, entity);
entity.setUpdatedBy(currentUserId);
entity.setUpdatedAt(new Date());
permissionGroupMapper.updateByPrimaryKeySelective(entity);
//
permissionInternalService.updateSubordinateRelationsByParent(param.getId(), param.getApiIds(), param.getPlatformType(), SubRelationType.PermToPermGroup);
}
示例18
@RequestMapping(value = "add", method = RequestMethod.POST)
@Transactional
public ResponseEntity<WrapperResponseEntity> addConfig(@RequestBody AddOrEditConfigRequest addRequest){
if(!addRequest.getGlobal() && addRequest.getAppIds() == null){
throw new JeesuiteBaseException(4001,"非全局绑定应用不能为空");
}
if(StringUtils.isBlank(addRequest.getEnv())){
throw new JeesuiteBaseException(4001,"绑定环境profile不能为空");
}
if(addRequest.getType().intValue() == 2 && StringUtils.isBlank(addRequest.getName())){
throw new JeesuiteBaseException(4001,"配置项名称不能空");
}
if(addRequest.getGlobal()){
SecurityUtil.requireSuperAdmin();
}else{
SecurityUtil.requireAllPermission(addRequest.getEnv(),addRequest.getAppIds(),GrantOperate.RW);
}
// if(StringUtils.isNotBlank(addRequest.getName())
// && appconfigMapper.findSameByName(addRequest.getEnv(), appId, addRequest.getName()) != null){
// throw new JeesuiteBaseException(4001,"配置名称已经存在");
// }
AppconfigEntity entity = BeanUtils.copy(addRequest, AppconfigEntity.class);
entity.setAppIds(StringUtils.join(addRequest.getAppIds(),","));
entity.setCreatedBy(SecurityUtil.getLoginUserInfo().getName());
entity.setCreatedAt(new Date());
entity.setUpdatedAt(entity.getCreatedAt());
entity.setUpdatedBy(entity.getCreatedBy());
encryptPropItemIfRequired(entity);
//
appconfigMapper.insertSelective(entity);
return new ResponseEntity<WrapperResponseEntity>(new WrapperResponseEntity(true),HttpStatus.OK);
}
示例19
public static void main(String[] args) {
User user = new User();
user.setName("小伙子");
user.setMobile("13800138000");
user.setFather(new User(1000, "你爹"));
BaseUser user2 = BeanUtils.copy(user, BaseUser.class);
System.out.println(JsonUtils.toPrettyJson(user2));
}
示例20
public void updateUserInfo(UserInfoEntity entity) {
UserInfoEntity originEntity = userInfoMapper.selectByPrimaryKey(entity.getId());
AssertUtil.notNull(originEntity);
BeanUtils.copy(entity, originEntity);
userInfoMapper.updateByPrimaryKey(originEntity);
}
示例21
public void updateEmployee(EmployeeEntity entity) {
EmployeeEntity originEntity = employeeMapper.selectByPrimaryKey(entity.getId());
AssertUtil.notNull(originEntity);
BeanUtils.copy(entity, originEntity);
employeeMapper.updateByPrimaryKey(originEntity);
}
示例22
public void updatePositions(PositionEntity entity) {
PositionEntity originEntity = positionsMapper.selectByPrimaryKey(entity.getId());
AssertUtil.notNull(originEntity);
BeanUtils.copy(entity, originEntity);
positionsMapper.updateByPrimaryKey(originEntity);
}
示例23
public void updateDepartment(DepartmentEntity entity) {
DepartmentEntity originEntity = departmentMapper.selectByPrimaryKey(entity.getId());
AssertUtil.notNull(originEntity);
BeanUtils.copy(entity, originEntity);
departmentMapper.updateByPrimaryKey(originEntity);
}
示例24
public void updateCmsArticle(ArticleEntity entity) {
ArticleEntity originEntity = cmsArticleMapper.selectByPrimaryKey(entity.getId());
AssertUtil.notNull(originEntity);
BeanUtils.copy(entity, originEntity);
cmsArticleMapper.updateByPrimaryKey(originEntity);
}
示例25
public void addCategory(CategoryParam param){
CategoryEntity entity = BeanUtils.copy(param, CategoryEntity.class);
categoryMapper.insertSelective(entity);
}
示例26
private static String objectToQueryParams(Object param){
Map<String, Object> map = BeanUtils.beanToMap(param);
return mapToQueryParams(map);
}
示例27
public static String mapToQueryParams(Map<String, Object> param){
if(param == null || param.isEmpty())return null;
StringBuilder sb = new StringBuilder();
List<String> keys = new ArrayList<>(param.keySet());
Collections.sort(keys);
Object value;
for (String key : keys) {
if(PARAM_SIGN_TYPE.equals(key) || PARAM_SIGN.equals(key))continue;
value = param.get(key);
if(value == null || StringUtils.isBlank(value.toString()))continue;
if(value instanceof Map){
value = mapToQueryParams((Map<String, Object>) value);
if(value != null){
value = JSON_PREFIX + value + JSON_SUFFIX;
}
}else if(value instanceof Iterable) {
StringBuilder sb1 = new StringBuilder();
sb1.append(BRACKET_PREFIX);
Iterator<?> it = ((Iterable<?>) value).iterator();
while (it.hasNext()) {
Object object = it.next();
if(BeanUtils.isSimpleDataType(object)){
sb1.append(object).append(SPLIT_STR);
}else{
sb1.append(JSON_PREFIX).append(objectToQueryParams(object)).append(JSON_SUFFIX).append(SPLIT_STR);
}
}
if(sb1.length() == 1){
value = null;
} else if(sb1.length() > 0){
sb1.deleteCharAt(sb1.length() - 1);
sb1.append(BRACKET_SUFFIX);
value = sb1.toString();
}
}
if(value != null){
sb.append(key).append(EQUALS_STR).append(value).append(CONTACT_STR);
}
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}