package com.kingdee.shr.customer.gtiit.osf;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.bsf.service.app.IHRMsfService;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.hr.base.HRBizDefineFactory;
import com.kingdee.eas.hr.base.util.HRUtil;
import com.kingdee.eas.hr.emp.web.service.GetPersonPartPosOrgInfoService;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.util.LocaleUtils;
import com.kingdee.shr.osf.service.ServiceLocator;
import com.kingdee.util.StringUtils;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * description: GetPersonPartPosOrgInfoServiceEx <br>
 * date: 2025/4/1 18:39 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */
public class GetPersonPartPosOrgInfoServiceEx extends GetPersonPartPosOrgInfoService {

    public Object process(Context ctx, Map param) throws EASBizException, BOSException {
        String uiClass = (String)param.get("uiClass");
        List HRBizDefineList = new ArrayList();

        if (!StringUtils.isEmpty(uiClass)) {
            IHRMsfService rs = (IHRMsfService) ServiceLocator.getInstance().lookupService(ctx, "getHRBizDefineByUiClass");
            HashMap list = new HashMap();
            list.put("uiClass", uiClass);
            HRBizDefineList = (List)rs.process(ctx, list);
        }
        IRowSet rs = this.getResult(ctx, param);
        List<Map<String, Object>> list = this.assembleData(ctx, rs, (List)HRBizDefineList);
        return list;
    }

