package com.kingdee.eas.custom.interfacelog.businessoperationlog.app;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.metadata.entity.EntityObjectInfo;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.framework.*;
import com.kingdee.eas.hr.affair.EmpEnrollBizBillEntryCollection;
import com.kingdee.eas.hr.affair.EmpEnrollBizBillEntryInfo;
import com.kingdee.eas.hr.affair.EmpEnrollBizBillInfo;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.base.syssetting.util.MetaDataUtil;
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.CoreBillBaseControllerBean;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.eas.custom.interfacelog.businessoperationlog.OperateLogCollection;

import java.lang.String;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import com.kingdee.eas.custom.interfacelog.businessoperationlog.OperateLogInfo;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;

/**
 * OperateLogControllerBean 类负责处理操作日志的业务逻辑，继承自 AbstractOperateLogControllerBean。
 * 该类重写了 _addnew 方法，用于在添加新操作日志时进行额外的业务信息处理。
 */
public class OperateLogControllerBean extends AbstractOperateLogControllerBean {
    // 定义日志记录器，用于记录该类的相关日志信息
    private static Logger logger =
            Logger.getLogger("com.kingdee.eas.custom.interfacelog.businessoperationlog.app.OperateLogControllerBean");

    /**
     * 重写父类的 _addnew 方法，在添加新操作日志时进行额外的业务信息处理。
     *
     * @param ctx   上下文对象，包含了操作的环境信息
     * @param model 要添加的对象值，这里是操作日志信息
     * @return 返回添加对象的主键
     * @throws BOSException    当发生 BOS 相关异常时抛出
     * @throws EASBizException 当发生 EAS 业务异常时抛出
     */
    @Override
    protected IObjectPK _addnew(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        // 将传入的对象值转换为 OperateLogInfo 类型
        OperateLogInfo operateLogInfo = (OperateLogInfo) model;
        // 获取操作日志中的业务 ID
        String businessId = operateLogInfo.getBusinessId();
        // 检查业务 ID 是否存在且不为空
        if (businessId != null && !businessId.isEmpty()) {
            // 将业务 ID 转换为 BOSUuid 对象
            BOSUuid read = BOSUuid.read(businessId);
            // 获取 BOSUuid 对象的类型
            BOSObjectType type = read.getType();
            // 根据 BOS 类型获取实体对象信息
            EntityObjectInfo entityObjectInfo = MetaDataUtil.getEntityObjectByBosType(type);
            // 获取实体对象的别名
            String alias = entityObjectInfo.getAlias();

            // 设置操作日志的 BOS 类型
            operateLogInfo.setBosType(type.toString());
            // 设置操作日志的业务名称
            operateLogInfo.setBusinessName(alias);

            // 创建 BOS 对象实例
            ICoreBase bosObject = (ICoreBase) (BOSObjectFactory.createBOSObject(ctx, type));
            // 根据业务 ID 获取对象集合

            CoreBaseCollection collection = bosObject.getCollection(" where id = '" + businessId + "'");

            // 检查集合中是否有元素
            if (collection.size() > 0) {
                // 获取集合中的第一个元素
                CoreBaseInfo coreBaseInfo = collection.get(0);
                if (coreBaseInfo instanceof EmpEnrollBizBillInfo){
                    EmpEnrollBizBillInfo empEnrollBizBillInfo = (EmpEnrollBizBillInfo)coreBaseInfo;
                    EmpEnrollBizBillEntryCollection entrys = empEnrollBizBillInfo.getEntrys();
                    String name = "";
                    for (int i = 0; i <entrys.size(); i++) {
                        EmpEnrollBizBillEntryInfo empEnrollBizBillEntryInfo = entrys.get(i);
                        name+=empEnrollBizBillEntryInfo.getEmpName()+",";
                    }
                    if (name.length()>0){
                        name = name.substring(0,name.length()-1);
                        // 设置操作日志的名称
                        operateLogInfo.setName(name);
                    }
                }else {
                    // 设置操作日志的名称
                    operateLogInfo.setName(coreBaseInfo.getString("name"));
                }

                // 设置操作日志的编号
                operateLogInfo.setNumber(coreBaseInfo.getString("number"));
            }

        }
        // 调用父类的 _addnew 方法完成添加操作
        return super._addnew(ctx, operateLogInfo);
    }
}