portal web service

TeamService.java 17KB

    package com.ekexiu.portal.team; import com.ekexiu.portal.dao.PpaperDao; import com.ekexiu.portal.dao.PpatentDao; import com.ekexiu.portal.dao.ProfessorDao; import com.ekexiu.portal.dao.WatchDao; import com.ekexiu.portal.po.Ppaper; import com.ekexiu.portal.po.Ppatent; import com.ekexiu.portal.pojo.SortedPro; import com.ekexiu.portal.resResult.ResearchResult; import com.ekexiu.portal.resResult.ResearchResultDao; import com.ekexiu.portal.service.ImageService; import com.ekexiu.portal.service.KeyWordService; import com.ekexiu.portal.service.ProfessorService; import org.jfw.apt.annotation.Autowrie; import org.jfw.apt.annotation.DefaultValue; import org.jfw.apt.annotation.Nullable; import org.jfw.apt.web.annotation.Path; import org.jfw.apt.web.annotation.operate.Get; import org.jfw.apt.web.annotation.operate.Post; import org.jfw.apt.web.annotation.param.JdbcConn; import org.jfw.apt.web.annotation.param.Upload; import org.jfw.util.ListUtil; import org.jfw.util.PageQueryResult; import org.jfw.util.StringUtil; import org.jfw.util.exception.JfwBaseException; import org.jfw.util.jdbc.JdbcUtil; import org.jfw.util.jdbc.PreparedStatementConfig; import org.jfw.util.web.fileupload.Item; import org.jfw.util.web.fileupload.UploadItemIterator; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.LinkedList; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; /** * Created by TT on 2019/7/9. */ @Path("/team") public class TeamService { private static final AtomicInteger FN_IDX = new AtomicInteger(1); private File imgPath; private int imgMaxWidth = 70; @Autowrie private TeamDao teamDao; @Autowrie private KeyWordService keyWordService; @Autowrie private WatchDao watchDao; @Autowrie private ProfessorDao professorDao; @Autowrie private ImageService imageService; @Autowrie private ResearchResultDao researchResultDao; @Autowrie private PpatentDao ppatentDao; @Autowrie private PpaperDao ppaperDao; public TeamDao getTeamDao() { return teamDao; } public void setTeamDao(TeamDao teamDao) { this.teamDao = teamDao; } public KeyWordService getKeyWordService() { return keyWordService; } public void setKeyWordService(KeyWordService keyWordService) { this.keyWordService = keyWordService; } public static AtomicInteger getFnIdx() { return FN_IDX; } public File getImgPath() { return imgPath; } public void setImgPath(File imgPath) { this.imgPath = imgPath; } public int getImgMaxWidth() { return imgMaxWidth; } public void setImgMaxWidth(int imgMaxWidth) { this.imgMaxWidth = imgMaxWidth; } public WatchDao getWatchDao() { return watchDao; } public void setWatchDao(WatchDao watchDao) { this.watchDao = watchDao; } public ProfessorDao getProfessorDao() { return professorDao; } public void setProfessorDao(ProfessorDao professorDao) { this.professorDao = professorDao; } public ImageService getImageService() { return imageService; } public void setImageService(ImageService imageService) { this.imageService = imageService; } public ResearchResultDao getResearchResultDao() { return researchResultDao; } public void setResearchResultDao(ResearchResultDao researchResultDao) { this.researchResultDao = researchResultDao; } public PpatentDao getPpatentDao() { return ppatentDao; } public void setPpatentDao(PpatentDao ppatentDao) { this.ppatentDao = ppatentDao; } public PpaperDao getPpaperDao() { return ppaperDao; } public void setPpaperDao(PpaperDao ppaperDao) { this.ppaperDao = ppaperDao; } /** * 上传文件 * * @param it * @return * @throws Exception */ @Path("/upload") @Post public List<UploadFile> upload(@Upload UploadItemIterator it) throws Exception { List<UploadFile> ret = new LinkedList<>(); try { while (it.hasNext()) { Item item = it.next(); if (!item.isFormField()) { String name = normalizeFileName(item.getName()); int index = name.lastIndexOf('.'); String ext = index >= 0 ? name.substring(index + 1) : ""; ext = ext.trim(); long fileLen = 0; int len = 0; UploadFile uf = this.buildTargetFile(ext); uf.setName(name); File file = uf.getFn(); ret.add(uf); byte[] buf = new byte[8092]; OutputStream of = new FileOutputStream(file); try { InputStream in = item.getInputStream(); try { while ((len = in.read(buf)) >= 0) { if (len > 0) { of.write(buf, 0, len); fileLen += len; } } uf.setSize(fileLen); } finally { in.close(); } } finally { of.close(); } } } return ret; } catch (Exception e) { this.remove(ret); throw e; } finally { if (it != null) it.clean(); } } @Post @Path("/apply") public String apply(@JdbcConn(true) Connection con, Team team) throws SQLException { String id = StringUtil.buildUUID(); team.setId(id); team.setStatus("1"); String pro = team.getSecretary(); TeamPro teamPro = new TeamPro(); teamPro.setId(id); teamPro.setProfessor(pro); teamPro.setSecretary(true); teamDao.insert(con, team); teamDao.insert(con, teamPro); return id; } @Post @Path("/update") public void update(@JdbcConn(true)Connection con,Team team)throws SQLException { String[] kws = null; if (team.getSubject() != null) { kws = ListUtil.splitTrimExcludeEmpty(team.getSubject(), ',').toArray(new String[0]); } keyWordService.refreshTeam(con, team.getId(), kws); teamDao.update(con, team); } @Get @Path("/pq") public PageQueryResult<Team> pageQuery(@JdbcConn Connection con, String status, @Nullable String key, @Nullable String subject, @Nullable String industry,@Nullable String city, int pageSize, int pageNo) throws SQLException { return teamDao.pageQuery(con, status, key == null ? null : "%" + key + "%", subject == null ? null : "%" + subject + "%", industry == null ? null : "%" + industry + "%", city == null ? null : "%" + city + "%", pageSize, pageNo); } @Get @Path("/myTeam") public PageQueryResult<Team> pageQuery(@JdbcConn Connection con, String status, String professor, int pageSize, int pageNo) throws SQLException { return teamDao.pageQuery(con, status, professor, pageSize, pageNo); } @Get @Path("/qo") public Team query(@JdbcConn Connection con, String id) throws SQLException { return teamDao.query(con, id); } @Get @Path("/qm") public List<Team> query(@JdbcConn Connection con,String[] id)throws SQLException { return teamDao.query(con, id); } @Post @Path("/delete") public void delete(@JdbcConn(true) Connection con, String id) throws SQLException { watchDao.deleteWatch(con,(short)13, id); teamDao.delete(con, id); keyWordService.refreshTeam(con,id,null); } @Get @Path("/pro") public PageQueryResult<TeamPro> queryPro(@JdbcConn Connection con,String id,int pageSize,int pageNo)throws SQLException { return teamDao.queryPro(con, id, pageSize, pageNo); } @Get @Path("/pro/search") public List<SortedPro> proSearch(@JdbcConn(false) Connection con, @Nullable String key,String team, @Nullable Integer authType, @DefaultValue("99999") int sortFirst, @DefaultValue("\"Z\"")String id, @DefaultValue("0")double starLevel, @DefaultValue("10000000") int rows) throws SQLException { if (key != null) { key = "%" + key + "%"; } String sortDesc = "000000"+ sortFirst; String a = "0000" + (int)(starLevel*100); sortDesc = sortDesc.substring(sortDesc.length()-5)+a.substring(a.length()-4)+id; List<SortedPro> queryResult = this.professorDao.teamSearch(con,sortDesc, key,team, authType,rows ); if (!queryResult.isEmpty()) { for (SortedPro editProfessor : queryResult) { editProfessor.setHasHeadImage(this.imageService.hasProfessorImage(editProfessor.getId())); } ProfessorService.hiddenPrivacyInfo(queryResult); } return queryResult; } @Get @Path("/patent") public PageQueryResult<TeamPatent> queryPatent(@JdbcConn Connection con,String id,int pageSize,int pageNo)throws SQLException { return teamDao.queryPatent(con, id, pageSize, pageNo); } @Get @Path("/patent/search") public List<Ppatent> patentSearch(@JdbcConn Connection con, @Nullable String key, String team,@DefaultValue("Long.MAX_VALUE") long sortNum, @DefaultValue("\"9\"") String createTime, @DefaultValue("\"G\"")String id, @DefaultValue("10000000") int rows)throws SQLException { if (key != null) { key = "%" + key + "%"; } return this.ppatentDao.searchForTeam(con, key,team, sortNum, createTime,id, rows); } @Get @Path("/paper") public PageQueryResult<TeamPaper> queryPaper(@JdbcConn Connection con,String id,int pageSize,int pageNo)throws SQLException { return teamDao.queryPaper(con, id, pageSize, pageNo); } @Get @Path("/paper/search") public List<Ppaper> paperSearch(@JdbcConn Connection con, @Nullable String key,String team, @DefaultValue("Long.MAX_VALUE") long sortNum, @DefaultValue("\"9\"") String createTime, @DefaultValue("\"G\"")String id, @DefaultValue("10000000") int rows)throws SQLException { if (key != null) { key = "%" + key + "%"; } return this.ppaperDao.searchForTeam(con, key,team, sortNum, createTime, id, rows); } @Get @Path("/resResult") public PageQueryResult<TeamResResult> queryResResult(@JdbcConn Connection con,String id,int pageSize,int pageNo)throws SQLException { return teamDao.queryResResult(con, id, pageSize, pageNo); } @Get @Path("/resResult/search") public PageQueryResult<ResearchResult> resResultSearch(@JdbcConn Connection con, @Nullable String[] status,String team, @Nullable String key, int pageSize, int pageNo) throws SQLException { return researchResultDao.searchForTeam(con, status, key == null ? null : "%" + key + "%",team, pageSize, pageNo); } @Post @Path("/deletePro") public void deletePro(@JdbcConn(true) Connection con,String id,String professor)throws SQLException { teamDao.deletePro(con, id, professor); keyWordService.refreshTeam(con, id, null); Team team = teamDao.query(con, id); if (team.getChief().equals(professor)) { team.setChief(null); } } @Post @Path("/deletePatent") public void deletePatent(@JdbcConn(true) Connection con,String id,String patent)throws SQLException { teamDao.deletePatent(con, id, patent); } @Post @Path("/deletePaper") public void deletePaper(@JdbcConn(true) Connection con,String id,String paper)throws SQLException { teamDao.deletePaper(con, id, paper); } @Post @Path("/deleteResResult") public void deleteResResult(@JdbcConn(true) Connection con,String id,String researchResult)throws SQLException { teamDao.deleteResResult(con, id, researchResult); } @Post @Path("/insertPro") public void insertPro(@JdbcConn(true)Connection con,String id,String professor)throws SQLException { TeamPro teamPro = new TeamPro(); teamPro.setId(id); teamPro.setProfessor(professor); teamDao.insert(con, teamPro); } @Post @Path("/insertPatent") public void insertPatent(@JdbcConn(true)Connection con,String id,String patent)throws SQLException { TeamPatent teamPatent = new TeamPatent(); teamPatent.setId(id); teamPatent.setPatent(patent); teamDao.insert(con, teamPatent); } @Post @Path("/insertPaper") public void insertPaper(@JdbcConn(true)Connection con,String id,String paper)throws SQLException { TeamPaper teamPaper = new TeamPaper(); teamPaper.setId(id); teamPaper.setPaper(paper); teamDao.insert(con, teamPaper); } @Post @Path("/insertResResult") public void insertResResult(@JdbcConn(true)Connection con,String id,String researchResult)throws SQLException { TeamResResult teamResResult = new TeamResResult(); teamResResult.setId(id); teamResResult.setResearchResult(researchResult); teamDao.insert(con, teamResResult); } @Post @Path("/secretary") public void changeSecretary(@JdbcConn(true)Connection con,String id,String newPro)throws SQLException { teamDao.updateSecretary(con, newPro, id); teamDao.update(con, id); teamDao.update(con, id, newPro); } @Post @Path("/chief") public void changeChief(@JdbcConn(true)Connection con,String id,@Nullable String newPro)throws SQLException { teamDao.updateCh(con, id); teamDao.updateChief(con, newPro, id); if (newPro != null) { teamDao.updateCh(con, id, newPro); } } @Get @Path("/count") public long count(@JdbcConn Connection con, final String id)throws SQLException { return JdbcUtil.queryLong(con, "select count(1) from team where id in (select id from team_pro where professor = ?) and status='3'", new PreparedStatementConfig() { @Override public void config(PreparedStatement preparedStatement) throws SQLException { preparedStatement.setString(1,id); } }, 0, 0); } @Get @Path("/pro/count") public long countPro(@JdbcConn Connection con,String id)throws SQLException { return teamDao.countPro(con, id); } @Post @Path("/quit") public void quit(@JdbcConn(true) Connection con,String id,String professor)throws SQLException,JfwBaseException{ Team team = teamDao.query(con, id); if (professor.equals(team.getSecretary())) { throw new JfwBaseException(801, "Team secretary can't leave the team"); }else { teamDao.deletePro(con, id, professor); if (team.getChief().equals(professor)) { team.setChief(null); } } } private void remove(List<UploadFile> list) { for (UploadFile file : list) { if (file.fn != null) file.fn.delete(); } } private UploadFile buildTargetFile(String ext) { StringBuilder sb = new StringBuilder(); sb.append(System.currentTimeMillis() / (1000 * 60 * 60 * 24)); File path = new File(this.imgPath, sb.toString()); if (!path.exists()) { synchronized (this) { if (!path.mkdirs()) throw new RuntimeException("mkdir error[" + path + "]"); FN_IDX.set(1); } } File file; do { String fn = FN_IDX.toString(); if (ext.isEmpty()) { file = new File(path, fn); } else { file = new File(path, fn + "." + ext); } FN_IDX.incrementAndGet(); } while (file.exists()); sb.append("/").append(file.getName()); UploadFile uf = new UploadFile(); uf.setFn(file); uf.setUri("/" + sb.toString()); return uf; } private String normalizeFileName(String fn) { int index = fn.indexOf('\\'); if (index >= 0) { fn = fn.substring(fn.lastIndexOf('\\') + 1); } index = fn.indexOf('/'); if (index >= 0) { fn = fn.substring(fn.lastIndexOf('/') + 1); } if (fn.length() == 0) throw new RuntimeException("invalid filename in Multipart/data request"); return fn; } public static class UploadFile { private String name; private String uri; private long size; private transient File fn; public File getFn() { return fn; } public void setFn(File fn) { this.fn = fn; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUri() { return uri; } public void setUri(String uri) { this.uri = uri; } public long getSize() { return size; } public void setSize(long size) { this.size = size; } } }