CustomerTask.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package com.uas.eis.task;
  2. import com.uas.eis.beans.result.BaseResult;
  3. import com.uas.eis.beans.result.CrmQueryResult;
  4. import com.uas.eis.beans.result.Customer;
  5. import com.uas.eis.dao.BaseDao;
  6. import com.uas.eis.dao.SqlRowList;
  7. import com.uas.eis.manager.CommonManager;
  8. import com.uas.eis.manager.CustomerManager;
  9. import com.uas.eis.utils.Constant;
  10. import com.uas.eis.utils.DateUtil;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.scheduling.annotation.EnableAsync;
  15. import org.springframework.scheduling.annotation.EnableScheduling;
  16. import org.springframework.scheduling.annotation.Scheduled;
  17. import org.springframework.stereotype.Component;
  18. import java.math.BigDecimal;
  19. import java.text.SimpleDateFormat;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. @Component
  25. @EnableAsync
  26. @EnableScheduling
  27. public class CustomerTask {
  28. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  29. @Autowired
  30. private BaseDao baseDao;
  31. @Autowired
  32. private CustomerManager customerManager;
  33. @Autowired
  34. private CommonManager commonManager;
  35. @Scheduled(cron = "0 0/15 * * * ?")
  36. public void syncCustomers() {
  37. try {
  38. List<String> field_values = new ArrayList<>();
  39. SqlRowList rs = baseDao.queryForRowSet("select ml_syncTime from (select * from FXXKDockingErrorlog where nvl(ML_TYPE,' ')='客户资料下载' and ML_SYNCTIME is not null order by ml_date desc) where rownum=1");
  40. if(rs.next()){
  41. String ml_syncTime = rs.getGeneralString("ml_syncTime");
  42. field_values.add(ml_syncTime);
  43. CrmQueryResult crmQueryResult = customerManager.queryCustomer(field_values);
  44. int errorCode = crmQueryResult.getErrorCode();
  45. if(errorCode == 0){
  46. Map<String, Object> data = crmQueryResult.getData();
  47. List<Map<String, Object>> dataList = (List<Map<String,Object>>) data.get("dataList");
  48. //m:成功条数 n:失败条数
  49. int m=0,n=0;
  50. if(dataList.size()>0){
  51. //最后(即最近)一次同步时间
  52. Long create_time_Last = Long.parseLong(new BigDecimal(dataList.get(dataList.size()-1).get("create_time").toString()).toPlainString());
  53. for (int i = 0; i < dataList.size(); i++) {
  54. Object _id = dataList.get(i).get("_id");
  55. Object name = dataList.get(i).get("name");
  56. //判断负责人是否存在于人员资料名称中 CRM负责人名称字段:owner__r
  57. Map<String,Object> owner__r = (Map<String,Object>) dataList.get(i).get("owner__r");
  58. //负责人姓名
  59. String owner = owner__r==null ? "" : owner__r.get("name").toString();
  60. SimpleDateFormat sdf = new SimpleDateFormat(Constant.YMD_HMS);
  61. if(baseDao.checkIf("PreCustomer", "cu_name='" + name + "'")){
  62. baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'客户名称已存在','客户资料下载','"+name+"')");
  63. logger.info("异常信息:客户名称已存在");
  64. n++;
  65. continue;
  66. }
  67. try {
  68. Object em_code = baseDao.getFieldDataByCondition("employee", "max(em_code) em_code", "em_name='" + dataList.get(i).get("field_w9e4q__c") + "'");
  69. Object cu_sellercode = baseDao.getFieldDataByCondition("employee", "max(em_code) em_code", "em_name='" + owner + "'");
  70. Long create_time = Long.parseLong(new BigDecimal(dataList.get(i).get("create_time").toString()).toPlainString());
  71. Long last_modified_time = Long.parseLong(new BigDecimal(dataList.get(i).get("last_modified_time").toString()).toPlainString());
  72. String cuKind = getDisplayField(dataList, i, "account_type", "客户类型");
  73. String wbsk_user = getDisplayField(dataList, i, "UDSSel1__c", "收款方式");//外部收款方式
  74. Object[] pa_data = baseDao.getFieldsDataByCondition("Payments", new String[]{"pa_id","pa_code"}, "nvl(pa_class,' ')='收款方式' and pa_name='" + wbsk_user + "'");//获取收款方式编号
  75. String paID = (pa_data == null || pa_data[0] == null) ? "" : pa_data[0].toString();
  76. String paCode = (pa_data == null || pa_data[1] == null) ? "" : pa_data[1].toString();
  77. String cu_level = getDisplayField(dataList, i, "account_level", "客户级别");
  78. String cu_turnSAP = getDisplayField(dataList, i, "field_RYzRf__c", "是否转正式客户在SAP内建档");
  79. String cu_currency = getDisplayField(dataList, i, "field_e2HeP__c", "货币");
  80. String cu_recordType = getDisplayField(dataList, i, "record_type", "业务类型");
  81. String cu_industry = getDisplayField(dataList, i, "field_00Ijp__c", "所属行业");
  82. String cu_taxrate = getDisplayField(dataList, i, "field_nhi2w__c", "税率");
  83. String cu_code = baseDao.sGetMaxNumber("PreCustomer", 2);
  84. //field_giBJk__c:收货人 UDSText5__c:送货地址 UDSText1__c:法定代表人 UDSText2__c:注册资金
  85. String sql="insert into PreCustomer(cu_id,cu_code,cu_name,cu_shortname,cu_add2,cu_sellercode,cu_sellername,cu_servicecode,cu_servicename,cu_kind," +
  86. "cu_contact,cu_add1,cu_lawman,cu_regamount,cu_rate,cu_wbsk_user," +
  87. //"cu_paymentid,cu_paymentscode,cu_payments," +
  88. "cu_level,cu_industry,cu_mainbusiness,cu_turnSAP,cu_currency,cu_taxrate," +
  89. "cu_dealstatus,cu_Shipmentamount,cu_receivable,cu_businessRegister,cu_recordType," +
  90. "cu_mobile,cu_email,cu_recordman,cu_recorddate,cu_lastdate,cu_auditstatus,cu_auditstatuscode,cu_remark,CU_FXXKID)" +
  91. "values(PRECUSTOMER_SEQ.nextval,'"+cu_code+"','"+name+"','"+dataList.get(i).get("UDSText3__c")+"','"+ dataList.get(i).get("address")+"'," +
  92. "'"+(cu_sellercode==null?"":cu_sellercode)+"','"+owner+"','"+em_code+"','"+dataList.get(i).get("field_w9e4q__c")+"','"+cuKind+"'," +
  93. "'"+getNotNull(dataList,i,"field_giBJk__c")+"','"+getNotNull(dataList,i,"UDSText5__c")+"'," +
  94. "'"+getNotNull(dataList,i,"UDSText1__c")+"','"+getNotNull(dataList,i,"UDSText2__c")+"','" +
  95. getNotNull(dataList,i,"field_1l3xo__c")+"','"+wbsk_user+"','"
  96. //+paID+"','"+paCode+"','"+wbsk_user+"','"
  97. +cu_level+"','"+cu_industry+"'," +
  98. "'"+getNotNull(dataList,i,"UDSText4__c")+"','"+cu_turnSAP+"','"+cu_currency+"',"+cu_taxrate+",'"+getNotNull(dataList,i,"deal_status")+"'," +
  99. "'"+getNotNull(dataList,i,"field_Ezg0w__c")+"','"+getNotNull(dataList,i,"field_59oLF__c")+"','"+getNotNull(dataList,i,"biz_reg_name")+"','"+cu_recordType+"',"+
  100. "'"+dataList.get(i).get("tel")+"','"+dataList.get(i).get("email")+"'," +
  101. "'CRM',"+DateUtil.parseDateToOracleString(Constant.YMD_HMS,sdf.parse(sdf.format(create_time)))+"," +
  102. DateUtil.parseDateToOracleString(Constant.YMD_HMS,sdf.parse(sdf.format(last_modified_time)))+",'已审核','AUDITED','"+getNotNull(dataList,i,"remark")+"','"+_id+"')";
  103. baseDao.execute(sql);
  104. m++;
  105. }catch (Exception e){
  106. baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'"+crmQueryResult.getErrorMessage()+"','客户资料下载','"+name+"')");
  107. logger.info("异常信息:"+e);
  108. n++;
  109. }
  110. }
  111. baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_syncTime) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'下载记录"+dataList.size()+"条,成功"+m+"条,失败"+n+"条','客户资料下载','"+create_time_Last+"')");
  112. }
  113. }else{
  114. baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'"+crmQueryResult.getErrorMessage()+"','客户资料下载','')");
  115. logger.info("异常信息:"+crmQueryResult.getErrorMessage());
  116. }
  117. }
  118. }catch (Exception e){
  119. baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'"+e.getMessage()+"','客户资料下载','')");
  120. logger.info("异常信息:"+e.getMessage());
  121. e.printStackTrace();
  122. }
  123. }
  124. @Scheduled(cron = "0 0 23 * * ?")
  125. public void syncUpdateCustomers() {
  126. SqlRowList rs = baseDao.queryForRowSet("select * from (select distinct cui_code from customerUpdInfo where CUI_UPDATEDATE is null and nvl(cui_type,' ')='普通变更' and trunc(cui_date)=trunc(sysdate)) left join customer on cui_code=cu_code where nvl(cu_name,' ')<>' ' order by cu_id");
  127. while (rs.next()){
  128. try {
  129. Customer customer = new Customer();
  130. String valueField = getValueField("客户资料", "客户级别", rs.getGeneralString("cu_level"));
  131. customer.setDataObjectApiName("AccountObj");
  132. customer.set_id(rs.getGeneralString("cu_fxxkid"));
  133. customer.setName(rs.getGeneralString("cu_name"));
  134. customer.setAccount_level(valueField);
  135. customer.setField_w9e4q__c(rs.getGeneralString("cu_servicename"));
  136. BaseResult baseResult = customerManager.updateCustomer(customer);
  137. int errorCode = baseResult.getErrorCode();
  138. if(errorCode == 0){
  139. baseDao.execute("update customerUpdInfo set cui_updatestatus='已更新',cui_updatedate=sysdate where nvl(cui_type,' ')='普通变更' and cui_code='"+rs.getGeneralString("cui_code")+"' and trunc(cui_date)=trunc(sysdate)");
  140. }else{
  141. baseDao.execute("update customerUpdInfo set cui_error='"+baseResult.getErrorMessage()+"' where nvl(cui_type,' ')='普通变更' and cui_code='"+rs.getGeneralString("cui_code")+"' and trunc(cui_date)=trunc(sysdate)");
  142. logger.info("异常信息:"+baseResult.getErrorMessage());
  143. }
  144. }catch (Exception e){
  145. baseDao.execute("update customerUpdInfo set cui_error='"+e.getMessage()+"' where nvl(cui_type,' ')='普通变更' and cui_code='"+rs.getGeneralString("cui_code")+"' and trunc(cui_date)=trunc(sysdate)");
  146. logger.info("异常信息:"+e.getMessage());
  147. e.printStackTrace();
  148. }
  149. }
  150. }
  151. /**
  152. * 同步人员资料crm唯一id
  153. */
  154. @Scheduled(cron = "0 0/5 * * * ?")
  155. public void syncEmployeeFXXKID() {
  156. SqlRowList rs = baseDao.queryForRowSet("select em_id,em_name,em_code from employee where nvl(EM_FXXKID,' ')=' ' and nvl(EM_ISSYNCFXXKID,0)=-1 order by em_id");
  157. while (rs.next()){
  158. try {
  159. List<String> field_values = new ArrayList<>();
  160. field_values.add(rs.getGeneralString("em_name"));
  161. CrmQueryResult crmQueryResult = commonManager.queryCommon("name", field_values, "EQ", "name", true, 50, "PersonnelObj", true);
  162. int errorCode = crmQueryResult.getErrorCode();
  163. if(errorCode == 0) {
  164. Map<String, Object> data = crmQueryResult.getData();
  165. List<Map<String, Object>> dataList = (List<Map<String, Object>>) data.get("dataList");
  166. if(dataList.size()>0){
  167. Object user_id = dataList.get(0).get("user_id");
  168. if(user_id!=null){
  169. baseDao.execute("update employee set em_fxxkid='"+user_id+"' where em_id="+rs.getGeneralInt("em_id"));
  170. baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'人员资料纷享销客主键下载成功','人员资料纷享销客主键下载','"+rs.getGeneralString("em_code")+"')");
  171. }else{
  172. baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'"+crmQueryResult.getErrorMessage()+"','人员资料纷享销客主键下载','"+rs.getGeneralString("em_code")+"')");
  173. logger.info("异常信息:crm不存在该人员资料");
  174. }
  175. }
  176. }else{
  177. baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'"+crmQueryResult.getErrorMessage()+"','人员资料纷享销客主键下载','"+rs.getGeneralString("em_code")+"')");
  178. logger.info("异常信息:"+crmQueryResult.getErrorMessage());
  179. }
  180. }catch (Exception e){
  181. baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'"+e.getMessage()+"','人员资料纷享销客主键下载','"+rs.getGeneralString("em_code")+"')");
  182. logger.info("异常信息:"+e.getMessage());
  183. e.printStackTrace();
  184. }
  185. }
  186. }
  187. /**
  188. * 变更负责人
  189. */
  190. @Scheduled(cron = "0 0 23 * * ?")
  191. public void syncChangeSellerCustomers() {
  192. SqlRowList rs = baseDao.queryForRowSet("select * from (select distinct cui_code from customerUpdInfo where CUI_UPDATEDATE is null and nvl(cui_type,' ')='变更负责人' and trunc(cui_date)=trunc(sysdate)) left join customer on cui_code=cu_code left join employee on cu_sellercode=em_code where nvl(cu_name,' ')<>' ' order by cu_id");
  193. String dataObjectApiName="AccountObj";
  194. while (rs.next()){
  195. try {
  196. if(rs.getGeneralString("em_fxxkid")!=null && !"".equals(rs.getGeneralString("em_fxxkid")) && rs.getGeneralString("cu_fxxkid")!=null && !"".equals(rs.getGeneralString("cu_fxxkid"))){
  197. List<Map<String,Object>> list = new ArrayList<>();
  198. Map<String,Object> map = new HashMap<>();
  199. List<String> emfxxidList = new ArrayList<>();
  200. emfxxidList.add(rs.getGeneralString("em_fxxkid"));
  201. map.put("ownerId",emfxxidList);
  202. map.put("objectDataId",rs.getGeneralString("cu_fxxkid"));
  203. list.add(map);
  204. BaseResult baseResult = commonManager.changeOwnerCommon(list, dataObjectApiName);
  205. int errorCode = baseResult.getErrorCode();
  206. if(errorCode == 0){
  207. baseDao.execute("update customerUpdInfo set cui_updatestatus='已更新',cui_updatedate=sysdate where nvl(cui_type,' ')='变更负责人' and cui_code='"+rs.getGeneralString("cui_code")+"' and trunc(cui_date)=trunc(sysdate)");
  208. }else{
  209. baseDao.execute("update customerUpdInfo set cui_error='"+baseResult.getErrorMessage()+"' where nvl(cui_type,' ')='变更负责人' and cui_code='"+rs.getGeneralString("cui_code")+"' and trunc(cui_date)=trunc(sysdate)");
  210. logger.info("异常信息:"+baseResult.getErrorMessage());
  211. }
  212. }else{
  213. baseDao.execute("update customerUpdInfo set cui_error='客户对应业务员对应人员表中纷享销客ID为空或客户资料纷享销客ID为空' where nvl(cui_type,' ')='变更负责人' and cui_code='"+rs.getGeneralString("cui_code")+"' and trunc(cui_date)=trunc(sysdate)");
  214. logger.info("异常信息:客户对应业务员对应人员表中纷享销客ID为空或客户资料纷享销客ID为空");
  215. }
  216. }catch (Exception e){
  217. baseDao.execute("update customerUpdInfo set cui_error='"+e.getMessage()+"' where nvl(cui_type,' ')='变更负责人' and cui_code='"+rs.getGeneralString("cui_code")+"' and trunc(cui_date)=trunc(sysdate)");
  218. logger.info("异常信息:"+e.getMessage());
  219. e.printStackTrace();
  220. }
  221. }
  222. }
  223. private String getDisplayField(List<Map<String, Object>> dataList, int i, String Field, String APINAME){
  224. String ValueField ="";
  225. Object account_type = dataList.get(i).get(Field);
  226. if(account_type !=null && !"".equals(account_type.toString())){
  227. Object DISPLAYFIELD = baseDao.getFieldDataByCondition("comboData", "DISPLAYFIELD", "VALUEFIELD='" + account_type + "' and APINAME='"+APINAME+"' and MODULEAPINAME='客户资料'");
  228. ValueField= DISPLAYFIELD==null? "" :DISPLAYFIELD.toString();
  229. }
  230. return ValueField;
  231. }
  232. private String getValueField(String MODULEAPINAME, String APINAME, String DISPLAYFIELD){
  233. String result ="";
  234. if(DISPLAYFIELD !=null && !"".equals(DISPLAYFIELD)){
  235. Object VALUEFIELD = baseDao.getFieldDataByCondition("comboData", "VALUEFIELD", "MODULEAPINAME='" + MODULEAPINAME + "' and APINAME='"+APINAME+"' and DISPLAYFIELD='"+DISPLAYFIELD+"'");
  236. result = VALUEFIELD==null? "" :VALUEFIELD.toString();
  237. }
  238. return result;
  239. }
  240. private String getNotNull(List<Map<String, Object>> dataList, int i, String Field){
  241. return (dataList.get(i).get(Field)==null?"":dataList.get(i).get(Field)).toString();
  242. }
  243. }