/**
 * 系统每个月末最后一天运行Batch统计日报中对应这个客户以下项目
 * 巡检次数、上门次数、点检次数、点检设备数、培训次数、维修委托书填写、更新日期
 */
global class UpdateMonthlyContactBatch implements Database.Batchable<sObject> {

	public string testid;
	public List<String> idList;
	Boolean IsNeedExecute = false; // 2021-03-03  mzy  WLIG-BYHD79  SFDC环境batch合并调查   是否符合执行条件

	global UpdateMonthlyContactBatch() {
		idList = null;

	}
	global UpdateMonthlyContactBatch(List<String> idlist) {
		this.idList = idlist;
	}
	global UpdateMonthlyContactBatch(string testid) {
		this.testid = testid;
	}

	//2021-03-03  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
	global UpdateMonthlyContactBatch(Boolean needExecute) {
		idList = null;
		this.IsNeedExecute = needExecute;
	}
	//2021-03-03  mzy  WLIG-BYHD79  SFDC环境batch合并调查  end

	global Database.QueryLocator start(Database.BatchableContext BC) {
		String sql = 'select id, Name,Campaign__c,pollingTime__c,VisitTime__c,InspectTime__c,InspectEquipmentTime__c,' +
		             'TeachingTime__c, ServiceBookInput__c from Contact where Campaign__c !=null ';
		if (idList != null ) {
			sql += ' and id in: idList';
		}
		if (!string.isblank(testid)) {
			sql +=  ' and id =: testid';
		}

		// 2021-03-03  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
	    //  每月 1日   不满足运行时间，可以使查询到的结果为空
	    //Date mon1stDate = Date.newInstance(today.year(), today.month(), 1);
	    Integer executeMonth = Date.today().month();
	    Integer executeDay = Date.today().day();
	    Boolean inexecutionDateFlag = true;
	    if (executeDay == 1){
	          inexecutionDateFlag = false;
	    }
	    if(inexecutionDateFlag && IsNeedExecute ==true){
	        sql = 'Select Id from Contact where Name = \'\' AND Name != \'\' ';
	    }
	    //2021-03-03  mzy  WLIG-BYHD79  SFDC环境batch合并调查 end

		return Database.getQueryLocator(sql);
	}

	global void execute(Database.BatchableContext BC, List<Contact> Contactlist) {
		List<InstructStatusMonthly__c> TMCList = new List<InstructStatusMonthly__c>();
		String temp = '';
		Integer m = 0;
		if (Date.today().day() == 1 && Date.today().month() != 1) {
			m =  Date.today().month() - 1;
		} else if (Date.today().day() == 1 && Date.today().month() == 1) {
			m = 12;
		} else {
			m = Date.today().month();
		}

		for (Contact clist : Contactlist) {
			InstructStatusMonthly__c TMC = new InstructStatusMonthly__c();
			TMC.Name = clist.Name;
			TMC.Campaign__c = clist.Campaign__c;
			TMC.pollingTime__c = clist.pollingTime__c;
			TMC.VisitTime__c = clist.VisitTime__c;
			TMC.InspectTime__c = clist.InspectTime__c;
			TMC.InspectEquipmentTime__c = clist.InspectEquipmentTime__c;
			TMC.TeachingTime__c = clist.TeachingTime__c;
			TMC.ServiceBookInput__c = clist.ServiceBookInput__c;
			TMC.Contact__c = clist.id;
			Integer y = Date.today().month() == 1 ? Date.today().year() : Date.today().year() - 1;
			TMC.CreatedYear__c = String.valueOf(y);
			TMC.CreatedMonth__c = m + '';
			TMC.CreatedDate__c = Date.today();
			TMCList.add(TMC);
		}
		insert TMCList;


	}

	global void finish(Database.BatchableContext BC) {

	}

}