WebApp.cs 2.78 KB
using System;
using System.Linq;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 业务层基类,UnitWork用于事务操作,Repository用于普通的数据库操作
    /// <para>如用户管理:Class UserManagerApp:WebApp<User></para>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class WebApp<T> where T : SysEntity
    {
        /// <summary>
        /// 用于事务操作
        /// </summary>
        /// <value>The unit work.</value>
        protected IUnitWork UnitWork;

        /// <summary>
        /// 用于普通的数据库操作
        /// </summary>
        /// <value>The repository.</value>
        protected IRepository<T> Repository;

        public WebApp(IUnitWork unitWork, IRepository<T> repository)
        {
            UnitWork = unitWork;
            Repository = repository;
        }

        /// <summary>
        /// 按id批量删除
        /// </summary>
        /// <param name="ids"></param>
        public void Delete(int[] ids)
        {
            Repository.Delete(u => ids.Contains(u.Id.Value));
        }

        public T Get(int id)
        {
            return Repository.FindSingle(u => u.Id == id);
        }

        /// <summary>
        /// 如果一个类有层级结构(树状),则修改该节点时,要修改该节点的所有子节点
        /// //修改对象的级联ID,生成类似XXX.XXX.X.XX
        /// </summary>
        /// <typeparam name="U">U必须是一个继承TreeEntity的结构</typeparam>
        /// <param name="entity"></param>

        public void ChangeModuleCascade<U>(U entity) where U : TreeEntity
        {
            string cascadeId;
            int currentCascadeId = 1;  //当前结点的级联节点最后一位
            var sameLevels = UnitWork.Find<U>(o => o.ParentId == entity.ParentId && o.Id != entity.Id);
            foreach (var obj in sameLevels)
            {
                int objCascadeId = int.Parse(obj.CascadeId.TrimEnd('.').Split('.').Last());
                if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1;
            }

            if (entity.ParentId != null)
            {
                var parentOrg = UnitWork.FindSingle<U>(o => o.Id == entity.ParentId);
                if (parentOrg != null)
                {
                    cascadeId = parentOrg.CascadeId + currentCascadeId + ".";
                    entity.ParentName = parentOrg.Name;
                }
                else
                {
                    throw new Exception("未能找到该组织的父节点信息");
                }
            }
            else
            {
                cascadeId = ".0." + currentCascadeId + ".";
                entity.ParentName = "根节点";
            }

            entity.CascadeId = cascadeId;
        }
    }
}