|
package com.ekexiu.console.system.service;
import com.ekexiu.console.system.dao.OrgDao;
import com.ekexiu.console.system.dao.OrgRecordDao;
import com.ekexiu.console.system.dao.OrgUserDao;
import com.ekexiu.console.system.po.OrgRecord;
import com.ekexiu.console.system.po.OrgUser;
import com.ekexiu.console.system.po.Organization;
import com.ekexiu.console.system.pojo.OrganizationInfo;
import com.ekexiu.console.system.vo.ConsoleAuthUser;
import org.jfw.apt.annotation.Autowrie;
import org.jfw.apt.annotation.DefaultValue;
import org.jfw.apt.annotation.Nullable;
import org.jfw.apt.web.annotation.LoginUser;
import org.jfw.apt.web.annotation.Path;
import org.jfw.apt.web.annotation.operate.Delete;
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.apt.web.annotation.param.RequestParam;
import org.jfw.util.DateUtil;
import org.jfw.util.JpgUtil;
import org.jfw.util.PageQueryResult;
import org.jfw.util.StringUtil;
import org.jfw.util.exception.JfwBaseException;
import org.jfw.util.io.IoUtil;
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;
@Path("/sys/org")
public class OrgService extends com.ekexiu.console.service.Upload {
private String defaultOrgType;
/**
* 默认企业认证状态为 0-未认证
*/
private String defaultAuthStatus = "0";
private File imagePath;
private File tmpPath;
private File orgPath;
private int largeHeadPhotoWidth = 200;
private int largeHeadPhotoHeight = 200;
private int middleHeadPhotoWidth = 200;
private int middleHeadPhotoHeight = 200;
private int smallHeadPhotoWidth = 200;
private int smallHeadPhotoHeight = 200;
private File defaultHeadPhoto;
private File defaultOrgLogo;
private File defaultResourcePhoto;
private byte[] dli;
private byte[] dmi;
private byte[] dsi;
private byte[] dol;
private byte[] dsl;
public static final String DEFAULT_PW_STR = StringUtil.md5("123456");
@Autowrie
private OrgDao orgDao;
@Autowrie
private OrgUserDao orgUserDao;
@Autowrie
private OrgRecordDao orgRecordDao;
public OrgRecordDao getOrgRecordDao() {
return orgRecordDao;
}
public void setOrgRecordDao(OrgRecordDao orgRecordDao) {
this.orgRecordDao = orgRecordDao;
}
public File getDefaultHeadPhoto() {
return defaultHeadPhoto;
}
public void setDefaultHeadPhoto(File defaultHeadPhoto) {
this.defaultHeadPhoto = defaultHeadPhoto;
}
public File getDefaultOrgLogo() {
return defaultOrgLogo;
}
public void setDefaultOrgLogo(File defaultOrgLogo) {
this.defaultOrgLogo = defaultOrgLogo;
}
public File getDefaultResourcePhoto() {
return defaultResourcePhoto;
}
public void setDefaultResourcePhoto(File defaultResourcePhoto) {
this.defaultResourcePhoto = defaultResourcePhoto;
}
public String getDefaultOrgType() {
return defaultOrgType;
}
public void setDefaultOrgType(String defaultOrgType) {
this.defaultOrgType = defaultOrgType;
}
public String getDefaultAuthStatus() {
return defaultAuthStatus;
}
public void setDefaultAuthStatus(String defaultAuthStatus) {
this.defaultAuthStatus = defaultAuthStatus;
}
public File getImagePath() {
return imagePath;
}
public void setImagePath(File imagePath) {
this.imagePath = imagePath;
this.orgPath = new File(this.imagePath, "org");
}
public File getTmpPath() {
return tmpPath;
}
public void setTmpPath(File tmpPath) {
this.tmpPath = tmpPath;
}
public File getOrgPath() {
return orgPath;
}
public void setOrgPath(File orgPath) {
this.orgPath = orgPath;
}
public OrgUserDao getOrgUserDao() {
return orgUserDao;
}
public void setOrgUserDao(OrgUserDao orgUserDao) {
this.orgUserDao = orgUserDao;
}
public OrgDao getOrgDao() {
return orgDao;
}
public void setOrgDao(OrgDao orgDao) {
this.orgDao = orgDao;
}
public int getLargeHeadPhotoWidth() {
return largeHeadPhotoWidth;
}
public void setLargeHeadPhotoWidth(int largeHeadPhotoWidth) {
this.largeHeadPhotoWidth = largeHeadPhotoWidth;
}
public int getLargeHeadPhotoHeight() {
return largeHeadPhotoHeight;
}
public void setLargeHeadPhotoHeight(int largeHeadPhotoHeight) {
this.largeHeadPhotoHeight = largeHeadPhotoHeight;
}
public int getMiddleHeadPhotoWidth() {
return middleHeadPhotoWidth;
}
public void setMiddleHeadPhotoWidth(int middleHeadPhotoWidth) {
this.middleHeadPhotoWidth = middleHeadPhotoWidth;
}
public int getMiddleHeadPhotoHeight() {
return middleHeadPhotoHeight;
}
public void setMiddleHeadPhotoHeight(int middleHeadPhotoHeight) {
this.middleHeadPhotoHeight = middleHeadPhotoHeight;
}
public int getSmallHeadPhotoWidth() {
return smallHeadPhotoWidth;
}
public void setSmallHeadPhotoWidth(int smallHeadPhotoWidth) {
this.smallHeadPhotoWidth = smallHeadPhotoWidth;
}
public int getSmallHeadPhotoHeight() {
return smallHeadPhotoHeight;
}
public void setSmallHeadPhotoHeight(int smallHeadPhotoHeight) {
this.smallHeadPhotoHeight = smallHeadPhotoHeight;
}
@Post
@Path
public String insert(@JdbcConn(true) Connection con, @RequestParam(excludeFields = {"createTime", "modifyTime", "id"}) Organization orgn)
throws SQLException {
String id = StringUtil.buildUUID();
orgn.setId(id);
this.orgDao.insert(con, orgn);
return id;
}
public void saveOrgLogo(String id, String fn) throws IOException {
InputStream in = new FileInputStream(new File(this.tmpPath, fn));
try {
IoUtil.copy(in, new FileOutputStream(new File(this.orgPath, id + ".jpg")), false, true);
} finally {
in.close();
}
}
public void saveDefaultOrgLogo(String id) throws IOException {
this.initDefaultImage();
IoUtil.saveStream(new FileOutputStream(new File(this.orgPath, id + ".jpg")), this.dol, true);
}
private byte[] zoomImage(byte[] src, int w, int h) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(src);
ByteArrayOutputStream out = new ByteArrayOutputStream();
JpgUtil.zoom(in, out, w, h);
out.flush();
return out.toByteArray();
}
private void initDefaultImage() {
if (this.dli != null)
return;
synchronized (this) {
if (this.dli != null)
return;
try {
byte[] dd = IoUtil.readStream(new FileInputStream(this.defaultHeadPhoto), true);
this.dli = this.zoomImage(dd, this.largeHeadPhotoWidth, this.largeHeadPhotoHeight);
this.dmi = this.zoomImage(dd, this.middleHeadPhotoWidth, this.middleHeadPhotoHeight);
this.dsi = this.zoomImage(dd, this.smallHeadPhotoWidth, this.smallHeadPhotoHeight);
this.dol = IoUtil.readStream(new FileInputStream(this.defaultOrgLogo), true);
this.dsl = IoUtil.readStream(new FileInputStream(this.defaultResourcePhoto), true);
} catch (IOException e) {
this.dli = null;
this.dmi = null;
this.dsi = null;
this.dol = null;
this.dsl = null;
throw new RuntimeException("init image error", e);
}
}
}
public boolean hasOrgLogo(String id) {
String orgPath = this.orgPath+"/"+id+".jpg";
File file = new File(orgPath);
if(file.exists()){
return true;
}else{
return false;
}
}
@Post
@Path("/create")
public String insertOrg(@JdbcConn(true) Connection con, OrganizationInfo info,@Nullable String fn)
throws SQLException,IOException {
String id = StringUtil.buildUUID();
OrgUser orgUser = new OrgUser();
info.setId(id);
orgUser.setId(id);
orgUser.setEmail(info.getEmail());
orgUser.setPasswd(DEFAULT_PW_STR);
orgUser.setInviterId(info.getInviterId());
orgUser.setUserType("1");
if(fn != null){
this.saveOrgLogo(orgUser.getId(), fn);
}
this.orgUserDao.insert(con, orgUser);
this.orgDao.insert(con, info);
return orgUser.getId();
}
@Path("/entryCheck")
@Get
public int entryCheck(@JdbcConn Connection con, String name, String email) throws SQLException, JfwBaseException {
OrgUser orgUser = this.orgUserDao.entryCheck(con, email);
if (orgUser != null) {
return 1;//该邮箱已被注册
}
String orgId = this.orgDao.queryByName(con, name);
if ((orgId != null) && (this.orgUserDao.query(con, orgId) != null)) {
return 2;//该企业已注册科袖账号
}
return 3;//可以创建
}
@Path("/editCheck")
@Get
public int editCheck(@JdbcConn Connection con, String name, String email,String id) throws SQLException, JfwBaseException {
String orgUserId = this.orgUserDao.queryByEmail(con, email);
if ((orgUserId != null)&&(!Objects.equals(orgUserId, id))) {
return 2;//该邮箱已被注册
}
String orgId = this.orgDao.queryByName(con, name);
if ((orgId != null) && (!Objects.equals(orgId, id))) {
return 3;//该企业名称已经注册
}
return 1;
}
@Post
@Path("/createAccount")
public void createOrg(@JdbcConn(true) Connection con, String email,String name,@LoginUser ConsoleAuthUser user) throws SQLException, IOException, JfwBaseException {
String orgId = this.orgDao.queryByName(con, name);
if ((orgId != null) && (this.orgUserDao.query(con, orgId) != null)) {
throw new JfwBaseException(3002, "该企业已注册科袖账号");
}
String orgUserId = this.orgUserDao.queryByEmail(con,email);
if (orgUserId!=null){
throw new JfwBaseException(3003, "该邮箱已被注册");
}
if ((orgId !=null)&&(this.orgUserDao.query(con,orgId)==null)){
this.orgDao.updateIsJoin(con, orgId, "1");
OrgUser orgUser = new OrgUser();
orgUser.setId(orgId);
orgUser.setEmail(email);
orgUser.setInviterId(user.getId());
orgUser.setPasswd(DEFAULT_PW_STR);
orgUser.setUserType("1");
this.orgUserDao.insert(con, orgUser);
OrgRecord orgRecord = new OrgRecord();
orgRecord.setId(orgId);
orgRecord.setCreator(user.getId());
orgRecord.setCreateTime(DateUtil.formatDateTime(System.currentTimeMillis()));
orgRecord.setModifyTime(DateUtil.formatDateTime(System.currentTimeMillis()));
this.orgRecordDao.insert(con, orgRecord);
}
if ((orgId == null)&&(this.orgUserDao.queryByEmail(con,email)==null)) {
String id = this.createOrganization(con, name);
this.orgDao.updateIsJoin(con, orgId, "1");
OrgUser orgUser = new OrgUser();
orgUser.setId(id);
orgUser.setEmail(email);
orgUser.setInviterId(user.getId());
orgUser.setPasswd(DEFAULT_PW_STR);
orgUser.setUserType("1");
this.orgUserDao.insert(con, orgUser);
OrgRecord orgRecord = new OrgRecord();
orgRecord.setId(id);
orgRecord.setCreator(user.getId());
orgRecord.setCreateTime(DateUtil.formatDateTime(System.currentTimeMillis()));
orgRecord.setModifyTime(DateUtil.formatDateTime(System.currentTimeMillis()));
this.orgRecordDao.insert(con, orgRecord);
}
}
@Get
@Path("/queryAccount/{id}")
public Account queryAccount(@JdbcConn Connection con,@PathVar String id)throws SQLException {
Organization organization = this.orgDao.query(con, id);
OrgUser orgUser = this.orgUserDao.query(con, id);
Account account = new Account();
if (orgUser!=null) {
account.setEmail(orgUser.getEmail());
}
account.setName(organization.getName());
account.setId(id);
return account;
}
@Post
@Path("/updateAccount")
public void updateAccount(@JdbcConn(true) Connection con, String email, String name, String id,@LoginUser ConsoleAuthUser user) throws SQLException, JfwBaseException {
String orgUserId = this.orgUserDao.queryByEmail(con, email);
if ((orgUserId != null)&&(!Objects.equals(orgUserId, id))) {
throw new JfwBaseException(2001, "该邮箱已被注册");
}
String orgId = this.orgDao.queryByName(con, name);
if ((orgId != null) && (!Objects.equals(orgId, id))) {
throw new JfwBaseException(2002, "该企业已注册科袖账号");
}
this.orgUserDao.updateEmail(con, id, email);
this.orgDao.updateOrgName(con, id, name);
OrgRecord orgRecord = this.orgRecordDao.queryById(con, id);
if (orgRecord == null){
OrgRecord orgRecord1 = new OrgRecord();
orgRecord1.setId(id);
orgRecord1.setModifier(user.getId());
orgRecord1.setModifyTime(DateUtil.formatDateTime(System.currentTimeMillis()));
this.orgRecordDao.insert(con, orgRecord1);
}else {
this.orgRecordDao.updateModifier(con, id, user.getId(), DateUtil.formatDateTime(System.currentTimeMillis()));
}
}
@Post
@Path("/update")
public void update(@JdbcConn(true) Connection con, Organization orgn, @Nullable String fn,@LoginUser ConsoleAuthUser user) throws SQLException, IOException {
if (fn != null) {
this.saveOrgLogo(orgn.getId(), fn);
}
this.orgDao.update(con, orgn);
OrgRecord orgRecord = this.orgRecordDao.queryById(con, orgn.getId());
if (orgRecord == null){
OrgRecord orgRecord1 = new OrgRecord();
orgRecord1.setId(orgn.getId());
orgRecord1.setModifier(user.getId());
orgRecord1.setModifyTime(DateUtil.formatDateTime(System.currentTimeMillis()));
this.orgRecordDao.insert(con, orgRecord1);
}else {
this.orgRecordDao.updateModifier(con, orgn.getId(), user.getId(),DateUtil.formatDateTime(System.currentTimeMillis()));
}
}
@Post
@Path("/updateBusinessData")
public void updateBusinessData(@JdbcConn(true) Connection con,String id,@Nullable String authStatus,@Nullable Long sortNum)throws SQLException{
this.orgDao.updateBusinessData(con, id, authStatus, sortNum);
}
@Get
@Path("/pq")
public PageQueryResult<OrganizationInfo> pageQuery(@JdbcConn Connection con, @Nullable String name, @Nullable String industry, @Nullable String subject, @Nullable String city,@Nullable String authStatus,int orderBy, @Nullable String bt, @Nullable String et, @DefaultValue("1") int pageNo, @DefaultValue("10") int pageSize) throws SQLException {
if (orderBy == 1) {
return this.orgDao.queryTime(con, name == null ? null : "%" + name + "%", industry == null ? null : "%" + industry + "%", subject == null ? null : "%" + subject + "%", city == null ? null : "%" + city + "%", authStatus, bt == null ? null : bt + "000000", et == null ? null : et + "235959", pageSize, pageNo);
}else{
return this.orgDao.queryViews(con, name == null ? null : "%" + name + "%", industry == null ? null : "%" + industry + "%", subject == null ? null : "%" + subject + "%", city == null ? null : "%" + city + "%", authStatus, bt == null ? null : bt + "000000", et == null ? null : et + "235959", pageSize, pageNo);
}
}
@Get
@Path("/id/{id}")
public OrganizationInfo query(@JdbcConn Connection con, @PathVar String id) throws SQLException {
OrganizationInfo organizationInfo = this.orgDao.query(con, id);
if (organizationInfo != null) {
organizationInfo.setHasOrgLogo(this.hasOrgLogo(organizationInfo.getId()));
}
return organizationInfo;
}
@Delete
@Path("/id/{id}")
public void deleteOne(@JdbcConn(true) Connection con, @PathVar String id) throws SQLException {
this.orgDao.delete(con, id);
}
@Post
@Path("/del")
public void delete(@JdbcConn(true) Connection con, String[] ids) throws SQLException {
this.orgDao.delete(con, ids);
}
public String createOrganization(Connection con, String orgName) throws SQLException, IOException {
String id = StringUtil.buildUUID();
Organization org = new Organization();
org.setId(id);
org.setName(orgName);
org.setOrgType(this.defaultOrgType);
org.setAuthStatus(this.defaultAuthStatus);
try {
orgDao.insert(con, org);
this.saveDefaultOrgLogo(id);
} catch (SQLException e) {
if ("23505".equals(e.getSQLState())) {
con.rollback();
id = orgDao.queryByName(con, orgName);
if (id == null)
throw new RuntimeException("not fonud with query organization by name");
return id;
}
throw e;
}
return id;
}
public static class Account{
private String id;
private String email;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
|