Customer.java 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. package restcontroller.sale.customer;
  2. import beans.contract.Contract;
  3. import beans.datacontrllog.DataContrlLog;
  4. import beans.dataextend.DataExtend;
  5. import beans.datatag.DataTag;
  6. import beans.datateam.DataTeam;
  7. import beans.hr.Hr;
  8. import beans.phonebook.PhoneBook;
  9. import beans.proxy.Proxy;
  10. import beans.recycle.Recycle;
  11. import beans.salearea.SaleArea;
  12. import beans.salesfees.SalesFees;
  13. import com.alibaba.fastjson.JSONArray;
  14. import com.alibaba.fastjson.JSONObject;
  15. import common.Controller;
  16. import common.YosException;
  17. import common.annotation.API;
  18. import common.annotation.CACHEING;
  19. import common.annotation.CACHEING_CLEAN;
  20. import common.data.*;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  23. import org.apache.poi.xssf.usermodel.XSSFSheet;
  24. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  25. import restcontroller.R;
  26. import restcontroller.webmanage.executorService.Executor;
  27. import java.io.IOException;
  28. import java.math.BigDecimal;
  29. import java.util.ArrayList;
  30. import java.util.HashMap;
  31. import java.util.HashSet;
  32. import static beans.customers.Customers.getRepeatCustomers;
  33. import static beans.datateam.DataTeam.getProxyUserids;
  34. @API(title = "客户")
  35. public class Customer extends Controller {
  36. /**
  37. * 构造函数
  38. *
  39. * @param content
  40. */
  41. public Customer(JSONObject content) throws YosException {
  42. super(content);
  43. }
  44. @API(title = "新增或更新", apiversion = R.ID20221012163902.v1.class, intervaltime = 2000)
  45. @CACHEING_CLEAN(apiversions = {R.ID20221022165503.v1.class, R.ID20221011133602.class, R.ID20221013102602.class,
  46. R.ID20221012164402.class, R.ID20221014164702.class, R.ID20220920083901.class, R.ID20220929085401.class,
  47. R.ID20220920083901.v1.class})
  48. public String insertOrUpdate() throws YosException {
  49. String tablename = "sa_customers";
  50. Long sa_customersid = content.getLong("sa_customersid");
  51. String type = content.getString("type");
  52. Long sys_enterpriseid = content.getLong("sys_enterpriseid");
  53. String enterprisename = content.getString("enterprisename");
  54. String address = content.getStringValue("address");
  55. String province = content.getStringValue("province");
  56. String city = content.getStringValue("city");
  57. String county = content.getStringValue("county");
  58. Long saler_userid = content.getLongValue("saler_userid");
  59. if (userInfo.isSaler()) {
  60. saler_userid = userid;
  61. }
  62. ArrayList<String> sqlList = new ArrayList<>();
  63. //创建企业档案
  64. if (sys_enterpriseid <= 0) {
  65. sys_enterpriseid = createTableID("sys_enterprise");
  66. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sys_enterprise");
  67. insertSQL.setSiteid(siteid);
  68. insertSQL.setUniqueid(sys_enterpriseid);
  69. insertSQL.setValue("province", province);
  70. insertSQL.setValue("city", city);
  71. insertSQL.setValue("county", county);
  72. insertSQL.setValue("address", address);
  73. insertSQL.setValue("enterprisename", enterprisename);
  74. sqlList.add(insertSQL.getSQL());
  75. } else {
  76. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sys_enterprise");
  77. updateSQL.setSiteid(siteid);
  78. updateSQL.setUniqueid(sys_enterpriseid);
  79. updateSQL.setValue("province", province);
  80. updateSQL.setValue("city", city);
  81. updateSQL.setValue("county", county);
  82. updateSQL.setValue("address", address);
  83. updateSQL.setValue("enterprisename", enterprisename);
  84. sqlList.add(updateSQL.getSQL());
  85. }
  86. boolean isAdd = false;
  87. if (sa_customersid <= 0) {
  88. isAdd = true;
  89. Rows stagenameRows = dbConnect.runSqlQuery("SELECT stagename from sa_devstage WHERE siteid='" + siteid + "' order by sequence LIMIT 1");
  90. sa_customersid = createTableID(tablename);
  91. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_customers");
  92. insertSQL.setSiteid(siteid);
  93. insertSQL.setUniqueid(sa_customersid);
  94. insertSQL.setValue("billno", createBillCode("hospital"));
  95. insertSQL.setValue("sys_enterpriseid", sys_enterpriseid);
  96. insertSQL.setValue("name", enterprisename);
  97. insertSQL.setValue("type", type);
  98. insertSQL.setValue("grade", content.getStringValue("grade"));
  99. insertSQL.setValue("host", content.getLongValue("host"));
  100. insertSQL.setValue("room", content.getLongValue("room"));
  101. insertSQL.setValue("bed", content.getLongValue("bed"));
  102. insertSQL.setValue("scale", content.getLongValue("scale"));
  103. insertSQL.setValue("sa_saleareaid", content.getLongValue("sa_saleareaid"));
  104. insertSQL.setValue("province", province);
  105. insertSQL.setValue("city", city);
  106. insertSQL.setValue("county", county);
  107. insertSQL.setValue("address", address);
  108. insertSQL.setValue("stagename", stagenameRows.isNotEmpty() ? stagenameRows.get(0).getString("stagename") : "");
  109. sqlList.add(insertSQL.getSQL());
  110. } else {
  111. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "sa_customers");
  112. updateSQL.setSiteid(siteid);
  113. updateSQL.setUniqueid(sa_customersid);
  114. updateSQL.setValue("name", enterprisename);
  115. updateSQL.setValue("type", type);
  116. updateSQL.setValue("grade", content.getStringValue("grade"));
  117. updateSQL.setValue("host", content.getLongValue("host"));
  118. updateSQL.setValue("room", content.getLongValue("room"));
  119. updateSQL.setValue("bed", content.getLongValue("bed"));
  120. updateSQL.setValue("scale", content.getLongValue("scale"));
  121. updateSQL.setValue("sa_saleareaid", content.getStringValue("sa_saleareaid"));
  122. sqlList.add(updateSQL.getSQL());
  123. }
  124. if (isAdd) {
  125. //标签
  126. sqlList.add(DataTag.createTagSql(this, "sa_customers", sa_customersid, type));
  127. //数据团队新增
  128. sqlList.addAll(DataTeam.createTeamSQL(this, tablename, sa_customersid, saler_userid));
  129. sqlList.add(DataContrlLog.createLog(this, tablename, sa_customersid, "新建", "新建医院成功").getSQL());
  130. //标记数据来源
  131. DataExtend.markDataFrom(this, "sa_customers", sa_customersid);
  132. } else {
  133. sqlList.add(DataContrlLog.createLog(this, tablename, sa_customersid, "编辑", "编辑医院成功").getSQL());
  134. }
  135. //新增时插入财务信息和联系人信息
  136. if (isAdd) {
  137. //添加地址
  138. Long contactsid = createTableID("sys_enterprise_contacts");
  139. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sys_enterprise_contacts");
  140. insertSQL.setSiteid(siteid);
  141. insertSQL.setValue("contactsid", contactsid);
  142. insertSQL.setValue("sys_enterpriseid", sys_enterpriseid);
  143. insertSQL.setValue("province", province);
  144. insertSQL.setValue("city", city);
  145. insertSQL.setValue("county", county);
  146. insertSQL.setValue("address", address);
  147. insertSQL.setValue("workaddress", 1);
  148. insertSQL.setValue("isdefault", 1);
  149. insertSQL.setValue("isprimary", 1);
  150. sqlList.add(insertSQL.getSQL());
  151. }
  152. content.put("sa_customersid", sa_customersid);
  153. String time = userInfo.getDateTime_Str();
  154. dbConnect.runSqlUpdate(sqlList);
  155. DataTag.deleteTag(this, "sa_customers", sa_customersid, "疑似重复");
  156. Rows teamRows = DataTeam.queryTeamRowWithoutHeadpic(this, "sa_customers", sa_customersid);
  157. Rows leaderRows = teamRows.toRowsMap("isleader").get("1");
  158. Long leaderuserid = leaderRows.isNotEmpty() ? leaderRows.get(0).getLong("userid") : 0;
  159. Long departmentid = getDepartment(leaderuserid).getLong("departmentid");
  160. ArrayList<Long> userids = new ArrayList<>();
  161. userids.add(leaderuserid);
  162. userids.addAll(Proxy.getProxyUserids(this));
  163. userids.add(Hr.getReportUserid(this, leaderuserid));
  164. userids.addAll(Hr.getLeaderRows(this, departmentid).toArrayList("userid", new ArrayList<Long>()));
  165. JSONObject extradata = new JSONObject();
  166. extradata.put("extraUserList", userids);
  167. //客户查重
  168. if (isAdd) {
  169. ArrayList<Long> repeatIds = getRepeatCustomers(this, sa_customersid);
  170. repeatIds.add(120L);
  171. for (Object obj : repeatIds) {
  172. Long id = Long.valueOf(obj.toString());
  173. JSONObject extradata1 = new JSONObject();
  174. extradata1.put("extraUserList", DataTeam.queryTeamRowWithoutHeadpic(this, "sa_customers", id).toArrayList("userid", new ArrayList<>()));
  175. Executor.sendEml_controller(this, "customers_repeat", sa_customersid, extradata1);
  176. }
  177. Executor.sendEml_controller(this, "customers_add_my", sa_customersid, extradata);
  178. } else {
  179. String changedValue = getChangedValue(sa_customersid, time);
  180. if (StringUtils.isEmpty(changedValue)) {
  181. DataContrlLog.createLog(this, "sa_customers", sa_customersid, "编辑", "编辑医院" + enterprisename + "(" + sa_customersid + "),无字段值更新").insert();
  182. } else {
  183. DataContrlLog.createLog(this, "sa_customers", sa_customersid, "编辑", "编辑医院" + enterprisename + "(" + sa_customersid + ");" + changedValue).insert();
  184. Executor.sendEml_controller(this, "customers_update_my", sa_customersid, extradata);
  185. }
  186. }
  187. return selectDetail();
  188. }
  189. public String getChangedValue(Long sa_customersid, String time) throws YosException {
  190. SQLFactory sqlFactory = new SQLFactory(this, "查询客户编辑操作记录值变化");
  191. sqlFactory.addParameter("sa_customersid", sa_customersid);
  192. sqlFactory.addParameter("time", time);
  193. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false));
  194. if (rows.isNotEmpty()) {
  195. return rows.get(0).getString("content");
  196. }
  197. return "";
  198. }
  199. @API(title = "查询业务员负责的经销商", apiversion = R.ID20230214162602.v1.class)
  200. public String getAgentList() throws YosException {
  201. StringBuffer where = new StringBuffer(" 1=1 ");
  202. if (content.containsKey("where")) {
  203. JSONObject whereObject = content.getJSONObject("where");
  204. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  205. where.append(" and(");
  206. where.append("t1.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  207. where.append(")");
  208. }
  209. }
  210. if (content.containsKey("hrid")) {
  211. hrid = content.getLongValue("hrid");
  212. }
  213. Rows rows = dbConnect.runSqlQuery("SELECT userid from sys_hr WHERE hrid=" + hrid + " and siteid='" + siteid + "'");
  214. if (rows.isNotEmpty()) {
  215. userid = rows.get(0).getLong("userid");
  216. }
  217. ArrayList<Long> proxyUserids = getProxyUserids(this, userid, true);
  218. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sys_hr");
  219. querySQL.setSiteid(siteid);
  220. querySQL.setWhere("userid", proxyUserids);
  221. ArrayList<Long> hrids = querySQL.query().toArrayList("hrid", new ArrayList<>());
  222. SQLFactory sqlFactory = new SQLFactory(this, "查询业务员负责的经销商", pageSize, pageNumber, pageSorting);
  223. sqlFactory.addParameter("siteid", siteid);
  224. sqlFactory.addParameter_in("hrid", hrids);
  225. sqlFactory.addParameter_SQL("where", where);
  226. rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  227. return getSucReturnObject().setData(rows).toString();
  228. }
  229. @API(title = "客户管理的合同", apiversion = R.ID20230214163402.v1.class)
  230. @CACHEING
  231. public String list() throws YosException {
  232. String tablename = "sa_contract";
  233. StringBuffer where = new StringBuffer(" 1=1 ");
  234. if (content.containsKey("where")) {
  235. JSONObject whereObject = content.getJSONObject("where");
  236. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  237. where.append(" and(");
  238. where.append("t1.createby like'%").append(whereObject.getString("condition")).append("%' ");
  239. where.append("or t1.title like'%").append(whereObject.getString("condition")).append("%' ");
  240. where.append("or t1.type like'%").append(whereObject.getString("condition")).append("%' ");
  241. where.append("or t1.billno like'%").append(whereObject.getString("condition")).append("%' ");
  242. where.append("or t2.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  243. where.append("or t3.projectnum like'%").append(whereObject.getString("condition")).append("%' ");
  244. where.append("or t3.projectname like'%").append(whereObject.getString("condition")).append("%' ");
  245. where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' ");
  246. where.append(")");
  247. }
  248. }
  249. if (content.containsKey("sys_enterpriseid")) {
  250. sys_enterpriseid = content.getLongValue("sys_enterpriseid");
  251. }
  252. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_contract", "sa_contractid", "createby", "createdate", "changeby",
  253. "changedate", "sa_projectid", "billno", "type", "begdate", "enddate", "sys_enterpriseid", "discountrate", "saler_hrid", "signdate",
  254. "signby", "calculatemodel", "orderratio", "productdiscount", "remarks", "title", "typemx", "ascription_contractid");
  255. querySQL.setTableAlias("t1");
  256. querySQL.addJoinTable(JOINTYPE.left, "sys_enterprise", "t2", "t2.sys_enterpriseid = t1.sys_enterpriseid and t2.siteid = t1.siteid", "enterprisename");
  257. querySQL.addJoinTable(JOINTYPE.left, "sa_project", "t3", "t3.sa_projectid = t1.sa_projectid and t3.siteid = t1.siteid", "projectname", "projectnum");
  258. querySQL.addJoinTable(JOINTYPE.left, "sys_hr", "t4", "t4.hrid = t1.saler_hrid and t4.siteid = t1.siteid");
  259. querySQL.addJoinTable(JOINTYPE.left, "sa_contract", "t5", "t1.siteid = t5.siteid and t1.ascription_contractid = t5.sa_contractid");
  260. querySQL.addJoinTable(JOINTYPE.left, "sys_enterprise", "t6", "t5.siteid = t6.siteid and t5.sys_enterpriseid = t6.sys_enterpriseid");
  261. querySQL.addQueryFields("status", "if(t1.enddate < current_date and t1.status = '审核', '已过期', t1.status)");
  262. querySQL.addQueryFields("salername", "t4.name");
  263. querySQL.addQueryFields("source", "''");
  264. querySQL.addQueryFields("ascription_enterprisename", "t6.enterprisename");
  265. querySQL.setSiteid(siteid);
  266. querySQL.setWhere(where);
  267. querySQL.setWhere("t1.sys_enterpriseid", sys_enterpriseid);
  268. querySQL.setWhere("t1.deleted", 0);
  269. querySQL.setPage(pageSize, pageNumber).setOrderBy(pageSorting);
  270. Rows rows = querySQL.query();
  271. ArrayList<Long> ids = rows.toArrayList("sa_contractid", new ArrayList<>());
  272. //标签
  273. HashMap<Long, ArrayList<String>> tagList = DataTag.queryTag(this, tablename, ids, false);
  274. //系统标签
  275. HashMap<Long, ArrayList<String>> sysTagList = DataTag.queryTag(this, tablename, ids, true);
  276. //查询价格
  277. RowsMap signAmountRowsMap = Contract.getContractSignAmount(this, ids);
  278. for (Row row : rows) {
  279. Long id = row.getLong("sa_contractid");
  280. //非系统标签
  281. row.put("tag", tagList.get(id) != null ? tagList.get(id) : new ArrayList<String>());
  282. //系统标签
  283. row.put("tag_sys", sysTagList.get(id) != null ? sysTagList.get(id) : new ArrayList<String>());
  284. if (signAmountRowsMap.get(id.toString()).isNotEmpty()) {
  285. row.put("signamount", signAmountRowsMap.get(id.toString()).get(0).getBigDecimal("signamount").toPlainString());
  286. } else {
  287. row.put("signamount", 0);
  288. }
  289. }
  290. return getSucReturnObject().setData(rows).toString();
  291. }
  292. @API(title = "数据状态变更", apiversion = R.ID20221012164002.v1.class)
  293. @CACHEING_CLEAN(apiversions = {R.ID20221011133602.class, R.ID20221013102602.class, R.ID20221012164402.class, R.ID20221014164702.class})
  294. public String changeDataStatus() throws YosException {
  295. String tablename = "sa_customers";
  296. JSONArray sa_customersids = content.getJSONArray("sa_customersids");
  297. Long datastatus = content.getLong("datastatus");
  298. if (datastatus == 1) {
  299. for (Object obj : sa_customersids) {
  300. Long sa_customersid = Long.valueOf(obj.toString());
  301. SQLFactory sqlFactory = new SQLFactory(this, "查询客户管理审核通过的合同");
  302. sqlFactory.addParameter("siteid", siteid);
  303. sqlFactory.addParameter("sa_customersid", sa_customersid);
  304. if (dbConnect.runSqlQuery(sqlFactory).isNotEmpty()) {
  305. return getReturnObject().setCode("0057", new String[0]).toString(); //存在审核通过的合同,无法作废
  306. }
  307. }
  308. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_agents", "*");
  309. querySQL.setTableAlias("t1");
  310. querySQL.setSiteid(siteid);
  311. querySQL.addJoinTable(JOINTYPE.inner, "sys_enterprise", "t2", "t1.siteid=t2.siteid and t1.sys_enterpriseid=t2.sys_enterpriseid");
  312. querySQL.addJoinTable(JOINTYPE.inner, "sa_customers", "t3", "t2.siteid=t3.siteid and t2.sys_enterpriseid=t3.sys_enterpriseid");
  313. querySQL.addJoinTable(JOINTYPE.inner, "sys_dataextend", "t4", "t2.siteid=t4.siteid and t2.sys_enterpriseid=t4.ownerid and t4.ownertable='sys_enterprise'");
  314. querySQL.setWhere("(t4.erpagentnum !='' and t4.erpagentnum is not null)");
  315. querySQL.setWhere("t1.status='启用'");
  316. querySQL.setWhere("t3.sa_customersid", sa_customersids.toArray());
  317. querySQL.getSQL();
  318. Rows query = querySQL.query();
  319. if (query.isNotEmpty()) {
  320. return getReturnObject().setCode("0058", new String[0]).toString(); //存在合作伙伴,无法作废
  321. }
  322. }
  323. ArrayList<String> sqlList = new ArrayList<>();
  324. SQLFactory sqlFactory = new SQLFactory(this, "客户数据状态变更");
  325. sqlFactory.addParameter("siteid", siteid);
  326. sqlFactory.addParameter("userid", userid);
  327. sqlFactory.addParameter("username", username);
  328. sqlFactory.addParameter_in("datastatus", datastatus);
  329. sqlFactory.addParameter_in("sa_customersid", sa_customersids.toArray());
  330. sqlList.add(sqlFactory.getSQL());
  331. if (datastatus == 1) {
  332. //新增删除或作废原因
  333. DataExtend.createDeleteReasonSql(this, tablename, sa_customersids.toJavaList(Long.class), content.getStringValue("deletereason"));
  334. }
  335. //操作记录
  336. for (Object obj : sa_customersids) {
  337. Long id = Long.valueOf(obj.toString());
  338. if (datastatus == 0) {
  339. sqlList.add(DataContrlLog.createLog(this, tablename, id, "正常", "恢复正常").getSQL());
  340. }
  341. if (datastatus == 1) {
  342. sqlList.add(DataContrlLog.createLog(this, tablename, id, "作废", "作废客户至回收站").getSQL());
  343. Rows leaderRows = DataTeam.getLeader(this, "sa_customers", id);
  344. ArrayList<String> userids = dbConnect.runSqlQuery("SELECT userid from sys_hr WHERE isleader=1 and siteid='" + siteid + "' and departmentid=" + departmentid).toArrayList("userid");
  345. JSONObject extradata = new JSONObject();
  346. if (leaderRows.isNotEmpty()) {
  347. userids.add(String.valueOf(leaderRows.get(0).getLong("userid")));
  348. }
  349. extradata.put("extraUserList", userids);
  350. Executor.sendEml_controller(this, "customers_invalid", id, extradata);
  351. //客户查重
  352. ArrayList<Long> repeatIds = getRepeatCustomers(this, id);
  353. DataTag.deleteTag(this, "sa_customers", id, "疑似重复");
  354. if (repeatIds.size() == 1) {
  355. Rows tempRows = dbConnect.runSqlQuery("SELECT sa_customersid from sa_customers WHERE sys_enterpriseid=" + repeatIds.get(0) + " and siteid='" + siteid + "'");
  356. if (tempRows.isNotEmpty()) {
  357. DataTag.deleteTag(this, "sa_customers", tempRows.get(0).getLong("sa_customersid"), "疑似重复");
  358. }
  359. }
  360. }
  361. if (datastatus == 2) {
  362. sqlList.add(DataContrlLog.createLog(this, tablename, id, "锁定", "锁定成功").getSQL());
  363. }
  364. }
  365. dbConnect.runSqlUpdate(sqlList);
  366. return getSucReturnObject().toString();
  367. }
  368. @API(title = "交易状态变更", apiversion = R.ID20221012164102.v1.class)
  369. @CACHEING_CLEAN(apiversions = {R.ID20221011133602.class, R.ID20221013102602.class, R.ID20221012164402.class, R.ID20221014164702.class})
  370. public String changeTradingStatus() throws YosException {
  371. String tablename = "sa_customers";
  372. JSONArray sa_customersids = content.getJSONArray("sa_customersids");
  373. String tradingstatus = content.getString("tradingstatus");
  374. ArrayList<String> sqlList = new ArrayList<>();
  375. SQLFactory sqlFactory = new SQLFactory(this, "客户交易状态变更");
  376. sqlFactory.addParameter("siteid", siteid);
  377. sqlFactory.addParameter("userid", userid);
  378. sqlFactory.addParameter("username", username);
  379. sqlFactory.addParameter_in("tradingstatus", tradingstatus);
  380. sqlFactory.addParameter_in("sa_customersid", sa_customersids.toArray());
  381. sqlList.add(sqlFactory.getSQL());
  382. //操作记录
  383. for (Object obj : sa_customersids) {
  384. Long id = Long.valueOf(obj.toString());
  385. sqlList.add(DataContrlLog.createLog(this, tablename, id, "交易状态变更", "交易状态变更为" + tradingstatus).getSQL());
  386. }
  387. dbConnect.runSqlUpdate(sqlList);
  388. return getSucReturnObject().toString();
  389. }
  390. @API(title = "详细", apiversion = R.ID20221012164302.v1.class)
  391. public String selectDetail() throws YosException {
  392. Long sa_customersid = content.getLong("sa_customersid");
  393. SQLFactory sqlFactory = new SQLFactory(this, "客户详情");
  394. sqlFactory.addParameter("siteid", siteid);
  395. sqlFactory.addParameter("sa_customersid", sa_customersid);
  396. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false));
  397. ArrayList<Long> ids = rows.toArrayList("sa_customersid", new ArrayList<>());
  398. RowsMap leaderRows = DataTeam.getLeader(this, "sa_customers", rows.toArrayList("sa_customersid")).toRowsMap("ownerid");
  399. //标签
  400. HashMap<Long, ArrayList<String>> tagList = DataTag.queryTag(this, "sa_customers", ids, false);
  401. //系统标签
  402. HashMap<Long, ArrayList<String>> sysTagList = DataTag.queryTag(this, "sa_customers", ids, true);
  403. for (Row row : rows) {
  404. Long id = row.getLong("sa_customersid");
  405. row.put("leader", leaderRows.get(row.getString("sa_customersid")));
  406. if (leaderRows.get(String.valueOf(id)).isNotEmpty()) {
  407. row.put("depname", leaderRows.get(String.valueOf(id)).get(0).getString("depname"));
  408. }
  409. ArrayList<String> tag = tagList.get(id) != null ? tagList.get(id) : new ArrayList<String>();
  410. ArrayList<String> sys_tag = sysTagList.get(id) != null ? sysTagList.get(id) : new ArrayList<String>();
  411. if (row.getString("agentsstatus").equals("禁用") || row.getString("agentsstatus").equals("作废")) {
  412. tag.removeAll(row.getJSONArray("agentstag").toJavaList(String.class));
  413. sys_tag.removeAll(row.getJSONArray("agentstag").toJavaList(String.class));
  414. }
  415. //非系统标签
  416. row.put("tag", tag);
  417. //系统标签
  418. row.put("tag_sys", sys_tag);
  419. //预估手术总量
  420. row.putIfAbsent("totalop", 0);
  421. //关键人
  422. row.putIfAbsent("keyperson", "");
  423. //签约经销商
  424. row.putIfAbsent("signagent", "");
  425. }
  426. Row row = rows.isNotEmpty() ? rows.get(0) : new Row();
  427. Long sys_enterpriseid = rows.isNotEmpty() ? row.getLong("sys_enterpriseid") : 0;
  428. Rows addressRows = dbConnect.runSqlQuery("SELECT * from sys_enterprise_contacts WHERE isdefault=1 and workaddress=1 and deleted=0 and sys_enterpriseid=" + sys_enterpriseid + " and siteid='" + siteid + "'");
  429. row.put("province", addressRows.isNotEmpty() ? addressRows.get(0).getString("province") : new Row());
  430. row.put("city", addressRows.isNotEmpty() ? addressRows.get(0).getString("city") : new Row());
  431. row.put("county", addressRows.isNotEmpty() ? addressRows.get(0).getString("county") : new Row());
  432. row.put("address", addressRows.isNotEmpty() ? addressRows.get(0).getString("address") : new Row());
  433. return getSucReturnObject().setData(row).toString();
  434. }
  435. @API(title = "医院列表", apiversion = R.ID20221012164402.v1.class)
  436. public String selectList() throws YosException, IOException {
  437. // 1:我负责的;2:我参与的;3:我下属负责的;4:我下属参与的
  438. int type = content.getIntValue("type");
  439. Long deleted = content.getLongValue("deleted");
  440. String tablename = "sa_customers";
  441. StringBuffer where = new StringBuffer(" 1=1 ");
  442. if (content.containsKey("where")) {
  443. JSONObject whereObject = content.getJSONObject("where");
  444. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  445. where.append(" and(");
  446. where.append("t1.billno like'%").append(whereObject.getString("condition")).append("%' ");
  447. where.append("or t1.province like'%").append(whereObject.getString("condition")).append("%' ");
  448. where.append("or t1.city like'%").append(whereObject.getString("condition")).append("%' ");
  449. where.append("or t1.county like'%").append(whereObject.getString("condition")).append("%' ");
  450. where.append("or t1.address like'%").append(whereObject.getString("condition")).append("%' ");
  451. where.append("or t2.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  452. where.append("or t2.abbreviation like'%").append(whereObject.getString("condition")).append("%' ");
  453. where.append("or t6.name like'%").append(whereObject.getString("condition")).append("%' ");
  454. where.append("or t8.depname like'%").append(whereObject.getString("condition")).append("%' ");
  455. where.append(")");
  456. }
  457. if (whereObject.containsKey("startdate") && !"".equals(whereObject.getString("startdate"))) {
  458. where.append(" and(");
  459. where.append("t1.createdate >='").append(whereObject.getString("startdate")).append(" 00:00:00' ");
  460. where.append(")");
  461. }
  462. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  463. where.append(" and(");
  464. where.append("t1.createdate <='").append(whereObject.getString("enddate")).append(" 23:59:59' ");
  465. where.append(")");
  466. }
  467. //开发状态
  468. if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) {
  469. where.append(" and(");
  470. where.append("t1.status ='").append(whereObject.getString("status")).append("' ");
  471. where.append(")");
  472. }
  473. //医院类型
  474. if (whereObject.containsKey("type") && !"".equals(whereObject.getString("type"))) {
  475. where.append(" and(");
  476. where.append("t1.type ='").append(whereObject.getString("type")).append("' ");
  477. where.append(")");
  478. }
  479. //医院等级
  480. if (whereObject.containsKey("grade") && !"".equals(whereObject.getString("grade"))) {
  481. where.append(" and(");
  482. where.append("t1.grade ='").append(whereObject.getString("grade")).append("' ");
  483. where.append(")");
  484. }
  485. //成交状态
  486. if (whereObject.containsKey("tradingstatus") && !"".equals(whereObject.getString("tradingstatus"))) {
  487. where.append(" and(");
  488. where.append("t1.tradingstatus ='").append(whereObject.getString("tradingstatus")).append("' ");
  489. where.append(")");
  490. }
  491. //标签
  492. if (whereObject.containsKey("tag") && !whereObject.getJSONArray("tag").isEmpty()) {
  493. for (Object o : whereObject.getJSONArray("tag")) {
  494. where.append(" and exists(select 1 from sys_datatag WHERE ownertable = 'sa_customers' and siteid='").append(siteid).append("' ");
  495. where.append(" and tag='").append(o).append("' and t1.sa_customersid=ownerid ");
  496. where.append(")");
  497. }
  498. }
  499. //开发阶段
  500. if (whereObject.containsKey("stagename") && !"".equals(whereObject.getString("stagename"))) {
  501. where.append(" and(");
  502. where.append("t1.stagename ='").append(whereObject.getString("stagename")).append("' ");
  503. where.append(")");
  504. }
  505. //营销区域
  506. if (whereObject.containsKey("sa_saleareaid") && !"".equals(whereObject.getString("sa_saleareaid"))) {
  507. Long sa_saleareaid = whereObject.getLong("sa_saleareaid");
  508. ArrayList<Long> sa_saleareaids = SaleArea.getSubSaleAreaIds(this, sa_saleareaid);
  509. sa_saleareaids.add(-1L);
  510. Rows rows = dbConnect.runSqlQuery("SELECT DISTINCT t2.userid from sa_salearea_hr t1 " +
  511. "inner join sys_hr t2 ON t2.hrid=t1.hrid and t2.siteid=t1.siteid " +
  512. "WHERE t1.siteid='" + siteid + "' and t1.sa_saleareaid in(" + StringUtils.join(sa_saleareaids, ",") + ") and t2.userid>0");
  513. HashSet<Long> userIds = new HashSet<>(rows.toArrayList("userid", new ArrayList<>()));
  514. userIds.add(-1L);
  515. where.append(" and(");
  516. where.append(" t6.userid in ").append(userIds.toString().replace("[", "(").replace("]", ")"));
  517. where.append(")");
  518. }
  519. }
  520. if (deleted == 1) {
  521. where.append(" and ( t1.datastatus =1 )");
  522. } else {
  523. where.append(" and ( t1.datastatus in (0,2) )");
  524. }
  525. String where2 = " 1=1 ";
  526. if (deleted == 0) {
  527. if (type == 0) {
  528. where2 = "(" + DataTeam.getDataWhereStr(this, tablename, "t1", 1)
  529. + " or " + DataTeam.getDataWhereStr(this, tablename, "t1", 2) + ")";
  530. } else {
  531. where2 = DataTeam.getDataWhereStr(this, tablename, "t1", type);
  532. }
  533. } else {
  534. where2 = Recycle.getWhereSql(this, tablename, "t1.sa_customersid");
  535. }
  536. boolean isExport = content.getBoolean("isExport");
  537. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "sa_customers", "sa_customersid", "createby", "createdate",
  538. "sys_enterpriseid", "status", "type", "tradingstatus", "datastatus", "billno", "province", "city", "county", "address", "stagename", "grade");
  539. querySQL.setTableAlias("t1");
  540. querySQL.addJoinTable(JOINTYPE.left, "sys_enterprise", "t2", "t2.sys_enterpriseid = t1.sys_enterpriseid AND t2.siteid = t1.siteid", "enterprisename");
  541. querySQL.addJoinTable(JOINTYPE.left, "sa_salearea", "t3", "t3.sa_saleareaid = t1.sa_saleareaid AND t3.siteid = t1.siteid", "areaname");
  542. QuerySQL t5 = SQLFactory.createQuerySQL(this, "sys_datafollowup", "ownerid");
  543. t5.setWhere("ownertable", "sa_customers");
  544. t5.setSiteid(siteid);
  545. t5.addGroupBy("ownerid");
  546. t5.addQueryFields("followdate", "max(createdate)");
  547. t5.setRoleDataLimit(false);
  548. querySQL.addJoinTable(JOINTYPE.left, t5, "t5", "t5.ownerid = t1.sa_customersid", "followdate");
  549. querySQL.addJoinTable(JOINTYPE.left, "sys_datateam", "t6", "t6.ownerid = t1.sa_customersid and t6.siteid = t1.siteid and t6.ownertable = 'sa_customers' and t6.isleader = 1");
  550. querySQL.addJoinTable(JOINTYPE.left, "sys_hr", "t7", "t7.userid = t6.userid and t7.siteid = t6.siteid ");
  551. querySQL.addJoinTable(JOINTYPE.left, "sys_department", "t8", "t8.departmentid = t7.departmentid and t8.siteid = t7.siteid ");
  552. querySQL.setSiteid(siteid);
  553. querySQL.setWhere(where);
  554. querySQL.setWhere(where2);
  555. if (isExport) {
  556. querySQL.setPage(999999, 1);
  557. } else {
  558. querySQL.setPage(pageSize, pageNumber);
  559. }
  560. querySQL.setOrderBy(pageSorting);
  561. querySQL.setRoleDataLimit(false);
  562. querySQL.withDeleteData(true);
  563. Rows rows = querySQL.query();
  564. RowsMap leaderRows = DataTeam.getLeaderWithoutHeadpic(this, "sa_customers", rows.toArrayList("sa_customersid")).toRowsMap("ownerid");
  565. ArrayList<Long> ids = rows.toArrayList("sa_customersid", new ArrayList<>());
  566. //标签
  567. HashMap<Long, ArrayList<String>> tagList = DataTag.queryTag(this, tablename, ids, false);
  568. //系统标签
  569. HashMap<Long, ArrayList<String>> sysTagList = DataTag.queryTag(this, tablename, ids, true);
  570. for (Row row : rows) {
  571. Long id = row.getLong("sa_customersid");
  572. row.put("leader", leaderRows.get(String.valueOf(id)));
  573. if (leaderRows.get(String.valueOf(id)).isNotEmpty()) {
  574. row.put("name", leaderRows.get(String.valueOf(id)).get(0).getString("name"));
  575. row.put("depname", leaderRows.get(String.valueOf(id)).get(0).getString("depname"));
  576. }
  577. ArrayList<String> tag = tagList.get(id) != null ? tagList.get(id) : new ArrayList<String>();
  578. ArrayList<String> sys_tag = sysTagList.get(id) != null ? sysTagList.get(id) : new ArrayList<String>();
  579. if (row.getString("agentsstatus").equals("禁用") || row.getString("agentsstatus").equals("作废")) {
  580. tag.removeAll(row.getJSONArray("agentstag").toJavaList(String.class));
  581. sys_tag.removeAll(row.getJSONArray("agentstag").toJavaList(String.class));
  582. }
  583. //非系统标签
  584. row.put("tag", tag);
  585. //系统标签
  586. row.put("tag_sys", sys_tag);
  587. row.putIfAbsent("followdate", "");
  588. //预估手术总量
  589. row.putIfAbsent("totalop", 0);
  590. //关键人
  591. row.putIfAbsent("keyperson", "");
  592. //签约经销商
  593. row.putIfAbsent("signagent", "");
  594. }
  595. if (isExport) {
  596. ExcelFactory excelFactory = new ExcelFactory("医院管理导出");
  597. XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet("医院管理");
  598. XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook();
  599. ExportExcel.setBatchDetailSheetColumn(sheet);// 设置工作薄列宽
  600. XSSFCellStyle titleCellStyle1 = ExportExcel.createTitleCellStyle(xssfFWorkbook);
  601. XSSFCellStyle titleCellStyle2 = ExportExcel.createBodyCellStyle(xssfFWorkbook);
  602. ExportExcel.download(sheet, titleCellStyle1, titleCellStyle2, rows);// 写入标题
  603. Rows aa = uploadExcelToObs(excelFactory);
  604. String url = "";
  605. if (!aa.isEmpty()) {
  606. url = aa.get(0).getString("url");
  607. }
  608. return getSucReturnObject().setData(url).toString();
  609. }
  610. return getSucReturnObject().setData(rows).toString();
  611. }
  612. @API(title = "公海客户列表", apiversion = R.ID20221014164702.v1.class)
  613. @CACHEING
  614. public String selectPublicList() throws YosException, IOException {
  615. StringBuffer where = new StringBuffer(" 1=1 ");
  616. if (content.containsKey("where")) {
  617. JSONObject whereObject = content.getJSONObject("where");
  618. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  619. where.append(" and(");
  620. where.append("t1.createby like'%").append(whereObject.getString("condition")).append("%' ");
  621. where.append("or t1.sa_customersid like'%").append(whereObject.getString("condition")).append("%' ");
  622. where.append("or t1.status like'%").append(whereObject.getString("condition")).append("%' ");
  623. where.append("or t1.type like'%").append(whereObject.getString("condition")).append("%' ");
  624. where.append("or t1.tradingstatus like'%").append(whereObject.getString("condition")).append("%' ");
  625. where.append("or t1.datastatus like'%").append(whereObject.getString("condition")).append("%' ");
  626. where.append("or t2.poolname like'%").append(whereObject.getString("condition")).append("%' ");
  627. where.append("or t3.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  628. where.append("or t3.abbreviation like'%").append(whereObject.getString("condition")).append("%' ");
  629. where.append("or t3.industry like'%").append(whereObject.getString("condition")).append("%' ");
  630. where.append("or t3.grade like'%").append(whereObject.getString("condition")).append("%' ");
  631. where.append(")");
  632. }
  633. if (whereObject.containsKey("startdate") && !"".equals(whereObject.getString("startdate"))) {
  634. where.append(" and(");
  635. where.append("t1.createdate >='").append(whereObject.getString("startdate")).append("' ");
  636. where.append(")");
  637. }
  638. if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) {
  639. where.append(" and(");
  640. where.append("t1.createdate <='").append(whereObject.getString("enddate")).append("' ");
  641. where.append(")");
  642. }
  643. if (whereObject.containsKey("sa_customerpoolid") && !"".equals(whereObject.getString("sa_customerpoolid"))) {
  644. where.append(" and(");
  645. where.append("t1.sa_customerpoolid ='").append(whereObject.getString("sa_customerpoolid")).append("' ");
  646. where.append(")");
  647. }
  648. }
  649. boolean isExport = content.getBoolean("isExport");
  650. SQLFactory sqlFactory = new SQLFactory(this, "公海客户列表", pageSize, pageNumber, pageSorting);
  651. if (isExport) {
  652. sqlFactory = new SQLFactory(this, "公海客户列表");
  653. }
  654. sqlFactory.addParameter("siteid", siteid);
  655. sqlFactory.addParameter("userid", userid);
  656. sqlFactory.addParameter_SQL("where", where);
  657. String sql = sqlFactory.getSQL();
  658. Rows rows = dbConnect.runSqlQuery(sql);
  659. RowsMap leaderRows = DataTeam.getLeaderWithoutHeadpic(this, "sa_customers", rows.toArrayList("sa_customersid")).toRowsMap("ownerid");
  660. for (Row row : rows) {
  661. row.put("leader", leaderRows.get(row.getString("sa_customersid")));
  662. }
  663. if (isExport) {
  664. //去除不要的导出项
  665. rows.getFieldList().remove("sa_customersid");
  666. rows.getFieldList().remove("parentid");
  667. rows.getFieldList().remove("sys_enterpriseid");
  668. rows.getFieldList().remove("sa_customerpoolid");
  669. for (Row row : rows) {
  670. switch (row.getString("datastatus")) {
  671. case "0":
  672. row.put("datastatusStr", "正常");
  673. case "1":
  674. row.put("datastatusStr", "作废");
  675. case "2":
  676. row.put("datastatusStr", "锁定");
  677. }
  678. }
  679. rows.getFieldList().remove("datastatus");
  680. rows.getFieldList().add("datastatusStr");
  681. rows.getFieldList().remove("province");
  682. rows.getFieldList().remove("city");
  683. rows.getFieldList().remove("county");
  684. rows.getFieldList().remove("address");
  685. rows.getFieldList().remove("taxno");
  686. rows.getFieldList().remove("phonenumber");
  687. rows.getFieldList().remove("contact");
  688. Rows uploadRows = uploadExcelToObs("1111", "客户列表", rows, getTitleMap());
  689. return getSucReturnObject().setData(uploadRows).toString();
  690. }
  691. return getSucReturnObject().setData(rows).toString();
  692. }
  693. //返回导出的标题
  694. public HashMap<String, String> getTitleMap() {
  695. HashMap<String, String> titleMap = new HashMap<>();
  696. titleMap.put("sa_customersid", "客户ID");
  697. titleMap.put("createby", "创建人");
  698. titleMap.put("createdate", "创建时间");
  699. titleMap.put("status", "客户状态");
  700. titleMap.put("type", "客户类型");
  701. titleMap.put("sa_customerpoolid", "公海池ID");
  702. titleMap.put("source", "客户来源");
  703. titleMap.put("tradingstatus", "交易状态");
  704. titleMap.put("datastatusStr", "数据状态");
  705. titleMap.put("poolname", "公海池名称");
  706. titleMap.put("enterprisename", "客户名称");
  707. titleMap.put("abbreviation", "客户简称");
  708. titleMap.put("industry", "所属行业");
  709. titleMap.put("grade", "客户等级");
  710. titleMap.put("superiorenterprisename", "上级企业");
  711. titleMap.put("followdate", "最近跟进时间");
  712. return titleMap;
  713. }
  714. @API(title = "上级客户", apiversion = R.ID20221014101002.v1.class)
  715. @Deprecated
  716. public String selectParentCustomers() throws YosException {
  717. StringBuffer where = new StringBuffer(" 1=1 ");
  718. if (content.containsKey("where")) {
  719. JSONObject whereObject = content.getJSONObject("where");
  720. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  721. where.append(" and(");
  722. where.append("t1.createby like'%").append(whereObject.getString("condition")).append("%' ");
  723. where.append("or t3.enterprisename like'%").append(whereObject.getString("condition")).append("%' ");
  724. where.append(")");
  725. }
  726. }
  727. SQLFactory sqlFactory = new SQLFactory(this, "上级客户列表", pageSize, pageNumber, pageSorting);
  728. sqlFactory.addParameter("siteid", siteid);
  729. sqlFactory.addParameter_SQL("where", where);
  730. sqlFactory.addParameter_SQL("where2", DataTeam.getDataWhereStr(this, "sa_customers", "t1", 6));
  731. String sql = sqlFactory.getSQL();
  732. Rows rows = dbConnect.runSqlQuery(sql);
  733. return getSucReturnObject().setData(rows).toString();
  734. }
  735. @API(title = "转移", apiversion = R.ID20221014102502.v1.class)
  736. @CACHEING_CLEAN(apiversions = {R.ID20221011133602.class, R.ID20221013102602.class, R.ID20221012164402.class, R.ID20221014164702.class})
  737. public String transfer() throws YosException {
  738. String tablename = "sa_customers";
  739. Long sa_customerpoolid = content.getLong("sa_customerpoolid");
  740. JSONArray sa_customersids = content.getJSONArray("sa_customersids");
  741. ArrayList<String> sqlList = new ArrayList<>();
  742. SQLFactory sqlFactory = new SQLFactory(this, "客户转移");
  743. sqlFactory.addParameter("siteid", siteid);
  744. sqlFactory.addParameter_in("sa_customersid", sa_customersids.toArray());
  745. sqlFactory.addParameter("sa_customerpoolid", sa_customerpoolid);
  746. sqlList.add(sqlFactory.getSQL());
  747. //操作记录
  748. for (Object obj : sa_customersids) {
  749. Long id = Long.valueOf(obj.toString());
  750. sqlList.add(DataContrlLog.createLog(this, tablename, id, "转移", "客户转移").getSQL());
  751. }
  752. dbConnect.runSqlUpdate(sqlList);
  753. return getSucReturnObject().setData("").toString();
  754. }
  755. @API(title = "退回", apiversion = R.ID20221014102602.v1.class)
  756. @CACHEING_CLEAN(apiversions = {R.ID20221011133602.class, R.ID20221013102602.class, R.ID20221012164402.class, R.ID20221014164702.class})
  757. public String goback() throws YosException {
  758. String tablename = "sa_customers";
  759. Long sa_customerpoolid = content.getLong("sa_customerpoolid");
  760. Long sa_customersid = content.getLong("sa_customersid");
  761. Rows rows = dbConnect.runSqlQuery("SELECT * from sa_customers WHERE sa_customersid = " + sa_customersid + " and siteid ='" + siteid + "'");
  762. if (rows.get(0).getLong("ispublic") == 0) {
  763. return getReturnObject().setCode("0059", new String[0]).toString(); //非公海客户无法退回
  764. }
  765. if (dbConnect.runSqlQuery("SELECT 1 from sa_customerpool WHERE sa_customerpoolid = " + sa_customerpoolid + " and siteid = '" + siteid + "'").isEmpty()) {
  766. rows = dbConnect.runSqlQuery("SELECT sa_customerpoolid from sa_customerpool WHERE isdefault =1 and siteid = '" + siteid + "'");
  767. if (rows.isNotEmpty()) {
  768. sa_customerpoolid = rows.get(0).getLong("sa_customerpoolid");
  769. } else {
  770. return getReturnObject().setCode("0060", new String[0]).toString(); //没有默认公海池,请先设置默认公海池。
  771. }
  772. }
  773. ArrayList<String> sqlStr = new ArrayList<>();
  774. SQLFactory sqlFactory = new SQLFactory(this, "客户退回");
  775. sqlFactory.addParameter("siteid", siteid);
  776. sqlFactory.addParameter_in("sa_customersid", sa_customersid);
  777. sqlFactory.addParameter("sa_customerpoolid", sa_customerpoolid);
  778. sqlStr.add(sqlFactory.getSQL());
  779. String str = "delete from sys_datateam where siteid='" + siteid + "' and ownertable='sa_customers' and ownerid = " + sa_customersid + " and userid ='" + userid + "'";
  780. sqlStr.add(str);
  781. //操作记录
  782. sqlStr.add(DataContrlLog.createLog(this, tablename, sa_customersid, "退回", "客户退回").getSQL());
  783. ArrayList<Long> userids = DataTeam.queryTeamRowWithoutHeadpic(this, "sa_customers", sa_customersid).toArrayList("userid", new ArrayList<>());
  784. JSONObject extradata = new JSONObject();
  785. extradata.put("extraUserList", userids);
  786. Executor.sendEml_controller(this, "customers_return", sa_customersid, extradata);
  787. dbConnect.runSqlUpdate(sqlStr);
  788. DataTeam.updatephonebookteam(this, "sa_customers", sa_customersid);
  789. return getSucReturnObject().toString();
  790. }
  791. @API(title = "领取", apiversion = R.ID20221014160202.v1.class)
  792. @CACHEING_CLEAN(apiversions = {R.ID20221011133602.class, R.ID20221013102602.class, R.ID20221012164402.class, R.ID20221014164702.class})
  793. public String receive() throws YosException {
  794. String tablename = "sa_customers";
  795. Long sa_customersid = content.getLong("sa_customersid");
  796. if (DataTeam.getLeader(this, "sa_customers", sa_customersid).isNotEmpty()) {
  797. return getReturnObject().setCode("0061", new String[0]).toString(); //当前客户已领取
  798. }
  799. String sql = "SELECT * from sa_customerpool WHERE sa_customerpoolid in (SELECT sa_customerpoolid_source from sa_customers WHERE sa_customersid =" + sa_customersid + " and siteid='" + siteid + "') and siteid='" + siteid + "'";
  800. Rows customerpoolRows = dbConnect.runSqlQuery(sql);
  801. if (customerpoolRows.isNotEmpty()) {
  802. Long sa_customerpoolid = customerpoolRows.get(0).getLong("sa_customerpoolid");
  803. ArrayList<Long> sa_saleareaids = userInfo.getSaleAreaIds();
  804. sa_saleareaids.add(0L);
  805. // String tempSql = "SELECT 1 from sa_customerpool_auth WHERE siteid='" + siteid + "' and (hrid=" + hrid + " and departmentid = " + departmentid + " and sa_agentsid=" + sa_agentsid + " or sa_saleareaid in " + sa_saleareaids + ") and sa_customerpoolid = " + sa_customerpoolid;
  806. // tempSql = tempSql.replace("[", "(").replace("]", ")");
  807. // if (dbConnect.runSqlQuery(tempSql).isEmpty()) {
  808. // return getErrReturnObject().setErrMsg("未授权,无法领取").toString();
  809. // }
  810. boolean freeget = customerpoolRows.get(0).getBoolean("freeget");
  811. if (!freeget) {
  812. return getReturnObject().setCode("0062", new String[0]).toString(); //数据所属公海池不支持自由领取!
  813. }
  814. }
  815. ArrayList<String> sqlStr = new ArrayList<>();
  816. SQLFactory sqlFactory = new SQLFactory(this, "客户领取");
  817. sqlFactory.addParameter("siteid", siteid);
  818. sqlFactory.addParameter_in("sa_customersid", sa_customersid);
  819. sqlStr.add(sqlFactory.getSQL());
  820. //数据团队新增
  821. sqlStr.addAll(DataTeam.createTeamSQL(this, "sa_customers", sa_customersid, userid));
  822. sqlStr.addAll(DataTeam.createTeamMemberSql(this, "sa_customers", sa_customersid, userid));
  823. //操作记录
  824. sqlStr.add(DataContrlLog.createLog(this, tablename, sa_customersid, "领取", "领取公海客户" + getCustomerEnterprisename(sa_customersid)).getSQL());
  825. Rows contactsRows = dbConnect.runSqlQuery("select sys_phonebookid from sa_customers t1 inner join sys_enterprise_contacts t2 on t1.siteid=t2.siteid and t1.sys_enterpriseid=t2.sys_enterpriseid where t1.siteid='" + siteid + "' and t1.sa_customersid=" + sa_customersid + " and workaddress=0");
  826. for (Row row : contactsRows) {
  827. Long sys_phonebookid = row.getLong("sys_phonebookid");
  828. sqlStr.addAll(DataTeam.createTeamSQL(this, "sys_phonebook", sys_phonebookid, userid));
  829. sqlStr.addAll(DataTeam.createTeamMemberSql(this, "sys_phonebook", sys_phonebookid, userid));
  830. }
  831. dbConnect.runSqlUpdate(sqlStr);
  832. ArrayList<Long> userids = DataTeam.queryTeamRowWithoutHeadpic(this, "sa_customers", sa_customersid).toArrayList("userid", new ArrayList<>());
  833. JSONObject extradata = new JSONObject();
  834. extradata.put("extraUserList", userids);
  835. Executor.sendEml_controller(this, "customers_receive", sa_customersid, extradata);
  836. return getSucReturnObject().toString();
  837. }
  838. /**
  839. * 获取客户名称
  840. *
  841. * @param sa_customersid
  842. * @return
  843. * @throws YosException
  844. */
  845. public String getCustomerEnterprisename(Long sa_customersid) throws YosException {
  846. String enterprisename = "";
  847. Rows rows = dbConnect.runSqlQuery("SELECT enterprisename FROM sys_enterprise WHERE sys_enterpriseid in (SELECT sys_enterpriseid from sa_customers WHERE sa_customersid = " + sa_customersid + " ) and siteid = '" + siteid + "'");
  848. if (rows.isNotEmpty()) {
  849. enterprisename = rows.get(0).getString("enterprisename");
  850. }
  851. return enterprisename;
  852. }
  853. @API(title = "客户系统信息", apiversion = R.ID20221019091602.v1.class)
  854. public String getCustomerSysInfo() throws YosException {
  855. Long sa_customersid = content.getLong("sa_customersid");
  856. Rows infoRows = dbConnect.runSqlQuery("SELECT createby,createdate,changeby,changedate,tradingstatus,datastatus FROM sa_customers WHERE sa_customersid = " + sa_customersid + " AND siteid = '" + siteid + "'");
  857. Row row = new Row();
  858. if (infoRows.isNotEmpty()) {
  859. row = infoRows.get(0);
  860. }
  861. row.put("changecount", 0);
  862. row.put("returnreson", "");
  863. row.put("percentage", "100%");
  864. SQLFactory sqlFactory = new SQLFactory(this, "查询客户最近信息");
  865. sqlFactory.addParameter("siteid", siteid);
  866. sqlFactory.addParameter("ownerid", sa_customersid);
  867. infoRows = dbConnect.runSqlQuery(sqlFactory);
  868. if (infoRows.isNotEmpty()) {
  869. row.putAll(infoRows.get(0));
  870. } else {
  871. row.put("followby", "");
  872. row.put("followdate", "");
  873. }
  874. return getSucReturnObject().setData(row).toString();
  875. }
  876. @API(title = "获取可更换负责人列表", apiversion = R.ID20221020083902.v1.class)
  877. public String getChangeUserList() throws YosException {
  878. StringBuffer where = new StringBuffer(" 1=1 ");
  879. if (content.containsKey("where")) {
  880. JSONObject whereObject = content.getJSONObject("where");
  881. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  882. where.append(" and(");
  883. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  884. where.append(")");
  885. }
  886. }
  887. SQLFactory sqlFactory = new SQLFactory(this, "获取可更换负责人列表", pageSize, pageNumber, pageSorting);
  888. sqlFactory.addParameter("siteid", siteid);
  889. sqlFactory.addParameter_SQL("where", where);
  890. String sql = sqlFactory.getSQL();
  891. Rows rows = dbConnect.runSqlQuery(sql);
  892. return getSucReturnObject().setData(rows).toString();
  893. }
  894. @API(title = "我的公海池", apiversion = R.ID20221206195102.v1.class)
  895. public String myPoolList() throws YosException {
  896. StringBuffer where = new StringBuffer(" 1=1 ");
  897. if (content.containsKey("where")) {
  898. JSONObject whereObject = content.getJSONObject("where");
  899. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  900. where.append(" and(");
  901. where.append("t1.name like'%").append(whereObject.getString("condition")).append("%' ");
  902. where.append(")");
  903. }
  904. }
  905. SQLFactory sqlFactory = new SQLFactory(this, "我的公海池", pageSize, pageNumber, pageSorting);
  906. sqlFactory.addParameter("siteid", siteid);
  907. sqlFactory.addParameter("hrid", hrid);
  908. sqlFactory.addParameter("sa_agentsid", userInfo.getAgentId());
  909. sqlFactory.addParameter_in("sa_saleareaid", userInfo.getSaleAreaIds());
  910. sqlFactory.addParameter("departmentid", departmentid);
  911. sqlFactory.addParameter_SQL("where", where);
  912. String sql = sqlFactory.getSQL();
  913. Rows rows = dbConnect.runSqlQuery(sql);
  914. return getSucReturnObject().setData(rows).toString();
  915. }
  916. }