using ProjectBase.Data.BaseDAL;
using ProjectBase.Util;
using SIASUN.Autopilot.DAL.IDALSQL;
using SIASUN.Autopilot.Model;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;

namespace SIASUN.Autopilot.BLL
{
    public class BlLogOperation : BaseBLL<MoLogOperation>
    {
        private IDalLogOperation dalLogOperation;

        /// <summary>
        /// 构造函数
        /// </summary>   
        public BlLogOperation() : base()
        {
            base.Init(this.GetType().FullName, System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);

            dalLogOperation = baseDal as IDalLogOperation;
        }

        /// <summary>
        /// 获取组织ID与组织名称的集合
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetGroupName()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            //BlAuthoryGroup bl = new BlAuthoryGroup();
            List<MoAuthoryGroup> group = BLLFactory<BlAuthoryGroup>.Instance.GetAll();
            if (group.Count == 0 || group == null)
            {
                return null;
            }
            foreach (MoAuthoryGroup item in group)
            {
                dic.Add(item.GroupId, item.GroupName);
            }
            return dic;
        }

        /// <summary>
        /// 根据相关信息，写入用户的操作日志记录
        /// </summary>
        /// <param name="userId">操作用户</param>
        /// <param name="tableName">操作表名称</param>
        /// <param name="operationType">操作类型</param>
        /// <param name="note">操作详细表述,目前数据表不包含这个字段，赋值为空</param>
        /// <param name="trans">事务对象</param>
        /// <returns></returns>
        public static bool OnOperationLog(string userId, string tableName, string operationType, string note = null, DbTransaction trans = null)
        {
            //虽然实现了这个事件，但是我们还需要判断该表是否在配置表里面，如果不在，则不记录操作日志。
            //BlLogOperationSetting blSetting = new BlLogOperationSetting();
            MoLogOperationSetting settingInfo = BLLFactory<BlLogOperationSetting>.Instance.FindByTableName(tableName, trans);
            if (settingInfo != null)
            {
                bool insert = operationType == "增加" && settingInfo.InsertLog;
                bool update = operationType == "修改" && settingInfo.UpdateLog;
                bool delete = operationType == "删除" && settingInfo.DeleteLog;
                if (insert || update || delete)
                {
                    MoLogOperation info = new MoLogOperation();
                    info.TableName = tableName;
                    info.Type = operationType;
                    info.Time = DateTime.Now;

                    if (!string.IsNullOrEmpty(userId))
                    {
                        //BlAuthoryUser blUser = new BlAuthoryUser();
                        MoAuthoryUser userInfo = BLLFactory<BlAuthoryUser>.Instance.FindByID(new string[] { userId }, trans);
                        if (userInfo != null)
                        {
                            info.Account = userId;
                            info.Name = userInfo.UserName;
                            info.GroupId = userInfo.GroupId;
                        }
                    }
                    info.Ip = SysEnvironment.Ip;
                    info.Mac = SysEnvironment.Mac;
                    //BlLogOperation bl = new BlLogOperation();
                    return BLLFactory<BlLogOperation>.Instance.Insert(info, trans);
                    //return true;
                }
            }
            return false;
        }
    }
}
