StoreAgent.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. package restcontroller.webmanage.saletool.store;
  2. import beans.attachment.Attachment;
  3. import beans.parameter.Parameter;
  4. import beans.time.Time;
  5. import com.alibaba.fastjson.JSONArray;
  6. import com.alibaba.fastjson.JSONObject;
  7. import common.Controller;
  8. import common.YosException;
  9. import common.annotation.API;
  10. import common.data.*;
  11. import restcontroller.R;
  12. import java.util.ArrayList;
  13. /**
  14. * 门店小程序(经销商)
  15. */
  16. public class StoreAgent extends Controller {
  17. /**
  18. * 构造函数
  19. *
  20. * @param content
  21. */
  22. public StoreAgent(JSONObject content) throws YosException {
  23. super(content);
  24. }
  25. String sa_store = "sa_store";
  26. Controller controller = this;
  27. @API(title = "门店列表", apiversion = R.ID20240410095602.v1.class)
  28. public String list() throws YosException {
  29. StringBuffer where = new StringBuffer(" 1=1 ");
  30. if (content.containsKey("where")) {
  31. JSONObject whereObject = content.getJSONObject("where");
  32. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  33. where.append(" and (");
  34. where.append("t1.storename like'%").append(whereObject.getString("condition")).append("%' ");
  35. where.append("or t2.name like'%").append(whereObject.getString("condition")).append("%' ");
  36. where.append("or t2.phonenumber like'%").append(whereObject.getString("condition")).append("%' ");
  37. where.append(")");
  38. }
  39. if (whereObject.containsKey("storetype") && !"".equals(whereObject.getString("storetype"))) {
  40. where.append(" and (");
  41. where.append("t1.storetype='").append(whereObject.getString("storetype")).append("' ");
  42. where.append(")");
  43. }
  44. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  45. where.append(" and (");
  46. where.append("t1.status='").append(whereObject.getString("status")).append("' ");
  47. where.append(")");
  48. }
  49. if (whereObject.containsKey("markettype") && !"".equals(whereObject.getString("markettype"))) {
  50. where.append(" and (");
  51. where.append("t1.markettype='").append(whereObject.getString("markettype")).append("' ");
  52. where.append(")");
  53. }
  54. }
  55. if (content.containsKey("sys_enterpriseid") && content.getLongValue("sys_enterpriseid") != 0) {
  56. sys_enterpriseid = content.getLongValue("sys_enterpriseid");
  57. }
  58. QuerySQL listQuery = SQLFactory.createQuerySQL(controller, sa_store)
  59. .setTableAlias("t1");
  60. listQuery.addJoinTable(JOINTYPE.left, "sys_enterprise_hr", "t2", "t2.sys_enterprise_hrid=t1.leader_hrid and t2.siteid=t1.siteid"
  61. , "name", "phonenumber");
  62. listQuery.addJoinTable(JOINTYPE.left, "sys_users", "t3", "t3.userid=t2.userid "
  63. , "accountno");
  64. listQuery.addJoinTable(JOINTYPE.left, "sys_enterprise", "t4", "t4.sys_enterpriseid=t1.sys_enterpriseid and t4.siteid=t1.siteid"
  65. , "enterprisename");
  66. listQuery.setWhere("sys_enterpriseid", sys_enterpriseid);
  67. listQuery.setWhere(where.toString());
  68. listQuery.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  69. Rows rows = listQuery.query();
  70. RowsMap attinfoRows = Attachment.get(controller, sa_store, rows.toArrayList("sa_storeid", new ArrayList<>()));
  71. for (Row row : rows) {
  72. row.put("attinfos", attinfoRows.getOrDefault(row.getString("sa_storeid"), new Rows()));
  73. row.put("detailaddress", row.getString("province") + row.getString("city") + row.getString("county") + row.getString("address"));
  74. }
  75. return getSucReturnObject().setData(rows).toString();
  76. }
  77. @API(title = "门店新增或编辑", apiversion = R.ID20240410095302.v1.class)
  78. public String insertOrUpdate() throws YosException {
  79. Long sa_storeid = content.getLongValue("sa_storeid");
  80. if (sa_storeid <= 0) {
  81. sa_storeid = createTableID(sa_store);
  82. InsertSQL sqlFactory = SQLFactory.createInsertSQL(controller, sa_store);
  83. sqlFactory.setSiteid(siteid);
  84. sqlFactory.setUniqueid(sa_storeid);
  85. sqlFactory.setValue("sys_enterpriseid", sys_enterpriseid);
  86. sqlFactory.setValue("storename", content.getStringValue("storename"));
  87. sqlFactory.setValue("sale_phonenmber", content.getStringValue("sale_phonenmber"));
  88. sqlFactory.setValue("area", content.getStringValue("area"));
  89. sqlFactory.setValue("leader_hrid", content.getLongValue("leader_hrid"));
  90. sqlFactory.setValue("storetype", content.getStringValue("storetype"));
  91. sqlFactory.setValue("markettype", content.getStringValue("markettype"));
  92. sqlFactory.setValue("service_phonenmber", content.getStringValue("service_phonenmber"));
  93. sqlFactory.setValue("latitude", content.getStringValue("latitude"));
  94. sqlFactory.setValue("longitude", content.getStringValue("longitude"));
  95. sqlFactory.setValue("province", content.getStringValue("province"));
  96. sqlFactory.setValue("city", content.getStringValue("city"));
  97. sqlFactory.setValue("county", content.getStringValue("county"));
  98. sqlFactory.setValue("address", content.getStringValue("address"));
  99. sqlFactory.setValue("storeno", content.getStringValue("storeno"));
  100. sqlFactory.setValue("status", "新建");
  101. sqlFactory.insert();
  102. content.put("sa_storeid", sa_storeid);
  103. } else {
  104. UpdateSQL sqlFactory = SQLFactory.createUpdateSQL(controller, sa_store);
  105. sqlFactory.setSiteid(siteid);
  106. sqlFactory.setUniqueid(sa_storeid);
  107. sqlFactory.setValue("sys_enterpriseid", sys_enterpriseid);
  108. sqlFactory.setValue("storename", content.getStringValue("storename"));
  109. sqlFactory.setValue("sale_phonenmber", content.getStringValue("sale_phonenmber"));
  110. sqlFactory.setValue("area", content.getStringValue("area"));
  111. sqlFactory.setValue("leader_hrid", content.getLongValue("leader_hrid"));
  112. sqlFactory.setValue("storetype", content.getStringValue("storetype"));
  113. sqlFactory.setValue("markettype", content.getStringValue("markettype"));
  114. sqlFactory.setValue("service_phonenmber", content.getStringValue("service_phonenmber"));
  115. sqlFactory.setValue("latitude", content.getStringValue("latitude"));
  116. sqlFactory.setValue("longitude", content.getStringValue("longitude"));
  117. sqlFactory.setValue("province", content.getStringValue("province"));
  118. sqlFactory.setValue("city", content.getStringValue("city"));
  119. sqlFactory.setValue("county", content.getStringValue("county"));
  120. sqlFactory.setValue("address", content.getStringValue("address"));
  121. sqlFactory.setValue("storeno", content.getStringValue("storeno"));
  122. sqlFactory.update();
  123. }
  124. InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, "sa_store_hr");
  125. insertSQL.setSiteid(siteid);
  126. insertSQL.setUniqueid(createTableID("sa_store_hr"));
  127. insertSQL.setValue("sys_enterpriseid", sys_enterpriseid);
  128. insertSQL.setValue("sa_storeid", sa_storeid);
  129. insertSQL.setValue("sys_enterprise_hrid", content.getLongValue("leader_hrid"));
  130. insertSQL.setWhere("not exists(select 1 from sa_store_hr where sys_enterprise_hrid=" + content.getLongValue("leader_hrid") + " and sa_storeid=" + sa_storeid + " and siteid='" + siteid + "')");
  131. insertSQL.insert();
  132. return detail();
  133. }
  134. @API(title = "门店详情", apiversion = R.ID20240410095402.v1.class)
  135. public String detail() throws YosException {
  136. Long sa_storeid = content.getLongValue("sa_storeid");
  137. QuerySQL detailQuery = SQLFactory.createQuerySQL(controller, sa_store).setTableAlias("t1");
  138. detailQuery.addJoinTable(JOINTYPE.left, "sys_enterprise_hr", "t2", "t2.sys_enterprise_hrid=t1.leader_hrid and t2.siteid=t1.siteid"
  139. , "name");
  140. detailQuery.addJoinTable(JOINTYPE.left, "sys_users", "t3", "t3.userid=t2.userid "
  141. , "accountno");
  142. detailQuery.addJoinTable(JOINTYPE.left, "sys_enterprise", "t4", "t4.sys_enterpriseid=t1.sys_enterpriseid and t4.siteid=t1.siteid"
  143. , "enterprisename");
  144. detailQuery.addQueryFields("phonenumber", "ifnull(t2.phonenumber,t3.phonenumber)");
  145. detailQuery.setUniqueid(sa_storeid);
  146. Rows rows = detailQuery.query();
  147. Row detailRow = rows.isNotEmpty() ? rows.get(0) : new Row();
  148. detailRow.putIfAbsent("checkdate", "");
  149. detailRow.putIfAbsent("submitdate", "");
  150. Rows attinfoRows = Attachment.get(controller, sa_store, sa_storeid);
  151. detailRow.put("attinfos", attinfoRows);
  152. return getSucReturnObject().setData(detailRow).toString();
  153. }
  154. @API(title = "门店删除", apiversion = R.ID20240410095502.v1.class)
  155. public String delete() throws YosException {
  156. JSONArray sa_storeids = content.getJSONArray("sa_storeids");
  157. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, sa_store);
  158. querySQL.setSiteid(siteid);
  159. querySQL.setWhere("sa_storeid", sa_storeids);
  160. querySQL.setWhere("status!='新建'");
  161. Rows rows = querySQL.query();
  162. if (rows.isNotEmpty()) {
  163. return getErrReturnObject().setErrMsg("当前状态不可删除").toString();
  164. }
  165. DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(controller, sa_store);
  166. deleteSQL.setSiteid(siteid);
  167. deleteSQL.setWhere("sa_storeid", sa_storeids);
  168. deleteSQL.delete();
  169. return getSucReturnObject().toString();
  170. }
  171. @API(title = "门店提交", apiversion = R.ID20240410110302.v1.class)
  172. public String submit() throws YosException {
  173. Long sa_storeid = content.getLongValue("sa_storeid");
  174. Rows rows = dbConnect.runSqlQuery("SELECT status from sa_store WHERE sa_storeid=" + sa_storeid + " and siteid='" + siteid + "' ");
  175. if (rows.isEmpty()) {
  176. return getErrReturnObject().setErrMsg("数据不存在").toString();
  177. }
  178. if (!rows.get(0).getString("status").equals("新建")) {
  179. return getErrReturnObject().setErrMsg("当前状态不允许提交").toString();
  180. }
  181. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, sa_store);
  182. updateSQL.setSiteid(siteid);
  183. updateSQL.setUniqueid(sa_storeid);
  184. updateSQL.setValue("status", "提交");
  185. updateSQL.setValue("submitby", username);
  186. updateSQL.setValue("submitdate", Time.getDateTime_Str());
  187. updateSQL.update();
  188. return getSucReturnObject().toString();
  189. }
  190. @API(title = "门店撤回", apiversion = R.ID20240410110802.v1.class)
  191. public String unsubmit() throws YosException {
  192. Long sa_storeid = content.getLongValue("sa_storeid");
  193. Rows rows = dbConnect.runSqlQuery("SELECT status from sa_store WHERE sa_storeid=" + sa_storeid + " and siteid='" + siteid + "' ");
  194. if (rows.isEmpty()) {
  195. return getErrReturnObject().setErrMsg("数据不存在").toString();
  196. }
  197. if (!rows.get(0).getString("status").equals("提交")) {
  198. return getErrReturnObject().setErrMsg("当前状态不允许撤回").toString();
  199. }
  200. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, sa_store);
  201. updateSQL.setSiteid(siteid);
  202. updateSQL.setUniqueid(sa_storeid);
  203. updateSQL.setValue("status", "新建");
  204. updateSQL.setValue("submitby", "null");
  205. updateSQL.setValue("submitdate", "null");
  206. updateSQL.update();
  207. return getSucReturnObject().toString();
  208. }
  209. @API(title = "门店退回", apiversion = R.ID20240410135402.v1.class)
  210. public String returnBack() throws YosException {
  211. Long sa_storeid = content.getLongValue("sa_storeid");
  212. Rows rows = dbConnect.runSqlQuery("SELECT status from sa_store WHERE sa_storeid=" + sa_storeid + " and siteid='" + siteid + "' ");
  213. if (rows.isEmpty()) {
  214. return getErrReturnObject().setErrMsg("数据不存在").toString();
  215. }
  216. if (!rows.get(0).getString("status").equals("提交")) {
  217. return getErrReturnObject().setErrMsg("当前状态不允许退回").toString();
  218. }
  219. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, sa_store);
  220. updateSQL.setSiteid(siteid);
  221. updateSQL.setUniqueid(sa_storeid);
  222. updateSQL.setValue("status", "新建");
  223. updateSQL.setValue("submitby", "null");
  224. updateSQL.setValue("submitdate", "null");
  225. updateSQL.update();
  226. return getSucReturnObject().toString();
  227. }
  228. @API(title = "门店审核", apiversion = R.ID20240410141002.v1.class)
  229. public String check() throws YosException {
  230. Long sa_storeid = content.getLongValue("sa_storeid");
  231. Rows rows = dbConnect.runSqlQuery("SELECT status from sa_store WHERE sa_storeid=" + sa_storeid + " and siteid='" + siteid + "' ");
  232. if (rows.isEmpty()) {
  233. return getErrReturnObject().setErrMsg("数据不存在").toString();
  234. }
  235. if (!rows.get(0).getString("status").equals("提交")) {
  236. return getErrReturnObject().setErrMsg("当前状态不允许审核").toString();
  237. }
  238. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, sa_store);
  239. updateSQL.setSiteid(siteid);
  240. updateSQL.setUniqueid(sa_storeid);
  241. updateSQL.setValue("status", "审核");
  242. updateSQL.setValue("checkby", username);
  243. updateSQL.setValue("checkdate", Time.getDateTime_Str());
  244. updateSQL.update();
  245. return getSucReturnObject().toString();
  246. }
  247. @API(title = "门店反审核", apiversion = R.ID20240410141402.v1.class)
  248. public String uncheck() throws YosException {
  249. Long sa_storeid = content.getLongValue("sa_storeid");
  250. Rows rows = dbConnect.runSqlQuery("SELECT status from sa_store WHERE sa_storeid=" + sa_storeid + " and siteid='" + siteid + "' ");
  251. if (rows.isEmpty()) {
  252. return getErrReturnObject().setErrMsg("数据不存在").toString();
  253. }
  254. if (!rows.get(0).getString("status").equals("审核")) {
  255. return getErrReturnObject().setErrMsg("当前状态不允许反审核").toString();
  256. }
  257. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(controller, sa_store);
  258. updateSQL.setSiteid(siteid);
  259. updateSQL.setUniqueid(sa_storeid);
  260. updateSQL.setValue("status", "新建");
  261. updateSQL.setValue("checkby", "null");
  262. updateSQL.setValue("checkdate", "null");
  263. updateSQL.setValue("submitby", "null");
  264. updateSQL.setValue("submitdate", "null");
  265. updateSQL.update();
  266. return getSucReturnObject().toString();
  267. }
  268. @API(title = "选择负责人(添加门店人员)", apiversion = R.ID20240410142002.v1.class)
  269. public String chooseLeader() throws YosException {
  270. Long sa_storeid = content.getLongValue("sa_storeid");
  271. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sys_enterprise_hr"
  272. , "sys_enterprise_hrid", "name", "position", "phonenumber", "isleader", "userid")
  273. .setTableAlias("t1");
  274. querySQL.setSiteid(siteid);
  275. querySQL.setWhere("sys_enterpriseid", sys_enterpriseid);
  276. querySQL.setWhere(" not exists(select 1 from sa_store where leader_hrid=t1.sys_enterprise_hrid and sa_storeid=" + sa_storeid + " and siteid='" + siteid + "')");
  277. querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  278. Rows rows = querySQL.query();
  279. //查询角色
  280. RowsMap roleRowsMap = StoreHelper.getRoleRowsMap(controller, rows.toArray("userid"));
  281. //查询门店
  282. RowsMap storeRowsMap = StoreHelper.getStoreRowsMap(controller, rows.toArray("sys_enterprise_hrid"));
  283. for (Row row : rows) {
  284. row.put("headpic", getHeadPic(row.getLong("userid")));
  285. Rows roleRows = roleRowsMap.getOrDefault(row.getString("userid"), new Rows());
  286. row.put("rolenames", String.join(",", roleRows.toArrayList("rolename")));
  287. Rows storeRows = storeRowsMap.getOrDefault(row.getString("sys_enterprise_hrid"), new Rows());
  288. row.put("storenames", String.join(",", storeRows.toArrayList("storename")));
  289. if (roleRows.toArrayList("rolename").contains(Parameter.get("storerolename"))) {
  290. row.put("status", "启用");
  291. } else {
  292. row.put("status", "停用");
  293. }
  294. }
  295. return getSucReturnObject().setData(rows).toString();
  296. }
  297. @API(title = "门店人员", apiversion = R.ID20240410150702.v1.class)
  298. public String storeHrs() throws YosException {
  299. StringBuffer where = new StringBuffer(" 1=1 ");
  300. if (content.containsKey("where")) {
  301. JSONObject whereObject = content.getJSONObject("where");
  302. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  303. where.append(" and (");
  304. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  305. where.append("or t1.phonenumber like'%").append(whereObject.getString("condition")).append("%' ");
  306. where.append(")");
  307. }
  308. }
  309. Long sa_storeid = content.getLongValue("sa_storeid");
  310. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sys_enterprise_hr"
  311. , "sys_enterprise_hrid", "name", "position", "phonenumber", "isleader", "userid", "createby", "createdate", "changeby", "changedate")
  312. .setTableAlias("t1");
  313. querySQL.addJoinTable(JOINTYPE.inner, "sa_store_hr", "t2", "t2.sys_enterprise_hrid=t1.sys_enterprise_hrid and t2.siteid=t1.siteid"
  314. , "sa_store_hrid");
  315. querySQL.addJoinTable(JOINTYPE.left, "sys_users", "t3", "t3.userid=t1.userid "
  316. , "accountno");
  317. querySQL.addJoinTable(JOINTYPE.left, "sys_userrole", "t4", "t4.userid=t1.userid and t4.siteid=t1.siteid");
  318. querySQL.addJoinTable(JOINTYPE.left, "sys_role", "t5", "t5.roleid=t4.roleid and t5.siteid=t4.siteid and t5.rolename='" + Parameter.get("storerolename") + "'");
  319. querySQL.addQueryFields("status", "case when t5.roleid >0 then '启用' else '停用' end");
  320. querySQL.setSiteid(siteid);
  321. querySQL.setWhere(where.toString());
  322. querySQL.setWhere("t2.sa_storeid", sa_storeid);
  323. querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  324. Rows rows = querySQL.query();
  325. //查询角色
  326. RowsMap roleRowsMap = StoreHelper.getRoleRowsMap(controller, rows.toArray("userid"));
  327. //查询门店
  328. RowsMap storeRowsMap = StoreHelper.getStoreRowsMap(controller, rows.toArray("sys_enterprise_hrid"));
  329. for (Row row : rows) {
  330. row.put("headpic", getHeadPic(row.getLong("userid")));
  331. Rows roleRows = roleRowsMap.getOrDefault(row.getString("userid"), new Rows());
  332. row.put("rolenames", String.join(",", roleRows.toArrayList("rolename")));
  333. Rows storeRows = storeRowsMap.getOrDefault(row.getString("sys_enterprise_hrid"), new Rows());
  334. row.put("storenames", String.join(",", storeRows.toArrayList("storename")));
  335. row.putIfAbsent("createdate", "");
  336. row.putIfAbsent("changedate", "");
  337. }
  338. return getSucReturnObject().setData(rows).toString();
  339. }
  340. @API(title = "添加门店人员", apiversion = R.ID20240410153502.v1.class)
  341. public String addstoreHrs() throws YosException {
  342. Long sa_storeid = content.getLongValue("sa_storeid");
  343. JSONArray sys_enterprise_hrids = content.getJSONArray("sys_enterprise_hrids");
  344. ArrayList<String> sqlList = new ArrayList<>();
  345. for (Object object : sys_enterprise_hrids) {
  346. InsertSQL insertSQL = SQLFactory.createInsertSQL(controller, "sa_store_hr");
  347. insertSQL.setSiteid(siteid);
  348. insertSQL.setUniqueid(createTableID("sa_store_hr"));
  349. insertSQL.setValue("sys_enterpriseid", sys_enterpriseid);
  350. insertSQL.setValue("sa_storeid", sa_storeid);
  351. insertSQL.setValue("sys_enterprise_hrid", object);
  352. insertSQL.setWhere("not exists(select 1 from sa_store_hr where sys_enterprise_hrid=" + object + " and sa_storeid=" + sa_storeid + " and siteid='" + siteid + "')");
  353. sqlList.add(insertSQL.getSQL());
  354. }
  355. dbConnect.runSqlUpdate(sqlList);
  356. return getSucReturnObject().toString();
  357. }
  358. @API(title = "删除门店人员", apiversion = R.ID20240410153602.v1.class)
  359. public String deleteStoreHrs() throws YosException {
  360. JSONArray sa_store_hrids = content.getJSONArray("sa_store_hrids");
  361. DeleteSQL deleteSQL = SQLFactory.createDeleteSQL(controller, "sa_store_hr");
  362. deleteSQL.setSiteid(siteid);
  363. deleteSQL.setWhere("sa_store_hrid", sa_store_hrids);
  364. deleteSQL.delete();
  365. return getSucReturnObject().toString();
  366. }
  367. }