package com.landtool.lanbase.modules.api.controller; import com.alibaba.excel.EasyExcel; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SimplePropertyPreFilter; import com.landtool.lanbase.common.annotation.LogAction; import com.landtool.lanbase.common.annotation.SysLog; import com.landtool.lanbase.common.utils.ComplexPropertyPreFilter; import com.landtool.lanbase.common.utils.IPUtils; import com.landtool.lanbase.common.utils.Query; import com.landtool.lanbase.common.utils.Result; import com.landtool.lanbase.modules.org.dao.OrgUserDao; import com.landtool.lanbase.modules.org.entity.OrgUnit; import com.landtool.lanbase.modules.org.entity.OrgUnitJoinRegion; import com.landtool.lanbase.modules.org.entity.OrgUnitRegion; import com.landtool.lanbase.modules.org.entity.OrgUnitmanager; import com.landtool.lanbase.modules.org.entity.OrgUser; import com.landtool.lanbase.modules.org.entity.OrgUserGroup; import com.landtool.lanbase.modules.org.entity.OrgUserJoinUnit; import com.landtool.lanbase.modules.org.entity.OrgUserWithUnitCell; import com.landtool.lanbase.modules.org.entity.OrgUserunit; import com.landtool.lanbase.modules.org.listener.OrgUserReadListener; import com.landtool.lanbase.modules.org.service.OrgUnitService; import com.landtool.lanbase.modules.org.service.OrgUnitmanagerService; import com.landtool.lanbase.modules.org.service.OrgUserService; import com.landtool.lanbase.modules.sys.entity.PubNews; import com.landtool.lanbase.modules.sys.entity.SysResource; import com.landtool.lanbase.modules.sys.service.SysResourceService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.apache.commons.lang.StringUtils; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.support.MultipartFilter; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URLDecoder; import java.net.URLEncoder; import java.sql.Timestamp; import java.util.*; /** * @Description: 用户信息模块提供的api * @Author: zimao.guo * @Date: 9:53 2018/1/31 */ @Controller @RequestMapping(path = "/api/org/user/") @Api(value = "", tags = { "用户相关接口" }) public class OrgUserApiController { @Autowired private OrgUserService orgUserService; @Autowired private OrgUnitmanagerService unitmanagerService; @Autowired private SysResourceService sysResourceService; @Autowired private OrgUnitService orgUnitService; private String http = "http://"; @Value("${sys.uploadPath}") private String uploadpath; @Resource private OrgUserDao orgUserDao; /** * @param userId * @Description: 查询用户信息以及所属单位信息 {@link OrgUserService} * @Author: zimao.guo * @Date: 9:56 2018/1/31 * @return: OrgUserJoinUnit * @see OrgUserJoinUnit */ @GetMapping(path = "/getInfoById/{userId}") @ApiOperation(value = "查询用户信息", notes = "包含用用户所属单位信息") public void getInfoById( @ApiParam(name = "userId", value = "用户Id", required = true) @PathVariable(name = "userId") Long userId, HttpServletResponse response, HttpServletRequest request) throws IOException { OrgUserJoinUnit orgUser = orgUserService.queryObjectJoinUnit(userId); if (orgUser == null) { return; } String photourl = ""; if (orgUser.getPhotourl() != null) { photourl = http + (IPUtils.getIpAddr(request)) + ":" + request.getServerPort() + "/uploadFile" + orgUser.getPhotourl(); orgUser.setPhotourl(photourl); } response.setHeader("Content-Type", "application/json;charset=UTF-8"); response.getWriter().write(JSONObject.toJSONString(orgUser, getOrgUserJoinUnitFilter())); } // 建议查询用户表信息,无需关联查询 @ResponseBody @GetMapping(path = "/findUserByWord/{keyWord}") @ApiOperation(value = "查询用户名称(自动补全)", notes = "自动补全用户姓名") public String findUserByWord( @ApiParam(name = "keyWord", value = "用户拼音字母", required = true) @PathVariable(name = "keyWord") String keyWord, HttpServletResponse response, HttpServletRequest request) throws IOException { List users = orgUserService.findUserByWord(URLDecoder.decode(keyWord)); try { StringBuilder rsb = new StringBuilder(); rsb.append("["); List> maps = new LinkedList<>(); for (int i = 0; i < users.size(); i++) { Map map =new HashMap<>(); map.put("label", users.get(i).getChinesename()); map.put("value", users.get(i).getChinesename()); map.put("id", users.get(i).getUserid()); maps.add(map); // if(i > 0) rsb.append(","); // rsb.append("{'label':'" + users.get(i).getChinesename() + "'"); // rsb.append(",'value':'" + users.get(i).getChinesename() + "'"); // rsb.append(",'id':" + users.get(i).getUserid() + "}"); } rsb.append("]"); return JSON.toJSONString(maps); } catch (Exception e) { return "[]"; } } @GetMapping(path = "/getInfoByName/{loginname}") @ApiOperation(value = "查询用户信息", notes = "包含用用户所属单位信息") public void getInfoByLoginname( @ApiParam(name = "loginname", value = "用户名称", required = true) @PathVariable(name = "loginname") String loginname, HttpServletResponse response, HttpServletRequest request) throws IOException { OrgUserJoinUnit orgUser = orgUserService.queryObjectJoinUnitByLoginname(loginname); String photourl = ""; response.setHeader("Content-Type", "application/json;charset=UTF-8"); response.getWriter().write(JSONObject.toJSONString(orgUser, getOrgUserJoinUnitFilter())); } /** * 用户对应单位信息 */ private ComplexPropertyPreFilter getOrgUserJoinUnitFilter() { ComplexPropertyPreFilter filter = new ComplexPropertyPreFilter(); filter.setExcludes(new HashMap, String[]>() { private static final long serialVersionUID = -23423423423423434L; { put(OrgUserJoinUnit.class, new String[] { "password", "rcreatedate", "rcreateuser", "rlasteditdate" }); put(OrgUserunit.class, new String[] { "userid", "rorder", "rcreateuser", "rcreatedate", "username", "rlasteditdate" }); put(OrgUserGroup.class, new String[] { "userId", "createUnitId", "rCreateDate", "rCreateUser" }); } }); return filter; } @GetMapping(path = "/getInfoBychineseName/{chineseName}") @ApiOperation(value = "查询用户信息", notes = "包含用用户所属单位信息") public void getInfoBychineseName( @ApiParam(name = "chineseName", value = "用户名称", required = true) @PathVariable(name = "chineseName") String chineseName, HttpServletResponse response, HttpServletRequest request) throws IOException { OrgUserJoinUnit orgUser = orgUserService.queryObjectJoinUnitByChineseName(chineseName); response.setHeader("Content-Type", "application/json;charset=UTF-8"); response.getWriter().write(JSONObject.toJSONString(orgUser, getOrgUserJoinUnitFilter())); } /** * @param xzqhId * @Description: 条件查询行政区划用户列表 {@link OrgUserService} * @Author: zimao.guo * @Date: 11:29 2018/1/31 * @return: List * @see OrgUser */ @GetMapping(path = "/queryListByXzqhId/{xzqhId}") @ApiOperation(value = "条件查询行政区划用户列表", notes = "") public void queryListByXzqhId( @ApiParam(name = "xzqhId", value = "行政区划Id", required = true) @PathVariable(name = "xzqhId") long xzqhId, HttpServletResponse response, HttpServletRequest request) throws IOException { SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); filter.getExcludes().add("password"); filter.getExcludes().add("rcreatedate"); filter.getExcludes().add("rcreateuser"); filter.getExcludes().add("rlasteditdate"); // filter.getExcludes().add("salt"); List orgUser = orgUserService.queryListByXzqhId(xzqhId); String ourl = ""; Iterator it = orgUser.iterator(); while (it.hasNext()) { OrgUser user = (OrgUser) it.next(); String photourl = user.getPhotourl(); if (photourl != null) { ourl = http + (IPUtils.getIpAddr(request)) + ":" + request.getServerPort() + "/uploadFile" + photourl; user.setPhotourl(ourl); } } response.setHeader("Content-Type", "application/json;charset=UTF-8"); response.getWriter().write(JSONObject.toJSONString(orgUser, filter)); } /** * @param unitId * @Description: 条件查询某个单位用户列表 {@link OrgUserService} * @Author: zimao.guo * @Date: 11:45 2018/1/31 * @return: List * @see OrgUser */ @GetMapping(path = "/queryListByUnitId/{unitId}") @ApiOperation(value = "条件查询某个单位用户列表", notes = "") public void queryListByUnitId( @ApiParam(name = "unitid", value = "单位Id", required = true) @PathVariable(name = "unitId") long unitId, HttpServletResponse response, HttpServletRequest request) throws IOException { SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); filter.getExcludes().add("password"); filter.getExcludes().add("rcreatedate"); filter.getExcludes().add("rcreateuser"); filter.getExcludes().add("rlasteditdate"); // filter.getExcludes().add("salt"); List orgUser = orgUserService.queryListByUnitId(unitId); String ourl = ""; Iterator it = orgUser.iterator(); while (it.hasNext()) { OrgUser user = (OrgUser) it.next(); String photourl = user.getPhotourl(); if (photourl != null) { ourl = http + (IPUtils.getIpAddr(request)) + ":" + request.getServerPort() + "/uploadFile" + photourl; user.setPhotourl(ourl); } } response.setHeader("Content-Type", "application/json;charset=UTF-8"); response.getWriter().write(JSONObject.toJSONString(orgUser, filter)); } /** * @param appId * @Description: 查询系统可访问用户列表 {@link OrgUserService} * @Author: zimao.guo * @Date: 13:52 2018/1/31 * @return: List * @see OrgUser */ @GetMapping(path = "/queryListByAppId/{appId}") @ApiOperation(value = "查询系统可访问用户列表", notes = "") public void queryListByAppId( @ApiParam(name = "appId", value = "可访问Id", required = true) @PathVariable(name = "appId") long appId, HttpServletResponse response, HttpServletRequest request) throws IOException { SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); filter.getExcludes().add("password"); filter.getExcludes().add("rcreatedate"); filter.getExcludes().add("rcreateuser"); filter.getExcludes().add("rlasteditdate"); // filter.getExcludes().add("salt"); List orgUser = orgUserService.queryListByAppId(appId); String ourl = ""; Iterator it = orgUser.iterator(); while (it.hasNext()) { OrgUser user = (OrgUser) it.next(); String photourl = user.getPhotourl(); if (photourl != null) { ourl = http + (IPUtils.getIpAddr(request)) + ":" + request.getServerPort() + "/uploadFile" + photourl; user.setPhotourl(ourl); } } response.setHeader("Content-Type", "application/json;charset=UTF-8"); response.getWriter().write(JSONObject.toJSONString(orgUser, filter)); } /** * @param time * @Description: 根据时间戳获取信息 {@link OrgUserService} * @Author: zimao.guo * @Date: 14:51 2018/1/31 * @return: List * @see OrgUser */ @GetMapping(path = "/queryListByTime/{time}") @ApiOperation(value = "根据时间戳获取信息", notes = "") public void queryListByTime( @ApiParam(name = "time", value = "时间戳", required = true) @PathVariable(name = "time") Long time, HttpServletResponse response, HttpServletRequest request) throws IOException { SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); filter.getExcludes().add("password"); filter.getExcludes().add("rcreatedate"); filter.getExcludes().add("rcreateuser"); filter.getExcludes().add("rlasteditdate"); // filter.getExcludes().add("salt"); Date date = new Date(time); List orgUser = orgUserService.queryListByTime(date); String ourl = ""; Iterator it = orgUser.iterator(); while (it.hasNext()) { OrgUser user = (OrgUser) it.next(); String photourl = user.getPhotourl(); if (photourl != null) { ourl = http + (IPUtils.getIpAddr(request)) + ":" + request.getServerPort() + "/uploadFile" + photourl; user.setPhotourl(ourl); } } response.setHeader("Content-Type", "application/json;charset=UTF-8"); response.getWriter().write(JSONObject.toJSONString(orgUser, filter)); } @GetMapping(path = "/isExistPermission") @ApiOperation(value = "是否存在标识", notes = "") @ResponseBody public boolean isExistPermission( @ApiParam(name = "userid", value = "用户Id", required = true) @RequestParam(name = "userid") Integer userid, @ApiParam(name = "permission", value = "标识", required = true) @RequestParam(name = "permission") String permission) { SysResource info = sysResourceService.queryResourceByUserAndCode(userid, permission); if (info != null) { return true; } else { return false; } } /** * @Description: 查询用户列表(按首字母、所在单位、用户角色排序,排除登录用户) */ @GetMapping(path = "/queryListOrderByType/{type}/{userid}") @ApiOperation(value = ".查询用户列表(按首字母、所在单位、用户角色排序", notes = ".查询用户列表(按首字母、所在单位、用户角色排序") public void queryListOrderByType( @ApiParam(name = "type", value = "字段", required = true) @PathVariable(name = "type") String type, @ApiParam(name = "userid", value = "用户ID", required = true) @PathVariable(name = "userid") Integer userid, HttpServletResponse response, HttpServletRequest request) throws IOException { ComplexPropertyPreFilter filter = new ComplexPropertyPreFilter(); StringBuilder result = new StringBuilder(); StringBuilder nullStr = new StringBuilder(); List sList = new ArrayList(); if (type.equals("spellfirst")) { sList = orgUserService.querySpellFirst(userid); } if (type.equals("unit")) { sList = orgUserService.queryUserUnitSpellFirst(userid); } if (type.equals("role")) { sList = orgUserService.queryUserRoleName(userid); } if (sList.size() > 0) { for (int i = 0; i < sList.size(); i++) { if (result.toString().length() > 0) { result.append(","); } List list = new ArrayList(); Map paramMap = new HashMap(); paramMap.put("userid", userid); if (type.equals("spellfirst")) { paramMap.put("spellfirst", sList.get(i)); result.append("{ name: '" + sList.get(i) + "', children: ["); } if (type.equals("unit")) { if (sList.get(i) == null) { paramMap.put("unitspellfirst", "-1"); } else { paramMap.put("unitspellfirst", sList.get(i).split("_")[0]); result.append("{ name: '" + sList.get(i).split("_")[1] + "', children: ["); } } if (type.equals("role")) { if (sList.get(i) == null) { paramMap.put("rolename", "-11"); } else { paramMap.put("rolename", sList.get(i)); result.append("{ name: '" + sList.get(i) + "', children: ["); } } list = orgUserService.queryListOrderByType(paramMap); if (sList.get(i) == null) { for (OrgUser orgUser : list) { // if (!nullStr.toString().isEmpty()) { // nullStr.append(","); // } /** alert ykm 2019-04-12 **/ if (!result.toString().isEmpty() && !result.toString().endsWith("[") && !result.toString().endsWith(",")) { result.append(","); } result.append("{ name: '" + orgUser.getChinesename() + "[" + orgUser.getLoginname() + "]', id: '" + orgUser.getUserid() + "'}"); } } else { for (OrgUser orgUser : list) { if (!result.toString().isEmpty() && !result.toString().endsWith("[") && !result.toString().endsWith(",")) { result.append(","); } result.append("{ name: '" + orgUser.getChinesename() + "[" + orgUser.getLoginname() + "]', id: '" + orgUser.getUserid() + "'}"); } result.append("]}"); } } // if (nullStr.toString().length() > 0) { // if (result.toString().length() > 0) { // result.append(","); // } // result.append(nullStr.toString()); // } } response.setHeader("Content-Type", "application/json;charset=UTF-8"); response.getWriter().write("[" + result.toString() + "]"); } @GetMapping(path = "/getDefaultUnit/{userId}") @ApiOperation(value = "查询用户默认单位信息", notes = "查询用户默认单位信息") public void getDefaultUnit( @ApiParam(name = "userId", value = "用户Id", required = true) @PathVariable(name = "userId") Long userId, HttpServletResponse response, HttpServletRequest request) throws IOException { OrgUnit orgUnit =null; OrgUserJoinUnit orgUser = orgUserService.queryObjectJoinUnit(userId); Long unid =(long)0; if (orgUser.getOrguserunits().size() > 0) { unid = orgUser.getOrguserunits().get(0).getUnitid(); } if(unid != null && unid>0) { orgUnit =orgUnitService.queryObject(unid); } SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); filter.getExcludes().add("spellfirst"); filter.getExcludes().add("rcreatedate"); filter.getExcludes().add("rcreateuser"); filter.getExcludes().add("rlasteditdate"); filter.getExcludes().add("rorder"); response.setHeader("Content-Type", "application/json;charset=UTF-8"); response.getWriter().write((orgUnit == null)?"":JSONObject.toJSONString(orgUnit,filter)); } /** * @Description: 查询全部正常用户列表(id,中文名) */ @GetMapping(path = "/queryAllList") @ApiOperation(value = "查询全部正常用户列表(id,中文名)", notes = "查询全部正常用户列表(id,中文名)") public void queryAllList( HttpServletResponse response, HttpServletRequest request) throws IOException { ComplexPropertyPreFilter filter = new ComplexPropertyPreFilter(); StringBuilder result = new StringBuilder(); List list = orgUserService.queryAllList(); for(OrgUser user : list){ if (result.toString().length() > 0) { result.append(","); } result.append("{id:'"+user.getUserid()+"',name:'"+user.getChinesename()+"'}"); } response.setHeader("Content-Type", "application/json;charset=UTF-8"); response.getWriter().write("[" + result + "]"); } /** * 批量导入各应用系统已有用户 */ @PostMapping("/batchimportUser") @ApiOperation( value = "批量导入用户", notes = "" ) public String batchimportUser(@RequestParam(value="file") MultipartFile uploadfile) throws Exception { if(uploadfile.isEmpty()){ return "file is empty"; } String filename=uploadfile.getOriginalFilename(); String suffixname=filename.substring(filename.lastIndexOf(".")); filename=UUID.randomUUID()+suffixname; File file =new File(uploadpath+filename); uploadfile.transferTo(file); EasyExcel.read(file, OrgUser.class,new OrgUserReadListener(orgUserDao)).doReadAll(); return "操作成功" ; } /** * 批量导入各应用系统已有用户 */ @PostMapping("/importUser") @ApiOperation( value = "导入用户", notes = "" ) public String importUser(String userinfo) throws Exception { OrgUser user= JSONObject.parseObject(userinfo,OrgUser.class); orgUserService.save(user); return "插入成功" ; } /** * @param userId * @Description: 查询用户信息以及角色 {@link OrgUserService} * @Author: zimao.guo * @Date: 9:56 2018/1/31 * @return: OrgUserJoinUnit * @see OrgUserJoinUnit */ @GetMapping(path = "/queryUserResCatalog/{userId}") @ApiOperation(value = "查询用户信息", notes = "包含用用户所属单位信息") public @ResponseBody String getUserInfos( @ApiParam(name = "userId", value = "用户Id", required = true) @PathVariable(name = "userId") Long userId, HttpServletResponse response, HttpServletRequest request) throws IOException { List list= orgUserService.queryUserResCatalog(userId); return JSONObject.toJSONString(list); } /** * 批量导入各应用系统已有用户 * @return */ @PostMapping("/batchExportUser") @ApiOperation( value = "批量导入用户", notes = "" ) @RequiresPermissions(value = {"org:user:list","org:user:edit"}, logical = Logical.OR) public @ResponseBody void batchExportUser(HttpServletResponse rep,@RequestParam Map params) throws Exception { try{ rep.setContentType("application/vnd.ms-excel"); rep.setCharacterEncoding("utf-8"); String filename=URLEncoder.encode("用户信息表","UTF-8").replaceAll("\\+", "%20"); rep.setHeader("Content-isposition", "attachment;filename*=utf-8''"+filename+".xlsx"); OrgUser orgUser = (OrgUser) SecurityUtils.getSubject().getPrincipal(); //判读登录用户是否为属单位的管理员 OrgUnitmanager unitmanager = unitmanagerService.queryObject(orgUser.getUserid()); params.put("userid", orgUser.getUserid()); if(unitmanager != null){ params.put("unitid", unitmanager.getUnitid()); }else{ params.put("unitid", -1); } /** % 号为通配符,但是会导致部分用户不带单位而与空查询结果数不相符 * alert ykm 2019-04-12 */ if(params.get("unitname") != null && params.get("unitname").equals("%")) { params.replace("unitname", null); } //查询列表数据 //用户列表展示的数据是:管理下的单位用户 or 自己录的用户 or 管理员可以查看所有用户 //Set permissions = OrgUserService.getUserPermissions(getUserId()); //if(permissions.contains("org_user_admin")){//管理员可以查看所有用户 if(SecurityUtils.getSubject().isPermitted("org_user_admin")){//管理员可以查看所有用户 params.put("isadmin", 1); } else{//管理下的单位用户 or 自己录的用户 params.put("isadmin", 0); } Query query = new Query(params); List userList = orgUserService.queryList(query); // OrgUserWithUnitCell os=new OrgUserWithUnitCell(); // os.setChinesename("3223"); // List userList = new ArrayList(); // userList.add(os); String filepath="D://"+"ORGUSER"+System.currentTimeMillis()+".xlsx"; EasyExcel.write(filepath,OrgUserWithUnitCell.class).sheet("用户信息").doWrite(userList); // EasyExcel.write(rep.getOutputStream(),OrgUserWithUnitCell.class).sheet("用户信息").doWrite(userList); File file=new File(filepath); ServletOutputStream sos=rep.getOutputStream(); FileInputStream fis=new FileInputStream(file); try{ byte[] bytes=new byte[1024]; while(fis.read(bytes)!=-1){ sos.write(bytes); } }catch(Exception e){ e.printStackTrace(); }finally{ sos.close(); fis.close(); } }catch(Exception e){ rep.reset(); rep.setContentType("application/json"); rep.setCharacterEncoding("utf-8"); JSONObject json=new JSONObject(); json.put("status", "failure"); json.put("message", "文件下载失败"); rep.getWriter().println(JSON.toJSONString(json)); } } }