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

import com.kingdee.bos.metadata.entity.*;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.basedata.org.OrgException;
import com.kingdee.eas.basedata.org.OrgTreeInfo;
import com.kingdee.eas.framework.*;
import com.kingdee.eas.framework.util.FilterUtility;
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.bos.dao.IObjectPK;

import java.lang.String;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryCollection;
import com.kingdee.eas.custom.dormitorysystem.dormitory.DormitoryInfo;
import com.kingdee.eas.framework.app.TreeBaseControllerBean;
import com.kingdee.eas.common.EASBizException;

public class DormitoryControllerBean extends AbstractDormitoryControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.dormitorysystem.dormitory.app.DormitoryControllerBean");

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

    @Override
    protected IObjectPK[] _saveBatchData(Context ctx, IObjectCollection colls) throws BOSException, EASBizException {
        for (int i = 0; i < colls.size(); i++) {
            DormitoryInfo dormitoryInfo = (DormitoryInfo)(colls.getObject(i));
            dormitoryInfo.setState(BasicItemStateEnum.getEnum(1));
            dormitoryInfo.setIsSealUp(false);
            this._checkNumberDup(ctx,dormitoryInfo);
        }
        return super._saveBatchData(ctx, colls);
    }

    public void _checkNumberDup(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        DormitoryInfo dormitoryInfo = (DormitoryInfo)model;
        if (dormitoryInfo.getId() == null) {
            FilterInfo filterInfo = new FilterInfo();
//            filterInfo.getFilterItems().add(new FilterItemInfo("type", new Integer(treeInfo.getType().getValue())));
            filterInfo.getFilterItems().add(new FilterItemInfo("number", dormitoryInfo.getNumber()));
            if (this._exists(ctx, filterInfo)) {
                throw new OrgException(OrgException.NUMBER_CAN_NOT_DUP);
            }
        } else {
            this.checkNumberDupOnUpdate(ctx, dormitoryInfo);
        }

    }

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

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