global class SparePartsSequencingBatch implements Database.Batchable<SObject> {
|
|
//有参无参构造 start execute finish
|
|
global Date startDate; //排序开始日期
|
global Date endDate; //排序结束日期
|
|
public String OCSMProvince;
|
|
// global SparePartsSequencingBatch(){
|
// }
|
|
// 带年月的构造参数,用来刷历史数据的
|
// global SparePartsSequencingBatch(Integer year, Integer month) {
|
// startDate = Date.newInstance(year, month, 1);
|
// endDate = startDate.addMonths(1);
|
// }
|
|
// // OSCM管理省
|
// global SparePartsSequencingBatch(String OCSMProvince){
|
// this.OCSMProvince = OCSMProvince;
|
// }
|
|
// // 带年月和OSCM管理省的构造函数
|
// global SparePartsSequencingBatch(Integer year, Integer month, String OCSMProvince) {
|
// startDate = Date.newInstance(year, month, 1);
|
// endDate = startDate.addMonths(1);
|
// this.OCSMProvince = OCSMProvince;
|
// }
|
|
global SparePartsSequencingBatch(Date startat, Date endat){
|
startDate = startat;
|
endDate = endat;
|
}
|
|
global SparePartsSequencingBatch(Date startat, Date endat, String province){
|
startDate = startat;
|
endDate = endat;
|
OCSMProvince = province;
|
}
|
|
|
global Database.QueryLocator start(Database.BatchableContext BC) {
|
|
String sql = 'select name from OCM_Management_Province__c';
|
if(String.isNotBlank(OCSMProvince)){
|
sql += ' Where name =: OCSMProvince';
|
}
|
|
return Database.getQueryLocator(sql);
|
|
}
|
|
// global void execute(Database.BatchableContext BC, List<OCM_Management_Province__c> ompList) {
|
// System.debug('lt123ompList<OCSM管理省>(初始)'+ompList);
|
|
// List<String> ocsmList = new List<String>();
|
// for(OCM_Management_Province__c omp : ompList){
|
// ocsmList.add(omp.name);
|
// }
|
|
// // Date today = Date.today();
|
// // Integer year = today.year();
|
// // Integer month = today.month() - 1;
|
|
// // 开始 结束日期
|
// if(startDate == null) {
|
// // 上个月
|
// Date outdate = Date.today().addMonths(-1);
|
// // 确保开始日期是1号,结束日期是下月1号
|
// startDate = Date.newInstance(outdate.year(), outdate.month(), 1);
|
// endDate = startDate.addMonths(1);
|
// }
|
|
// if(ocsmList.size()>0){
|
// List<OPDPlan__c> opdList = [Select Id, OPDPlanOCM_man_province__c, Deliver_Index__c, Bollowdate__c,
|
// Approved_Index__c, SparePartsAppDate__c
|
// From OPDPlan__c
|
// Where OPDPlanOCM_man_province__c in: ocsmList
|
// AND ((Bollowdate__c >=: startDate And Bollowdate__c <: endDate)
|
// OR (SparePartsAppDate__c >=: startDate And SparePartsAppDate__c <: endDate))];
|
|
// //Map<OCSM管理省,List>,按照OCSM管理省分类
|
// Set<String> outList1 = new Set<String>();
|
// Set<String> appList1 = new Set<String>();
|
// Map<String,List<OPDPlan__c>> outMap = new Map<String,List<OPDPlan__c>>(); //出库
|
// Map<String,List<OPDPlan__c>> appMap = new Map<String,List<OPDPlan__c>>(); //审批
|
|
// for(OPDPlan__c opd : opdList){
|
// //备品出库优先度
|
// if(opd.Bollowdate__c != null){
|
|
// outList1.add(opd.OPDPlanOCM_man_province__c);
|
|
// if(outMap.containsKey(opd.OPDPlanOCM_man_province__c)){
|
// outMap.get(opd.OPDPlanOCM_man_province__c).add(opd);
|
// }else{
|
// List<OPDPlan__c> appList = new List<OPDPlan__c>(); //上个月批准日不为空的opd计划
|
// appList.add(opd);
|
// outMap.put(opd.OPDPlanOCM_man_province__c, appList);
|
// }
|
|
// // Integer outyear = opd.Bollowdate__c.year();
|
// // Integer outmonth = opd.Bollowdate__c.month();
|
// // if(year == outyear && month == outmonth){
|
|
// // }
|
// }
|
// System.debug('lt123outMap备品出库<OCSM管理省,List<OPD计划>>'+outMap);
|
|
// //备品审批优先度
|
// System.debug('lt123+opd.SparePartsAppDate__c+test---'+opd.SparePartsAppDate__c);
|
// if(opd.SparePartsAppDate__c != null){
|
|
// appList1.add(opd.OPDPlanOCM_man_province__c);
|
|
// if(appMap.containsKey(opd.OPDPlanOCM_man_province__c)){
|
// appMap.get(opd.OPDPlanOCM_man_province__c).add(opd);
|
// }else{
|
// List<OPDPlan__c> appList = new List<OPDPlan__c>(); //上个月出库日不为空的opd计划
|
// appList.add(opd);
|
// appMap.put(opd.OPDPlanOCM_man_province__c, appList);
|
// }
|
|
// // Integer appyear = opd.SparePartsAppDate__c.year();
|
// // Integer appmonth = opd.SparePartsAppDate__c.month();
|
// // if(year == appyear && month == appmonth){
|
// // }
|
// }
|
// System.debug('lt123appMap备品审批<OCSM管理省,List<OPD计划>>'+appMap);
|
// }
|
|
// //出库--冒泡排序
|
// List<OPDPlan__c> outList2 = new List<OPDPlan__c>();
|
// for(string out : outList1){
|
// if(outMap.get(out) != null){
|
// Integer len = outMap.get(out).size();
|
// for(Integer i = 0 ; i < len - 1 ; i++){
|
// for(Integer j = 0 ; j < len - 1 - i ; j++){
|
// if(outMap.get(out)[j].Bollowdate__c > outMap.get(out)[j + 1].Bollowdate__c){
|
// OPDPlan__c temp = outMap.get(out)[j + 1];
|
// outMap.get(out)[j + 1] = outMap.get(out)[j];
|
// outMap.get(out)[j] = temp;
|
// }
|
// }
|
// }
|
// //赋值 排序
|
// for(Integer i = 0 ; i < len ; i++){
|
// outMap.get(out)[i].Deliver_Index__c = i + 1;
|
// }
|
// outList2.addAll(outMap.get(out));
|
// System.debug('lt123outMap备品出库(排序后)'+outMap.get(out));
|
// }
|
// }
|
// update outList2;
|
// System.debug('lt123outList2备品出库(更新后)'+outList2);
|
|
// //审批--冒泡排序
|
// List<OPDPlan__c> appList2 = new List<OPDPlan__c>();
|
// for(string app : appList1){
|
// if(appMap.get(app) != null){
|
// Integer len1 = appMap.get(app).size();
|
// for(Integer i = 0 ; i < len1 - 1 ; i++){
|
// for(Integer j = 0 ; j < len1 - 1 - i ; j++){
|
// if(appMap.get(app)[j].SparePartsAppDate__c > appMap.get(app)[j + 1].SparePartsAppDate__c){
|
// OPDPlan__c temp = appMap.get(app)[j + 1];
|
// appMap.get(app)[j + 1] = appMap.get(app)[j];
|
// appMap.get(app)[j] = temp;
|
// }
|
// }
|
// }
|
// //赋值 排序
|
// for(Integer i = 0 ; i < len1 ; i++){
|
// appMap.get(app)[i].Approved_Index__c = i + 1;
|
// }
|
// appList2.addAll(appMap.get(app));
|
// System.debug('lt123appMap备品审批(排序后)'+appMap.get(app));
|
// }
|
|
// }
|
// update appList2;
|
// System.debug('lt123appList2备品审批(更新后)'+appList2);
|
|
// }
|
|
// }
|
|
|
|
global void execute(Database.BatchableContext BC, List<OCM_Management_Province__c> ompList) {
|
|
System.debug('lt123ompList'+ompList);
|
|
List<String> ocsmList = new List<String>();
|
for(OCM_Management_Province__c omp : ompList){
|
ocsmList.add(omp.name);
|
}
|
|
// 待更新的OPD计划
|
Map<String, OPDPlan__c> mapUpdate = new Map<String, OPDPlan__c>();
|
// 先做发货优先
|
// 按发货日排序 不用管省份
|
String sql1 = 'Select Id, OPDPlanOCM_man_province__c, Deliver_Index__c, Bollowdate__c, Approved_Index__c, SparePartsAppDate__c From OPDPlan__c Where Bollowdate__c >= :startDate And Bollowdate__c < :endDate';
|
if (ocsmList != null && ocsmList.size() > 0) {
|
sql1 += ' And OPDPlanOCM_man_province__c in :ocsmList';
|
}
|
// if(String.isNotBlank(OCSMProvince)){
|
// sql1 += ' And OPDPlanOCM_man_province__c =: OCSMProvince Order By Bollowdate__c';
|
// }else{
|
// sql1 += ' Order By Bollowdate__c';
|
// }
|
sql1 += ' Order By Bollowdate__c';
|
List<OPDPlan__c> checkList1 = Database.query(sql1);
|
|
System.debug('lt123sql1(备品出库sql语句)'+sql1);
|
System.debug('lt123checkList1(备品出库OPD计划)'+checkList1);
|
|
// List<OPDPlan__c> checkList1 = [Select Id, OPDPlanOCM_man_province__c, Deliver_Index__c, Bollowdate__c,
|
// Approved_Index__c, SparePartsAppDate__c
|
// From OPDPlan__c
|
// Where Bollowdate__c >= :startDate And Bollowdate__c < :endDate
|
// Order By Bollowdate__c];
|
if (checkList1 != null && checkList1.size() > 0) {
|
checkList1 = setSort(checkList1, 1);
|
for (OPDPlan__c opd : checkList1) {
|
mapUpdate.put(opd.Id, opd);
|
}
|
}
|
|
// 再做审批日排序 不用管省份
|
String sql2 = 'Select Id, OPDPlanOCM_man_province__c, Deliver_Index__c, Bollowdate__c, Approved_Index__c, SparePartsAppDate__c From OPDPlan__c Where SparePartsAppDate__c >= :startDate And SparePartsAppDate__c < :endDate';
|
if (ocsmList != null && ocsmList.size() > 0) {
|
sql2 += ' And OPDPlanOCM_man_province__c in :ocsmList';
|
}
|
// if(String.isNotBlank(OCSMProvince)){
|
// sql2 += ' And OPDPlanOCM_man_province__c =: OCSMProvince Order By SparePartsAppDate__c';
|
// }else{
|
// sql2 += ' Order By SparePartsAppDate__c';
|
// }
|
sql2 += ' Order By SparePartsAppDate__c';
|
List<OPDPlan__c> checkList2 = Database.query(sql2);
|
|
System.debug('lt123sql2(备品审批sql语句)'+sql2);
|
System.debug('lt123checkList2(备品审批OPD计划)'+checkList2);
|
|
// List<OPDPlan__c> checkList2 = [Select Id, OPDPlanOCM_man_province__c, Deliver_Index__c, Bollowdate__c,
|
// Approved_Index__c, SparePartsAppDate__c
|
// From OPDPlan__c
|
// Where SparePartsAppDate__c >= :startDate And SparePartsAppDate__c < :endDate
|
// Order By SparePartsAppDate__c];
|
if (checkList2 != null && checkList2.size() > 0) {
|
checkList2 = setSort(checkList2, 2);
|
for (OPDPlan__c opd : checkList2) {
|
OPDPlan__c opdInMap = mapUpdate.get(opd.Id);
|
if (opdInMap == null) {
|
mapUpdate.put(opd.Id, opd);
|
} else {
|
opdInMap.Approved_Index__c = opd.Approved_Index__c;
|
}
|
}
|
}
|
|
if (mapUpdate.size() > 0) {
|
update mapUpdate.values();
|
}
|
}
|
|
// 写序号操作
|
private List<OPDPlan__c> setSort(List<OPDPlan__c> opdList, Integer sortType) {
|
// 初始化参数
|
// <省份, 当前序号>
|
Map<String, Integer> ProvinceSort = new Map<String, Integer>();
|
// 循环写序号
|
for (OPDPlan__c opd : opdList) {
|
// 取对应省份的当前序号
|
Integer SortNumber = ProvinceSort.get(opd.OPDPlanOCM_man_province__c);
|
// 判断省份是否已记录
|
if (SortNumber == null) {
|
SortNumber = 0;
|
}
|
// 序号加算;赋值;存入映射
|
SortNumber += 1;
|
if (sortType == 1) {
|
opd.Deliver_Index__c = SortNumber;
|
} else {
|
opd.Approved_Index__c = SortNumber;
|
}
|
ProvinceSort.put(opd.OPDPlanOCM_man_province__c, SortNumber);
|
}
|
return opdList;
|
}
|
|
global void finish(Database.BatchableContext BC) {
|
// 2022/01/04 从OPDPlanMarkBatchSchedule里挪过来的
|
// 这里以及OPDPlanMarkBatchSchedule的修改先不上线
|
// 数据确认没有问题之后,从2022/01月底的拍照开始修改
|
// Id execBTId = Database.executebatch(new OPDPlanSystemDelayBatch(), 100);
|
}
|
|
}
|