package com.kingdee.eas.hr.affair.util;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.HROrgUnitFactory;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.hr.affair.ResignBizBillEntryCollection;
import com.kingdee.eas.hr.affair.ResignBizBillEntryFactory;
import com.kingdee.eas.hr.base.*;
import com.kingdee.eas.hr.base.app.EmpPosOrgRelationQueryHelper;
import com.kingdee.eas.hr.base.util.HRUtilExtend;
import com.kingdee.eas.hr.emp.*;
import com.kingdee.shr.base.bizmanage.AdminOrgBURelationCollection;
import com.kingdee.shr.base.bizmanage.AdminOrgBURelationFactory;
import com.kingdee.shr.certificationservice.util.CertificationUtil;
import com.kingdee.util.DateTimeUtils;

import java.util.Date;
import java.util.Map;

/**
 * 离职单_工具类
 * @author coyle
 * 202411
 */
public class ResignBillUtil {

	/**
	 * 用于获取人员上其他信息，如联系方式、证件信息、离职日期等
	 * @param ctx
	 * @param data
	 * @param personId
	 * @throws BOSException
	 * @throws EASBizException
	 */
	public static  void assemblePersonInfoDataMap(Context ctx, Map<String, Object> data, String personId)
			throws BOSException, EASBizException {
		PersonContactMethodInfo personContact = PersonContactMethodFactory.getLocalInstance(ctx)
				.getPersonContactMethodInfo("select email,nCell,globalRoaming where person.id = '" + personId + "'");
		data.put("personContact", personContact);
		PersonOtherInfoInfo personOther = PersonOtherInfoFactory.getLocalInstance(ctx).getPersonOtherInfoInfo(
				"select id,credentialsType.id,credentialsType.name,credentialsType.number where person.id = '"
						+ personId + "'");
		data.put("personOther", personOther);
		ResignBizBillEntryCollection resignBizBillEntryColl = ResignBizBillEntryFactory.getLocalInstance(ctx)
				.getResignBizBillEntryCollection(
						"select id,leftCompanyDate where person.id = '" + personId + "' order by bizDate desc");
		if (resignBizBillEntryColl.size() > 0) {
			data.put("leftCompanyDate", resignBizBillEntryColl.get(0).getLeftCompanyDate());
		}

		PersonInfo person = PersonFactory.getLocalInstance(ctx)
				.getPersonCollection("select number,idCardNO,passportNO where id = '" + personId + "'").get(0);
		data.put("person", person);
		PersonPositionInfo ppInfo = PersonPositionFactory.getLocalInstance(ctx).getPersonPositionInfo(
				"select personDep.id,personDep.name,primaryPosition.id,primaryPosition.name,enterDate where person.id = '"
						+ personId + "'");
		EmpLaborRelationInfo elpInfo = EmpLaborRelationFactory.getLocalInstance(ctx)
				.getEmpLaborRelationInfo("select enterDate where person.id = '" + personId + "'");
		AdminOrgUnitInfo adminOrg = ppInfo.getPersonDep();
		PositionInfo position = ppInfo.getPrimaryPosition();
		String adminOrgId = adminOrg.getId().toString();
		HROrgUnitInfo hrOrgUnitInfo = null;
		AdminOrgBURelationCollection adminBURelationCol = AdminOrgBURelationFactory.getLocalInstance(ctx)
				.getAdminOrgBURelationCollection("select id,manageHrOrg.id where adminOrg.id = '" + adminOrgId
						+ "' and bizManageType.id = '" + "SGuJm3nXW0GshQfKRXenomWJ1dE=" + "' and state = 1");
		Date effdt = DateTimeUtils.truncateDate(new Date());
		EmpPosOrgRelationQueryHelper helper = new EmpPosOrgRelationQueryHelper();
		SelectorItemCollection sic = new SelectorItemCollection();
		sic.add(new SelectorItemInfo("person.name"));
		sic.add(new SelectorItemInfo("position.name"));
		sic.add(new SelectorItemInfo("adminorg.name"));
		sic.add(new SelectorItemInfo("adminorg.displayName"));
		EmpPosOrgRelationInfo empPosInfo = helper.getAppointDateActiveMainlinePerson(ctx, personId, effdt, sic);
		String hrOrgUnitId;
		if (empPosInfo != null) {
			EmpHRORelationHisCollection empOrgBURelationHisColl = EmpHRORelationHisFactory.getLocalInstance(ctx)
					.getEmpHRORelationHisCollection(
							"where state = 1 and empOrgRelation.id = '" + empPosInfo.getId().toString() + "'");
			if (empOrgBURelationHisColl.size() > 0) {
				hrOrgUnitId = empOrgBURelationHisColl.get(0).getHrOrgUnit().getId().toString();
				hrOrgUnitInfo = HROrgUnitFactory.getLocalInstance(ctx)
						.getHROrgUnitInfo("select id,name where id = '" + hrOrgUnitId + "'");
			}
		}

		LabContractFirstPartyInfo labContractFirstPartyInfo = CertificationUtil.getLabContractFirstPartyInfo(ctx,
				person);
		data.put("hrOrgUnit", hrOrgUnitInfo);
		data.put("adminOrg", adminOrg);
		data.put("position", position);
		data.put("labContractFirstParty", labContractFirstPartyInfo);
		hrOrgUnitId = elpInfo.getEnterDate() != null ? HRUtilExtend.format(elpInfo.getEnterDate(), "yyyy-MM-dd") : "";
		data.put("enterDate", hrOrgUnitId);
	}
}
