salearea.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. package restcontroller.webmanage.sale.salearea;
  2. import beans.data.BatchDeleteErr;
  3. import beans.salearea.SaleArea;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import common.Controller;
  7. import common.YosException;
  8. import common.annotation.API;
  9. import common.annotation.CACHEING;
  10. import common.annotation.CACHEING_CLEAN;
  11. import common.annotation.cm;
  12. import common.data.*;
  13. import restcontroller.R;
  14. import java.io.IOException;
  15. import java.math.BigDecimal;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. @API(title = "营销区域管理")
  19. public class salearea extends Controller {
  20. public salearea(JSONObject content) throws YosException {
  21. super(content);
  22. }
  23. @API(title = "营销区域列表查询")
  24. @CACHEING
  25. public String query_area() throws YosException {
  26. HashMap<Long, JSONArray> map = new HashMap<>();
  27. /*
  28. 查询所有分类
  29. */
  30. Rows allrows = dbConnect.runSqlQuery("select sa_saleareaid,areaname,isused,remarks,sequence,ifnull(parentid,0) as parentid from sa_salearea where siteid='" + siteid + "' order by sequence,sa_saleareaid");
  31. /*
  32. 获取所有一级分类
  33. */
  34. Rows toprows = allrows.toRowsMap("parentid").get("0");
  35. /*
  36. 递归查询
  37. */
  38. for (Row row : toprows) {
  39. row.put("subarea", getSubArea(row, allrows));
  40. }
  41. return getSucReturnObject().setData(toprows).toString();
  42. }
  43. @API(title = "营销区域列表查询(范围)")
  44. public String query_area_auth() throws YosException {
  45. Long obiectId = content.getLong("obiectId");
  46. String obiectName = content.getString("obiectName");
  47. HashMap<Long, JSONArray> map = new HashMap<>();
  48. /*
  49. 查询所有分类
  50. */
  51. Rows allrows = dbConnect.runSqlQuery("select sa_saleareaid,areaname,isused,remarks,sequence,ifnull(parentid,0) as parentid from sa_salearea where siteid='" + siteid + "' order by sequence,sa_saleareaid");
  52. /*
  53. 获取所有一级分类
  54. */
  55. Rows toprows = allrows.toRowsMap("parentid").get("0");
  56. /*
  57. 递归查询
  58. */
  59. for (Row row : toprows) {
  60. row.put("subarea", getSubArea(row, allrows));
  61. }
  62. String tablename = "";
  63. String tableid = "";
  64. /*
  65. * 商学院
  66. */
  67. if (obiectName.equals("sat_courseware")) {
  68. tablename = "sat_courseware_auth";
  69. tableid = "sat_coursewareid";
  70. }
  71. //通告
  72. if (obiectName.equals("sat_notice")) {
  73. tablename = "sat_notice_auth";
  74. tableid = "sat_noticeid";
  75. }
  76. //营销物料
  77. if (obiectName.equals("sat_salematerial")) {
  78. tablename = "sat_salematerial_auth";
  79. tableid = "attachmentid";
  80. }
  81. //素材
  82. if (obiectName.equals("sat_sharematerial")) {
  83. tablename = "sat_sharematerial_auth";
  84. tableid = "sat_sharematerialid";
  85. }
  86. //提报
  87. if (obiectName.equals("sat_submiteditmodel")) {
  88. tablename = "sat_submiteditmodel_auth";
  89. tableid = "sat_submiteditmodelid";
  90. }
  91. //公海池
  92. if (obiectName.equals("sa_customerpool")) {
  93. tablename = "sa_customerpool_auth";
  94. tableid = "sa_customerpoolid";
  95. }
  96. Rows authrows = new Rows();
  97. if (!tablename.equals("") && !tableid.equals("")) {
  98. authrows = dbConnect.runSqlQuery("SELECT t1.sa_saleareaid,t1.sa_agentsid,t2.sa_saleareaid sa_saleareaid_temp FROM " + tablename + " t1 LEFT JOIN sys_enterprise_tradefield t2 ON t1.sa_agentsid=t2.sa_agentsid WHERE " + tableid + "=" + obiectId + " AND t1.siteid='" + siteid + "'");
  99. }
  100. setRows(toprows, authrows);
  101. return getSucReturnObject().setData(toprows).toString();
  102. }
  103. /**
  104. * 设置状态 status:0:未选,1:部分选,2:全选
  105. *
  106. * @param rows
  107. * @param authrows
  108. */
  109. public void setRows(Rows rows, Rows authrows) {
  110. for (Row subRow : rows) {
  111. setRows((Rows) subRow.get("subarea"), authrows);
  112. Long sa_saleareaid = subRow.getLong("sa_saleareaid");
  113. int status = 0;
  114. for (Row authRow : authrows) {
  115. Long sa_saleareaid_temp = authRow.getLong("sa_saleareaid");
  116. if (sa_saleareaid_temp != 0) {
  117. if (sa_saleareaid_temp.equals(sa_saleareaid)) {
  118. status = 2;
  119. }
  120. }
  121. }
  122. if (status != 2) {
  123. for (Row authRow : authrows) {
  124. Long sa_saleareaid_temp = authRow.getLong("sa_saleareaid_temp");
  125. Long sa_agentsid = authRow.getLong("sa_agentsid");
  126. if (sa_saleareaid.equals(sa_saleareaid_temp)) {
  127. if (sa_agentsid > 0) {
  128. status = 1;
  129. }
  130. }
  131. }
  132. }
  133. subRow.put("status", status);
  134. }
  135. }
  136. /**
  137. * 递归查询下级部门
  138. *
  139. * @param root
  140. * @param allDepartments
  141. * @return
  142. */
  143. private Rows getSubArea(Row root, Rows allDepartments) {
  144. Rows childrenRows = allDepartments.toRowsMap("parentid").get(root.getString("sa_saleareaid"));
  145. for (Row row : childrenRows) {
  146. row.put("subarea", getSubArea(row, allDepartments));
  147. }
  148. return childrenRows;
  149. }
  150. @API(title = "营销区域新增修改")
  151. @CACHEING_CLEAN(cms = {@cm(clazz = salearea.class, method = {"query_area"})})
  152. public String insertormodify_area() throws YosException {
  153. long sa_saleareaid = content.getLongValue("sa_saleareaid");//新增时传0
  154. String areaname = content.getString("areaname", "sa_salearea");
  155. boolean isused = content.getBoolean("isused");
  156. long parentid = content.getLongValue("parentid");
  157. String remarks = content.getString("remarks", "sa_salearea");
  158. int sequence = content.getInteger("sequence");
  159. if (parentid != 0 && dbConnect.runSqlQuery("select * from sa_salearea where siteid='" + siteid + "' and sa_saleareaid=" + parentid).isEmpty()) {
  160. return getErrReturnObject().setErrMsg("指定的上级区域不存在").toString();
  161. }
  162. boolean isrefresh;
  163. SQLFactory sqlFactory;
  164. if (sa_saleareaid <= 0 || dbConnect.runSqlQuery("select sa_saleareaid from sa_salearea where sa_saleareaid=" + sa_saleareaid).isEmpty()) {
  165. sa_saleareaid = createTableID("sa_salearea");
  166. sqlFactory = new SQLFactory(this, "营销区域新增");
  167. sqlFactory.addParameter("siteid", siteid);
  168. sqlFactory.addParameter("sa_saleareaid", sa_saleareaid);
  169. sqlFactory.addParameter("areaname", areaname);
  170. sqlFactory.addParameter("isused", isused);
  171. sqlFactory.addParameter("parentid", parentid == 0 ? "null" : String.valueOf(parentid));
  172. sqlFactory.addParameter("remarks", remarks);
  173. sqlFactory.addParameter("sequence", sequence);
  174. sqlFactory.addParameter("sa_saleareaids", getSaleareaids(parentid, sa_saleareaid));
  175. isrefresh = true;
  176. } else {
  177. sqlFactory = new SQLFactory(this, "营销区域修改");
  178. sqlFactory.addParameter("siteid", siteid);
  179. sqlFactory.addParameter("sa_saleareaid", sa_saleareaid);
  180. sqlFactory.addParameter("areaname", areaname);
  181. sqlFactory.addParameter("isused", isused);
  182. sqlFactory.addParameter("parentid", parentid == 0 ? "null" : String.valueOf(parentid));
  183. sqlFactory.addParameter("remarks", remarks);
  184. sqlFactory.addParameter("sequence", sequence);
  185. isrefresh = dbConnect.runSqlQuery("select *from sa_salearea where siteid='" + siteid + "' and sa_saleareaid='" + sa_saleareaid + "' and areaname='" + areaname + "'").isEmpty();
  186. }
  187. dbConnect.runSqlUpdate(sqlFactory);
  188. if (isrefresh) {
  189. SaleArea.RefreshSaleAreaMsg(this);
  190. }
  191. return query_area();
  192. }
  193. public JSONArray getSaleareaids(Long parentid, Long sa_saleareaid) throws YosException {
  194. JSONArray jsonArray = new JSONArray();
  195. if (parentid != 0) {
  196. Rows rows = dbConnect.runSqlQuery("SELECT sa_saleareaids from sa_salearea WHERE sa_saleareaid=" + parentid + " and siteid ='" + siteid + "'");
  197. if (rows.isNotEmpty()) {
  198. jsonArray = rows.get(0).getJSONArray("sa_saleareaids");
  199. }
  200. }
  201. jsonArray.add(sa_saleareaid);
  202. return jsonArray;
  203. }
  204. @API(title = "营销区域删除")
  205. @CACHEING_CLEAN(cms = {@cm(clazz = salearea.class, method = {"query_area"})})
  206. public String delete_area() throws YosException {
  207. long sa_saleareaid = content.getLongValue("sa_saleareaid");
  208. if (dbConnect.runSqlQuery("select * from sa_salearea where siteid='" + siteid + "' and isused=1 and sa_saleareaid=" + sa_saleareaid).isNotEmpty()) {
  209. return getErrReturnObject().setErrMsg("启用状态不可删除").toString();
  210. }
  211. if (dbConnect.runSqlQuery("select * from sa_salearea where siteid='" + siteid + "' and parentid=" + sa_saleareaid).isNotEmpty()) {
  212. return getErrReturnObject().setErrMsg("存在下级区域,不可删除").toString();
  213. }
  214. if (dbConnect.runSqlQuery("select * from sa_salearea_hr where siteid='" + siteid + "' and sa_saleareaid=" + sa_saleareaid).isNotEmpty()) {
  215. return getErrReturnObject().setErrMsg("区域存在业务员信息,不可删除").toString();
  216. }
  217. dbConnect.runSqlUpdate("delete from sa_salearea where siteid='" + siteid + "' and sa_saleareaid=" + sa_saleareaid);
  218. return query_area();
  219. }
  220. @API(title = "营销区域查询业务员", apiversion = R.ID20221011144603.v1.class)
  221. @CACHEING
  222. public String query_saler() throws YosException {
  223. long sa_saleareaid = content.getLongValue("sa_saleareaid");
  224. /*
  225. 过滤条件设置
  226. */
  227. StringBuffer where = new StringBuffer(" 1=1 ");
  228. if (content.containsKey("where")) {
  229. JSONObject whereObject = content.getJSONObject("where");
  230. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  231. where.append(" and(");
  232. where.append("t1.phonenumber like'%").append(whereObject.getString("condition")).append("%' ");
  233. where.append("or t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  234. where.append(")");
  235. }
  236. }
  237. ArrayList<Long> ids = SaleArea.getSubSaleAreaIds(this, sa_saleareaid);
  238. ids.add(sa_saleareaid);
  239. SQLFactory sqlFactory = new SQLFactory(this, "业务员列表查询", pageSize, pageNumber, pageSorting);
  240. sqlFactory.addParameter("siteid", siteid);
  241. sqlFactory.addParameter_in("sa_saleareaid", ids);
  242. sqlFactory.addParameter_SQL("where", where);
  243. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  244. return getSucReturnObject().setData(rows).toString();
  245. }
  246. @API(title = "营销区域查询业务员详情查询")
  247. public String query_salermain() throws YosException {
  248. long sa_salearea_hrid = content.getLongValue("sa_salearea_hrid");
  249. SQLFactory sqlFactory = new SQLFactory(this, "业务员详情查询");
  250. sqlFactory.addParameter("siteid", siteid);
  251. sqlFactory.addParameter("sa_salearea_hrid", sa_salearea_hrid);
  252. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  253. return getSucReturnObject().setData(rows.isNotEmpty() ? rows.get(0) : new Row()).toString();
  254. }
  255. @API(title = "营销区域添加业务员", apiversion = R.ID20221011144703.v1.class)
  256. @CACHEING_CLEAN(cms = {@cm(clazz = salearea.class, method = {"query_saler"})})
  257. public String add_saler() throws YosException {
  258. long sa_salearea_hrid = content.getLongValue("sa_salearea_hrid");
  259. long sa_saleareaid = content.getLongValue("sa_saleareaid");
  260. long hrid = content.getLongValue("hrid");
  261. double discountrate = 1;
  262. SQLFactory sqlFactory = new SQLFactory(this, "营销区域添加业务员");
  263. if (sa_salearea_hrid <= 0) {
  264. // if (dbConnect.runSqlQuery("select * from sa_salearea_hr where siteid='" + siteid + "' and sa_saleareaid=" + sa_saleareaid).isNotEmpty()) {
  265. // return getErrReturnObject().setErrMsg("区域存在业务员信息,不可新增").toString();
  266. // }
  267. sa_salearea_hrid = createTableID("sa_salearea_hr");
  268. } else {
  269. sqlFactory = new SQLFactory(this, "营销区域更新业务员");
  270. }
  271. sqlFactory.addParameter("siteid", siteid);
  272. sqlFactory.addParameter("sa_salearea_hrid", sa_salearea_hrid);
  273. sqlFactory.addParameter("sa_saleareaid", sa_saleareaid);
  274. sqlFactory.addParameter("hrid", hrid);
  275. sqlFactory.addParameter("discountrate", content.getBigDecimalValue("discountrate").compareTo(BigDecimal.ZERO) == 0 ? 1 : content.getBigDecimalValue("discountrate"));
  276. sqlFactory.addParameter("iseditprice", content.getBooleanValue("iseditprice"));
  277. dbConnect.runSqlUpdate(sqlFactory.getSQL());
  278. return getSucReturnObject().toString();
  279. }
  280. @API(title = "营销区域删除业务员", apiversion = R.ID20221011144803.v1.class)
  281. @CACHEING_CLEAN(cms = {@cm(clazz = salearea.class, method = {"query_saler"})})
  282. public String delete_saler() throws YosException {
  283. JSONArray sa_saleareaids = content.getJSONArray("sa_saleareaids");
  284. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_saleareaids.size());
  285. for (Object o : sa_saleareaids) {
  286. long sa_saleareaid = ((JSONObject) o).getLong("sa_saleareaid");
  287. long hrid = ((JSONObject) o).getLong("hrid");
  288. dbConnect.runSqlUpdate("delete from sa_salearea_hr where siteid='" + siteid + "' and sa_saleareaid='" + sa_saleareaid + "' and hrid='" + hrid + "'");
  289. }
  290. return batchDeleteErr.getReturnObject().toString();
  291. }
  292. @API(title = "营销区域查询经销商", apiversion = R.ID20221011144903.v1.class)
  293. @CACHEING
  294. public String query_agent() throws YosException, IOException {
  295. /*
  296. 过滤条件设置
  297. */
  298. StringBuffer where = new StringBuffer(" 1=1 ");
  299. if (content.containsKey("where")) {
  300. JSONObject whereObject = content.getJSONObject("where");
  301. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  302. where.append(" and(");
  303. where.append("t2.phonenumber like'%").append(whereObject.getString("condition")).append("%' ");
  304. where.append("or t2.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  305. where.append("or t1.agentnum like'%").append(whereObject.getString("condition")).append("%' ");
  306. where.append("or t4.erpagentnum like'%").append(whereObject.getString("condition")).append("%' ");
  307. where.append(")");
  308. }
  309. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  310. where.append(" and ");
  311. where.append("t1.status = '").append(whereObject.getString("status").equals("ACTIVE") ? "启用" : "禁用").append("' ");
  312. }
  313. }
  314. boolean containssub = content.getBoolean("containssub");
  315. JSONArray sa_saleareaidsArray = content.getJSONArray("sa_saleareaids");
  316. ArrayList<Long> sa_saleareaidsList = new ArrayList<>();
  317. for (Object o : sa_saleareaidsArray) {
  318. Long sa_saleareaid = Long.valueOf(o.toString());
  319. sa_saleareaidsList.add(sa_saleareaid);
  320. if (containssub) {
  321. sa_saleareaidsList.addAll(SaleArea.getSubSaleAreaIds(this, sa_saleareaid));
  322. }
  323. }
  324. boolean isExport = content.getBooleanValue("isExport");
  325. Boolean isAll = content.getBooleanValue("isAll");
  326. if (sa_saleareaidsList.isEmpty()) {
  327. isAll = true;
  328. }
  329. if (!isAll) {
  330. String sqlStr = "and t1.sys_enterpriseid in (SELECT sys_enterpriseid from sys_enterprise_tradefield WHERE sa_saleareaid in " + sa_saleareaidsList + ")";
  331. sqlStr = sqlStr.replace("[", "(").replace("]", ")");
  332. where.append(sqlStr);
  333. }
  334. // SQLFactory sqlFactory = new SQLFactory(this, "营销区域经销商查询", pageSize, pageNumber, pageSorting);
  335. // if (isExport) {
  336. // sqlFactory = new SQLFactory(this, "营销区域经销商查询");
  337. // }
  338. // sqlFactory.addParameter("siteid", siteid);
  339. // sqlFactory.addParameter_SQL("where", where);
  340. // String sql = sqlFactory.getSQL();
  341. // Rows rows = dbConnect.runSqlQuery(sql);
  342. QuerySQL querySQL = getAgentList(where.toString());
  343. querySQL.setOrderBy(pageSorting);
  344. querySQL.setPage(pageSize, pageNumber);
  345. Rows rows = querySQL.query();
  346. ArrayList<Long> ids = rows.toArrayList("sys_enterpriseid", new ArrayList<>());
  347. SQLFactory sqlFactory = new SQLFactory(this, "查询经销商主账号信息");
  348. sqlFactory.addParameter("siteid", siteid);
  349. sqlFactory.addParameter_in("sys_enterpriseid", ids);
  350. RowsMap rowsMap = dbConnect.runSqlQuery(sqlFactory).toRowsMap("sys_enterpriseid");
  351. for (Row row : rows) {
  352. Rows tempRows = rowsMap.getOrDefault(row.getString("sys_enterpriseid"), new Rows());
  353. if (tempRows.isNotEmpty()) {
  354. row.put("userid", tempRows.get(0).getLong("userid"));
  355. row.put("accountno", tempRows.get(0).getString("accountno"));
  356. } else {
  357. row.put("userid", 0);
  358. row.put("accountno", "");
  359. }
  360. }
  361. // if (isExport) {
  362. // //去除不需要导出项
  363. // String[] removeFieldList = {"sa_agentsid", "sys_enterpriseid", "createdate", "limitreturnday", "freefreightamount",
  364. // "invoicingpoint", "userid"};
  365. // for (String key : removeFieldList) {
  366. // rows.getFieldList().remove(key);
  367. // }
  368. //
  369. // Rows uploadRows = uploadExcelToObs("经销商档案", "经销商档案", rows, getTitleMap());
  370. // return getSucReturnObject().setData(uploadRows).toString();
  371. // }
  372. return getSucReturnObject().setData(rows).toString();
  373. }
  374. public QuerySQL getAgentList(String where) throws YosException {
  375. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_agents");
  376. querySQL.setTableAlias("t1");
  377. querySQL.addJoinTable(JOINTYPE.inner, "sys_enterprise", "t2", "t1.siteid = t2.siteid and t1.sys_enterpriseid = t2.sys_enterpriseid",
  378. "enterprisename", "phonenumber", "contact", "province", "city", "county", "address", "limitreturnday", "freefreightamount", "invoicingpoint","saleclassauth");
  379. querySQL.addJoinTable(JOINTYPE.left, "sys_dataextend", "t4", "t1.sys_enterpriseid = t4.ownerid and ownertable = 'sys_enterprise' and t1.siteid = t4.siteid",
  380. "erpagentnum");
  381. querySQL.setWhere("t1.siteid", siteid);
  382. querySQL.setWhere(where);
  383. return querySQL;
  384. }
  385. //返回导出的标题
  386. public HashMap<String, String> getTitleMap() {
  387. //
  388. HashMap<String, String> titleMap = new HashMap<>();
  389. titleMap.put("agentnum", "经销商编号");
  390. titleMap.put("erpagentnum", "ERP经销商编号");
  391. titleMap.put("isservice", "是否服务经销商");
  392. titleMap.put("accountno", "登录账号");
  393. titleMap.put("enterprisename", "经销商名称");
  394. titleMap.put("province", "省");
  395. titleMap.put("city", "市");
  396. titleMap.put("county", "县");
  397. titleMap.put("address", "地址");
  398. titleMap.put("contact", "经销商联系人");
  399. titleMap.put("phonenumber", "联系人手机号码");
  400. titleMap.put("status", "状态");
  401. return titleMap;
  402. }
  403. @API(title = "查询所有业务员", apiversion = R.ID20221122153902.v1.class)
  404. public String getSalers() throws YosException {
  405. /*
  406. 过滤条件设置
  407. */
  408. StringBuffer where = new StringBuffer(" 1=1 ");
  409. if (content.containsKey("where")) {
  410. JSONObject whereObject = content.getJSONObject("where");
  411. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  412. where.append(" and(");
  413. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  414. where.append(")");
  415. }
  416. }
  417. SQLFactory sqlFactory = new SQLFactory(this, "查询所有业务员", pageSize, pageNumber, pageSorting);
  418. sqlFactory.addParameter("siteid", siteid);
  419. sqlFactory.addParameter_SQL("where", where);
  420. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  421. ArrayList<Long> hrids = rows.toArrayList("hrid", new ArrayList<>());
  422. sqlFactory = new SQLFactory(this, "查询业务员营销区域");
  423. sqlFactory.addParameter("siteid", siteid);
  424. sqlFactory.addParameter_in("hrid", hrids);
  425. RowsMap saleareaRowsMap = dbConnect.runSqlQuery(sqlFactory).toRowsMap("hrid");
  426. for (Row row : rows) {
  427. row.put("salearea", saleareaRowsMap.getOrDefault(row.getString("hrid"), new Rows()));
  428. }
  429. return getSucReturnObject().setData(rows).toString();
  430. }
  431. }