package com.kingdee.eas.custom.dormitorysystem.occupants.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.dormitory.DormitoryInfo;
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.custom.dormitorysystem.occupants.OccupantsCollection;
import com.kingdee.eas.framework.ObjectBaseCollection;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.eas.custom.dormitorysystem.occupants.OccupantsInfo;
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 java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

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.bos.metadata.entity.SelectorItemCollection;

public class OccupantsControllerBean extends AbstractOccupantsControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.dormitorysystem.occupants.app.OccupantsControllerBean");

    @Override
    protected IObjectPK _save(Context ctx,  IObjectValue model) throws BOSException, EASBizException {
        OccupantsInfo occupantsInfo = (OccupantsInfo)model;
        occupantsInfo.setState(BasicItemStateEnum.getEnum(1));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = dateFormat.parse("2199-12-31");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        occupantsInfo.setCheckOutDate(parse);
        this._checkNumberDup(ctx,occupantsInfo);
        return super._save(ctx, occupantsInfo);
    }

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

    }

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

                throw new OrgException(OrgException.NUMBER_CAN_NOT_DUP);
            }
        } catch (SQLException var8) {
            throw new BOSException(var8);
        }
    }
    @Override
    protected IObjectPK[] _saveBatchData(Context ctx, IObjectCollection colls) throws BOSException, EASBizException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        for (int i = 0; i < colls.size(); i++) {
            OccupantsInfo occupantsInfo = (OccupantsInfo)(colls.getObject(i));
            this._checkNumberDup(ctx,occupantsInfo);
            try {
                parse = dateFormat.parse("2199-12-31");
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            occupantsInfo.setCheckOutDate(parse);
            occupantsInfo.setState(BasicItemStateEnum.getEnum(1));
        }
        return super._saveBatchData(ctx, colls);
    }

}