package com.kingdee.eas.custom.dormitorysystem.application.handler;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.ctrl.swing.StringUtils;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.base.ApproveTypeEnum;
import com.kingdee.eas.hr.base.HRBillBaseInfo;
import com.kingdee.eas.hr.base.IHRBillBase;
import com.kingdee.eas.hr.emp.PersonPositionFactory;
import com.kingdee.eas.hr.emp.PersonPositionInfo;
import com.kingdee.shr.base.syssetting.IMSFServiceFacade;
import com.kingdee.shr.base.syssetting.MSFServiceFacadeFactory;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

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

/**
 * @Description 申请单通用Handler
 * @Date 2025/4/29 9:30
 * @Created by Heyuan
 */
public class DormitoryApplicationEditHandler extends EditHandler {
    private static Logger logger = Logger.getLogger(DormitoryApplicationEditHandler.class);
    private Context ctx = SHRContext.getInstance().getContext();


    /**
     * 设置HR组织
     *
     * @param request
     * @param response
     * @param model
     * @throws SHRWebException
     */
    protected void beforeSave(
            HttpServletRequest request,
            HttpServletResponse response,
            CoreBaseInfo model
    ) throws SHRWebException {
        super.beforeSave(request, response, model);
        HRBillBaseInfo hrBillBaseInfo = (HRBillBaseInfo) model;
        HROrgUnitInfo hrOrgUnitInfo = new HROrgUnitInfo();
        hrOrgUnitInfo.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
        hrBillBaseInfo.setHrOrgUnit(hrOrgUnitInfo);
    }



    /**
     * 提交生效
     *
     * @param request
     * @param response
     * @param modelMap
     * @throws SHRWebException
     */
    public void submitEffectAction(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws SHRWebException {
        CoreBaseInfo model = (CoreBaseInfo) request.getAttribute("dynamic_model");
        this.beforeSave(request, response, model);
        HRBillBaseInfo hrBillBaseInfo = (HRBillBaseInfo) model;
        hrBillBaseInfo.setApproveType(ApproveTypeEnum.DIRECT);
        try {
            IHRBillBase ihrBillBase = (IHRBillBase) this.getBizInterface();
            IObjectPK objectPK = ihrBillBase.submitEffect(model);
            model.setId(BOSUuid.read(objectPK.toString()));
        } catch (EASBizException var8) {
            logger.error(var8);
            throw new ShrWebBizException(var8.getMessage(), var8);
        } catch (BOSException var9) {
            logger.error(var9);
            throw new ShrWebBizException(var9.getMessage(), var9);
        } catch (Exception var10) {
            logger.error(var10);
            throw new ShrWebBizException(var10);
        }
        this.afterSave(request, response, model);
        this.saveAttachment(request, response, model);
        //this.reverseAttachment(request, response, model);
        this.writeSuccessData(model.getId().toString());
    }

    /**
     * 根据人员id获取组织职位信息
     *
     * @param request
     * @param response
     * @param modelMap
     * @throws SHRWebException
     */
    public void getOrgPositionByPersonIdAction(
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws SHRWebException {
        String personId = request.getParameter("personId");
        if (StringUtils.isEmpty(personId)) {
            throw new ShrWebBizException("人员id不能为空!");
        }
        try {
            Map result = new HashMap();
            PersonPositionInfo pp = PersonPositionFactory.getLocalInstance(ctx).getPersonPositionInfo(
                    "select personDep.*,primaryPosition.* where person = '" + personId + "'");
            result.put("orgUnit", pp.getPersonDep());
            result.put("position", pp.getPrimaryPosition());
            JSONUtils.SUCCESS(result);
        } catch (Exception e) {
            throw new ShrWebBizException("获取员工任职信息报错: " + e.getMessage());
        }
    }
}
