package com.kingdee.eas.custom.dormitorysystem.otherbasicdata.app;

import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.eas.basedata.org.AdminOrgUnitFactory;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.OrgException;
import com.kingdee.eas.custom.dormitorysystem.otherbasicdata.PersonDormitoryTypeInfo;
import com.kingdee.eas.custom.dormitorysystem.otherbasicdata.RoomTypeFactory;
import com.kingdee.shr.base.syssetting.BasicItemStateEnum;
import org.apache.log4j.Logger;
import javax.ejb.*;
import java.rmi.RemoteException;
import com.kingdee.bos.*;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.rule.RuleExecutor;
import com.kingdee.bos.metadata.MetaDataPK;
//import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;
import com.kingdee.bos.framework.ejb.AbstractBizControllerBean;
//import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.bos.service.IServiceContext;

import com.kingdee.eas.framework.app.DataBaseControllerBean;
import com.kingdee.eas.framework.ObjectBaseCollection;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import java.lang.String;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.framework.DataBaseCollection;
import com.kingdee.eas.custom.dormitorysystem.otherbasicdata.RoomTypeCollection;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.custom.dormitorysystem.otherbasicdata.RoomTypeInfo;

public class RoomTypeControllerBean extends AbstractRoomTypeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.dormitorysystem.otherbasicdata.app.RoomTypeControllerBean");
    @Override
    protected IObjectPK _save(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        RoomTypeInfo roomTypeInfo= (RoomTypeInfo)model;
        roomTypeInfo.setState(BasicItemStateEnum.getEnum(1));
        this._checkNumberDup(ctx,roomTypeInfo);
        return super._save(ctx, roomTypeInfo);
    }

    @Override
    protected IObjectPK[] _saveBatchData(Context ctx, IObjectCollection colls) throws BOSException, EASBizException {
        for (int i = 0; i < colls.size(); i++) {
            RoomTypeInfo roomTypeInfo = (RoomTypeInfo)(colls.getObject(i));
            roomTypeInfo.setState(BasicItemStateEnum.getEnum(1));
            this._checkNumberDup(ctx,roomTypeInfo);
        }
        return super._saveBatchData(ctx, colls);
    }
    public void _checkNumberDup(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        RoomTypeInfo roomTypeInfo = (RoomTypeInfo)model;
        if (roomTypeInfo.getId() == null) {
            FilterInfo filterInfo = new FilterInfo();
//            filterInfo.getFilterItems().add(new FilterItemInfo("type", new Integer(treeInfo.getType().getValue())));
            filterInfo.getFilterItems().add(new FilterItemInfo("number", roomTypeInfo.getNumber()));
            if (this._exists(ctx, filterInfo)) {
                throw new OrgException(OrgException.NUMBER_CAN_NOT_DUP);
            }
        } else {
            this.checkNumberDupOnUpdate(ctx, roomTypeInfo);
        }

    }

    private void checkNumberDupOnUpdate(Context ctx, RoomTypeInfo roomTypeInfo) throws BOSException, EASBizException {
        StringBuffer sql = (new StringBuffer()).append("select fid from CT_OTH_RoomType where fnumber = '")
                .append(roomTypeInfo.getNumber()).append("'");
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            conn = this.getConnection(ctx);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql.toString());
            if (rs.next()) {
                do {
                    String fid = rs.getString(1);
                    if (fid.equals(roomTypeInfo.getId().toString())) {
                        return;
                    }
                } while(rs.next());

                throw new OrgException(OrgException.NUMBER_CAN_NOT_DUP);
            }
        } catch (SQLException var8) {
            throw new BOSException(var8);
        }
    }
}