    private List<Map<String, Object>> assembleData(Context ctx, IRowSet rs, List HRBizDefineList) throws BOSException, EASBizException {
        Map<String, Object> positionDataMap = null;
        Map<String, Object> adminOrgDataMap = null;
        Map<String, Object> companyDataMap = null;
        Map<String, Object> departmentDataMap = null;
        Map<String, Object> personDataMap = null;
        Map<String, Object> empTypeDataMap = null;
        Map<String, Object> ppDataMap = null;
        Map<String, Object> oldJobDataMap = null;
        Map<String, Object> dataMap = null;
        Map<String, Object> empLaborRelationDataMap = null;
        Map<String, Object> empPostRankDataMap = null;
        Map<String, Object> hrOrgUnitDataMap = null;
        Map<String, Object> oldPositionDataMap = null;
        Map<String, Object> oldadminOrgDataMap = null;
        Map<String, Object> oldCompany = null;
        Map<String, Object> oldDepartment = null;
        ArrayList list = new ArrayList();

        try {
            while(rs.next()) {
                positionDataMap = new HashMap();
                positionDataMap.put("id", rs.getString("id"));
                positionDataMap.put("number", rs.getString("posnumber"));
                positionDataMap.put("name", rs.getString("name"));
                positionDataMap.put("positionTypeName", rs.getString("positionTypeName"));
                oldPositionDataMap = new HashMap();
                oldPositionDataMap.put("id", rs.getString("parttimeid"));
                oldPositionDataMap.put("number", rs.getString("parttimeposnumber"));
                oldPositionDataMap.put("name", rs.getString("parttimename"));
                oldPositionDataMap.put("positionTypeName", rs.getString("parttimepositionTypeName"));
                oldPositionDataMap.put("beginDate", rs.getString("parttimebeginDate"));
                oldCompany = new HashMap();
                oldCompany.put("name", rs.getString("parttimecompanyName"));
                oldDepartment = new HashMap();
                oldDepartment.put("name", rs.getString("parttimedepartmentName"));
                oldadminOrgDataMap = new HashMap();
                oldadminOrgDataMap.put("id", rs.getString("oldAdminOrgId"));
                oldadminOrgDataMap.put("number", rs.getString("oldAdminOrgNumber"));
                oldadminOrgDataMap.put("longNumber", rs.getString("oldAdminOrgLogNumber"));
                oldadminOrgDataMap.put("name", rs.getString("oldAdminOrgName"));
                oldadminOrgDataMap.put("displayName", rs.getString("oldAdminDisplayName"));
                empTypeDataMap = new HashMap();
                empTypeDataMap.put("id", rs.getString("empTypeID"));
                empTypeDataMap.put("name", rs.getString("empTypeName"));
                empTypeDataMap.put("number", rs.getString("empTypeNumber"));
                List HRBizMatchEmpTypeList = new ArrayList();

                for(int i = 0; i < HRBizDefineList.size(); ++i) {
                    HashMap defMap = (HashMap)HRBizDefineList.get(i);
                    String HRBizDefineId = defMap.get("id").toString();
                    boolean defineIsOn = false;

                    try {
                        defineIsOn = HRBizDefineFactory.getRemoteInstance().exists("where id='" + HRBizDefineId + "' and state=1");
                    } catch (EASBizException var30) {
                        var30.printStackTrace();
                    }

                    List inEmpTypeList = (List)defMap.get("inEmpType");
                    boolean canMatchInEmpType = false;

                    for(int j = 0; j < inEmpTypeList.size(); ++j) {
                        HashMap inEmpType = (HashMap)inEmpTypeList.get(j);
                        if (inEmpType.get("id").equals(rs.getString("empTypeID"))) {
                            canMatchInEmpType = true;
                        }
                    }

                    if (canMatchInEmpType && defineIsOn) {
                        HRBizMatchEmpTypeList.add(defMap);
                    }

                    if (inEmpTypeList.size() == 0) {
                        HRBizMatchEmpTypeList.add(defMap);
                    }
                }

                adminOrgDataMap = new HashMap();
                adminOrgDataMap.put("id", rs.getString("adminOrgId"));
                adminOrgDataMap.put("number", rs.getString("adminOrgNumber"));
                adminOrgDataMap.put("longNumber", rs.getString("adminOrgLogNumber"));
                adminOrgDataMap.put("name", rs.getString("adminOrgName"));
                adminOrgDataMap.put("displayName", rs.getString("adminDisplayName"));
                personDataMap = new HashMap();
                personDataMap.put("id", rs.getString("personId"));
                personDataMap.put("name", rs.getString("personName"));
                personDataMap.put("number", rs.getString("personNumber"));
                personDataMap.put("gender", rs.getString("gender"));
                personDataMap.put("idCardNO", rs.getString("idCardNO"));
                personDataMap.put("nativePlace", rs.getString("nativePlace"));
                personDataMap.put("regresidenceName", rs.getString("regresidenceName"));
                personDataMap.put("regresidenceId", rs.getString("regresidenceId"));
                personDataMap.put("hjAddress", rs.getString("hjAddress"));
                personDataMap.put("cell", rs.getString("personCell"));
                personDataMap.put("birthday", rs.getString("birthday"));
                personDataMap.put("hrPoliticalName", rs.getString("hrPoliticalName"));
                personDataMap.put("hrStandingName", rs.getString("hrStandingName"));
                ppDataMap = new HashMap();
                empLaborRelationDataMap = new HashMap();
                empLaborRelationDataMap.put("enterDate", rs.getDate("enterDate"));
                empLaborRelationDataMap.put("planFormalDate", rs.getDate("planFormalDate"));
                empLaborRelationDataMap.put("formalDate", rs.getDate("planFormalDate"));
                empLaborRelationDataMap.put("probation", rs.getInt("tryoutMonth"));
                empLaborRelationDataMap.put("employerUnitId", rs.getString("employerUnitId"));
                empLaborRelationDataMap.put("employerUnitName", rs.getString("employerUnitName"));
                oldJobDataMap = new HashMap();
                oldJobDataMap.put("id", rs.getString("jobId"));
                oldJobDataMap.put("name", rs.getString("jobName"));
                companyDataMap = new HashMap();
                companyDataMap.put("id", rs.getString("companyId"));
                companyDataMap.put("number", rs.getString("companyNumber"));
                companyDataMap.put("longNumber", rs.getString("companyLongNumber"));
                companyDataMap.put("name", rs.getString("companyName"));
                companyDataMap.put("displayName", rs.getString("companyDisplayName"));
                departmentDataMap = new HashMap();
                departmentDataMap.put("id", rs.getString("departmentId"));
                departmentDataMap.put("number", rs.getString("departmentNumber"));
                departmentDataMap.put("longNumber", rs.getString("departmentLongNumber"));
                departmentDataMap.put("name", rs.getString("departmentName"));
                departmentDataMap.put("displayName", rs.getString("departmentDisplayName"));
                departmentDataMap.put("departmentExId", rs.getString("departmentExId"));
                departmentDataMap.put("displayExName", rs.getString("departmentExDisplayName"));
                adminOrgDataMap.put("department", departmentDataMap);
                empPostRankDataMap = new HashMap();
                empPostRankDataMap.put("jobGradeId", rs.getString("jobGradeId"));
                empPostRankDataMap.put("jobGradeName", rs.getString("jobGradeName"));
                empPostRankDataMap.put("jobLevelId", rs.getString("jobLevelId"));
                empPostRankDataMap.put("jobLevelName", rs.getString("jobLevelName"));
                hrOrgUnitDataMap = new HashMap();
                hrOrgUnitDataMap.put("id", rs.getString("hrOrgUnitId"));
                hrOrgUnitDataMap.put("name", rs.getString("hrOrgUnitName"));
                dataMap = new HashMap();
                dataMap.put("adminOrg", adminOrgDataMap);
                dataMap.put("company", companyDataMap);
                dataMap.put("department", departmentDataMap);
                dataMap.put("position", positionDataMap);
                dataMap.put("person", personDataMap);
                dataMap.put("empType", empTypeDataMap);
                dataMap.put("empLaborRelation", empLaborRelationDataMap);
                dataMap.put("pp", ppDataMap);
                dataMap.put("oldJob", oldJobDataMap);
                dataMap.put("hrbizdefine", HRBizMatchEmpTypeList);
                dataMap.put("oldEmpPostRank", empPostRankDataMap);
                dataMap.put("hrOrgUnit", hrOrgUnitDataMap);
                dataMap.put("oldPosition", oldPositionDataMap);
                dataMap.put("oldCompany", oldCompany);
                dataMap.put("oldDepartment", oldDepartment);
                dataMap.put("oldAdminOrg", oldadminOrgDataMap);
                list.add(dataMap);
            }

            return list;
        } catch (SQLException var31) {
            throw new BOSException(var31);
        }
    }

