Project.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package restcontroller.webmanage.sale.project;
  2. import beans.contract.Contract;
  3. import beans.datatag.DataTag;
  4. import beans.datateam.DataTeam;
  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.data.Row;
  11. import common.data.Rows;
  12. import common.data.RowsMap;
  13. import common.data.SQLFactory;
  14. import restcontroller.R;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. @API(title = "工程项目表")
  18. public class Project extends Controller {
  19. /**
  20. * 构造函数
  21. *
  22. * @param content
  23. */
  24. public Project(JSONObject content) throws YosException {
  25. super(content);
  26. }
  27. @API(title = "变更经销商", apiversion = R.ID20221114133502.v1.class)
  28. public String changeAgent() throws YosException {
  29. String tablename = "sa_project";
  30. Long sa_projectid = content.getLong("sa_projectid");
  31. //经销商团队负责人id
  32. Long userid = content.getLong("userid");
  33. String sql = "SELECT sys_datatagid from sys_datatag WHERE ownertable = '" + tablename + "' and ownerid = " + sa_projectid + " and siteid='" + siteid + "' AND tag='直销'";
  34. Rows rows = dbConnect.runSqlQuery(sql);
  35. if (rows.isEmpty()) {
  36. if (dbConnect.runSqlQuery("SELECT 1 from sys_datatag WHERE ownertable = '" + tablename + "' and ownerid = " + sa_projectid + " and siteid='" + siteid + "' AND tag='经销'").isEmpty()) {
  37. DataTag.createTag(this, tablename, sa_projectid, "经销");
  38. }
  39. } else {
  40. Long sys_datatagid = rows.get(0).getLong("sys_datatagid");
  41. dbConnect.runSqlUpdate("UPDATE sys_datatag SET tag='经销' WHERE sys_datatagid =" + sys_datatagid);
  42. }
  43. ArrayList<String> sqlList = new ArrayList<>();
  44. sqlList.add("delete from sys_datateam WHERE ownertable = '" + tablename + "' and siteid = '" + siteid + "' and ownerid = " + sa_projectid + " AND sys_enterpriseid >0");
  45. //数据团队新增
  46. sqlList.addAll(DataTeam.createTeamSQL(this, tablename, sa_projectid, userid));
  47. sqlList.addAll(DataTeam.createTeamMemberSql(this, "sa_project", sa_projectid, userid));
  48. dbConnect.runSqlUpdate(sqlList);
  49. return getSucReturnObject().toString();
  50. }
  51. @API(title = "撤回", apiversion = R.ID20221114133602.v1.class)
  52. public String revoke() throws YosException {
  53. String tablename = "sa_project";
  54. Long sa_projectid = content.getLong("sa_projectid");
  55. //1:变更为经销,2:变更为直销
  56. Long type = content.getLong("type");
  57. if (content.containsKey("userid")) {
  58. userid = content.getLong("userid");
  59. }
  60. String oldTag = type == 1 ? "直销" : "经销";
  61. String newTag = type == 1 ? "经销" : "直销";
  62. String sql = "SELECT sys_datatagid from sys_datatag WHERE ownertable = '" + tablename + "' and ownerid = " + sa_projectid + " and siteid='" + siteid + "' AND tag='" + oldTag + "'";
  63. Rows rows = dbConnect.runSqlQuery(sql);
  64. if (rows.isEmpty()) {
  65. if (dbConnect.runSqlQuery("SELECT 1 from sys_datatag WHERE ownertable = '" + tablename + "' and ownerid = " + sa_projectid + " and siteid='" + siteid + "' AND tag='" + newTag + "'").isEmpty()) {
  66. DataTag.createTag(this, tablename, sa_projectid, newTag);
  67. }
  68. } else {
  69. Long sys_datatagid = rows.get(0).getLong("sys_datatagid");
  70. dbConnect.runSqlUpdate("UPDATE sys_datatag SET tag='" + newTag + "' WHERE sys_datatagid =" + sys_datatagid);
  71. }
  72. ArrayList<String> sqlList = new ArrayList<>();
  73. if (type == 1) {
  74. sqlList.add("delete from sys_datateam WHERE ownertable = '" + tablename + "' and siteid = '" + siteid + "' and ownerid = " + sa_projectid + " AND sys_enterpriseid >0");
  75. } else if (type == 2) {
  76. sqlList.add("delete from sys_datateam WHERE ownertable = '" + tablename + "' and siteid = '" + siteid + "' and ownerid = " + sa_projectid);
  77. }
  78. //数据团队新增
  79. sqlList.addAll(DataTeam.createTeamSQL(this, tablename, sa_projectid, userid));
  80. sqlList.addAll(DataTeam.createTeamMemberSql(this, "sa_project", sa_projectid, userid));
  81. dbConnect.runSqlUpdate(sqlList);
  82. return getSucReturnObject().toString();
  83. }
  84. @API(title = "获取变更经销商或业务员人员列表", apiversion = R.ID20221114141902.v1.class)
  85. public String getUserList() throws YosException {
  86. String sql = "";
  87. switch (usertype) {
  88. case 1:
  89. sql = "SELECT name,userid from sys_hr WHERE hrid in (SELECT hrid from sa_salearea_hr WHERE siteid='" + siteid + "') AND userid is not null";
  90. break;
  91. case 21:
  92. case 22:
  93. sql = "SELECT name,userid from sys_enterprise_hr WHERE sys_enterpriseid in (SELECT sys_enterpriseid from sys_enterprise_tradefield WHERE sa_saleareaid in (SELECT sa_saleareaid from sa_salearea_hr WHERE hrid IN (SELECT hrid from sys_hr WHERE userid = " + userid + "))) AND isleader";
  94. break;
  95. }
  96. Rows rows = dbConnect.runSqlQuery(sql);
  97. return getSucReturnObject().setData(rows).toString();
  98. }
  99. @API(title = "更改模式(经销或直销)", apiversion = R.ID20221114142002.v1.class)
  100. public String changeModel() throws YosException {
  101. Long sa_projectid = content.getLong("sa_projectid");
  102. String newtag = content.getString("tag");
  103. String oldtag = newtag.equals("直销") ? "经销" : "直销";
  104. String where1 = "where ownertable = 'sa_project' and ownerid = " + sa_projectid + " and tag='" + newtag + "'";
  105. String where2 = "where ownertable = 'sa_project' and ownerid = " + sa_projectid + " and tag='" + oldtag + "'";
  106. if (dbConnect.runSqlQuery("SELECT * from sys_datatag " + where1).isNotEmpty()) {
  107. dbConnect.runSqlUpdate("UPDATE sys_datatag set tag='" + newtag + "' " + where1);
  108. } else {
  109. if ((dbConnect.runSqlQuery("SELECT * from sys_datatag " + where2).isNotEmpty())) {
  110. dbConnect.runSqlUpdate("UPDATE sys_datatag set tag='" + newtag + "' " + where2);
  111. } else {
  112. dbConnect.runSqlUpdate("INSERT INTO sys_datatag VALUES('" + siteid + "', " + createTableID("sys_datatag") + ",'" + newtag + "'," + sa_projectid + ",'sa_project',1)");
  113. }
  114. }
  115. return getSucReturnObject().toString();
  116. }
  117. @API(title = "根据项目查报价单", apiversion = R.ID20221124190702.v1.class)
  118. public String getQuotedPriceList() throws YosException {
  119. StringBuffer where = new StringBuffer(" 1=1 ");
  120. if (content.containsKey("where")) {
  121. JSONObject whereObject = content.getJSONObject("where");
  122. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  123. where.append(" and(");
  124. where.append("t1.projectname like'%").append(whereObject.getString("condition")).append("%' ");
  125. where.append("or t1.projectnum like'%").append(whereObject.getString("condition")).append("%' ");
  126. where.append("or t1.address like'%").append(whereObject.getString("condition")).append("%' ");
  127. where.append("or t1.scale like'%").append(whereObject.getString("condition")).append("%' ");
  128. where.append(")");
  129. }
  130. }
  131. long sa_projectid = content.getLongValue("sa_projectid");
  132. SQLFactory sqlFactory = new SQLFactory(this, "根据项目查询报价单", pageSize, pageNumber, pageSorting);
  133. sqlFactory.addParameter("sa_projectid", sa_projectid);
  134. sqlFactory.addParameter("siteid", siteid);
  135. sqlFactory.addParameter_SQL("where", where);
  136. String sql = sqlFactory.getSQL();
  137. Rows rows = dbConnect.runSqlQuery(sql);
  138. return getSucReturnObject().setData(rows).toString();
  139. }
  140. @API(title = "根据项目查线索", apiversion = R.ID20221124110002.v1.class)
  141. public String getOrderClueList() throws YosException {
  142. StringBuffer where = new StringBuffer(" 1=1 ");
  143. if (content.containsKey("where")) {
  144. JSONObject whereObject = content.getJSONObject("where");
  145. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  146. where.append(" and(");
  147. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  148. where.append("or t1.address like'%").append(whereObject.getString("condition")).append("%' ");
  149. where.append(")");
  150. }
  151. }
  152. long sa_projectid = content.getLongValue("sa_projectid");
  153. SQLFactory sqlFactory = new SQLFactory(this, "根据项目查线索", pageSize, pageNumber, pageSorting);
  154. sqlFactory.addParameter("sa_projectid", sa_projectid);
  155. sqlFactory.addParameter("siteid", siteid);
  156. sqlFactory.addParameter_SQL("where", where);
  157. Rows rows = dbConnect.runSqlQuery(sqlFactory);
  158. RowsMap leaderRows = DataTeam.getLeader(this, "sat_orderclue", rows.toArrayList("sat_orderclueid")).toRowsMap("ownerid");
  159. for (Row row : rows) {
  160. row.put("leader", leaderRows.get(row.getString("sat_orderclueid")));
  161. }
  162. return getSucReturnObject().setData(rows).toString();
  163. }
  164. @API(title = "根据项目查询关联合同列表", apiversion = R.ID20221223102102.v1.class)
  165. @CACHEING
  166. public String getContractList() throws YosException {
  167. StringBuffer where = new StringBuffer(" 1=1 ");
  168. if (content.containsKey("where")) {
  169. JSONObject whereObject = content.getJSONObject("where");
  170. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  171. where.append(" and(");
  172. where.append("t1.createby like'%").append(whereObject.getString("condition")).append("%' ");
  173. where.append("or t1.title like'%").append(whereObject.getString("condition")).append("%' ");
  174. where.append("or t1.type like'%").append(whereObject.getString("condition")).append("%' ");
  175. where.append("or t1.billno like'%").append(whereObject.getString("condition")).append("%' ");
  176. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  177. where.append(")");
  178. }
  179. //状态
  180. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  181. where.append(" and(");
  182. where.append("t1.status ='").append(whereObject.getString("status")).append("' ");
  183. where.append(")");
  184. }
  185. //类型
  186. if (whereObject.containsKey("type") && !"".equals(whereObject.getString("type"))) {
  187. where.append(" and(");
  188. where.append("t1.type ='").append(whereObject.getString("type")).append("' ");
  189. where.append(")");
  190. }
  191. if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) {
  192. where.append(" and t1.createdate >='").append(whereObject.getString("begindate")).append("'");
  193. }
  194. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  195. where.append(" and t1.createdate <='").append(whereObject.getString("enddate")).append("'");
  196. }
  197. }
  198. String tablename = "sa_contract";
  199. Long sa_projectid = content.getLong("sa_projectid");
  200. SQLFactory sqlFactory = new SQLFactory(this, "根据项目查关联合同", pageSize, pageNumber, pageSorting);
  201. sqlFactory.addParameter("siteid", siteid);
  202. sqlFactory.addParameter("sa_projectid", sa_projectid);
  203. sqlFactory.addParameter_SQL("where", where);
  204. String sql = sqlFactory.getSQL();
  205. Rows rows = dbConnect.runSqlQuery(sql);
  206. ArrayList<Long> ids = rows.toArrayList("sa_contractid", new ArrayList<>());
  207. //标签
  208. HashMap<Long, ArrayList<String>> tagList = DataTag.queryTag(this, tablename, ids, false);
  209. //系统标签
  210. HashMap<Long, ArrayList<String>> sysTagList = DataTag.queryTag(this, tablename, ids, true);
  211. //查询价格
  212. RowsMap signAmountRowsMap = Contract.getContractSignAmount(this, ids);
  213. for (Row row : rows) {
  214. Long id = row.getLong("sa_contractid");
  215. //非系统标签
  216. row.put("tag", tagList.get(id) != null ? tagList.get(id) : new ArrayList<String>());
  217. //系统标签
  218. row.put("tag_sys", sysTagList.get(id) != null ? sysTagList.get(id) : new ArrayList<String>());
  219. if (signAmountRowsMap.get(id.toString()).isNotEmpty()) {
  220. row.put("signamount", signAmountRowsMap.get(id.toString()).get(0).getBigDecimal("signamount").toPlainString());
  221. } else {
  222. row.put("signamount", 0);
  223. }
  224. }
  225. return getSucReturnObject().setData(rows).toString();
  226. }
  227. /**
  228. * 获取合同中的项目
  229. *
  230. * @return
  231. */
  232. @API(title = "合同管理中使用到的项目列表", apiversion = R.ID20221201090802.v1.class)
  233. public String getContractProject() throws YosException {
  234. Long type = content.getLong("type");
  235. if (content.containsKey("sys_enterpriseid")) {
  236. sys_enterpriseid = content.getLongValue("sys_enterpriseid");
  237. }
  238. String sql = "SELECT ownerid from sys_datatag WHERE ownertable = 'sa_project' and ";
  239. ArrayList<Long> ids = new ArrayList<>();
  240. ids.add(userid);
  241. switch (type.toString()) {
  242. //查询我的直销项目
  243. case "1":
  244. sql = sql + "tag in ('直销')";
  245. break;
  246. //查询我负责的范围内的经销项目
  247. case "2":
  248. sql = sql + "tag in ('经销')";
  249. break;
  250. case "3":
  251. sql = sql + "tag in ('经销','直销')";
  252. break;
  253. default:
  254. }
  255. ArrayList<Long> ownerids = dbConnect.runSqlQuery(sql).toArrayList("ownerid", new ArrayList<>());
  256. StringBuffer where = new StringBuffer(" 1=1 ");
  257. if (content.containsKey("where")) {
  258. JSONObject whereObject = content.getJSONObject("where");
  259. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  260. where.append(" and(");
  261. where.append("t1.createby like'%").append(whereObject.getString("condition")).append("%' ");
  262. where.append("or t1.projectname like'%").append(whereObject.getString("condition")).append("%' ");
  263. where.append("or t1.province like'%").append(whereObject.getString("condition")).append("%' ");
  264. where.append("or t1.city like'%").append(whereObject.getString("condition")).append("%' ");
  265. where.append("or t1.county like'%").append(whereObject.getString("condition")).append("%' ");
  266. where.append("or t1.address like'%").append(whereObject.getString("condition")).append("%' ");
  267. where.append(")");
  268. }
  269. }
  270. if (type == 1 || type == 2) {
  271. where.append(" and(");
  272. where.append("t1.sa_projectid in ( SELECT sa_projectid from sa_project_parties WHERE sys_enterpriseid = " + sys_enterpriseid + " and siteid='" + siteid + "')");
  273. where.append(")");
  274. }
  275. SQLFactory sqlFactory = new SQLFactory(this, "合同管理中使用到的项目列表", pageSize, pageNumber, pageSorting);
  276. sqlFactory.addParameter("siteid", siteid);
  277. sqlFactory.addParameter_SQL("where", where);
  278. sqlFactory.addParameter_in("sa_projectid", ownerids);
  279. sql = sqlFactory.getSQL();
  280. Rows rows = dbConnect.runSqlQuery(sql);
  281. return getSucReturnObject().setData(rows).toString();
  282. }
  283. @API(title = "项目查重", apiversion = R.ID20221208184202.v1.class)
  284. public String checkRepeat() throws YosException {
  285. Long sa_projectid = content.getLong("sa_projectid");
  286. SQLFactory sqlFactory = new SQLFactory(this, "项目查重");
  287. sqlFactory.addParameter("siteid", siteid);
  288. sqlFactory.addParameter("projectname", content.getStringValue("projectname"));
  289. sqlFactory.addParameter("address", content.getStringValue("address"));
  290. sqlFactory.addParameter("sa_projectid", sa_projectid);
  291. String sql = sqlFactory.getSQL();
  292. Rows rows = dbConnect.runSqlQuery(sql);
  293. return getSucReturnObject().setData(rows).toString();
  294. }
  295. }