| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- package space.apps.moudules.personnel.services;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import space.apps.moudules.website.template.util.WebsiteGuestAuthService;
- import space.apps.moudules.website.util.AppWebsiteManageUtil;
- import space.apps.moudules.website.util.WebsiteGuestAccessUtil;
- import space.core.apps.common.bean.AppUser;
- import space.core.apps.common.BaseTransaction;
- import space.core.apps.common.bean.ResponsePackage;
- import space.core.common.utils.StringUtil;
- import space.core.db.bean.base.PageBean;
- import space.core.db.jdbc.GeneralDBAccess;
- import space.core.db.jdbc.SqlCommandImpl;
- /**
- * 人事-游客账号管理。
- */
- public class GuestAccountService extends BaseTransaction {
- private static final long serialVersionUID = 1L;
- public String list() throws Exception {
- GeneralDBAccess gdba = getGdbaInstance();
- Long websiteId = getParameterToLong("websiteid");
- String loginName = getParameter("loginName");
- String dcode = getParameter("dcode");
- StringBuilder sql = new StringBuilder();
- sql.append(" select g.guest_account_id, g.websiteid, w.name website_name, g.login_name, g.password, ");
- sql.append(" g.dcode, g.remark, g.create_time, g.update_time ");
- sql.append(" from website_guest_account g ");
- sql.append(" left join website w on w.websiteid = g.websiteid ");
- sql.append(" where g.dcode <> 72 ");
- if (websiteId != null) {
- sql.append(" and g.websiteid = :websiteid ");
- }
- if (StringUtil.notNull(loginName)) {
- sql.append(" and g.login_name like :loginName ");
- }
- if (StringUtil.notNull(dcode)) {
- sql.append(" and g.dcode = :dcode ");
- }
- sql.append(" order by g.websiteid, g.guest_account_id desc ");
- SqlCommandImpl sqlCmd = new SqlCommandImpl(sql.toString());
- if (websiteId != null) {
- sqlCmd.setString("websiteid", websiteId);
- }
- if (StringUtil.notNull(loginName)) {
- sqlCmd.setString("loginName", "%" + loginName.trim() + "%");
- }
- if (StringUtil.notNull(dcode)) {
- sqlCmd.setString("dcode", dcode);
- }
- PageBean pageBean = getAppsPageBean();
- List<Map<String, Object>> rows = gdba.selectAsMapList(sqlCmd, pageBean);
- ResponsePackage rp = getResponseSuccessPackage();
- rp.setRequestAttribute("guestAccountList", rows);
- setResponsePackage(rp);
- setRequestAttribute("websiteid", websiteId == null ? "" : String.valueOf(websiteId));
- setRequestAttribute("loginName", StringUtil.notNull(loginName) ? loginName : "");
- setRequestAttribute("dcode", StringUtil.notNull(dcode) ? dcode : "");
- loadManageWebsiteOptions(gdba);
- return DEFAULT_FORWARD;
- }
- public String add() throws Exception {
- List<Map<String, Object>> options = AppWebsiteManageUtil.getManageWebsiteListByAppUser(getGdbaInstance(),
- getAppUser());
- setRequestAttribute("manageWebsiteOptions", options);
- if (options != null && options.size() == 1) {
- setRequestAttribute("websiteid", String.valueOf(options.get(0).get("websiteid")));
- } else {
- setRequestAttribute("websiteid", "");
- }
- return DEFAULT_FORWARD;
- }
- public String info() throws Exception {
- GeneralDBAccess gdba = getGdbaInstance();
- Long guestAccountId = getParameterToLong("guest_account_id");
- Map<String, Object> row = selectGuestAccountById(gdba, guestAccountId);
- setRequestAttribute("guestAccount", row);
- loadManageWebsiteOptions(gdba);
- return DEFAULT_FORWARD;
- }
- public String change() throws Exception {
- return info();
- }
- public String addSubmit() throws Exception {
- GeneralDBAccess gdba = getGdbaInstance();
- Long websiteId = getParameterToLong("websiteid");
- String loginName = getParameter("login_name");
- String password = getParameter("password");
- String remark = getParameter("remark");
- if (websiteId == null || StringUtil.isNull(loginName) || StringUtil.isNull(password)) {
- return toTipsError("网站、账号、密码不能为空");
- }
- if (existsLoginName(gdba, websiteId, loginName, null)) {
- return toTipsError("该网站下账号已存在");
- }
- String sql = " insert into website_guest_account (unitid, websiteid, login_name, password, auth_token, dcode, remark, create_time, create_pid, update_time, update_pid) "
- + " values (:unitid, :websiteid, :loginName, :password, :authToken, :dcode, :remark, now(), :createPid, now(), :updatePid) ";
- SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
- Object userId = getAppUser().getUserId();
- sqlCmd.setString("unitid", getAppUser().getUnitId());
- sqlCmd.setString("websiteid", websiteId);
- sqlCmd.setString("loginName", loginName.trim());
- sqlCmd.setString("password", password);
- sqlCmd.setString("authToken", WebsiteGuestAuthService.newAuthToken());
- sqlCmd.setString("dcode", WebsiteGuestAccessUtil.GUEST_ACCOUNT_ENABLE);
- sqlCmd.setString("remark", remark);
- sqlCmd.setString("createPid", userId);
- sqlCmd.setString("updatePid", userId);
- gdba.modify(sqlCmd);
- gdba.commit();
- setResponsePackage(getResponseSuccessMsgPackage("保存成功"));
- return DEFAULT_FORWARD;
- }
- public String changeSubmit() throws Exception {
- GeneralDBAccess gdba = getGdbaInstance();
- Long guestAccountId = getParameterToLong("guest_account_id");
- Long websiteId = getParameterToLong("websiteid");
- String loginName = getParameter("login_name");
- String password = getParameter("password");
- String remark = getParameter("remark");
- if (guestAccountId == null || websiteId == null || StringUtil.isNull(loginName)) {
- return toTipsError("参数不完整");
- }
- if (existsLoginName(gdba, websiteId, loginName, guestAccountId)) {
- return toTipsError("该网站下账号已存在");
- }
- Map<String, Object> oldRow = selectGuestAccountById(gdba, guestAccountId);
- if (oldRow == null || oldRow.isEmpty()) {
- return toTipsError("游客账号不存在");
- }
- boolean passwordChanged = StringUtil.notNull(password)
- && !password.equals(String.valueOf(oldRow.get("password")));
- String authToken = passwordChanged ? WebsiteGuestAuthService.newAuthToken()
- : String.valueOf(oldRow.get("auth_token"));
- StringBuilder sql = new StringBuilder();
- sql.append(" update website_guest_account set websiteid = :websiteid, login_name = :loginName, remark = :remark, ");
- sql.append(" update_time = now(), update_pid = :updatePid ");
- if (passwordChanged) {
- sql.append(" , password = :password, auth_token = :authToken ");
- }
- sql.append(" where guest_account_id = :guestAccountId ");
- SqlCommandImpl sqlCmd = new SqlCommandImpl(sql.toString());
- sqlCmd.setString("websiteid", websiteId).setString("loginName", loginName.trim()).setString("remark", remark)
- .setString("updatePid", getAppUser().getUserId()).setString("guestAccountId", guestAccountId);
- if (passwordChanged) {
- sqlCmd.setString("password", password).setString("authToken", authToken);
- }
- gdba.modify(sqlCmd);
- gdba.commit();
- setResponsePackage(getResponseSuccessMsgPackage("保存成功"));
- return DEFAULT_FORWARD;
- }
- public String statusSubmit() throws Exception {
- GeneralDBAccess gdba = getGdbaInstance();
- Long guestAccountId = getParameterToLong("guest_account_id");
- Long dcode = getParameterToLong("dcode");
- if (guestAccountId == null || dcode == null) {
- return toTipsError("参数不完整");
- }
- String sql = " update website_guest_account set dcode = :dcode, auth_token = :authToken, update_time = now(), update_pid = :updatePid "
- + " where guest_account_id = :guestAccountId ";
- SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
- sqlCmd.setString("dcode", dcode).setString("authToken", WebsiteGuestAuthService.newAuthToken())
- .setString("updatePid", getAppUser().getUserId()).setString("guestAccountId", guestAccountId);
- gdba.modify(sqlCmd);
- gdba.commit();
- setResponsePackage(getResponseSuccessMsgPackage("状态已更新"));
- return DEFAULT_FORWARD;
- }
- public void manageWebsiteListApi() throws Exception {
- AppUser appUser = getAppUser();
- write(getResponseSuccessDataPackage(
- AppWebsiteManageUtil.getManageWebsiteListByAppUser(getGdbaInstance(), appUser)).toJson());
- }
- public void listByWebsiteApi() throws Exception {
- GeneralDBAccess gdba = getGdbaInstance();
- Long websiteId = getParameterToLong("websiteid");
- String sql = " select guest_account_id, login_name, remark from website_guest_account "
- + " where websiteid = :websiteid and dcode = :dcode order by login_name ";
- SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
- sqlCmd.setString("websiteid", websiteId).setString("dcode", WebsiteGuestAccessUtil.GUEST_ACCOUNT_ENABLE);
- List<Map<String, Object>> rows = gdba.selectAsMapList(sqlCmd);
- Map<String, Object> data = new HashMap<String, Object>();
- data.put("list", rows);
- write(getResponseSuccessDataPackage(data).toJson());
- }
- private void loadManageWebsiteOptions(GeneralDBAccess gdba) throws Exception {
- setRequestAttribute("manageWebsiteOptions",
- AppWebsiteManageUtil.getManageWebsiteListByAppUser(gdba, getAppUser()));
- }
- private Map<String, Object> selectGuestAccountById(GeneralDBAccess gdba, Long guestAccountId) throws Exception {
- String sql = " select guest_account_id, websiteid, login_name, password, auth_token, dcode, remark "
- + " from website_guest_account where guest_account_id = :guestAccountId ";
- SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
- sqlCmd.setString("guestAccountId", guestAccountId);
- return gdba.selectAsMap(sqlCmd);
- }
- private boolean existsLoginName(GeneralDBAccess gdba, Long websiteId, String loginName, Long excludeId)
- throws Exception {
- StringBuilder sql = new StringBuilder();
- sql.append(" select count(1) cnt from website_guest_account where websiteid = :websiteid and login_name = :loginName and dcode <> 72 ");
- if (excludeId != null) {
- sql.append(" and guest_account_id <> :excludeId ");
- }
- SqlCommandImpl sqlCmd = new SqlCommandImpl(sql.toString());
- sqlCmd.setString("websiteid", websiteId).setString("loginName", loginName.trim());
- if (excludeId != null) {
- sqlCmd.setString("excludeId", excludeId);
- }
- Map<String, Object> row = gdba.selectAsMap(sqlCmd);
- return row != null && Integer.parseInt(String.valueOf(row.get("cnt"))) > 0;
- }
- private String toTipsError(String message) throws Exception {
- setResponsePackage(getResponseErrorMsgPackage(message));
- return DEFAULT_FORWARD;
- }
- }
|