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.OrgException;
import com.kingdee.eas.custom.dormitorysystem.otherbasicdata.DormitoryTypeInfo;
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.custom.dormitorysystem.otherbasicdata.PersonDormitoryTypeCollection;
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.common.EASBizException;
import com.kingdee.eas.custom.dormitorysystem.otherbasicdata.PersonDormitoryTypeInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;

public class PersonDormitoryTypeControllerBean extends AbstractPersonDormitoryTypeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.dormitorysystem.otherbasicdata.app.PersonDormitoryTypeControllerBean");

    @Override
    protected IObjectPK _save(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        PersonDormitoryTypeInfo personDormitoryTypeInfo = (PersonDormitoryTypeInfo)model;
        personDormitoryTypeInfo.setState(BasicItemStateEnum.getEnum(1));
        this._checkNumberDup(ctx,personDormitoryTypeInfo);
        return super._save(ctx, personDormitoryTypeInfo);
    }

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

    }

    private void checkNumberDupOnUpdate(Context ctx, PersonDormitoryTypeInfo personDormitoryTypeInfo) throws BOSException, EASBizException {
        StringBuffer sql = (new StringBuffer()).append("select fid from CT_OTH_PersonDormitoryType where fnumber = '")
                .append(personDormitoryTypeInfo.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(personDormitoryTypeInfo.getId().toString())) {
                        return;
                    }
                } while(rs.next());

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