Store.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package restcontroller.webmanage.saletool.store;
  2. import beans.attachment.Attachment;
  3. import beans.salearea.SaleArea;
  4. import com.alibaba.fastjson.JSONObject;
  5. import common.Controller;
  6. import common.YosException;
  7. import common.annotation.API;
  8. import common.data.*;
  9. import restcontroller.R;
  10. import java.util.ArrayList;
  11. /**
  12. * 门店小程序(企业人员)
  13. */
  14. public class Store extends Controller {
  15. /**
  16. * 构造函数
  17. *
  18. * @param content
  19. */
  20. public Store(JSONObject content) throws YosException {
  21. super(content);
  22. }
  23. Controller controller = this;
  24. String sa_store = "sa_store";
  25. //查询当前企业人员授权的经销商列表
  26. @API(title = "查询授权的经销商列表", apiversion = R.ID20240411133702.v1.class)
  27. public String agentList() throws YosException {
  28. StringBuffer where = new StringBuffer(" 1=1 ");
  29. if (content.containsKey("where")) {
  30. JSONObject whereObject = content.getJSONObject("where");
  31. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  32. where.append(" and (");
  33. where.append("t1.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  34. where.append("or t2.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  35. where.append("or t1.province like'%").append(whereObject.getString("condition")).append("%' ");
  36. where.append("or t1.city like'%").append(whereObject.getString("condition")).append("%' ");
  37. where.append("or t1.county like'%").append(whereObject.getString("condition")).append("%' ");
  38. where.append("or t1.address like'%").append(whereObject.getString("condition")).append("%' ");
  39. where.append(")");
  40. }
  41. }
  42. Rows saleareaRows = dbConnect.runSqlQuery("SELECT sa_saleareaid from sa_salearea_hr WHERE hrid=" + hrid + " and siteid='" + siteid + "'");
  43. ArrayList<Long> sa_saleareaids = saleareaRows.toArrayList("sa_saleareaid", new ArrayList<>());
  44. sa_saleareaids.addAll(SaleArea.getSubSaleAreaIds(this, sa_saleareaids));
  45. if (sa_saleareaids.size() == 0) {
  46. sa_saleareaids.add(-1L);
  47. }
  48. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sys_enterprise"
  49. , "sys_enterpriseid", "enterprisename", "contact", "phonenumber", "province", "city", "county", "address")
  50. .setTableAlias("t1");
  51. querySQL.addJoinTable(JOINTYPE.left, "sa_agents", "t2", "t2.sys_enterpriseid=t1.sys_enterpriseid and t2.siteid=t1.siteid"
  52. , "agentnum", "status");
  53. querySQL.setSiteid(siteid);
  54. querySQL.setWhere(where.toString());
  55. querySQL.setWhere("EXISTS(SELECT 1 from sys_enterprise_tradefield WHERE sys_enterpriseid=t1.sys_enterpriseid and sa_saleareaid in " + sa_saleareaids.toString().replace("[", "(").replace("]", ")") + ")");
  56. querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  57. Rows rows = querySQL.query();
  58. if(rows.isEmpty()){
  59. return getSucReturnObject().setData(rows).toString();
  60. }
  61. ArrayList<Long> ids=rows.toArrayList("sys_enterpriseid",new ArrayList<>());
  62. ids.add(-1L);
  63. RowsMap rowsMap = dbConnect.runSqlQuery("SELECT sys_enterpriseid,count(sys_enterprise_hrid) count from sys_enterprise_hr WHERE siteid='" + siteid + "' and sys_enterpriseid in " + ids.toString().replace("[", "(").replace("]", ")") + " GROUP by sys_enterpriseid").toRowsMap("sys_enterpriseid");
  64. for (Row row : rows) {
  65. row.put("detailaddress", row.getString("province") + row.getString("city") + row.getString("county") + row.getString("address"));
  66. row.put("areanames", userInfo.getAreaName(this, row.getLong("sys_enterpriseid")));
  67. Rows countRows = rowsMap.getOrDefault(row.getString("sys_enterpriseid"), new Rows());
  68. row.put("hrcount", countRows.isNotEmpty() ? countRows.get(0).getLong("count") : 0);
  69. }
  70. return getSucReturnObject().setData(rows).toString();
  71. }
  72. @API(title = "待审核的门店列表", apiversion = R.ID20240411141602.v1.class)
  73. public String list() throws YosException {
  74. StringBuffer where = new StringBuffer(" 1=1 ");
  75. if (content.containsKey("where")) {
  76. JSONObject whereObject = content.getJSONObject("where");
  77. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  78. where.append(" and (");
  79. where.append("t1.storename like'%").append(whereObject.getString("condition")).append("%' ");
  80. where.append("or t2.name like'%").append(whereObject.getString("condition")).append("%' ");
  81. where.append("or t2.phonenumber like'%").append(whereObject.getString("condition")).append("%' ");
  82. where.append(")");
  83. }
  84. // if (whereObject.containsKey("storetype") && !"".equals(whereObject.getString("storetype"))) {
  85. // where.append(" and (");
  86. // where.append("t1.storetype='").append(whereObject.getString("storetype")).append("' ");
  87. // where.append(")");
  88. // }
  89. // if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  90. // where.append(" and (");
  91. // where.append("t1.status='").append(whereObject.getString("status")).append("' ");
  92. // where.append(")");
  93. // }
  94. }
  95. Rows saleareaRows = dbConnect.runSqlQuery("SELECT sa_saleareaid from sa_salearea_hr WHERE hrid=" + hrid + " and siteid='" + siteid + "'");
  96. ArrayList<Long> sa_saleareaids = saleareaRows.toArrayList("sa_saleareaid", new ArrayList<>());
  97. sa_saleareaids.addAll(SaleArea.getSubSaleAreaIds(this, sa_saleareaids));
  98. QuerySQL listQuery = SQLFactory.createQuerySQL(controller, sa_store)
  99. .setTableAlias("t1");
  100. listQuery.addJoinTable(JOINTYPE.left, "sys_enterprise_hr", "t2", "t2.sys_enterprise_hrid=t1.leader_hrid and t2.siteid=t1.siteid"
  101. , "name", "phonenumber");
  102. listQuery.addJoinTable(JOINTYPE.left, "sys_users", "t3", "t3.userid=t2.userid "
  103. , "accountno");
  104. listQuery.addJoinTable(JOINTYPE.left, "sys_enterprise", "t4", "t4.sys_enterpriseid=t1.sys_enterpriseid and t4.siteid=t1.siteid"
  105. , "enterprisename");
  106. listQuery.setWhere("EXISTS(SELECT 1 from sys_enterprise_tradefield WHERE sys_enterpriseid=t1.sys_enterpriseid and sa_saleareaid in " + sa_saleareaids.toString().replace("[", "(").replace("]", ")") + ")");
  107. listQuery.setWhere("status", "提交");
  108. listQuery.setWhere(where.toString());
  109. listQuery.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  110. Rows rows = listQuery.query();
  111. RowsMap attinfoRows = Attachment.get(controller, sa_store, rows.toArrayList("sa_storeid", new ArrayList<>()));
  112. for (Row row : rows) {
  113. row.put("attinfos", attinfoRows.getOrDefault(row.getString("sa_storeid"), new Rows()));
  114. row.put("detailaddress", row.getString("province") + row.getString("city") + row.getString("county") + row.getString("address"));
  115. }
  116. return getSucReturnObject().setData(rows).toString();
  117. }
  118. @API(title = "查询授权的经销商的团队人员列表", apiversion = R.ID20240411142602.v1.class)
  119. public String teamList() throws YosException {
  120. StringBuffer where = new StringBuffer(" 1=1 ");
  121. if (content.containsKey("where")) {
  122. JSONObject whereObject = content.getJSONObject("where");
  123. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  124. where.append(" and (");
  125. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  126. where.append("or t1.phonenumber like'%").append(whereObject.getString("condition")).append("%' ");
  127. where.append(")");
  128. }
  129. }
  130. Rows saleareaRows = dbConnect.runSqlQuery("SELECT sa_saleareaid from sa_salearea_hr WHERE hrid=" + hrid + " and siteid='" + siteid + "'");
  131. ArrayList<Long> sa_saleareaids = saleareaRows.toArrayList("sa_saleareaid", new ArrayList<>());
  132. sa_saleareaids.addAll(SaleArea.getSubSaleAreaIds(this, sa_saleareaids));
  133. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sys_enterprise_hr"
  134. , "sys_enterprise_hrid", "name", "position", "phonenumber", "isleader", "userid")
  135. .setTableAlias("t1");
  136. querySQL.setSiteid(siteid);
  137. querySQL.setWhere("EXISTS(SELECT 1 from sys_enterprise_tradefield WHERE sys_enterpriseid=t1.sys_enterpriseid and sa_saleareaid in " + sa_saleareaids.toString().replace("[", "(").replace("]", ")") + ")");
  138. querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  139. Rows rows = querySQL.query();
  140. //查询角色
  141. RowsMap roleRowsMap = StoreHelper.getRoleRowsMap(controller, rows.toArray("userid"));
  142. //查询门店
  143. RowsMap storeRowsMap = StoreHelper.getStoreRowsMap(controller, rows.toArray("sys_enterprise_hrid"));
  144. for (Row row : rows) {
  145. row.put("headpic", getHeadPic(row.getLong("userid")));
  146. Rows roleRows = roleRowsMap.getOrDefault(row.getString("userid"), new Rows());
  147. row.put("rolenames", String.join(",", roleRows.toArrayList("rolename")));
  148. Rows storeRows = storeRowsMap.getOrDefault(row.getString("sys_enterprise_hrid"), new Rows());
  149. row.put("storenames", String.join(",", storeRows.toArrayList("storename")));
  150. }
  151. return getSucReturnObject().setData(rows).toString();
  152. }
  153. @API(title = "小程序门店首页", apiversion = R.ID20240416153202.v1.class)
  154. public String appletHome() throws YosException {
  155. String longitude = content.getStringValue("longitude");
  156. String latitude = content.getStringValue("latitude");
  157. Long sa_storeid = content.getLongValue("sa_storeid");
  158. Rows rows;
  159. if (sa_storeid > 0) {
  160. rows = StoreHelper.queryStores(this, sa_storeid, longitude, latitude);
  161. } else {
  162. if (usertype == 21 || usertype == 22) {
  163. Long sys_enterprise_hrid = getEnterpriseHr(userid).getLong("sys_enterprise_hrid");
  164. rows = StoreHelper.queryAgentsStores(this, sys_enterprise_hrid, longitude, latitude);
  165. if (rows.isEmpty()) {
  166. rows = StoreHelper.queryStores(this, longitude, latitude);
  167. }
  168. } else {
  169. rows = StoreHelper.queryStores(this, longitude, latitude);
  170. }
  171. }
  172. for (Row row : rows) {
  173. row.put("distance", String.format("%.2f", row.getDouble("distance")));
  174. row.put("attinfos", Attachment.get(this, "sa_store", row.getLong("sa_storeid")));
  175. row.put("headpic", getHeadPic(row.getLong("userid")));
  176. addHistory(row.getLong("sa_storeid"));
  177. }
  178. return getSucReturnObject().setData(rows).toString();
  179. }
  180. public void addHistory(Long sa_storeid) throws YosException {
  181. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_store_history");
  182. querySQL.setSiteid(siteid);
  183. querySQL.setWhere("userid", userid);
  184. querySQL.setWhere("sa_storeid", sa_storeid);
  185. Rows rows = querySQL.query();
  186. if (rows.isNotEmpty()) {
  187. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sa_store_history");
  188. updateSQL.setSiteid(siteid);
  189. updateSQL.setValue("userid", userid);
  190. updateSQL.setWhere("userid", userid);
  191. updateSQL.setWhere("sa_storeid", sa_storeid);
  192. System.err.println(updateSQL.getSQL());
  193. updateSQL.update();
  194. } else {
  195. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_store_history");
  196. insertSQL.setUniqueid(createTableID("sa_store_history"));
  197. insertSQL.setSiteid(siteid);
  198. insertSQL.setValue("userid", userid);
  199. insertSQL.setValue("sa_storeid", sa_storeid);
  200. insertSQL.insert();
  201. }
  202. }
  203. @API(title = "查询历史足迹", apiversion = R.ID20240416162002.v1.class)
  204. public String getHistory() throws YosException {
  205. String longitude = content.getStringValue("longitude");
  206. String latitude = content.getStringValue("latitude");
  207. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sa_store")
  208. .setTableAlias("t1");
  209. querySQL.addJoinTable(JOINTYPE.left, "sys_enterprise_hr", "t2", "t2.sys_enterprise_hrid=t1.leader_hrid and t2.siteid=t1.siteid"
  210. , "name", "phonenumber", "userid");
  211. querySQL.addJoinTable(JOINTYPE.inner, "sa_store_history", "t3", "t3.sa_storeid=t1.sa_storeid and t3.siteid=t1.siteid");
  212. querySQL.addQueryFields("distance", "st_distance_sphere(point(longitude,latitude),point('" + longitude + "','" + latitude + "'))");
  213. querySQL.setSiteid(siteid);
  214. querySQL.setWhere("t3.userid", userid);
  215. querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  216. Rows rows = querySQL.query();
  217. RowsMap attinfoRows = Attachment.get(controller, sa_store, rows.toArrayList("sa_storeid", new ArrayList<>()));
  218. for (Row row : rows) {
  219. row.put("attinfos", attinfoRows.getOrDefault(row.getString("sa_storeid"), new Rows()));
  220. row.put("distance", String.format("%.2f", row.getDouble("distance")));
  221. row.put("headpic", getHeadPic(row.getLong("userid")));
  222. }
  223. return getSucReturnObject().setData(rows).toString();
  224. }
  225. }