using System;
using System.Collections.Generic;
using System.Linq;
namespace BLL
{
    /// 
    /// 项目检查项
    /// 
    public static class Check_ProjectCheckItemSetService
    {/// 
     /// 根据主键获取项目检查项
     /// 
     /// 
     /// 
        public static Model.Check_ProjectCheckItemSet GetCheckItemSetById(string checkItemSetId)
        {
            return Funs.DB.Check_ProjectCheckItemSet.FirstOrDefault(e => e.CheckItemSetId == checkItemSetId);
        }
        /// 
        /// 根据检测名称获取项目检查项
        /// 
        /// 
        /// 
        public static Model.Check_ProjectCheckItemSet GetCheckItemSetByCheckItemName(string checkItemName)
        {
            return Funs.DB.Check_ProjectCheckItemSet.FirstOrDefault(e => e.CheckItemName == checkItemName);
        }
        /// 
        /// 根据主键获取顶级检查项名称
        /// 
        /// 
        /// 
        public static string GetCheckItemNameBySupCheckItem(string supCheckItem)
        {
            string name = string.Empty;
            Model.Check_ProjectCheckItemSet checkItemSet = Funs.DB.Check_ProjectCheckItemSet.FirstOrDefault(e => e.CheckItemSetId == supCheckItem);
            if (checkItemSet != null)
            {
                if (checkItemSet.SupCheckItem == "0")
                {
                    name = checkItemSet.CheckItemName;
                }
                else
                {
                    name = GetCheckItemNameBySupCheckItem(checkItemSet.SupCheckItem);
                }
            }
            return name;
        }
        /// 
        /// 获取一级节点检查类型
        /// 
        /// 
        /// 
        public static string ConvertCheckItemType(object CheckItem)
        {
            string type = string.Empty;
            if (CheckItem != null)
            {
                Model.Check_ProjectCheckItemDetail detail = BLL.Check_ProjectCheckItemDetailService.GetCheckItemDetailById(CheckItem.ToString());
                if (detail != null)
                {
                    Model.Check_ProjectCheckItemSet item = BLL.Check_ProjectCheckItemSetService.GetCheckItemSetById(detail.CheckItemSetId);
                    if (item != null)
                    {
                        if (item.SupCheckItem == "0")
                        {
                            type = item.CheckItemName;
                        }
                        else
                        {
                            type = BLL.Check_ProjectCheckItemSetService.GetCheckItemNameBySupCheckItem(item.SupCheckItem);
                        }
                    }
                }
                else
                {
                    Model.Check_ProjectCheckItemSet item = BLL.Check_ProjectCheckItemSetService.GetCheckItemSetById(CheckItem.ToString());
                    if (item != null)
                    {
                        if (item.SupCheckItem == "0")
                        {
                            type = item.CheckItemName;
                        }
                        else
                        {
                            type = BLL.Check_ProjectCheckItemSetService.GetCheckItemNameBySupCheckItem(item.SupCheckItem);
                        }
                    }
                }
            }
            return type;
        }
        /// 
        /// 根据上一节点id获取项目检查项
        /// 
        /// 
        /// 
        public static List GetCheckItemSetBySupCheckItemSetId(string supCheckItemSetId)
        {
            return (from x in Funs.DB.Check_ProjectCheckItemSet where x.SupCheckItem == supCheckItemSetId select x).ToList();
        }
        /// 
        /// 添加项目检查项
        /// 
        /// 
        public static void AddCheckItemSet(Model.Check_ProjectCheckItemSet checkItemSet)
        {
            Model.Check_ProjectCheckItemSet newCheckItemSet = new Model.Check_ProjectCheckItemSet
            {
                CheckItemSetId = checkItemSet.CheckItemSetId,
                ProjectId = checkItemSet.ProjectId,
                CheckItemName = checkItemSet.CheckItemName,
                SupCheckItem = checkItemSet.SupCheckItem,
                CheckType = checkItemSet.CheckType,
                MapCode = checkItemSet.MapCode,
                IsEndLever = checkItemSet.IsEndLever,
                SortIndex = checkItemSet.SortIndex,
                IsBuiltIn = checkItemSet.IsBuiltIn
            };
            Funs.DB.Check_ProjectCheckItemSet.InsertOnSubmit(newCheckItemSet);
            Funs.DB.SubmitChanges();
        }
        /// 
        /// 修改项目检查项
        /// 
        /// 
        public static void UpdateCheckItemSet(Model.Check_ProjectCheckItemSet checkItemSet)
        {
            Model.Check_ProjectCheckItemSet newCheckItemSet = Funs.DB.Check_ProjectCheckItemSet.FirstOrDefault(e => e.CheckItemSetId == checkItemSet.CheckItemSetId);
            if (newCheckItemSet != null)
            {
                newCheckItemSet.CheckItemName = checkItemSet.CheckItemName;
                newCheckItemSet.SupCheckItem = checkItemSet.SupCheckItem;
                newCheckItemSet.MapCode = checkItemSet.MapCode;
                newCheckItemSet.IsEndLever = checkItemSet.IsEndLever;
                newCheckItemSet.SortIndex = checkItemSet.SortIndex;
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除项目检查项
        /// 
        /// 
        public static void DeleteCheckItemSet(string checkItemSetId, string projectId)
        {
            Model.Check_ProjectCheckItemSet checkItemSet = Funs.DB.Check_ProjectCheckItemSet.FirstOrDefault(e => e.CheckItemSetId == checkItemSetId && e.ProjectId == projectId);
            if (checkItemSet != null)
            {
                Funs.DB.Check_ProjectCheckItemSet.DeleteOnSubmit(checkItemSet);
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 是否末级
        /// 
        /// 项目检查项目主键
        /// 布尔值
        public static bool IsEndLevel(string checkItemSetId)
        {
            if (checkItemSetId == "0")
            {
                return false;
            }
            else
            {
                Model.Check_ProjectCheckItemSet checkItemSet = Funs.DB.Check_ProjectCheckItemSet.FirstOrDefault(e => e.CheckItemSetId == checkItemSetId);
                return Convert.ToBoolean(checkItemSet.IsEndLever);
            }
        }
        /// 
        /// 是否可删除资源节点
        /// 
        /// 
        /// true-可以,false-不可以
        public static bool IsDeleteCheckItemSet(string checkItemSetId)
        {
            bool isDelete = true;
            var checkItemSet = BLL.Check_ProjectCheckItemSetService.GetCheckItemSetById(checkItemSetId);
            if (checkItemSet != null)
            {
                //if (checkItemSet.IsBuiltIn == true)
                //{
                //    isDelete = false;
                //}
                if (checkItemSet.IsEndLever == true)
                {
                    var detailCout = Funs.DB.Check_ProjectCheckItemDetail.FirstOrDefault(x => x.CheckItemSetId == checkItemSetId);
                    if (detailCout != null)
                    {
                        isDelete = false;
                    }
                }
                else
                {
                    var supItemSetCount = BLL.Check_ProjectCheckItemSetService.GetCheckItemSetBySupCheckItemSetId(checkItemSetId);
                    if (supItemSetCount.Count() > 0)
                    {
                        isDelete = false;
                    }
                }
            }
            return isDelete;
        }
        /// 
        /// 是否存在项目检查项名称
        /// 
        /// 
        /// true-存在,false-不存在
        public static bool IsExistCheckItemName(string projectId, string type, string checkItemSetId, string supCheckItem, string checkItemName)
        {
            var q = Funs.DB.Check_ProjectCheckItemSet.FirstOrDefault(x => x.ProjectId == projectId && x.CheckType == type && x.SupCheckItem == supCheckItem && x.CheckItemName == checkItemName
                    && x.CheckItemSetId != checkItemSetId);
            if (q != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}