global class DealerConsumableManageBatch5 implements Database.Batchable<sObject> {
|
public Date currentMonth;
|
public Date endDate;
|
public Date startDate;
|
public String queryStr;
|
public String year;
|
public Boolean isIncrementOnly; //是否只跑增量
|
global DealerConsumableManageBatch5(Date startDate,Date endDate,Boolean isIncrementOnly) {
|
//当年
|
this.startDate=startDate;
|
this.endDate=endDate;
|
this.isIncrementOnly=isIncrementOnly;
|
queryStr = 'SELECT Contract_recognized_day_1__c,Opp_order_Type__c,Opportunity_ThousandY__c,Agency1__c,OCM_man_province_no_dealer__c '+
|
' FROM Opportunity '+
|
' WHERE Opportunity_ThousandY__c!=null and StageName in (\'出荷\',\'完了\',\'注残\') and ( (Estimation_Id__c like \'a15%\' and Opp_order_Type__c=\'ENG\') OR Opp_order_Type__c=\'ET\' ) ' +
|
' AND Contract_recognized_day_1__c>=:startDate and Contract_recognized_day_1__c <= :endDate ';
|
}
|
|
global Database.QueryLocator start(Database.BatchableContext bc) {
|
//数据超过一万条会报错
|
if(!isIncrementOnly){
|
Integer year = startDate.year();
|
Integer FYear = year;
|
if(startDate>=Date.newInstance(year, 4, 1)){
|
FYear = FYear+1;
|
}
|
String OCSMYear= 'FY'+FYear;
|
List<Dealer_ConsumableManage__c> dcmList= [SELECT id FROM Dealer_ConsumableManage__c WHERE FY__c = :OCSMYear ];
|
if(dcmList.size()>0){
|
for(Dealer_ConsumableManage__c dcm:dcmList){
|
dcm.FY_BO_1__c=0;
|
dcm.FY_BO_2__c=0;
|
dcm.FY_BO_3__c=0;
|
dcm.FY_BO_4__c=0;
|
dcm.FY_BO_5__c=0;
|
dcm.FY_BO_6__c=0;
|
dcm.FY_BO_7__c=0;
|
dcm.FY_BO_8__c=0;
|
dcm.FY_BO_9__c=0;
|
dcm.FY_BO_10__c=0;
|
dcm.FY_BO_11__c=0;
|
dcm.FY_BO_12__c=0;
|
}
|
update dcmList;
|
}
|
|
}
|
|
return Database.getQueryLocator(queryStr);
|
}
|
|
global void execute(Database.BatchableContext BC, list<Opportunity> scope) {
|
Integer year = startDate.year();
|
Integer FYear = year;
|
if(startDate>=Date.newInstance(year, 4, 1)){
|
FYear = FYear+1;
|
}
|
String OCSMYear= 'FY'+FYear;
|
|
|
Set<String> dcmKeySet=new Set<String>();
|
Map<String,List<Opportunity>> codMap = new Map<String,List<Opportunity>>();
|
for(Opportunity sa:scope){
|
String key;
|
if(sa.Opp_order_Type__c == 'ET'){
|
key=sa.Agency1__c+'-ET-'+OCSMYear+'-'+sa.OCM_man_province_no_dealer__c;
|
dcmKeySet.add(sa.Agency1__c+'-ET-'+OCSMYear+'-'+sa.OCM_man_province_no_dealer__c);
|
}else{
|
key=sa.Agency1__c+'-ENG-'+OCSMYear+'-'+sa.OCM_man_province_no_dealer__c;
|
dcmKeySet.add(sa.Agency1__c+'-ENG-'+OCSMYear+'-'+sa.OCM_man_province_no_dealer__c);
|
}
|
|
if(codMap.containsKey(key)){
|
codMap.get(key).add(sa);
|
}else{
|
List<Opportunity> codList = new List<Opportunity>();
|
codList.add(sa);
|
codMap.put(key,codList);
|
}
|
}
|
|
|
List<Dealer_ConsumableManage__c> ctmList= [SELECT TMS_Province__c,FY_BO_1__c,FY_BO_2__c,FY_BO_3__c,FY_BO_4__c
|
,FY_BO_5__c,FY_BO_6__c,FY_BO_7__c,FY_BO_8__c
|
,FY_BO_9__c,FY_BO_10__c,FY_BO_11__c,FY_BO_12__c
|
,TMS_Key__c
|
FROM Dealer_ConsumableManage__c WHERE TMS_Key__c in :dcmKeySet];
|
System.debug('获取到现存IMS:');
|
System.debug(ctmList);
|
|
for(Dealer_ConsumableManage__c ctm:ctmList){
|
String key = ctm.TMS_Key__c;
|
if(ctm.FY_BO_1__c==null){
|
ctm.FY_BO_1__c=0;
|
}
|
if(ctm.FY_BO_2__c==null){
|
ctm.FY_BO_2__c=0;
|
}
|
if(ctm.FY_BO_3__c==null){
|
ctm.FY_BO_3__c=0;
|
}
|
if(ctm.FY_BO_4__c==null){
|
ctm.FY_BO_4__c=0;
|
}
|
if(ctm.FY_BO_5__c==null){
|
ctm.FY_BO_5__c=0;
|
}
|
if(ctm.FY_BO_6__c==null){
|
ctm.FY_BO_6__c=0;
|
}
|
if(ctm.FY_BO_7__c==null){
|
ctm.FY_BO_7__c=0;
|
}
|
if(ctm.FY_BO_8__c==null){
|
ctm.FY_BO_8__c=0;
|
}
|
if(ctm.FY_BO_9__c==null){
|
ctm.FY_BO_9__c=0;
|
}
|
if(ctm.FY_BO_10__c==null){
|
ctm.FY_BO_10__c=0;
|
}
|
if(ctm.FY_BO_11__c==null){
|
ctm.FY_BO_11__c=0;
|
}
|
if(ctm.FY_BO_12__c==null){
|
ctm.FY_BO_12__c=0;
|
}
|
if(codMap.containsKey(key)){
|
List<Opportunity> saList=codMap.get(key);
|
for(Opportunity cod:saList){
|
if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear, 1, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear, 2, 1)){
|
ctm.FY_BO_1__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear, 2, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear, 3, 1)){
|
ctm.FY_BO_2__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear, 3, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear, 4, 1)){
|
ctm.FY_BO_3__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 4, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 5, 1)){
|
ctm.FY_BO_4__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 5, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 6, 1)){
|
ctm.FY_BO_5__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 6, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 7, 1)){
|
ctm.FY_BO_6__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 7, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 8, 1)){
|
ctm.FY_BO_7__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 8, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 9, 1)){
|
ctm.FY_BO_8__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 9, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 10, 1)){
|
ctm.FY_BO_9__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 10, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 11, 1)){
|
ctm.FY_BO_10__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 11, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 12, 1)){
|
ctm.FY_BO_11__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 12, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear, 1, 1)){
|
ctm.FY_BO_12__c+=cod.Opportunity_ThousandY__c;
|
}
|
}
|
codMap.remove(key);
|
}
|
}
|
|
List<Dealer_ConsumableManage__c> newCtm=new List<Dealer_ConsumableManage__c>();
|
for(String key:codMap.keySet()){
|
List<Opportunity> codList=codMap.get(key);
|
Dealer_ConsumableManage__c ctm=new Dealer_ConsumableManage__c();
|
ctm.Dealer_Name__c = codList[0].Agency1__c;
|
if(codList[0].Opp_order_Type__c == 'ET'){
|
ctm.ET_ENG__c='ET';
|
}else{
|
ctm.ET_ENG__c='ENG';
|
}
|
ctm.FY__c = OCSMYear;
|
ctm.TMS_Key__c = ctm.Dealer_Name__c+'-'+ctm.ET_ENG__c+'-'+ctm.FY__c+'-'+codList[0].OCM_man_province_no_dealer__c;
|
ctm.FY_BO_1__c=0;
|
ctm.FY_BO_2__c=0;
|
ctm.FY_BO_3__c=0;
|
ctm.FY_BO_4__c=0;
|
ctm.FY_BO_5__c=0;
|
ctm.FY_BO_6__c=0;
|
ctm.FY_BO_7__c=0;
|
ctm.FY_BO_8__c=0;
|
ctm.FY_BO_9__c=0;
|
ctm.FY_BO_10__c=0;
|
ctm.FY_BO_11__c=0;
|
ctm.FY_BO_12__c=0;
|
|
for(Opportunity cod:codList){
|
if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear, 1, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear, 2, 1)){
|
ctm.FY_BO_1__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear, 2, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear, 3, 1)){
|
ctm.FY_BO_2__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear, 3, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear, 4, 1)){
|
ctm.FY_BO_3__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 4, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 5, 1)){
|
ctm.FY_BO_4__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 5, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 6, 1)){
|
ctm.FY_BO_5__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 6, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 7, 1)){
|
ctm.FY_BO_6__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 7, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 8, 1)){
|
ctm.FY_BO_7__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 8, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 9, 1)){
|
ctm.FY_BO_8__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 9, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 10, 1)){
|
ctm.FY_BO_9__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 10, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 11, 1)){
|
ctm.FY_BO_10__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 11, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 12, 1)){
|
ctm.FY_BO_11__c+=cod.Opportunity_ThousandY__c;
|
}else if(cod.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 12, 1) && cod.Contract_recognized_day_1__c < Date.newInstance(FYear, 1, 1)){
|
ctm.FY_BO_12__c+=cod.Opportunity_ThousandY__c;
|
}
|
}
|
newCtm.add(ctm);
|
}
|
if(ctmList.size()>0){
|
update ctmList;
|
}
|
if(newCtm.size()>0){
|
insert newCtm;
|
}
|
|
}
|
|
global void finish(Database.BatchableContext BC) {
|
}
|
}
|