GuestAccountService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package space.apps.moudules.personnel.services;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import space.apps.moudules.website.template.util.WebsiteGuestAuthService;
  6. import space.apps.moudules.website.util.AppWebsiteManageUtil;
  7. import space.apps.moudules.website.util.WebsiteGuestAccessUtil;
  8. import space.core.apps.common.bean.AppUser;
  9. import space.core.apps.common.BaseTransaction;
  10. import space.core.apps.common.bean.ResponsePackage;
  11. import space.core.common.utils.StringUtil;
  12. import space.core.db.bean.base.PageBean;
  13. import space.core.db.jdbc.GeneralDBAccess;
  14. import space.core.db.jdbc.SqlCommandImpl;
  15. /**
  16. * 人事-游客账号管理。
  17. */
  18. public class GuestAccountService extends BaseTransaction {
  19. private static final long serialVersionUID = 1L;
  20. public String list() throws Exception {
  21. GeneralDBAccess gdba = getGdbaInstance();
  22. Long websiteId = getParameterToLong("websiteid");
  23. String loginName = getParameter("loginName");
  24. String dcode = getParameter("dcode");
  25. StringBuilder sql = new StringBuilder();
  26. sql.append(" select g.guest_account_id, g.websiteid, w.name website_name, g.login_name, g.password, ");
  27. sql.append(" g.dcode, g.remark, g.create_time, g.update_time ");
  28. sql.append(" from website_guest_account g ");
  29. sql.append(" left join website w on w.websiteid = g.websiteid ");
  30. sql.append(" where g.dcode <> 72 ");
  31. if (websiteId != null) {
  32. sql.append(" and g.websiteid = :websiteid ");
  33. }
  34. if (StringUtil.notNull(loginName)) {
  35. sql.append(" and g.login_name like :loginName ");
  36. }
  37. if (StringUtil.notNull(dcode)) {
  38. sql.append(" and g.dcode = :dcode ");
  39. }
  40. sql.append(" order by g.websiteid, g.guest_account_id desc ");
  41. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql.toString());
  42. if (websiteId != null) {
  43. sqlCmd.setString("websiteid", websiteId);
  44. }
  45. if (StringUtil.notNull(loginName)) {
  46. sqlCmd.setString("loginName", "%" + loginName.trim() + "%");
  47. }
  48. if (StringUtil.notNull(dcode)) {
  49. sqlCmd.setString("dcode", dcode);
  50. }
  51. PageBean pageBean = getAppsPageBean();
  52. List<Map<String, Object>> rows = gdba.selectAsMapList(sqlCmd, pageBean);
  53. ResponsePackage rp = getResponseSuccessPackage();
  54. rp.setRequestAttribute("guestAccountList", rows);
  55. setResponsePackage(rp);
  56. setRequestAttribute("websiteid", websiteId == null ? "" : String.valueOf(websiteId));
  57. setRequestAttribute("loginName", StringUtil.notNull(loginName) ? loginName : "");
  58. setRequestAttribute("dcode", StringUtil.notNull(dcode) ? dcode : "");
  59. loadManageWebsiteOptions(gdba);
  60. return DEFAULT_FORWARD;
  61. }
  62. public String add() throws Exception {
  63. List<Map<String, Object>> options = AppWebsiteManageUtil.getManageWebsiteListByAppUser(getGdbaInstance(),
  64. getAppUser());
  65. setRequestAttribute("manageWebsiteOptions", options);
  66. if (options != null && options.size() == 1) {
  67. setRequestAttribute("websiteid", String.valueOf(options.get(0).get("websiteid")));
  68. } else {
  69. setRequestAttribute("websiteid", "");
  70. }
  71. return DEFAULT_FORWARD;
  72. }
  73. public String info() throws Exception {
  74. GeneralDBAccess gdba = getGdbaInstance();
  75. Long guestAccountId = getParameterToLong("guest_account_id");
  76. Map<String, Object> row = selectGuestAccountById(gdba, guestAccountId);
  77. setRequestAttribute("guestAccount", row);
  78. loadManageWebsiteOptions(gdba);
  79. return DEFAULT_FORWARD;
  80. }
  81. public String change() throws Exception {
  82. return info();
  83. }
  84. public String addSubmit() throws Exception {
  85. GeneralDBAccess gdba = getGdbaInstance();
  86. Long websiteId = getParameterToLong("websiteid");
  87. String loginName = getParameter("login_name");
  88. String password = getParameter("password");
  89. String remark = getParameter("remark");
  90. if (websiteId == null || StringUtil.isNull(loginName) || StringUtil.isNull(password)) {
  91. return toTipsError("网站、账号、密码不能为空");
  92. }
  93. if (existsLoginName(gdba, websiteId, loginName, null)) {
  94. return toTipsError("该网站下账号已存在");
  95. }
  96. String sql = " insert into website_guest_account (unitid, websiteid, login_name, password, auth_token, dcode, remark, create_time, create_pid, update_time, update_pid) "
  97. + " values (:unitid, :websiteid, :loginName, :password, :authToken, :dcode, :remark, now(), :createPid, now(), :updatePid) ";
  98. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  99. Object userId = getAppUser().getUserId();
  100. sqlCmd.setString("unitid", getAppUser().getUnitId());
  101. sqlCmd.setString("websiteid", websiteId);
  102. sqlCmd.setString("loginName", loginName.trim());
  103. sqlCmd.setString("password", password);
  104. sqlCmd.setString("authToken", WebsiteGuestAuthService.newAuthToken());
  105. sqlCmd.setString("dcode", WebsiteGuestAccessUtil.GUEST_ACCOUNT_ENABLE);
  106. sqlCmd.setString("remark", remark);
  107. sqlCmd.setString("createPid", userId);
  108. sqlCmd.setString("updatePid", userId);
  109. gdba.modify(sqlCmd);
  110. gdba.commit();
  111. setResponsePackage(getResponseSuccessMsgPackage("保存成功"));
  112. return DEFAULT_FORWARD;
  113. }
  114. public String changeSubmit() throws Exception {
  115. GeneralDBAccess gdba = getGdbaInstance();
  116. Long guestAccountId = getParameterToLong("guest_account_id");
  117. Long websiteId = getParameterToLong("websiteid");
  118. String loginName = getParameter("login_name");
  119. String password = getParameter("password");
  120. String remark = getParameter("remark");
  121. if (guestAccountId == null || websiteId == null || StringUtil.isNull(loginName)) {
  122. return toTipsError("参数不完整");
  123. }
  124. if (existsLoginName(gdba, websiteId, loginName, guestAccountId)) {
  125. return toTipsError("该网站下账号已存在");
  126. }
  127. Map<String, Object> oldRow = selectGuestAccountById(gdba, guestAccountId);
  128. if (oldRow == null || oldRow.isEmpty()) {
  129. return toTipsError("游客账号不存在");
  130. }
  131. boolean passwordChanged = StringUtil.notNull(password)
  132. && !password.equals(String.valueOf(oldRow.get("password")));
  133. String authToken = passwordChanged ? WebsiteGuestAuthService.newAuthToken()
  134. : String.valueOf(oldRow.get("auth_token"));
  135. StringBuilder sql = new StringBuilder();
  136. sql.append(" update website_guest_account set websiteid = :websiteid, login_name = :loginName, remark = :remark, ");
  137. sql.append(" update_time = now(), update_pid = :updatePid ");
  138. if (passwordChanged) {
  139. sql.append(" , password = :password, auth_token = :authToken ");
  140. }
  141. sql.append(" where guest_account_id = :guestAccountId ");
  142. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql.toString());
  143. sqlCmd.setString("websiteid", websiteId).setString("loginName", loginName.trim()).setString("remark", remark)
  144. .setString("updatePid", getAppUser().getUserId()).setString("guestAccountId", guestAccountId);
  145. if (passwordChanged) {
  146. sqlCmd.setString("password", password).setString("authToken", authToken);
  147. }
  148. gdba.modify(sqlCmd);
  149. gdba.commit();
  150. setResponsePackage(getResponseSuccessMsgPackage("保存成功"));
  151. return DEFAULT_FORWARD;
  152. }
  153. public String statusSubmit() throws Exception {
  154. GeneralDBAccess gdba = getGdbaInstance();
  155. Long guestAccountId = getParameterToLong("guest_account_id");
  156. Long dcode = getParameterToLong("dcode");
  157. if (guestAccountId == null || dcode == null) {
  158. return toTipsError("参数不完整");
  159. }
  160. String sql = " update website_guest_account set dcode = :dcode, auth_token = :authToken, update_time = now(), update_pid = :updatePid "
  161. + " where guest_account_id = :guestAccountId ";
  162. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  163. sqlCmd.setString("dcode", dcode).setString("authToken", WebsiteGuestAuthService.newAuthToken())
  164. .setString("updatePid", getAppUser().getUserId()).setString("guestAccountId", guestAccountId);
  165. gdba.modify(sqlCmd);
  166. gdba.commit();
  167. setResponsePackage(getResponseSuccessMsgPackage("状态已更新"));
  168. return DEFAULT_FORWARD;
  169. }
  170. public void manageWebsiteListApi() throws Exception {
  171. AppUser appUser = getAppUser();
  172. write(getResponseSuccessDataPackage(
  173. AppWebsiteManageUtil.getManageWebsiteListByAppUser(getGdbaInstance(), appUser)).toJson());
  174. }
  175. public void listByWebsiteApi() throws Exception {
  176. GeneralDBAccess gdba = getGdbaInstance();
  177. Long websiteId = getParameterToLong("websiteid");
  178. String sql = " select guest_account_id, login_name, remark from website_guest_account "
  179. + " where websiteid = :websiteid and dcode = :dcode order by login_name ";
  180. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  181. sqlCmd.setString("websiteid", websiteId).setString("dcode", WebsiteGuestAccessUtil.GUEST_ACCOUNT_ENABLE);
  182. List<Map<String, Object>> rows = gdba.selectAsMapList(sqlCmd);
  183. Map<String, Object> data = new HashMap<String, Object>();
  184. data.put("list", rows);
  185. write(getResponseSuccessDataPackage(data).toJson());
  186. }
  187. private void loadManageWebsiteOptions(GeneralDBAccess gdba) throws Exception {
  188. setRequestAttribute("manageWebsiteOptions",
  189. AppWebsiteManageUtil.getManageWebsiteListByAppUser(gdba, getAppUser()));
  190. }
  191. private Map<String, Object> selectGuestAccountById(GeneralDBAccess gdba, Long guestAccountId) throws Exception {
  192. String sql = " select guest_account_id, websiteid, login_name, password, auth_token, dcode, remark "
  193. + " from website_guest_account where guest_account_id = :guestAccountId ";
  194. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  195. sqlCmd.setString("guestAccountId", guestAccountId);
  196. return gdba.selectAsMap(sqlCmd);
  197. }
  198. private boolean existsLoginName(GeneralDBAccess gdba, Long websiteId, String loginName, Long excludeId)
  199. throws Exception {
  200. StringBuilder sql = new StringBuilder();
  201. sql.append(" select count(1) cnt from website_guest_account where websiteid = :websiteid and login_name = :loginName and dcode <> 72 ");
  202. if (excludeId != null) {
  203. sql.append(" and guest_account_id <> :excludeId ");
  204. }
  205. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql.toString());
  206. sqlCmd.setString("websiteid", websiteId).setString("loginName", loginName.trim());
  207. if (excludeId != null) {
  208. sqlCmd.setString("excludeId", excludeId);
  209. }
  210. Map<String, Object> row = gdba.selectAsMap(sqlCmd);
  211. return row != null && Integer.parseInt(String.valueOf(row.get("cnt"))) > 0;
  212. }
  213. private String toTipsError(String message) throws Exception {
  214. setResponsePackage(getResponseErrorMsgPackage(message));
  215. return DEFAULT_FORWARD;
  216. }
  217. }