package com.highdatas.mdm.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.highdatas.mdm.entity.*; import com.highdatas.mdm.pojo.CodeMsg; import com.highdatas.mdm.pojo.MasterAuthorType; import com.highdatas.mdm.pojo.Result; import com.highdatas.mdm.service.*; import com.highdatas.mdm.util.Constant; import com.highdatas.mdm.util.DbUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.*; import java.util.stream.Collectors; /** *

* 前端控制器 *

* * @author kimi * @since 2020-03-23 */ @RestController @RequestMapping("/masterAuthor") public class MasterAuthorController { @Autowired IMasterAuthorService authorService; @Autowired IMasterAuthorDetailService authorDetailService; @Autowired ITUserRoleService userRoleService; @Autowired IMenuMappingService menuMappingService; @Autowired ISysMenuService menuService; public static final String masterAuthorType = "masterAuthorType"; public static final String masterId = "masterId"; public static final String tableName = "table_name"; public static final String characterId = "characterId"; public static final String character_id = "character_id"; public static final String fields = "fields"; private String maintainFieldId = "maintainFieldId"; private String maintain_field_id = "maintain_field_id"; @RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST) public Result deleteModel(@RequestBody MasterAuthor masterAuthor) { if (!StringUtils.isEmpty(masterAuthor.getId())) { masterAuthor.setUpdateTime(new Date()); }else { masterAuthor.setId(DbUtils.getUUID()).setCreateTime(new Date()); } String menuId = masterAuthor.getMenuId(); String tableName = menuMappingService.getTableNameByMenu(menuId); masterAuthor.setTableName(tableName); //delete pre boolean delete = authorDetailService.delete(new EntityWrapper().eq(Constant.PARENT_ID, masterAuthor.getId())); if (!delete){ return Result.error(CodeMsg.DELETE_ERROR); } for (MasterAuthorDetail detail : masterAuthor.getFields()) { detail.setId(DbUtils.getUUID()).setParentId(masterAuthor.getId()).insert(); } boolean b = masterAuthor.insertOrUpdate(); if (b) { return Result.success(masterAuthor); }else { return Result.error(CodeMsg.UPDATE_ERROR); } } @RequestMapping(value = "/get/{characterId}", method = RequestMethod.GET) public Result get(@PathVariable String characterId, @RequestParam MasterAuthorType type, HttpServletRequest request){ List masterAuthorList = authorService.selectList(new EntityWrapper().eq("type", type.name()).eq("character_id", characterId)); if (type.equals(MasterAuthorType.role) && masterAuthorList.isEmpty()) { return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED); }else if (type.equals(MasterAuthorType.user) && masterAuthorList.isEmpty()){ //user 获取角色 多脚色混合 String userId = characterId; List tUserRoles = userRoleService.selectList(new EntityWrapper().eq(Constant.USERID, userId)); List roleIdList = tUserRoles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList()); HashMap tableMasterAuthor = authorService.merageRoleAuthor(roleIdList); if (tableMasterAuthor == null) { Result.success(null); } JSONObject object = new JSONObject(); ArrayList list = new ArrayList(tableMasterAuthor.values()); object.fluentPut("type", MasterAuthorType.role); object.fluentPut("author",list); Set collect = list.stream().map(masterAuthor -> masterAuthor.getMenuId()).collect(Collectors.toSet()); LinkedHashSet menuIds= new LinkedHashSet<>(collect); LinkedHashSet byParentId = menuService.getByParentId(menuIds); List sysMenus = menuService.selectBatchIds(byParentId); object.fluentPut("audit",sysMenus); return Result.success(object); } for (MasterAuthor masterAuthor : masterAuthorList) { List masterAuthorDetails = authorDetailService .selectList(new EntityWrapper() .eq(Constant.PARENT_ID, masterAuthor.getId())); masterAuthor.setFields(masterAuthorDetails); } JSONObject object = new JSONObject(); Set collect = masterAuthorList.stream().map(masterAuthor -> masterAuthor.getMenuId()).collect(Collectors.toSet()); LinkedHashSet menuIds= new LinkedHashSet<>(collect); LinkedHashSet byParentId = menuService.getByParentId(menuIds); List sysMenus = menuService.selectBatchIds(byParentId); object.fluentPut("type", MasterAuthorType.user); object.fluentPut("author",masterAuthorList); object.fluentPut("audit",sysMenus); return Result.success(object); } @RequestMapping(value = "/delete/{characterId}", method = RequestMethod.GET) public Result delete(@PathVariable String characterId, @RequestParam MasterAuthorType type){ List masterAuthorList = authorService.selectList(new EntityWrapper() .eq(Constant.TYPE, type).eq(this.character_id, characterId)); if (masterAuthorList.isEmpty()) { return Result.success(null); } boolean delete = false; for (MasterAuthor masterAuthor : masterAuthorList) { delete = authorDetailService.delete(new EntityWrapper().eq(Constant.PARENT_ID, masterAuthor.getId())); if (delete) { delete = masterAuthor.deleteById(); } } if (delete) { return Result.success(CodeMsg.DELETE_SUCCESS); }else { return Result.error(CodeMsg.DELETE_ERROR); } } @RequestMapping(value = "/delete/menu/{menuId}", method = RequestMethod.GET) public Result deleteTable(@PathVariable String menuId, @RequestParam String characterId, @RequestParam MasterAuthorType type, HttpServletRequest request){ String maintainFieldId = request.getParameter(this.maintainFieldId); Wrapper masterAuthorWrapper = new EntityWrapper() .eq(Constant.TYPE, type) .eq(character_id, characterId) .eq("menu_id", menuId); if (!StringUtils.isEmpty(maintainFieldId)) { masterAuthorWrapper.eq(this.maintain_field_id, maintainFieldId); } List masterAuthorList = authorService.selectList(masterAuthorWrapper); if (masterAuthorList.isEmpty()) { return Result.success(null); } boolean partDel = false; if (masterAuthorList.size() == 1 && !StringUtils.isEmpty(maintainFieldId)) { // 只有一个且删除 字段版本 保留本条 partDel = true; } boolean delete = false; for (MasterAuthor masterAuthor : masterAuthorList) { delete = authorDetailService.delete(new EntityWrapper().eq(Constant.PARENT_ID, masterAuthor.getId())); if (delete) { if (partDel) { delete = masterAuthor.setMaintainFieldId(null).updateById(); }else { delete = masterAuthor.deleteById(); } } } if (delete) { return Result.success(CodeMsg.DELETE_SUCCESS); }else { return Result.error(CodeMsg.DELETE_ERROR); } } @RequestMapping(value = "/addRole/{roleId}", method = RequestMethod.GET) public Result addRole(@PathVariable String roleId, HttpServletRequest request){ TUser user = DbUtils.getUser(request); String userId = user.getUserId(); List tUserRoles = userRoleService.selectList(new EntityWrapper().eq(Constant.USERID, userId)); List roleIdList = tUserRoles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList()); roleIdList.add(roleId); HashMap tableMasterAuthor = authorService.merageRoleAuthor(roleIdList); if (tableMasterAuthor == null) { Result.success(null); } return Result.success(tableMasterAuthor.values()); } }