package com.farriver.bwf.service.system; import com.farriver.bwf.common.model.ApiData; import com.farriver.bwf.common.model.ApiPageData; import com.farriver.bwf.common.options.ActionType; import com.farriver.bwf.common.options.MapDataType; import com.farriver.bwf.common.options.RoleOption; import com.farriver.bwf.common.statics.LangConstants; import com.farriver.bwf.common.utilities.TreeBuilder; import com.farriver.bwf.common.utilities.TreeBuilderNode; import com.farriver.bwf.data.master.mapper.SystemMenuMapper; import com.farriver.bwf.data.master.model.SystemMenu; import com.farriver.bwf.data.master.model.SystemMenuExample; import com.farriver.bwf.data.transferobject.queryobject.system.SystemMenuQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.security.PermissionMasterViewModel; import com.farriver.bwf.data.transferobject.viewmodel.system.SystemMenuViewModel; import com.farriver.bwf.data.transferobject.viewmodel.system.SystemMenuWriteViewModel; import com.farriver.bwf.service.ServiceBase; import com.farriver.bwf.service.extend.rbac.RBACService; import jakarta.annotation.Resource; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.*; @Service public class SystemMenuService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(SystemMenuService.class); private String message; @Resource SystemMenuMapper systemMenuMapper; @Resource RBACService rbacService; @Value("${applicationcode}") private String applicationcode; public Map GetList(SystemMenuQueryObject queryObject) { if (queryObject == null) ApiPageData.error(LangConstants.MSG_ERROR_PARAMETERS); SystemMenuExample example = new SystemMenuExample(); SystemMenuExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andNameLike("%" + queryObject.getFilter() + "%"); SystemMenuExample.Criteria criteria2 = example.createCriteria(); criteria2.andCodeLike("%" + queryObject.getFilter() + "%"); example.or(criteria2); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getCode() != null && !queryObject.getCode().isEmpty()) { criteria.andCodeLike("%" + queryObject.getCode() + "%"); } if (queryObject.getName() != null && !queryObject.getName().isEmpty()) { criteria.andNameLike("%" + queryObject.getName() + "%"); } if (queryObject.getParentid() != null && !queryObject.getParentid().isEmpty()) { criteria.andParentidEqualTo(queryObject.getParentid()); } if (queryObject.getParentcode() != null && !queryObject.getParentcode().isEmpty()) { criteria.andParentcodeLike("%" + queryObject.getParentcode() + "%"); } if (queryObject.getParentname() != null && !queryObject.getParentname().isEmpty()) { criteria.andParentnameLike("%" + queryObject.getParentname() + "%"); } if (queryObject.getComponent() != null && !queryObject.getComponent().isEmpty()) { criteria.andComponentLike("%" + queryObject.getComponent() + "%"); } if (queryObject.getStartDateTime() != null && queryObject.getEndDateTime() != null) { criteria.andCreatetimeBetween(queryObject.getStartDateTime(), queryObject.getEndDateTime()); } } //查询总数 long total = systemMenuMapper.countByExample(example); //分页 int pageIndex = queryObject.getPageIndex(); int pageSize = queryObject.getPageSize(); if (pageIndex > 0 && pageSize > 0) { example.setRows(pageSize); example.setOffset((pageIndex - 1) * pageSize); } List list = systemMenuMapper.selectByExample(example); Map map = new HashMap<>(); map.put(MapDataType.DATA_LIST, list); map.put(MapDataType.TOTAL_COUNT, total); map.put(MapDataType.PAGE_INDEX, pageIndex); map.put(MapDataType.PAGE_SIZE, pageSize); return map; } public SystemMenu GetDetail(SystemMenuQueryObject queryObject) { Map map = GetList(queryObject); Object object = map.get(MapDataType.DATA_LIST); if (object != null) { List list = (List) object; if (list != null && list.size() == 1) return list.get(0); } return null; } public ApiData Create(SystemMenuWriteViewModel model) { SystemMenu entity = Check(ActionType.CREATE, model); if (entity != null) { try { PermissionMasterViewModel permissionViewModel = new PermissionMasterViewModel(); permissionViewModel.setId(model.getId()); permissionViewModel.setName(model.getName()); permissionViewModel.setApplicationcode(applicationcode); if (model.getUrl() != null && model.getUrl().isEmpty()) { String[] urlArray = model.getUrl().split(model.getComponent()); String url = urlArray[0]; PermissionMasterViewModel parent = rbacService.GetUriPermission(applicationcode, url); if (parent != null) { permissionViewModel.setParentid(parent.getId()); } } permissionViewModel.setRemark(model.getRemark()); permissionViewModel.setUrl(model.getUrl()); permissionViewModel.setType(0); //SystemMenu permissionViewModel.setLevel(RoleOption.Other.ordinal()); //Other rbacService.CreatePermission(permissionViewModel); } catch (Exception exception) { logger.warn(exception.getMessage()); } return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, systemMenuMapper.insertSelective(entity)); } else { return ApiData.error(message); } } public ApiData Update(SystemMenuWriteViewModel model) { SystemMenu entity = Check(ActionType.UPDATE, model); if (entity != null) { try { PermissionMasterViewModel permissionViewModel = new PermissionMasterViewModel(); permissionViewModel.setId(model.getId()); permissionViewModel.setName(model.getName()); permissionViewModel.setApplicationcode(applicationcode); if (model.getUrl() != null && !model.getUrl().isEmpty()) { String[] urlArray = model.getUrl().split(String.format("/%s", model.getComponent())); String url = urlArray[0]; PermissionMasterViewModel parent = rbacService.GetUriPermission(applicationcode, url); if (parent != null) { permissionViewModel.setParentid(parent.getId()); } } permissionViewModel.setRemark(model.getRemark()); permissionViewModel.setUrl(model.getUrl()); permissionViewModel.setType(0); //SystemMenu permissionViewModel.setLevel(RoleOption.Other.ordinal()); //Other rbacService.CreatePermission(permissionViewModel); } catch (Exception exception) { logger.warn(exception.getMessage()); } return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, systemMenuMapper.updateByPrimaryKeySelective(entity)); } else { return ApiData.error(message); } } public ApiData Delete(String id) { return ApiData.ok("", systemMenuMapper.deleteByPrimaryKey(id)); } public ApiData BatchDelete(SystemMenuQueryObject queryObject) { Map map = GetList(queryObject); Object object = map.get(MapDataType.DATA_LIST); int count = 0; if (object != null) { List list = (List) object; if (list != null && list.size() > 0) { List ids = list.stream().map(SystemMenu::getId).toList(); SystemMenuExample example = new SystemMenuExample(); SystemMenuExample.Criteria criteria = example.createCriteria(); criteria.andIdIn(ids); count += systemMenuMapper.deleteByExample(example); } } return ApiData.ok("", count); } public SystemMenuViewModel GetViewDetail(SystemMenuQueryObject queryObject) { SystemMenu entity = GetDetail(queryObject); if (entity == null) { return null; } return BuildViewModel(entity); } public ApiPageData GetViewPageList(SystemMenuQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (SystemMenu entity : list) { SystemMenuViewModel viewModel = BuildViewModel(entity); viewModels.add(viewModel); } ApiPageData bean = ApiPageData.build(); bean.setData(viewModels); bean.setTotal((Long) map.get(MapDataType.TOTAL_COUNT)); return bean; } public ApiPageData GetViewPageListWithChildren(SystemMenuQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); if (list == null || list.isEmpty()) { return ApiPageData.error(LangConstants.MSG_ERROR_PARAMETERS); } list.sort(Comparator.comparing(SystemMenu::getType).thenComparing(SystemMenu::getName)); List viewModels = new ArrayList<>(); for (SystemMenu entity : list) { SystemMenuViewModel viewModel = BuildViewModel(entity); viewModels.add(viewModel); } viewModels = TreeBuilder.buildTreeFromNodes(viewModels); ApiPageData bean = ApiPageData.build(); bean.setData(viewModels); bean.setTotal((Long) map.get(MapDataType.TOTAL_COUNT)); return bean; } public ApiData getTree() { SystemMenuQueryObject queryObject = new SystemMenuQueryObject(); Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); if (list == null || list.isEmpty()) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } list.sort(Comparator.comparing(SystemMenu::getType).thenComparing(SystemMenu::getName)); List viewModels = new ArrayList<>(); for (SystemMenu entity : list) { SystemMenuViewModel viewModel = BuildViewModel(entity); viewModels.add(viewModel); } viewModels = TreeBuilder.buildTreeFromNodes(viewModels); return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, viewModels); } //private methods private static SystemMenuViewModel BuildViewModel(SystemMenu entity) { if (entity == null) return null; SystemMenuViewModel model = new SystemMenuViewModel(); model.setId(entity.getId()); model.setCreatetime(entity.getCreatetime()); model.setParentid(entity.getParentid()); model.setParentcode(entity.getParentcode()); model.setParentname(entity.getParentname()); model.setName(entity.getName()); model.setCode(entity.getCode()); model.setUrl(entity.getUrl()); model.setPath(entity.getPath()); model.setComponent(entity.getComponent()); model.setType(entity.getType()); model.setIcon(entity.getIcon()); model.setKeepalive(entity.getKeepalive()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setLanguagekey(entity.getLanguagekey()); model.setLevel(entity.getLevel()); return model; } public SystemMenu Check(ActionType actionType, SystemMenuWriteViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } SystemMenu entity = null; if (ActionType.CREATE == actionType) { entity = new SystemMenu(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); String oldCode = GetLatestCode(); String newCode = generateCode(oldCode); if (newCode == null) return null; entity.setCode(newCode); } else { SystemMenuQueryObject queryObject = new SystemMenuQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); if (entity == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } entity.setUpdatetime(new Date()); } entity.setParentid(model.getParentid()); entity.setParentcode(model.getParentcode()); entity.setParentname(model.getParentname()); entity.setLanguagekey(model.getLanguagekey()); entity.setName(model.getName()); entity.setUrl(model.getUrl()); entity.setPath(model.getPath()); entity.setComponent(model.getComponent()); entity.setType(model.getType()); entity.setIcon(model.getIcon()); entity.setKeepalive(model.getKeepalive()); entity.setIsdeleted(model.getIsdeleted()); entity.setRemark(model.getRemark()); entity.setLevel(model.getLevel()); return entity; } @Nullable public String generateCode(String oldCode) { String preNo = "M"; int _newCodeNum = 1; try { int _oldCodeNum = 0; if (oldCode != null && !oldCode.equals("") && oldCode.length() > 2) { String _oldNoNumString = oldCode.substring(2); _oldCodeNum = Integer.parseInt(_oldNoNumString); } if (_oldCodeNum != 0) { int a = _oldCodeNum + 1; _newCodeNum = a; } } catch (NumberFormatException e) { logger.error(e.getMessage(), e); message = LangConstants.MSG_ERROR_SYSTEM; return null; } return String.format("%s%03d", preNo, _newCodeNum); } public String GetLatestCode() { String oldCode = ""; SystemMenuExample example = new SystemMenuExample(); example.setOrderByClause("code desc"); example.setRows(1); example.setOffset(0); List list = systemMenuMapper.selectByExample(example); if (list != null && list.size() > 0) { SystemMenu entity2 = list.get(0); if (entity2 != null) { oldCode = entity2.getCode(); } } return oldCode; } }