customer.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package common.crm.bean;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import common.YosException;
  4. import common.crm.bean.core.CrmBase;
  5. import common.data.*;
  6. import org.apache.logging.log4j.Logger;
  7. import java.util.ArrayList;
  8. public class customer extends CrmBase {
  9. public customer(Logger logger) {
  10. super(logger);
  11. }
  12. @Override
  13. public void autoAction() throws YosException {
  14. Rows rows = crmDBConnect.runSqlQuery("select t1.id as syncid,t1.ownerid,t2.* from (select max(id) id,ownerid from e_order_sync where ownertable='st_customer' group by ownerid) t1 left join st_customer t2 on t1.ownerid=t2.cid");
  15. logger.info("监测到crm有{}个客户档案待同步", rows.size());
  16. ArrayList<String> agentnums = rows.toArrayList("fagent_num_created");
  17. agentnums.addAll(rows.toArrayList("fagent_num"));
  18. QuerySQL agentQuery = SQLFactory.createQuerySQL(dbConnect, "sa_agents", "sa_agentsid", "sys_enterpriseid", "agentnum");
  19. agentQuery.setWhere("siteid", "MD");
  20. agentQuery.setWhere("agentnum", agentnums);
  21. RowsMap agentRowsMap = agentQuery.query().toRowsMap("agentnum");
  22. RowsMap regionsMap = dbConnect.runSqlQuery("select code,name from t_regions where countryid=1").toRowsMap("code");
  23. int sucesscount = 0;
  24. for (Row row : rows) {
  25. try {
  26. JSONObject jsonObject = row.toJsonObject();
  27. long syncid = jsonObject.getLong("syncid");
  28. long ownerid = jsonObject.getLong("ownerid");
  29. long cid = jsonObject.getLong("cid");
  30. if (cid > 0) {
  31. String createby = jsonObject.getString("createby");
  32. String createtime = jsonObject.getString("createtime");
  33. String changeby = jsonObject.getString("changeby");
  34. String changetime = jsonObject.getString("changetime");
  35. String customer_status = jsonObject.getString("customer_status");
  36. String fchannel = jsonObject.getString("fchannel");
  37. String fname = jsonObject.getString("fname");
  38. String fphone = jsonObject.getString("fphone");
  39. String fcommunity = jsonObject.getString("fcommunity");
  40. String fcustom_num = jsonObject.getString("fcustom_num");
  41. String fagent_num_created = jsonObject.getString("fagent_num_created");
  42. String fagent_num = jsonObject.getString("fagent_num");
  43. String fprovince = jsonObject.getString("fprovince");
  44. String fcity = jsonObject.getString("fcity");
  45. String fcounty = jsonObject.getString("fcounty");
  46. if (agentRowsMap.containsKey(fagent_num_created)) {
  47. if (dbConnect.runSqlQuery("select * from sa_customers where sa_customersid=" + cid).isEmpty()) {
  48. InsertSQL insertSQL = SQLFactory.createInsertSQL(dbConnect, "sa_customers");
  49. insertSQL.setValue("siteid", "MD");
  50. insertSQL.setValue("sa_customersid", cid);
  51. insertSQL.setValue("createby", createby);
  52. insertSQL.setValue("createdate", createtime);
  53. insertSQL.setValue("changeby", changeby);
  54. insertSQL.setValue("changedate", changetime);
  55. insertSQL.setValue("sys_enterpriseid", agentRowsMap.get(fagent_num_created).get(0).getLong("sys_enterpriseid"));
  56. insertSQL.setValue("sa_agentsid", agentRowsMap.get(fagent_num_created).get(0).getLong("sa_agentsid"));
  57. if (agentRowsMap.containsKey(fagent_num)) {
  58. insertSQL.setValue("sa_agentsid_to", agentRowsMap.get(fagent_num).get(0).getLong("sa_agentsid"));
  59. }
  60. insertSQL.setValue("status", customer_status);
  61. insertSQL.setValue("source", fchannel);
  62. insertSQL.setValue("tradingstatus", customer_status.equals("正式客户") ? "已成交" : "未成交");
  63. insertSQL.setValue("datastatus", 0);
  64. insertSQL.setValue("ispublic", 0);
  65. insertSQL.setValue("name", fname);
  66. insertSQL.setValue("phonenumber", fphone);
  67. if (regionsMap.containsKey(fprovince)) {
  68. insertSQL.setValue("province", regionsMap.get(fprovince).get(0).getString("name"));
  69. }
  70. if (regionsMap.containsKey(fcity)) {
  71. insertSQL.setValue("city", regionsMap.get(fcity).get(0).getString("name"));
  72. }
  73. if (regionsMap.containsKey(fcounty)) {
  74. insertSQL.setValue("county", regionsMap.get(fcounty).get(0).getString("name"));
  75. }
  76. insertSQL.setValue("address", fcommunity);
  77. insertSQL.setValue("custnum", fcustom_num);
  78. insertSQL.insert();
  79. } else {
  80. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(dbConnect, "sa_customers");
  81. updateSQL.setValue("changeby", changeby);
  82. updateSQL.setValue("changedate", changetime);
  83. if (agentRowsMap.containsKey(fagent_num)) {
  84. updateSQL.setValue("sa_agentsid_to", agentRowsMap.get(fagent_num).get(0).getLong("sa_agentsid"));
  85. }
  86. updateSQL.setValue("status", customer_status);
  87. updateSQL.setValue("source", fchannel);
  88. updateSQL.setValue("tradingstatus", customer_status.equals("正式客户") ? "已成交" : "未成交");
  89. updateSQL.setValue("name", fname);
  90. updateSQL.setValue("phonenumber", fphone);
  91. if (regionsMap.containsKey(fprovince)) {
  92. updateSQL.setValue("province", regionsMap.get(fprovince).get(0).getString("name"));
  93. }
  94. if (regionsMap.containsKey(fcity)) {
  95. updateSQL.setValue("city", regionsMap.get(fcity).get(0).getString("name"));
  96. }
  97. if (regionsMap.containsKey(fcounty)) {
  98. updateSQL.setValue("county", regionsMap.get(fcounty).get(0).getString("name"));
  99. }
  100. updateSQL.setValue("address", fcommunity);
  101. updateSQL.setValue("custnum", fcustom_num);
  102. updateSQL.setWhere("siteid", "MD");
  103. updateSQL.setWhere("sa_customersid", cid);
  104. updateSQL.update();
  105. }
  106. }
  107. }
  108. crmDBConnect.runSqlUpdate("delete from e_order_sync where ownertable='st_customer' and ownerid=" + ownerid + " and id<=" + syncid);
  109. sucesscount++;
  110. } catch (Exception e) {
  111. logger.error(e.getMessage(), e);
  112. }
  113. }
  114. logger.info("成功处理{}个客户档案同步", sucesscount);
  115. }
  116. }