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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
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.RoomChangeApplicationFactory;
import com.kingdee.eas.custom.dormitorysystem.application.RoomChangeApplicationInfo;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryInfo;
import com.kingdee.eas.custom.dormitorysystem.waterelectricity.*;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.base.HRBillBaseInfo;
import com.kingdee.eas.scm.util.datetime.DateTimeUtil;
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 org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

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

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

    @Override
    protected void beforeSubmit(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model) throws SHRWebException {
        HRBillBaseInfo hrBillBaseInfo = (HRBillBaseInfo) model;
        HROrgUnitInfo hrOrgUnitInfo = new HROrgUnitInfo();
        hrOrgUnitInfo.setId(BOSUuid.read("00000000-0000-0000-0000-000000000000CCE7AED4"));
        hrBillBaseInfo.setHrOrgUnit(hrOrgUnitInfo);
        super.beforeSubmit(request, response, model);
    }

    @Override
    public void againstApproveAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws SHRWebException, BOSException {
        logger.error("againstApproveAction---");
        String billId = this.getBillId(request);
        try {
            RoomChangeApplicationInfo roomChangeApplicationInfo = RoomChangeApplicationFactory.getLocalInstance(ctx)
                    .getRoomChangeApplicationInfo("select *, entrys.* ,entrys.person.* ,entrys.adjustRoom.* where id = '" + billId + "'");
            RoomChangeApplicationEntryCollection entrys = roomChangeApplicationInfo.getEntrys();
            RoomChangeApplicationEntryInfo entryInfo = entrys.get(0);
            //换宿日期
            Date checkInDate = entryInfo.getCheckInDate();
            //调整房间
            DormitoryInfo adjustRoom = entryInfo.getAdjustRoom();
            //人员
            PersonInfo person = entryInfo.getPerson();
            //年月
            String yearMonth = DateTimeUtil.getFmtDate(checkInDate, "yyyy-MM");
            logger.error("yearMonth---" + yearMonth);
            //水电明细
            IWaterElectricity iWaterElectricity = WaterElectricityFactory.getLocalInstance(ctx);
            String sql = "where person.id= '" + person.getId() + "' and yearMonth = '" + yearMonth + "' and dormitory.id = '" + adjustRoom.getId() + "'";
            logger.error("sql---" + sql);
            boolean exists = iWaterElectricity.exists(sql);
            logger.error("exists---" + exists);
            if (exists) {
                throw new ShrWebBizException(person.getName() + "已经在" + yearMonth + ",产生水电明细不能进行反审批！！");
            }
        } catch (EASBizException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        super.againstApproveAction(request, response, modelMap);
    }
}
