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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.common.EASBizException;
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.custom.dormitorysystem.occupants.IOccupants;
import com.kingdee.eas.custom.dormitorysystem.occupants.OccupantsFactory;
import com.kingdee.eas.custom.dormitorysystem.occupants.OccupantsInfo;
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.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 org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @author qingwu
 * @date 2025/4/28
 * @apiNote
 */
public class RoomChangeApplicationBatchEditHandler extends EditHandler {
    private Context ctx = SHRContext.getInstance().getContext();
    private Logger logger = Logger.getLogger(this.getClass());


    @Override
    protected void afterInitModel(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo coreBaseInfo) throws SHRWebException {
        logger.error("afterInitModel");
        RoomChangeApplicationInfo changeApplicationInfo = (RoomChangeApplicationInfo) coreBaseInfo;
        RoomChangeApplicationEntryCollection entrys = changeApplicationInfo.getEntrys();
        try {
            IOccupants iOccupants = OccupantsFactory.getLocalInstance(ctx);
            for (int i = 0; i < entrys.size(); i++) {
                RoomChangeApplicationEntryInfo entryInfo = entrys.get(i);
                OccupantsInfo oldOccupants = entryInfo.getOldOccupants();
                entryInfo.setOldOccupants(iOccupants.getOccupantsInfo(new ObjectUuidPK(oldOccupants.getId())));
            }
        } catch (BOSException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (EASBizException e) {
            throw new RuntimeException(e);
        }
        super.afterInitModel(request, response, coreBaseInfo);
    }

    @Override
    protected void beforeSave(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model) throws SHRWebException {
        //super.beforeSave(request, response, model);
        RoomChangeApplicationInfo changeApplicationInfo = (RoomChangeApplicationInfo) model;
        HROrgUnitInfo hrOrgUnitInfo = new HROrgUnitInfo();
        hrOrgUnitInfo.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
        changeApplicationInfo.setHrOrgUnit(hrOrgUnitInfo);
        changeApplicationInfo.setIsMultiEntry(true);
        //校验
    }

    /**
     * 提交生效
     *
     * @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());
    }

    @Override
    protected void afterCreateNewModel(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo coreBaseInfo) throws SHRWebException {
        super.afterInitModel(request, response, coreBaseInfo);
        RoomChangeApplicationInfo roomChangeApplicationInfo = (RoomChangeApplicationInfo) coreBaseInfo;
        try {
            RoomChangeApplicationEntryCollection entrys = roomChangeApplicationInfo.getEntrys();
            RoomChangeApplicationEntryInfo roomChangeApplicationEntryInfo = new RoomChangeApplicationEntryInfo();
            //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);
            UserInfo user = ContextUtil.getCurrentUserInfo(ctx);
            roomChangeApplicationInfo.setCreator(user);
            roomChangeApplicationInfo.setCreateTime(new Timestamp(new Date().getTime()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ShrWebBizException(e);
        }
    }
}
