|
package com.ekexiu.portal.service;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
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.RequestParam;
import org.jfw.util.PageQueryResult;
import org.jfw.util.StringUtil;
import org.jfw.util.context.JfwAppContext;
import org.jfw.util.exception.JfwBaseException;
import org.jfw.util.jdbc.JdbcUtil;
import org.jfw.util.state.StateCode;
import com.ekexiu.portal.dao.DemandDao;
import com.ekexiu.portal.dao.UserDao;
import com.ekexiu.portal.po.Demand;
import com.ekexiu.portal.po.Organization;
import com.ekexiu.portal.po.Professor;
import com.ekexiu.portal.po.User;
@Path("/demand")
public class DemandService {
public static final String MAX_CREATETIME = "9";
@Autowrie
private UserDao userDao;
@Autowrie
private ProfessorService professorService;
@Autowrie
private OrgService orgService;
@Autowrie
private DemandDao demandDao;
public OrgService getOrgService() {
return orgService;
}
public void setOrgService(OrgService orgService) {
this.orgService = orgService;
}
public ProfessorService getProfessorService() {
return professorService;
}
public void setProfessorService(ProfessorService professorService) {
this.professorService = professorService;
}
public DemandDao getDemandDao() {
return demandDao;
}
public void setDemandDao(DemandDao demandDao) {
this.demandDao = demandDao;
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/**
* 发布需求
*
* @param con
* @param demand
* @throws SQLException
*/
@Post
@Path
public String insert(@JdbcConn(true) Connection con,
@RequestParam(excludeFields = { "id", "state", "createTime", "modifyTime", "shareId", "pageViews", "modifier" }) Demand demand)
throws SQLException {
String demandId = StringUtil.buildUUID();
demand.setId(demandId);
demand.setState("1");
if (demand.getCost() == null) {
demand.setCost("0");
}
if (demand.getDuration() == null) {
demand.setDuration("0");
}
demand.setModifier(demand.getCreator());
this.demandDao.insert(con, demand);
return demandId;
}
/**
* 发布需求
*
* @param con
* @param demand
* @throws SQLException
*/
@Post
@Path("/publishInPlatform")
public String insert(@JdbcConn Connection con,
@RequestParam(excludeFields = { "creator", "id", "state", "createTime", "modifyTime", "shareId", "pageViews", "modifier" }) Demand demand,
String state, String vc, String name) throws Exception {
@SuppressWarnings("unchecked")
StateCode<String, String> sc = (StateCode<String, String>) JfwAppContext.getCachedObject(state);
if (sc == null || sc.getExpiredTime() < System.currentTimeMillis())
throw new JfwBaseException(-600001, "验证超时");
if (!sc.getKey().equals(demand.getContactNum())) {
throw new JfwBaseException(-2, "手机号与验证手机不匹配");
}
if (!sc.getValue().equals(vc)) {
throw new JfwBaseException(-3, "验证码错误");
}
String orgId = null;
try {
orgId = this.orgService.createOrganization(con, demand.getOrgName());
con.commit();
} catch (Exception e) {
JdbcUtil.rollback(con);
throw e;
}
String demandId = null;
try {
User user = null;
boolean exists = false;
String uid = StringUtil.buildUUID();
try {
user = new User();
user.setId(uid);
String passwd = StringUtil.md5("123456");
user.setMobilePhone(demand.getContactNum());
user.setPasswd(passwd);
user.setUserType("0");
user.setActiveTime(SysService.DATE.format(new Date()));
this.userDao.insert(con, user);
} catch (SQLException e) {
if ("23505".equals(e.getSQLState())) {
con.rollback();
user = userDao.queryByEmailOrMobilePhone(con, demand.getContactNum());
if (user == null)
throw new RuntimeException("not fonud with query user by phone");
exists = true;
uid = user.getId();
} else {
throw e;
}
}
if (exists) {
Professor professor = this.professorService.query(con, uid);
if (professor == null) {
professor = new Professor();
professor.setId(uid);
professor.setName(name);
professor.setPhone(demand.getContactNum());
professor.setOrgId(orgId);
this.professorService.insert(con, professor, null);
}
} else {
Professor professor = new Professor();
professor.setId(uid);
professor.setName(name);
professor.setPhone(demand.getContactNum());
professor.setOrgId(orgId);
this.professorService.insert(con, professor, null);
}
Organization org = this.orgService.query(con, orgId);
if (!demand.getOrgName().equals(org.getName())) {
demand.setOrgName(org.getName());
}
demandId = StringUtil.buildUUID();
demand.setId(demandId);
demand.setState("1");
if (demand.getCost() == null) {
demand.setCost("0");
}
if (demand.getDuration() == null) {
demand.setDuration("0");
}
demand.setCreator(user.getId());
demand.setModifier(user.getId());
this.demandDao.insert(con, demand);
con.commit();
} catch (Exception e) {
JdbcUtil.rollback(con);
throw e;
} finally {
JfwAppContext.removeCachedObject(state);
}
return demandId;
}
/**
* 修改需求
*
* @param con
* @param demand
* @throws SQLException
*/
@Post
@Path("/modify")
public boolean update(@JdbcConn(true) Connection con,
@RequestParam(excludeFields = { "state", "createTime", "modifyTime", "title","source", "descp", "creator", "pageViews", "shareId" }) Demand demand)
throws SQLException {
if (demand.getCost() == null) {
demand.setCost("0");
}
if (demand.getDuration() == null) {
demand.setDuration("0");
}
demand.setState("1");
return this.demandDao.update(con, demand) > 0;
}
/**
* 完成需求
*
* @param con
* @param id
* 需求ID
* @param uid
* 操作用户ID
* @return
* @throws SQLException
*/
@Post
@Path("/over")
public boolean over(@JdbcConn(true) Connection con, String id, String uid) throws SQLException {
return this.demandDao.over(con, id, uid) > 0;
}
/**
* 关闭需求
*
* @param con
* @param id
* 需求ID
* @param uid
* 需求ID
* @return
* @throws SQLException
*/
@Post
@Path("/close")
public boolean close(@JdbcConn(true) Connection con, String id, String uid) throws SQLException {
return this.demandDao.close(con, id, uid) > 0;
}
/**
* 延期需求
*
* @param con
* @param id
* 需求ID
* @param uid
* 操作用户ID
* @param day
* 延期日期 YYYYMMD
* @return
* @throws SQLException
*/
@Post
@Path("/defer")
public boolean defer(@JdbcConn(true) Connection con, String id, String uid, String day) throws SQLException {
return this.demandDao.defer(con, id, uid, day) > 0;
}
/**
* 增加浏览量
*
* @param con
* @param id
* 需求ID
* @throws SQLException
*/
@Post
@Path("/incPageViews")
public void incPageViews(@JdbcConn(true) Connection con, String id) throws SQLException {
this.demandDao.incPageViews(con, id);
}
/**
* 查询需求数量
*
* @param con
* @param state
* 需求状态
* @param uid
* 发布者ID
* @param oid
* 发布者发布时机构ID
* @return
* @throws SQLException
*/
@Get
@Path("/qc")
public int queryCount(@JdbcConn Connection con, @Nullable String[] state, @Nullable String uid, @Nullable String oid) throws SQLException {
return this.demandDao.cnt(con, state, uid, oid);
}
/**
* 查询需求
*
* @param con
* @param id
* 需求ID
* @return
* @throws SQLException
*/
@Get
@Path("/qo")
public Demand queryOne(@JdbcConn Connection con, String id) throws SQLException {
return this.demandDao.query(con, id);
}
/**
* 根据分享ID 查询需求
*
* @param con
* @param id
* 分享ID
* @return
* @throws SQLException
*/
@Get
@Path("/byShareId")
public Demand queryByShareId(@JdbcConn Connection con, long id) throws SQLException {
return this.demandDao.query(con, id);
}
/**
* 查询需求列表
*
* @param con
* @param id
* 需求ID 数组
* @return
* @throws SQLException
*/
@Get
@Path("/qm")
public List<Demand> queryOne(@JdbcConn Connection con, String[] id) throws SQLException {
return this.demandDao.query(con, id);
}
/**
* 分页查询需求(按状态及发布时间排序)
*
* @param con
* @param state
* @param uid
* @param oid
* @return
* @throws SQLException
*/
@Get
@Path("/pq")
public PageQueryResult<Demand> query(@JdbcConn Connection con, @Nullable String[] state, @Nullable String uid, @Nullable String oname,
@DefaultValue("1") int pageNo, @DefaultValue("5") int pageSize) throws SQLException {
return this.demandDao.pageQuery(con, state, uid, oname, pageSize, pageNo);
}
/**
* 分页查询需求(按发布时间排序)
*
* @param con
* @param state
* @param uid
* @param oid
* @return
* @throws SQLException
*/
@Get
@Path("/search")
public PageQueryResult<Demand> query(@JdbcConn Connection con, @Nullable String[] state, @Nullable String key, @DefaultValue("1") int pageNo,
@DefaultValue("5") int pageSize) throws SQLException {
return this.demandDao.pageQuery(con, state, key == null ? key : ("%" + key + "%"), pageSize, pageNo);
}
@Get
@Path("/search/platform")
public PageQueryResult<Demand> query(@JdbcConn Connection con, @Nullable String state, @Nullable String source,@Nullable String cost, @Nullable String duration, boolean byCreateTime, @Nullable String key, @DefaultValue("1") int pageNo,
@DefaultValue("5") int pageSize) throws SQLException {
if(byCreateTime){
return this.demandDao.pageQueryOrderByCreateTimeDesc(con, state, source, cost, duration, key, pageSize, pageNo);
}else{
return this.demandDao.pageQueryOrderByInvalidDayAsc(con, state, source, cost, duration, key, pageSize, pageNo);
}
}
}
|