package com.kingdee.shr.affair.web.handler.hrman;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.metadata.entity.*;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.hr.base.EmpPosOrgRelationCollection;
import com.kingdee.eas.hr.base.EmpPosOrgRelationFactory;
import com.kingdee.eas.hr.base.EmpPosOrgRelationInfo;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.util.StringUtils;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author qingwu
 * @date 2025/4/10
 * @apiNote 员工兼职终止（专员）
 */
public class PluralityDelBizBillHrManEditHandlerEx extends PluralityDelBizBillHrManEditHandler {
    /**
     * 选择兼职职位 带出兼职部门
     *
     * @param request
     * @param response
     * @param modelMap
     * @return
     * @throws SHRWebException
     * @throws EASBizException
     * @throws BOSException
     */
    public String getPluralityPositionDataExAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException, EASBizException, BOSException {
        Map map = new HashMap();
        int assignType = 1;
        PositionInfo positionInfo = null;
        AdminOrgUnitInfo adminOrgInfo = null;
        AdminOrgUnitInfo companyInfo = null;
        AdminOrgUnitInfo departmentInfo = null;
        Date beginDate = null;
        String positionId = request.getParameter("positionId");
        String personId = request.getParameter("personId");
        if (!StringUtils.isEmpty(positionId)) {
            EntityViewInfo entityView = new EntityViewInfo();
            SelectorItemCollection selectors = this.getEmpPosOrgRelSelectors();
            FilterInfo filterInfo = new FilterInfo();
            filterInfo.getFilterItems().add(new FilterItemInfo("assignType", 0));
            filterInfo.getFilterItems().add(new FilterItemInfo("position.id", positionId));
            filterInfo.getFilterItems().add(new FilterItemInfo("person.id", personId));
            //filterInfo.getFilterItems().add(new FilterItemInfo("LEFFDT", HRUtil.getMaxEndDate(), CompareType.GREATER_EQUALS));
            entityView.setSelector(selectors);
            entityView.setFilter(filterInfo);
            EmpPosOrgRelationCollection coll = EmpPosOrgRelationFactory.getRemoteInstance().getEmpPosOrgRelationCollection(entityView);
            EmpPosOrgRelationInfo empPosOrgRelationInfo = null;
            if (coll.size() > 0) {
                empPosOrgRelationInfo = coll.get(0);
                if (empPosOrgRelationInfo.getAssignType() != null && empPosOrgRelationInfo.getPosition() != null) {
                    assignType = empPosOrgRelationInfo.getAssignType().getValue();
                    positionInfo = empPosOrgRelationInfo.getPosition();
                    adminOrgInfo = empPosOrgRelationInfo.getPosition().getAdminOrgUnit();
                    companyInfo = empPosOrgRelationInfo.getPosition().getAdminOrgUnit().getCompany();
                    departmentInfo = empPosOrgRelationInfo.getPosition().getAdminOrgUnit().getDepartment();
                    beginDate = empPosOrgRelationInfo.getEFFDT();
                }
            }
        }

        map.put("assignType", assignType);
        map.put("position", positionInfo);
        map.put("adminOrgUnit", adminOrgInfo);
        map.put("company", companyInfo);
        map.put("department", departmentInfo);
        map.put("beginDate", beginDate);
        this.writeSuccessData(map);
        return null;
    }

    private SelectorItemCollection getEmpPosOrgRelSelectors() {
        SelectorItemCollection selectors = new SelectorItemCollection();
        selectors.add(new SelectorItemInfo("position.id"));
        selectors.add(new SelectorItemInfo("position.name"));
        selectors.add(new SelectorItemInfo("position.number"));
        selectors.add(new SelectorItemInfo("position.adminOrgUnit.id"));
        selectors.add(new SelectorItemInfo("position.adminOrgUnit.name"));
        selectors.add(new SelectorItemInfo("position.adminOrgUnit.displayName"));
        selectors.add(new SelectorItemInfo("position.adminOrgUnit.company.id"));
        selectors.add(new SelectorItemInfo("position.adminOrgUnit.company.name"));
        selectors.add(new SelectorItemInfo("position.adminOrgUnit.company.displayName"));
        selectors.add(new SelectorItemInfo("position.adminOrgUnit.department.id"));
        selectors.add(new SelectorItemInfo("position.adminOrgUnit.department.name"));
        selectors.add(new SelectorItemInfo("position.adminOrgUnit.department.displayName"));
        selectors.add(new SelectorItemInfo("EFFDT"));
        selectors.add(new SelectorItemInfo("assignType"));
        return selectors;
    }
}
