using ProjectBase.Data.BaseDAL;
using SIASUN.Autopilot.DAL.IDALSQL;
using SIASUN.Autopilot.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIASUN.Autopilot.BLL
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    public class BlMenuInfo : BaseBLL<MoMenuInfo>
    {
        private IDalMenuInfo dalMenuInfo;

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

            dalMenuInfo = baseDal as IDalMenuInfo;
            dalMenuInfo.OnOperationLog += new OperationLogEventHandler(BlLogOperation.OnOperationLog);
        }

        public string CreateFunctionId(string winfromType, string menuParentId)
        {
            string createId = null;
            string condition = " 1 = 1";
            condition += string.IsNullOrEmpty(menuParentId) ? " and  menu_parent_id is null" : string.Format(" and menu_parent_id = '{0}'", menuParentId);
            condition += string.IsNullOrEmpty(winfromType) ? "" : string.Format(" and menu_winform_type = '{0}'", winfromType);
            int count = dalMenuInfo.GetRecordCount(condition);
            if (winfromType.Equals("1"))
            {
                MoMenuInfo mo = dalMenuInfo.FindSingle(condition);
                createId = string.Format("{0}{1}", (count + 1).ToString().PadLeft(2, '0'), mo.ID.Substring(2, 4));
            }
            else if (winfromType.Equals("2"))
            {
                createId = string.Format("{0}{1}{2}", menuParentId.Substring(0, 2), (count + 1).ToString().PadLeft(2, '0'), menuParentId.Substring(4, 2));
            }
            else
            {
                createId = string.Format("{0}{1}", menuParentId.Substring(0, 4), (count + 1).ToString().PadLeft(2, '0'));
            }
            return createId;
        }
    }
}
