/**
 * 
 */
package com.kingdee.shr.customer.gtiit.imports;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.eas.basedata.person.IPerson;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.hr.affair.AffairBizCheckFacadeFactory;
import com.kingdee.eas.hr.affair.IAffairBizCheckFacade;
import com.kingdee.eas.hr.emp.IBank;
import com.kingdee.eas.hr.emp.IPersonDataSourceOptFacade;
import com.kingdee.eas.hr.emp.IPersonOtherInfo;
import com.kingdee.eas.hr.emp.PersonDataSourceOptFacadeFactory;
import com.kingdee.eas.hr.emp.PersonOtherInfoFactory;
import com.kingdee.shr.base.syssetting.app.io.fileImport.BaseRowInfo;
import com.kingdee.shr.base.syssetting.app.io.fileImport.DynamicModelHelper;
import com.kingdee.shr.base.syssetting.app.io.fileImport.ImportException;
import com.kingdee.shr.base.syssetting.app.io.impl.PersonDataSourceImportUtils;
import com.kingdee.shr.base.syssetting.app.io.impl.PersonOtherDataSourceImportService;
import com.kingdee.shr.base.syssetting.web.dynamic.model.DynamicModelInfo;
import com.kingdee.util.StringUtils;
import com.kingdee.eas.hr.emp.BankFactory;

/**
 * @author ISSUSER 教育经历parttime导入 PersonOtherDataSourceImportService
 */
public class PersonOtherParttimeImport extends PersonOtherDataSourceImportService {
	private boolean isRemovePersonEntity = false;
	private IPerson personLocalInstance;
	private IPersonDataSourceOptFacade PersonDataSourceOptFacadeLocalInstance;
	private IPersonOtherInfo PersonOtherInfoLocalInstance;
	private IAffairBizCheckFacade AffairBizCheckFacadeLocalInstance;
	private IBank bankLocalInstance;
	private String className = "";

	@Override
	protected void importDataSource(BaseRowInfo row) throws ImportException {
		// 获取日期 日期不能大于当天
		String graduation = row.getValueOfString("partperson.graduation");
		
		if (org.apache.commons.lang3.StringUtils.isNotBlank(graduation)) {

			Date nowdate = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String sdate = sdf.format(nowdate);

			try {
				Date dateGraduation = sdf.parse(graduation);
				Date tonowDate = sdf.parse(sdate);
				if(tonowDate.before(dateGraduation)) {
					throw new ImportException("The graduation time cannot be longer than the current time !");
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		DynamicModelHelper dHelper = this.getDynamicModelHelper();

		Context ctx = this.getContext();
		try {

			this.handlerEachRow(ctx, row, dHelper, ctx.getLocale());
		} catch (Exception var5) {
			throw new ImportException(var5.getMessage(), var5);
		}
	}

	private void handlerEachRow(Context ctx, BaseRowInfo row, DynamicModelHelper helper, Locale locale)
			throws Exception {
		this.personLocalInstance = PersonFactory.getLocalInstance(ctx);
		this.isRemovePersonEntity = PersonDataSourceImportUtils.checkPersonAssociatedAttributes(ctx,
				this.getTemplateInfo(), this.getScheme());
		this.personLocalInstance = PersonFactory.getLocalInstance(ctx);
		this.PersonDataSourceOptFacadeLocalInstance = PersonDataSourceOptFacadeFactory.getLocalInstance(ctx);
		this.PersonOtherInfoLocalInstance = PersonOtherInfoFactory.getLocalInstance(ctx);
		this.AffairBizCheckFacadeLocalInstance = AffairBizCheckFacadeFactory.getLocalInstance(ctx);
		this.bankLocalInstance = BankFactory.getLocalInstance(ctx);
		this.className = this.getClass().getName();
		String personName = row.getValueOfString("person.name");
		String number = row.getValueOfString("person.number");
		String personBirthday = row.getValueOfString("person.birthday");
		Boolean setBirthdayByIdCard = false;
		if (!StringUtils.isEmpty(personBirthday)) {
			setBirthdayByIdCard = false;
		} else {
			setBirthdayByIdCard = true;
		}
		PersonInfo personInfo = PersonDataSourceImportUtils.getPersonInfo(ctx, this.personLocalInstance, personName,
				number, locale);
		helper.setCurrentBillid(personInfo.getId().toString());
		DynamicModelInfo model = helper.convertRowToDynamicModel(row);
		HashMap<String, IObjectValue> saveModels = model.getSaveModelMap();
		PersonInfo person = (PersonInfo) saveModels.get("com.kingdee.eas.basedata.person.app.Person");
		String permItemId = this.getPermItemId();

		PersonDataSourceImportUtils.checkPersonLeffdt(row);

		PersonDataSourceImportUtils.calculateSocialServiceYears(ctx, saveModels);

		PersonDataSourceImportUtils.checkProjectExperience(saveModels);

		PersonDataSourceImportUtils.checkPersonWorkExp(saveModels);

		PersonDataSourceImportUtils.checkPersonRewardPunish(saveModels);

		PersonDataSourceImportUtils.validateDate(saveModels);

		PersonDataSourceImportUtils.checkPersonBasicInformation(ctx, this.personLocalInstance,
				this.AffairBizCheckFacadeLocalInstance, this.PersonOtherInfoLocalInstance, saveModels, row, permItemId,
				setBirthdayByIdCard, this.className);

		PersonDataSourceImportUtils.setPersonContactMethodInfo(saveModels, row);

		PersonDataSourceImportUtils.verifyAndCheckMobileAndEmail(ctx, saveModels, person.getId().toString());

		PersonDataSourceImportUtils.dealPersonBankRecord(ctx, saveModels, this.bankLocalInstance);

		PersonDataSourceImportUtils.setPersonModel(model);

		PersonDataSourceImportUtils.removePersonEntity(saveModels, this.isRemovePersonEntity);

		PersonDataSourceImportUtils.checkPersonCredentialsTypeModel(ctx, saveModels);

		this.PersonDataSourceOptFacadeLocalInstance.saveDataSource(saveModels);
	}

}
