package org.jeecg.modules.dnc.service.impl;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import org.jeecg.modules.dnc.entity.Button;
|
import org.jeecg.modules.dnc.entity.ObjectBase;
|
import org.jeecg.modules.dnc.entity.ObjectButton;
|
import org.jeecg.modules.dnc.entity.ObjectButtonPermission;
|
import org.jeecg.modules.dnc.exception.ExceptionCast;
|
import org.jeecg.modules.dnc.mapper.DncObjectMapper;
|
import org.jeecg.modules.dnc.request.ObjectBaseRequest;
|
import org.jeecg.modules.dnc.response.CommonCode;
|
import org.jeecg.modules.dnc.response.ObjectCode;
|
import org.jeecg.modules.dnc.response.QueryPageResponseResult;
|
import org.jeecg.modules.dnc.service.IButtonService;
|
import org.jeecg.modules.dnc.service.IObjectButtonPermissionService;
|
import org.jeecg.modules.dnc.service.IObjectButtonService;
|
import org.jeecg.modules.dnc.service.IObjectService;
|
import org.jeecg.modules.dnc.utils.ValidateUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.List;
|
|
@Service
|
public class ObjectServiceImpl extends ServiceImpl<DncObjectMapper, ObjectBase> implements IObjectService {
|
@Autowired
|
private IObjectButtonService objectButtonService;
|
@Autowired
|
private IButtonService buttonService;
|
@Autowired
|
private IObjectButtonPermissionService objectButtonPermissionService;
|
|
@Override
|
@Transactional(rollbackFor = {Exception.class})
|
public boolean addObject(ObjectBase objectBase) {
|
if(objectBase == null)
|
ExceptionCast.cast(CommonCode.INVALID_PARAM);
|
if(!ValidateUtil.validateString(objectBase.getObjectPerm()))
|
ExceptionCast.cast(ObjectCode.OBJ_PERM_CODE_NONE);
|
if(!ValidateUtil.validateString(objectBase.getObjectName()))
|
ExceptionCast.cast(ObjectCode.OBJ_NAME_NONE);
|
ObjectBase en = getByPermCode(objectBase.getObjectPerm());
|
if(en != null)
|
ExceptionCast.cast(ObjectCode.OBJ_PERM_CODE_EXIST);
|
return super.save(objectBase);
|
}
|
|
@Override
|
public ObjectBase getByPermCode(String permCode) {
|
if(!ValidateUtil.validateString(permCode))
|
return null;
|
List<ObjectBase> list = super.lambdaQuery().eq(ObjectBase::getObjectPerm, permCode).list();
|
if(list == null || list.isEmpty())
|
return null;
|
return list.get(0);
|
}
|
|
@Override
|
@Transactional(rollbackFor = {Exception.class})
|
public boolean assignButton(String objectId, List<Button> buttonList) {
|
if(!ValidateUtil.validateString(objectId))
|
ExceptionCast.cast(ObjectCode.OBJ_ID_NONE);
|
ObjectBase objectBase = super.getById(objectId);
|
if(objectBase == null)
|
ExceptionCast.cast(ObjectCode.OBJ_NOT_EXIST);
|
boolean b = objectButtonService.deleteByObjectId(objectId);
|
if(!b)
|
ExceptionCast.cast(CommonCode.FAIL);
|
if(buttonList != null && !buttonList.isEmpty()) {
|
List<ObjectButton> objButtons = new ArrayList<>();
|
List<String> ids = new ArrayList<>();
|
buttonList.forEach(item -> {
|
final ObjectButton en = new ObjectButton();
|
en.setObjectId(objectBase.getObjectId());
|
en.setButtonId(item.getButtonId());
|
en.setButtonUrl(item.getButtonUrl());
|
if(ValidateUtil.validateString(item.getButtonAlias())) {
|
en.setButtonAlias(item.getButtonAlias());
|
}else {
|
en.setButtonAlias(item.getButtonName());
|
}
|
en.setPermCode(objectBase.getObjectPerm() + "_" + item.getButtonPerm());
|
objButtons.add(en);
|
ids.add(item.getButtonId());
|
});
|
Collection<Button> buttons = buttonService.listByIds(ids);
|
if(buttons == null || buttons.isEmpty() || buttons.size() != buttonList.size())
|
ExceptionCast.cast(CommonCode.INVALID_PARAM);
|
if(!objButtons.isEmpty())
|
return objectButtonService.saveBatch(objButtons);
|
}
|
return b;
|
}
|
|
@Override
|
@Transactional(rollbackFor = {Exception.class})
|
public boolean editObject(String id, ObjectBase objectBase) {
|
if(!ValidateUtil.validateString(id) || objectBase == null)
|
ExceptionCast.cast(CommonCode.INVALID_PARAM);
|
ObjectBase en = super.getById(id);
|
if (en == null) {
|
ExceptionCast.cast(ObjectCode.OBJ_ID_NOT_EXIST);
|
}
|
objectBase.setObjectId(id);
|
objectBase.setObjectPerm(null);
|
return super.updateById(objectBase);
|
}
|
|
@Override
|
@Transactional(rollbackFor = {Exception.class})
|
public boolean deleteObjectById(String id) {
|
if(!ValidateUtil.validateString(id)) {
|
ExceptionCast.cast(CommonCode.INVALID_PARAM);
|
}
|
ObjectBase en = super.getById(id);
|
if (en == null)
|
ExceptionCast.cast(ObjectCode.OBJ_ID_NOT_EXIST);
|
List<ObjectButtonPermission> objectButtonPermissions = objectButtonPermissionService.findByObjectId(id);
|
if(objectButtonPermissions != null && !objectButtonPermissions.isEmpty())
|
ExceptionCast.cast(ObjectCode.OBJ_ROLE_EXIST);
|
boolean b = objectButtonService.deleteByObjectId(id);
|
if(!b)
|
ExceptionCast.cast(CommonCode.FAIL);
|
return super.removeById(id);
|
}
|
|
@Override
|
public QueryPageResponseResult<ObjectBase> findPageList(int page, int size, ObjectBaseRequest buttonRequest) {
|
if(page < 1 || size < 1) {
|
ExceptionCast.cast(CommonCode.INVALID_PAGE);
|
}
|
IPage<ObjectBase> pageData = new Page<>(page, size);
|
LambdaQueryChainWrapper<ObjectBase> lambdaQuery = super.lambdaQuery();
|
if(buttonRequest != null) {
|
if(ValidateUtil.validateString(buttonRequest.getObjectName())) {
|
lambdaQuery.eq(ObjectBase::getObjectName, buttonRequest.getObjectName());
|
}
|
if(ValidateUtil.validateString(buttonRequest.getObjectPerm())) {
|
lambdaQuery.like(ObjectBase::getObjectPerm, buttonRequest.getObjectPerm());
|
}
|
if(ValidateUtil.validateString(buttonRequest.getAscStr())) {
|
String[] ascArr = buttonRequest.getAscStr().split(",");
|
// ((Page<ObjectBase>) pageData).setAsc(ascArr);
|
}
|
if(ValidateUtil.validateString(buttonRequest.getDescStr())) {
|
String[] descStr = buttonRequest.getDescStr().split(",");
|
// ((Page<ObjectBase>) pageData).setDesc(descStr);
|
}
|
}
|
IPage<ObjectBase> userIPage = lambdaQuery.page(pageData);
|
return new QueryPageResponseResult<>(CommonCode.SUCCESS, userIPage);
|
}
|
}
|