    private IRowSet getResult(Context ctx, Map param) throws BOSException {
        String personID = (String)param.get("personID");
        String oldPositionID = (String)param.get("oldPositionID");
        String leffdtTS = HRUtil.getMaxEndDateTs();
        String l = LocaleUtils.getShortCode(ctx);

        StringBuffer sql = new StringBuffer();
        sql.append(" select position.fid as id,position.fnumber as posnumber,position.fname_" + l + " as name,").append(" parttimeposition.fid as parttimeid,parttimeposition.fnumber as parttimeposnumber,parttimeposition.fname_" + l + " as parttimename,").append(" parttime.feffdt as parttimebeginDate,").append(" person.fid as personId , person.fname_" + l + " as personName, person.fnumber as personNumber, person.fcell as personCell, ").append(" person.FGender as gender , person.FIdCardNO as idCardNO,person.FBIRTHDAY as birthday,").append(" person.FnativePlace_" + l + " as nativePlace,person.FHJAddress as hjAddress, ").append(" hrPolitical.fname_" + l + " as hrPoliticalName, ").append(" hrStanding.fname_" + l + " as hrStandingName, ").append(" positionType.fname_" + l + " as positionTypeName, ").append(" parttimepositionType.fname_" + l + " as parttimepositionTypeName, ").append(" regresidence.fname_" + l + " as regresidenceName, ").append(" regresidence.fid as regresidenceId, ").append(" adminOrg.fid as adminOrgId , adminOrg.fname_" + l + " as adminOrgName, ").append(" adminOrg.FNUMBER as adminOrgNumber , adminOrg.FLongNumber as adminOrgLogNumber ,").append(" adminOrg.FDISPLAYNAME_" + l + " as adminDisplayName, ").append(" parttimeadminOrg.fid as oldAdminOrgId , parttimeadminOrg.fname_" + l + " as oldAdminOrgName, ").append(" parttimeadminOrg.FNUMBER as oldAdminOrgNumber , parttimeadminOrg.FLongNumber as oldAdminOrgLogNumber ,").append(" parttimeadminOrg.FDISPLAYNAME_" + l + " as oldAdminDisplayName, ").append(" company.fid as companyId , company.fname_" + l + " as companyName, ").append(" company.FNUMBER as companyNumber , company.FLongNumber as companyLongNumber ,").append(" company.FDISPLAYNAME_" + l + " as companyDisplayName, ").append(" parttimecompany.fid as parttimecompanyId , parttimecompany.fname_" + l + " as parttimecompanyName, ").append(" department.fid as departmentId , department.fname_" + l + " as departmentName, ").append(" department.FNUMBER as departmentNumber , department.FLongNumber as departmentLongNumber ,").append(" department.FDISPLAYNAME_" + l + " as departmentDisplayName, ").append(" parttimedepartment.fid as parttimedepartmentId , parttimedepartment.fname_" + l + " as parttimedepartmentName, ").append(" departmentEx.FId as departmentExId, ").append(" departmentEx.FDepDisplayName_" + l + " as departmentExDisplayName, ").append(" hrOrgUnit.fid as hrOrgUnitId, hrOrgUnit.fname_" + l + " as hrOrgUnitName, ").append(" empType.fid as empTypeID, ").append(" empType.fnumber as empTypeNumber, ").append(" empType.fname_" + l + " as empTypeName, ").append(" oldJob.fid as jobId , oldJob.fname_" + l + " as jobName , ").append(" oldJobGrade.fid as jobGradeId , oldJobGrade.fname_" + l + " as jobGradeName ,").append(" oldJobLevel.fid as jobLevelId , oldJobLevel.fname_" + l + " as jobLevelName ,").append(" laborRelation.FEmployerUnitID as employerUnitId, laborRelation.fplanFormalDate planFormalDate, ").append(" laborRelation.FTryoutMonth tryoutMonth,laborRelation.fenterDate enterDate, ").append(" employerUnit.fname_" + l + " employerUnitName ").append(" from t_bd_person person ").append("\tinner join t_hr_emporgrelation relation on person.fid = relation.fpersonid ").append("\tinner join t_hr_emporgrelation parttime on person.fid = parttime.fpersonid ").append("\tleft join t_hr_emplaborRelation laborRelation on relation.FlaborRelationID = laborRelation.fid ").append("   left join t_org_hro employerUnit on laborRelation.FemployerUnitID = employerUnit.fid ").append("\tleft join t_org_position position on relation.FPositionID=position.fid").append("\tleft join t_org_position parttimeposition on parttime.FPositionID=parttimeposition.fid").append("\tleft join T_ORG_Admin adminOrg on relation.FADMINORGID =adminOrg.fid").append("\tleft join T_ORG_Admin company on adminOrg.FCompanyID =company.fid").append("\tleft join T_ORG_Admin department on adminOrg.FDepartmentID =department.fid").append("\tleft join T_ORG_Admin parttimeadminOrg on parttime.FADMINORGID =parttimeadminOrg.fid").append("\tleft join T_ORG_Admin parttimecompany on parttimeadminOrg.FCompanyID =parttimecompany.fid").append("\tleft join T_ORG_Admin parttimedepartment on parttimeadminOrg.FDepartmentID =parttimedepartment.fid").append("\tleft join T_HR_SHRAdminOrgUnitExtend departmentEx on department.fid =departmentEx.FADMINORGUNITID").append("\tleft join T_HR_PersonPosition pp on pp.fpersonid =person.fid").append("\tleft join T_BD_HRPolitical hrPolitical on hrPolitical.fid =person.FPoliticalFaceID").append("\tleft join T_BD_HRStanding hrStanding on hrStanding.fid =person.FStandingID").append("\tleft join T_BD_PositionType positionType on positionType.fid =position.FPositionTypeID").append("\tleft join T_BD_PositionType parttimepositionType on parttimepositionType.fid =parttimeposition.FPositionTypeID").append("\tleft join T_BD_Regpermresidence regresidence on regresidence.fid =person.FRegresidenceID").append("\tleft join T_HR_BDEmployeeType empType on empType.fid =person.FEmployeeTypeID").append("\tleft join T_ORG_Job oldJob on oldJob.fid =position.FJobID").append("   left join T_HR_EmpPostRank empPostRank on empPostRank.fpersonId = person.fid and  empPostRank.FLEFFDT=").append(leffdtTS).append("\tleft join T_HR_JobGrade oldJobGrade on oldJobGrade.fid = empPostRank.FJobGradeID ").append("   left join T_HR_JobLevel oldJobLevel on oldJobLevel.fid = empPostRank.FJobLevelID ").append("\tleft join t_hr_emphrorelationhis personBURel on ( personBURel.fpersonid = person.fid and personBURel.FEmpOrgRelationID = relation.fid").append("\tand personBURel.FBizManageTypeID = '").append("SGuJm3nXW0GshQfKRXenomWJ1dE=").append("' and personBURel.Fstate=1)").append("\tleft join T_ORG_HRO hrOrgUnit on personBURel.FHrOrgUnitID = hrOrgUnit.fid").append("\twhere person.fid=").append("?").append("\tand relation.FLEFFDT=").append(leffdtTS).append("\tand relation.FAssignType= ").append(1).append("\tand relation.Fisinner= ").append(1)
                .append("\tand parttime.FLEFFDT>=").append(" now() ")
                .append("\tand parttime.FAssignType= ").append(0).append("\tand parttime.Fisinner= ").append(1).append("\tand parttime.FPositionID= ").append("?").append("\torder by relation.fAssignType desc");
        Object[] params = new Object[]{personID, oldPositionID};
        return DbUtil.executeQuery(ctx, sql.toString(), params);
    }
}
