|
|
@@ -0,0 +1,199 @@
|
|
|
+package common.crm.bean;
|
|
|
+
|
|
|
+import beans.countryarea.CountryArea;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import common.YosException;
|
|
|
+import common.YosLogger;
|
|
|
+import common.data.QuerySQL;
|
|
|
+import common.data.Row;
|
|
|
+import common.data.Rows;
|
|
|
+import common.data.SQLFactory;
|
|
|
+import common.data.db.DBConnect;
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
+import common.crm.bean.core.CrmBase;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
+public class agent extends CrmBase {
|
|
|
+
|
|
|
+
|
|
|
+ public agent(Logger logger) {
|
|
|
+ super(logger);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void autoAction() throws YosException {
|
|
|
+ ArrayList<crm_datasync> datasyncList = get("经销商档案", "sa_agents", 100);
|
|
|
+ for (crm_datasync datasync : datasyncList) {
|
|
|
+ QuerySQL querySQL = SQLFactory.createQuerySQL(dbConnect, "sa_agents", "agentnum").setTableAlias("t1");
|
|
|
+ querySQL.setWhere("t1.sa_agentsid", datasync.ownerid);
|
|
|
+ Rows agentRows = querySQL.query();
|
|
|
+ if (agentRows.isNotEmpty() && !agentRows.get(0).getString("agentnum").isEmpty() && agentRows.get(0).getString("status").equals("启用")) {
|
|
|
+ String agentnum = agentRows.get(0).getString("agentnum");
|
|
|
+ if (oneAction(agentnum)) {
|
|
|
+ datasync.suc();
|
|
|
+ } else {
|
|
|
+ datasync.fail();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ datasync.del();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean oneAction(String agentnum) {
|
|
|
+ try {
|
|
|
+ String crm_agentsResult = getCRM("/ext/bragent/getAnyAgents?fbrandId=" + fbrandId + "&fagentNums=" + agentnum);
|
|
|
+ JSONObject crm_agentsResultObject = JSONObject.parseObject(crm_agentsResult);
|
|
|
+ boolean isNew = crm_agentsResultObject.getJSONArray("data").isEmpty();
|
|
|
+ JSONArray crm_agentsResultObjectDataArray = crm_agentsResultObject.getJSONArray("data");
|
|
|
+ JSONObject crm_agentObject = !crm_agentsResultObjectDataArray.isEmpty() ? crm_agentsResultObjectDataArray.getJSONObject(0) : new JSONObject();
|
|
|
+
|
|
|
+ JSONObject agentObject = getAgent(isNew, agentnum, crm_agentObject);
|
|
|
+ if (!agentObject.isEmpty()) {
|
|
|
+ String result = postCRM(agentObject.toJSONString(), isNew ? "/ext/bragent/addAgent" : "/ext/bragent/updateAgent");
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
+ if (jsonObject.getInteger("code") == 1) {
|
|
|
+ logger.info("经销商档案同步成功! request:" + agentObject + " response:" + result);
|
|
|
+ logger.info("尝试更新CRM!");
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ logger.error("经销商档案同步失败! request:" + agentObject + " response:" + result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject getAgent(boolean isNew, String agentnum, JSONObject crm_agentObject) throws YosException {
|
|
|
+ QuerySQL querySQL = SQLFactory.createQuerySQL(new DBConnect(), "sa_agents");
|
|
|
+ querySQL.setSiteid("MD").setWhere("agentnum", agentnum);
|
|
|
+ Rows agentRows = querySQL.query();
|
|
|
+ if (agentRows.isEmpty()) {
|
|
|
+ return new JSONObject();
|
|
|
+ }
|
|
|
+ Row agentRow = agentRows.get(0);
|
|
|
+ Row enterpriseRow = agentRow.getRow("sys_enterprise", "sys_enterpriseid=:sys_enterpriseid");
|
|
|
+ if (isNew) {
|
|
|
+ crm_agentObject.put("fbrandId", fbrandId);
|
|
|
+ crm_agentObject.put("fagentNum", agentnum);
|
|
|
+
|
|
|
+ switch (agentRow.getString("type")) {
|
|
|
+ case "普通": {
|
|
|
+ crm_agentObject.put("ftype", "1");
|
|
|
+ crm_agentObject.put("fisonline", false);
|
|
|
+ crm_agentObject.put("fisonlineJoin", false);
|
|
|
+ crm_agentObject.put("fisonlineRemodel", false);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case "电商": {
|
|
|
+ crm_agentObject.put("ftype", "2");
|
|
|
+ crm_agentObject.put("fisonline", true);
|
|
|
+ crm_agentObject.put("fisonlineJoin", false);
|
|
|
+ crm_agentObject.put("fisonlineRemodel", false);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case "网销": {
|
|
|
+ crm_agentObject.put("ftype", "1");
|
|
|
+ crm_agentObject.put("fisonline", true);
|
|
|
+ crm_agentObject.put("fisonlineJoin", true);
|
|
|
+ crm_agentObject.put("fisonlineRemodel", false);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case "家装": {
|
|
|
+ crm_agentObject.put("ftype", "1");
|
|
|
+ crm_agentObject.put("fisonline", true);
|
|
|
+ crm_agentObject.put("fisonlineJoin", false);
|
|
|
+ crm_agentObject.put("fisonlineRemodel", true);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (enterpriseRow.getString("phonenumber").isEmpty()) {
|
|
|
+ crm_agentObject.put("principalPhone", "123456");
|
|
|
+ } else {
|
|
|
+ crm_agentObject.put("principalPhone", enterpriseRow.getString("phonenumber"));
|
|
|
+ }
|
|
|
+ crm_agentObject.put("faftersaleNum", agentnum);//faftersaleNum 售后服务经销商编码.统一为自己;
|
|
|
+ crm_agentObject.put("usernameOfBoss", agentnum);
|
|
|
+ crm_agentObject.put("passwordOfBoss", "w123456");//e-订单密码不可逆
|
|
|
+ }
|
|
|
+ crm_agentObject.put("fagentName", enterpriseRow.getString("enterprisename"));
|
|
|
+ crm_agentObject.put("fagentShortName", enterpriseRow.getString("abbreviation"));
|
|
|
+ crm_agentObject.put("principalName", enterpriseRow.getString("contact").isEmpty() ? "__" : enterpriseRow.getString("contact"));
|
|
|
+ crm_agentObject.put("principalUname", agentnum);
|
|
|
+
|
|
|
+// crm_agentObject.put("fsalePhone", "");//售前电话
|
|
|
+// crm_agentObject.put("faftersalePhone", "");//售后电话
|
|
|
+ crm_agentObject.put("fisused", agentRow.getString("status").equals("启用"));
|
|
|
+ Rows sysEnterpriseTradefieldRows = agentRow.getRows("sys_enterprise_tradefield", "sa_agentsid=:sa_agentsid");
|
|
|
+ if (sysEnterpriseTradefieldRows.isNotEmpty() && sysEnterpriseTradefieldRows.get(0).getLong("sa_saleareaid") > 0) {
|
|
|
+ crm_agentObject.put("fareaNum", sysEnterpriseTradefieldRows.get(0).getRow("sa_salearea", "sa_saleareaid=:sa_saleareaid").getString("areanum"));
|
|
|
+ }
|
|
|
+
|
|
|
+ CountryArea countryArea = CountryArea.getByName(enterpriseRow.getString("province"), enterpriseRow.getString("city"), enterpriseRow.getString("county"));
|
|
|
+ if (countryArea != null) {
|
|
|
+ crm_agentObject.put("fprovince", countryArea.provincenum);
|
|
|
+ crm_agentObject.put("fcity", countryArea.citynum);
|
|
|
+ crm_agentObject.put("fcounty", countryArea.countynum);
|
|
|
+ }
|
|
|
+ if (!enterpriseRow.getString("address").isEmpty()) {
|
|
|
+ crm_agentObject.put("faddress", enterpriseRow.getString("address"));
|
|
|
+ }
|
|
|
+
|
|
|
+ if (enterpriseRow.getBoolean("saleclassauth")) {
|
|
|
+ crm_agentObject.put("fagentauthors", true);
|
|
|
+ crm_agentObject.put("brAgentAuthorsList", new JSONArray());
|
|
|
+ } else {
|
|
|
+ crm_agentObject.put("fagentauthors", false);
|
|
|
+ JSONArray authArray = new JSONArray();
|
|
|
+ QuerySQL saleclassQuery = SQLFactory.createQuerySQL(dbConnect, "sys_enterprise_saleclass", "sys_enterpriseid").setTableAlias("t1");
|
|
|
+ saleclassQuery.addJoinTable(JOINTYPE.left, "plm_item", "t2", "t2.itemid = t1.itemid and t2.siteid = t1.siteid", "itemno");
|
|
|
+ saleclassQuery.addJoinTable(JOINTYPE.left, "plm_itemclass", "t3", "t1.itemclassid = t3.itemclassid and t1.siteid = t3.siteid", "itemclassnum");
|
|
|
+ saleclassQuery.setSiteid("MD").setWhere("sys_enterpriseid", enterpriseRow.getLong("sys_enterpriseid"));
|
|
|
+ Rows authRows = saleclassQuery.query();
|
|
|
+ for (Row authRow : authRows) {
|
|
|
+ JSONObject authObject = new JSONObject();
|
|
|
+ authObject.put("brandId", fbrandId);
|
|
|
+ authObject.put("fagentNum", agentnum);
|
|
|
+ if (!authRow.getString("itemclassnum").isEmpty()) {
|
|
|
+ authObject.put("fsaleclsNum", authRow.getString("itemclassnum"));
|
|
|
+ }
|
|
|
+ if (!authRow.getString("itemno").isEmpty()) {
|
|
|
+ authObject.put("fsaleprodNum", authRow.getString("itemno"));
|
|
|
+ }
|
|
|
+ authArray.add(authObject);
|
|
|
+ }
|
|
|
+
|
|
|
+ crm_agentObject.put("brAgentAuthorsList", authArray);
|
|
|
+ }
|
|
|
+
|
|
|
+ // crm_agentObject.put("ftopareaNum","");//区域大类
|
|
|
+
|
|
|
+ return crm_agentObject;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Rows getUndeliqty(String agentnum) {
|
|
|
+ try {
|
|
|
+ return dbConnect.runSqlQuery("select t2.itemno as fitemno,sum(t2.undeliqty) as fundeliqty from sa_order t1 " +
|
|
|
+ "inner join sa_orderitems t2 on t1.sa_orderid=t2.sa_orderid and t2.isclose=0 " +
|
|
|
+ "inner join sa_agents t3 on t1.sys_enterpriseid=t3.sys_enterpriseid and t3.agentnum='" + agentnum + "' " +
|
|
|
+ "where t1.status in ('审核','提交') " +
|
|
|
+ "group by t2.itemno having sum(t2.undeliqty)>0");
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("CRM查询订单未发货数量失败");
|
|
|
+ }
|
|
|
+ return new Rows();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws YosException {
|
|
|
+ agent item = new agent(YosLogger.logger);
|
|
|
+ boolean b = item.oneAction("010001");
|
|
|
+ System.err.println(b);
|
|
|
+ }
|
|
|
+}
|