|
package com.ekexiu.console.system.service;
import com.ekexiu.console.system.dao.*;
import com.ekexiu.console.system.po.*;
import com.ekexiu.console.system.pojo.ArticleInfo;
import com.ekexiu.console.system.pojo.ProfessorInfo;
import com.ekexiu.console.system.pojo.ResourceInfo;
import com.ekexiu.console.util.HttpService;
import com.ekexiu.console.util.HttpServiceImpl;
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.PathVar;
import org.jfw.util.JpgUtil;
import org.jfw.util.PageQueryResult;
import org.jfw.util.exception.JfwBaseException;
import org.jfw.util.io.IoUtil;
import org.jfw.util.reflect.TypeReference;
import java.io.*;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by TT on 2017/8/3.
*/
@Path("/article")
public class ArticleService {
private static HttpService http = new HttpServiceImpl();
private File tmpPath;
private File articlePath;
private String dateFormat = "yyyyMMdd";
private String timeFormat = "yyyyMMddHHmmss";
private int artMaxLen = 200;
private static final String JPG = "jpg";
@Autowrie
private ArticleDao articleDao;
@Autowrie
private KeyWordService keyWordService;
@Autowrie
private ArticleProDao articleProDao;
@Autowrie
private ArticleOrgDao articleOrgDao;
@Autowrie
private ArticleResDao articleResDao;
@Autowrie
private ProfessorDao professorDao;
@Autowrie
private OrgDao orgDao;
@Autowrie
private ResourceDao resourceDao;
public ArticleDao getArticleDao() {
return articleDao;
}
public void setArticleDao(ArticleDao articleDao) {
this.articleDao = articleDao;
}
public KeyWordService getKeyWordService() {
return keyWordService;
}
public void setKeyWordService(KeyWordService keyWordService) {
this.keyWordService = keyWordService;
}
public ArticleProDao getArticleProDao() {
return articleProDao;
}
public void setArticleProDao(ArticleProDao articleProDao) {
this.articleProDao = articleProDao;
}
public ArticleOrgDao getArticleOrgDao() {
return articleOrgDao;
}
public void setArticleOrgDao(ArticleOrgDao articleOrgDao) {
this.articleOrgDao = articleOrgDao;
}
public ArticleResDao getArticleResDao() {
return articleResDao;
}
public void setArticleResDao(ArticleResDao articleResDao) {
this.articleResDao = articleResDao;
}
public ProfessorDao getProfessorDao() {
return professorDao;
}
public void setProfessorDao(ProfessorDao professorDao) {
this.professorDao = professorDao;
}
public OrgDao getOrgDao() {
return orgDao;
}
public void setOrgDao(OrgDao orgDao) {
this.orgDao = orgDao;
}
public ResourceDao getResourceDao() {
return resourceDao;
}
public void setResourceDao(ResourceDao resourceDao) {
this.resourceDao = resourceDao;
}
public File getTmpPath() {
return tmpPath;
}
public void setTmpPath(File tmpPath) {
this.tmpPath = tmpPath;
}
public File getArticlePath() {
return articlePath;
}
public void setArticlePath(File articlePath) {
this.articlePath = articlePath;
}
public String getDateFormat() {
return dateFormat;
}
public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}
public String getTimeFormat() {
return timeFormat;
}
public void setTimeFormat(String timeFormat) {
this.timeFormat = timeFormat;
}
public int getArtMaxLen() {
return artMaxLen;
}
public void setArtMaxLen(int artMaxLen) {
this.artMaxLen = artMaxLen;
}
public static String getJPG() {
return JPG;
}
private byte[] resImage(byte[] src, int maxLen) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(src);
ByteArrayOutputStream out = new ByteArrayOutputStream();
JpgUtil.scalingZoom(in, out, maxLen);
out.flush();
return out.toByteArray();
}
private byte[] readTmpFile(String fn) throws JfwBaseException {
File file = new File(this.tmpPath, fn);
if (!file.exists())
throw new JfwBaseException(90, "resource image not exists");
try {
InputStream in = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream();
IoUtil.copy(in, out, true, true);
return out.toByteArray();
} catch (IOException e) {
throw new JfwBaseException(91, "read temp resource image error", e);
}
}
private void saveArtImg(String fn, String id) throws JfwBaseException, IOException {
byte[] src = this.readTmpFile(fn);
src = JpgUtil.read(src);
byte[] shareResImage = this.resImage(src, this.artMaxLen);
String savePath = this.articlePath + "/" + this.createDate();
File dateFile = new File(savePath);
if (!dateFile.exists()) {
// 创建日期目录
dateFile.mkdirs();
}
IoUtil.saveStream(new FileOutputStream(new File(dateFile, id + "." + JPG)), src, true);
IoUtil.saveStream(new FileOutputStream(new File(dateFile, id + "_s." + JPG)), shareResImage, true);
}
private String createDate() {
SimpleDateFormat df = new SimpleDateFormat(this.dateFormat);
String date = df.format(new Date());
return date;
}
@Path("/pq")
@Get
public PageQueryResult<ArticleInfo> pageQueryResult(@JdbcConn Connection con, @Nullable String title, @Nullable String name, int orderBy, @Nullable String bt, @Nullable String et, @DefaultValue("1") int pageNo, @DefaultValue("10") int pageSize)throws SQLException {
if (orderBy == 1) {
return this.articleDao.queryByTime(con, title == null ? null : "%" + title + "%", name == null ? "%" : "%" + name + "%", bt == null ? null : bt + "000000", et == null ? null : et + "235959", pageSize, pageNo);
}
if (orderBy == 2) {
return this.articleDao.queryByPV(con, title == null ? null : "%" + title + "%", name == null ? "%" : "%" + name + "%", bt == null ? null : bt + "000000", et == null ? null : et + "235959", pageSize, pageNo);
}
return null;
}
@Post
@Path("/deleteArticle")
public void deleteArticle(@JdbcConn(false) Connection con,String[] articleIds) throws SQLException, JfwBaseException, IOException {
//修改文章状态为删除
try{
this.articleDao.updateStatus(con, articleIds, "3");
con.commit();
for (String articleId : articleIds) {
http.post("http://192.168.3.233:81/ajax/cms", "t=Article" + "&k=" + articleId, null);
}
} catch (JfwBaseException | IOException e) {
try {
con.rollback();
} catch (SQLException e1) {
}
throw e;
}
}
@Post
@Path("/sortNum")
public void updateSortNum(@JdbcConn(true) Connection con,String articleId,long sortNum) throws SQLException{
this.articleDao.updateSortNum(con, articleId, sortNum);
}
@Get
@Path("/id/{id}")
public Article query(@JdbcConn Connection con, @PathVar String id) throws SQLException {
return this.articleDao.query(con, id);
}
@Post
@Path("/colNum")
public void updateColNum(@JdbcConn(true) Connection con,String[] articleIds,int colNum)throws SQLException {
this.articleDao.updateColNum(con, articleIds, colNum);
}
@Post
@Path("/subject")
public void updateSubject(@JdbcConn(true) Connection con, String articleId, @Nullable String subject) throws SQLException {
this.articleDao.updateSubject(con, articleId, subject);
this.keyWordService.refreshArticle(con, articleId, subject);
}
@Get
@Path("/id")
public ArticleInfo queryInfo(@JdbcConn Connection con,String id) throws SQLException{
return this.articleDao.queryInfo(con, id);
}
@Get
@Path("/base")
public Article queryBaseInfo(@JdbcConn Connection con, String id) throws SQLException {
return this.articleDao.queryBaseInfo(con, id);
}
@Get
@Path("/qaHotKey")
public List<DataDictCode> queryHotKey(String key) throws JfwBaseException, IOException {
List<DataDictCode> dataDictCodes = http.get("http://www.ekexiu.com/ajax/dataDict/qaHotKey?key="+ URLEncoder.encode(key,"UTF-8"), new TypeReference<List<DataDictCode>>() {
}.getType());
return dataDictCodes;
}
@Get
@Path("/typeHotKey")
public List<TestService.TypeaheadItem> get(String q, @DefaultValue("10") int s) throws JfwBaseException,IOException {
List<TestService.TypeaheadItem> ret = new ArrayList<>(s);
List<DataDictCode> dataDictCodes = http.get("http://www.ekexiu.com/ajax/dataDict/qaHotKey?key="+ URLEncoder.encode( q,"UTF-8"), new TypeReference<List<DataDictCode>>() {
}.getType());
for (DataDictCode dictCode : dataDictCodes) {
TestService.TypeaheadItem thi = new TestService.TypeaheadItem();
thi.setCode(dictCode.getCaption());
thi.setCaption(dictCode.getCaption());
ret.add(thi);
}
return ret;
}
@Post
@Path("/save")
public String saveArticle(@JdbcConn(false) Connection con, Article article)throws SQLException,IOException,JfwBaseException {
String articleId = null;
try {
if (article.getArticleId().trim().length() == 32) {
articleId = article.getArticleId().trim();
if (article.getArticleImg() != null) {
this.saveArtImg(article.getArticleImg(), articleId);
article.setArticleImg(this.createDate() + "/" + articleId + "." + JPG);
}
Article article1 = this.articleDao.query(con, articleId);
article.setColNum(article1.getColNum());
this.articleDao.update(con, article);
this.keyWordService.refreshArticle(con, articleId, article.getSubject());
} else {
throw new JfwBaseException(-2, "bad parameter:articleId");
}
con.commit();
http.post("http://192.168.3.233:81/ajax/cms", "t=Article" + "&k=" + articleId, null);
} catch (SQLException | IOException | JfwBaseException e) {
try {
con.rollback();
} catch (SQLException ee) {
}
throw e;
}
return articleId;
}
@Post
@Path("/relatePro")
public List<ProfessorInfo> relatePro(@JdbcConn Connection con,String articleId)throws SQLException {
List<ArticlePro> articlePros = this.articleProDao.query(con, articleId);
List<ProfessorInfo> professorInfos =new ArrayList<ProfessorInfo>();
for (ArticlePro articlePro : articlePros) {
ProfessorInfo professorInfo = this.professorDao.query(con, articlePro.getProfessorId());
professorInfos.add(professorInfo);
}
return professorInfos;
}
@Post
@Path("/relateOrg")
public List<Organization> relateOrg(@JdbcConn Connection con, String articleId)throws SQLException {
List<ArticleOrg> articleOrgs = this.articleOrgDao.query(con, articleId);
List<Organization> orgs= new ArrayList<Organization>();
for (ArticleOrg articleOrg : articleOrgs) {
Organization org = this.orgDao.queryOrg(con,articleOrg.getOrgId());
orgs.add(org);
}
return orgs;
}
@Post
@Path("/relateRes")
public List<ResourceInfo> relateRes(@JdbcConn Connection con, String articleId)throws SQLException {
List<ArticleRes> articleRess = this.articleResDao.query(con, articleId);
List<ResourceInfo> resourceInfos = new ArrayList<ResourceInfo>();
for (ArticleRes articleRes : articleRess) {
ResourceInfo resourceInfo = this.resourceDao.queryResInfo(con, articleRes.getResourceId());
resourceInfos.add(resourceInfo);
}
return resourceInfos;
}
@Post
@Path("/relateSave")
public void relateSave(@JdbcConn(true) Connection con,String articleId,@Nullable String[] professors,@Nullable String[] resources,@Nullable String[] orgs)throws SQLException,JfwBaseException{
this.articleProDao.delete(con, articleId);
this.articleResDao.delete(con, articleId);
this.articleOrgDao.delete(con, articleId);
if (professors != null) {
for (String professor : professors) {
ArticlePro articlePro = new ArticlePro();
articlePro.setArticleId(articleId);
articlePro.setProfessorId(professor);
try {
this.articleProDao.insert(con, articlePro);
} catch (SQLException e) {
throw new JfwBaseException(6001, "关联专家不可重复");
}
}
}
if (resources != null) {
for (String resource : resources) {
ArticleRes articleRes = new ArticleRes();
articleRes.setArticleId(articleId);
articleRes.setResourceId(resource);
try {
this.articleResDao.insert(con, articleRes);
} catch (SQLException e) {
throw new JfwBaseException(6002, "关联资源不可重复");
}
}
}
if (orgs != null && orgs.length > 0) {
for (String org : orgs) {
ArticleOrg articleOrg = new ArticleOrg();
articleOrg.setArticleId(articleId);
articleOrg.setOrgId(org);
try {
this.articleOrgDao.insert(con, articleOrg);
} catch (SQLException e) {
throw new JfwBaseException(6003, "关联企业不可重复");
}
}
}
}
public class DataDictCode {
private long count;
private String caption;
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
}
public static void main(String[] args)throws Exception {
String articleId = "B0334EAE5D734A258F97E185CDD7E160";
http.post("http://192.168.3.233:81/ajax/cms", "t=Article" + "&k=" + articleId, null);
}
}
|