package com.uas.eis.task; import com.uas.eis.beans.result.BaseResult; import com.uas.eis.beans.result.CrmAddResult; import com.uas.eis.dao.BaseDao; import com.uas.eis.dao.SqlRowList; import com.uas.eis.manager.CommonManager; import com.uas.eis.utils.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Component @EnableAsync @EnableScheduling public class SaleMonthDataTask { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private BaseDao baseDao; @Autowired private CommonManager commonManager; @Resource(name = "configuration") private Configuration configuration; //每天0时每10分执行一次 @Scheduled(cron = "0 0/10 0 * * ?") public void syncSaleMonthData() { SqlRowList rs = baseDao.queryForRowSet("select * from (select * from saleMonthData left join customer on SM_CUSTCODE=CU_CODE left join employee on em_code=cu_sellercode where nvl(cu_fxxkid,' ')<>' ' and nvl(SM_SENDSTATUS,' ')='待上传' order by sm_id) where rownum<=500"); String openUserId = configuration.getOpenUserId(); while (rs.next()){ if(!"".equals(rs.getGeneralString("em_fxxkid"))){ openUserId=rs.getGeneralString("em_fxxkid"); } Map data = new HashMap<>(); Map map = new HashMap<>(); List owner = new ArrayList<>(); owner.add(openUserId); map.put("owner",owner); map.put("field_6secE__c",rs.getGeneralDouble("sm_salesum"));//接单金额 map.put("field_Frnzy__c",rs.getGeneralString("cu_fxxkid"));//客户名称 关联ID String sm_yearmonth = rs.getGeneralString("sm_yearmonth"); String year = sm_yearmonth.substring(0, 4); String month = sm_yearmonth.substring(4); String valueField_year = getValueField("销售月数据统计", "年份", year); String valueField_month = getValueField("销售月数据统计", "月份", month); map.put("field_22s1K__c",valueField_year);//年份 map.put("field_C71qd__c",rs.getGeneralDouble("sm_saleoutsum"));//出货金额 map.put("field_1s1t2__c",valueField_month);//月份 map.put("field_m2ypV__c",rs.getGeneralDouble("sm_paysum"));//应收货款 map.put("dataObjectApiName","object_8r53c__c"); map.put("created_by",openUserId); map.put("life_status","normal"); map.put("record_type","default__c"); data.put("object_data",map); try { CrmAddResult crmAddResult = commonManager.createCommonOfCustom(data); int errorCode = crmAddResult.getErrorCode(); String dataId = crmAddResult.getDataId(); if(errorCode == 0){ baseDao.execute("update saleMonthData set SM_SENDSTATUS='已上传',SM_FXXKDATAID='"+dataId+"' where sm_id="+rs.getInt("sm_id")); baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'销售月数据统计上传成功','销售月数据统计上传','"+rs.getGeneralString("sm_yearmonth")+rs.getString("sm_custcode")+"')"); }else{ baseDao.execute("update saleMonthData set SM_SENDSTATUS='未上传成功' where sm_id="+rs.getInt("sm_id")); baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'"+crmAddResult.getErrorMessage()+"','销售月数据统计上传','"+rs.getGeneralString("sm_yearmonth")+rs.getString("sm_custcode")+"')"); logger.info("异常信息:"+crmAddResult.getErrorMessage()); } }catch (Exception e){ baseDao.execute("update saleMonthData set SM_SENDSTATUS='未上传成功' where sm_id="+rs.getInt("sm_id")); baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'"+e.getMessage()+"','销售月数据统计上传','"+rs.getGeneralString("sm_yearmonth")+rs.getString("sm_custcode")+"')"); logger.info("异常信息:"+e.getMessage()); e.printStackTrace(); } } } //@Scheduled(cron = "0 0 1 * * ?") @Scheduled(cron = "0 0/20 0 * * ?") public void syncUpdateSaleMonthData() { // SqlRowList rs = baseDao.queryForRowSet("select * from saleMonthData left join customer on SM_CUSTCODE=CU_CODE where nvl(cu_fxxkid,' ')<>' ' and nvl(SM_ISUPDATE,0)=-1 and (SM_YEARMONTH=case when to_char(sysdate,'dd')='01' then to_char(add_months(sysdate,-1),'yyyymm') else to_char(add_months(sysdate,0),'yyyymm') end) order by sm_id"); SqlRowList rs = baseDao.queryForRowSet("select * from saleMonthData left join customer on SM_CUSTCODE=CU_CODE where nvl(cu_fxxkid,' ')<>' ' and nvl(SM_ISUPDATE,0)=-1 and SM_YEARMONTH in ('202401','202402') order by sm_id"); //SqlRowList rs = baseDao.queryForRowSet("select * from saleMonthData left join customer on SM_CUSTCODE=CU_CODE where nvl(cu_fxxkid,' ')<>' ' and nvl(SM_ISUPDATE,0)=-1 and (SM_YEARMONTH=case when to_char(to_date('20220501','yyyymmdd'),'dd')='01' then to_char(add_months(to_date('20220501','yyyymmdd'),-1),'yyyymm') else to_char(add_months(to_date('20220501','yyyymmdd'),0),'yyyymm') end) order by sm_id"); //SqlRowList rs = baseDao.queryForRowSet("select * from saleMonthData left join customer on SM_CUSTCODE=CU_CODE where nvl(cu_fxxkid,' ')<>' ' and sm_id=654 order by sm_id"); while (rs.next()) { Map data = new HashMap<>(); Map map = new HashMap<>(); map.put("field_6secE__c", rs.getGeneralDouble("sm_salesum"));//接单金额 map.put("field_C71qd__c", rs.getGeneralDouble("sm_saleoutsum"));//出货金额 map.put("field_m2ypV__c", rs.getGeneralDouble("sm_paysum"));//应收货款 map.put("_id", rs.getGeneralString("sm_fxxkdataid"));//应收货款 map.put("dataObjectApiName", "object_8r53c__c"); data.put("object_data", map); try { BaseResult baseResult = commonManager.updateCommonOfCustom(data); int errorCode = baseResult.getErrorCode(); if (errorCode == 0) { baseDao.execute("update saleMonthData set SM_UPDATESTATUS='已更新',SM_UPDATEDATE=sysdate where sm_id=" + rs.getInt("sm_id")); baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'销售月数据统计更新成功','销售月数据统计更新','" + rs.getGeneralString("sm_yearmonth") + rs.getString("sm_custcode") + "')"); } else { baseDao.execute("update saleMonthData set SM_UPDATESTATUS='更新失败' where sm_id=" + rs.getInt("sm_id")); baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'" + baseResult.getErrorMessage() + "','销售月数据统计更新','" + rs.getGeneralString("sm_yearmonth") + rs.getString("sm_custcode") + "')"); logger.info("异常信息:" + baseResult.getErrorMessage()); } } catch (Exception e) { baseDao.execute("update saleMonthData set SM_SENDSTATUS='更新失败' where sm_id=" + rs.getInt("sm_id")); baseDao.execute("insert into FXXKDockingErrorlog(ml_id,ml_date,ml_result,ml_type,ml_code) values(FXXKDOCKINGERRORLOG_SEQ.nextval,sysdate,'" + e.getMessage() + "','销售月数据统计更新','" + rs.getGeneralString("sm_yearmonth") + rs.getString("sm_custcode") + "')"); logger.info("异常信息:" + e.getMessage()); e.printStackTrace(); } } } private String getValueField(String MODULEAPINAME, String APINAME, String DISPLAYFIELD){ String result =""; if(DISPLAYFIELD !=null && !"".equals(DISPLAYFIELD)){ Object VALUEFIELD = baseDao.getFieldDataByCondition("comboData", "VALUEFIELD", "MODULEAPINAME='" + MODULEAPINAME + "' and APINAME='"+APINAME+"' and DISPLAYFIELD='"+DISPLAYFIELD+"'"); result = VALUEFIELD==null? "" :VALUEFIELD.toString(); } return result; } }