global class UpdateConrenewalpriceBatch implements Database.Batchable<sObject> {
// 设定运行的维修合同的list<ID>
public list<string> TestIDList;

global UpdateConrenewalpriceBatch() {

}

global UpdateConrenewalpriceBatch(list<string> TestIDList) {
	this.TestIDList = TestIDList;
}

global Database.QueryLocator start(Database.BatchableContext bc) {
	string query =
		' select id , previousTotalRepairAmount__c , currentTotalRepairAmount__c ,'+
		'Contract_Consumption_rate__c,' +
		' previousTotalContractAmount__c , currentTotalContractAmountVM__c , currentTotalGuaranteePrice__c '+
		' , RecordType_DeveloperName__c  ' +
		'  from Maintenance_Contract__c '
		+ ' where Status__c in (\'契約\',\'契約満了\') ';
	if (TestIDList != null && TestIDList.size() > 0) {
		query += ' and id in: TestIDList';
	}
	return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, list<Maintenance_Contract__c> scope) {
	List<Data> datatemp = new List<Data>();
	datatemp = getChartData();
	for (Maintenance_Contract__c selectedLocal : scope) {
		// if (String.isBlank(String.valueOf(selectedLocal.Contract_Consumption_rate__c))) {
		// 	continue;
		// }else {
		for (Data da : datatemp) {
			if (String.isBlank(String.valueOf(da.rate_Lower)) &&  selectedLocal.Contract_Consumption_rate__c < da.rate_Upper) {
				selectedLocal.Adjustment_ratio_Lower__c = da.price_Lower;
				selectedLocal.Adjustment_ratio_Upper__c = da.price_Upper;
				continue;
			} else if (selectedLocal.Contract_Consumption_rate__c >= da.rate_Lower && String.isBlank(String.valueOf(da.rate_Upper))) {
				selectedLocal.Adjustment_ratio_Lower__c = da.price_Lower;
				selectedLocal.Adjustment_ratio_Upper__c = da.price_Upper;
				continue;
			} else if (selectedLocal.Contract_Consumption_rate__c >= da.rate_Lower
			           && selectedLocal.Contract_Consumption_rate__c < da.rate_Upper) {
				selectedLocal.Adjustment_ratio_Lower__c = da.price_Lower;
				selectedLocal.Adjustment_ratio_Upper__c = da.price_Upper;
				continue;
			}
		}
		// }
	}
	update scope;
}

global void finish(Database.BatchableContext BC) {

}

public static List<Data> getChartData() {
	List<Data> data = new List<Data>();
	List<Achievement_linkage__c> achlist = [select id, Consumption_rate_Lower__c, Consumption_rate_Upper__c,
	                                        PriceCount_Lower__c, PriceCount_Upper__c from Achievement_linkage__c];
	for (Achievement_linkage__c al : achlist ) {
		data.add(new Data(al.Consumption_rate_Lower__c, al.Consumption_rate_Upper__c, al.PriceCount_Lower__c, al.PriceCount_Upper__c));
	}
	return data;
}

// 价格体系 class
public class Data {
public Decimal rate_Upper { get; set; }
public Decimal rate_Lower { get; set; }
public Decimal price_Upper { get; set; }
public Decimal price_Lower { get; set; }
public Data(Decimal rate_Lower, Decimal rate_Upper, Decimal price_Lower, Decimal price_Upper) {
	this.rate_Upper = rate_Upper;
	this.rate_Lower = rate_Lower;
	this.price_Upper = price_Upper;
	this.price_Lower = price_Lower;
}
}
}