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

import com.kingdee.bos.Context;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.custom.dormitorysystem.application.RoomChangeApplicationEntryCollection;
import com.kingdee.eas.custom.dormitorysystem.application.RoomChangeApplicationEntryInfo;
import com.kingdee.eas.custom.dormitorysystem.application.RoomChangeApplicationInfo;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.emp.IPersonPosition;
import com.kingdee.eas.hr.emp.PersonPositionFactory;
import com.kingdee.eas.hr.emp.PersonPositionInfo;
import com.kingdee.eas.util.app.ContextUtil;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author qingwu
 * @date 2025/4/28
 * @apiNote ABK_换宿申请单-员工宿舍
 */
public class RoomChangeApplicationSelfEditHandler extends HRBillBaseEditHandler {

    private Context ctx = SHRContext.getInstance().getContext();

    @Override
    protected void afterCreateNewModel(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo coreBaseInfo) throws SHRWebException {
        super.afterInitModel(request, response, coreBaseInfo);
        RoomChangeApplicationInfo roomChangeApplicationInfo = (RoomChangeApplicationInfo) coreBaseInfo;
        UserInfo user = ContextUtil.getCurrentUserInfo(ctx);
        PersonInfo personInfo = user.getPerson();
        try {
            IPersonPosition iPersonPosition = PersonPositionFactory.getLocalInstance(ctx);
            PersonPositionInfo wherePerson = iPersonPosition.
                    getPersonPositionInfo("select person.id, person.name,personDep.id,personDep.name,primaryPosition.id,primaryPosition.name  " +
                            "where  person = '" + personInfo.getId().toString() + "'");
            RoomChangeApplicationEntryCollection entrys = roomChangeApplicationInfo.getEntrys();
            RoomChangeApplicationEntryInfo roomChangeApplicationEntryInfo = new RoomChangeApplicationEntryInfo();
            roomChangeApplicationEntryInfo.setPerson(wherePerson.getPerson());//人员
            roomChangeApplicationEntryInfo.setAdminOrg(wherePerson.getPersonDep());//组织
            roomChangeApplicationEntryInfo.setPosition(wherePerson.getPrimaryPosition());//职位
            //managementNotes  宿舍管理说明
            StringBuilder managementNotes = new StringBuilder();
            managementNotes.append("本人愿意遵守以下条款：").append("\n");
            managementNotes.append("1、自觉遵守公司《宿舍管理制度》和员工宿舍区有关管理办法，不私自调换宿舍和床位。").append("\n");
            managementNotes.append("2、自觉维护宿舍公共区域的卫生，爱护公物，损坏公物自觉赔偿。").append("\n");
            managementNotes.append("3、自觉遵守消防安全，节约用电用水。").append("\n");
            roomChangeApplicationInfo.setManagementNotes(managementNotes.toString());
            entrys.add(roomChangeApplicationEntryInfo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }
    }
}
