using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BLL
{
    /// 
    /// 安全隐患主表
    /// 
    public static class RectifyService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 根据主键获取安全隐患
        /// 
        /// 
        /// 
        public static Model.Technique_Rectify GetRectifyById(string rectifyId)
        {
            return Funs.DB.Technique_Rectify.FirstOrDefault(e => e.RectifyId == rectifyId);
        }
        /// 
        /// 根据上一节点id获取安全隐患
        /// 
        /// 
        /// 
        /// 
        public static List GetRectifyBySupRectifyId(string supRectifyId, string type)
        {
            return (from x in Funs.DB.Technique_Rectify where x.SupRectifyId == supRectifyId && x.RectifyType == type select x).ToList();
        }
        /// 
        /// 添加安全隐患
        /// 
        /// 
        public static void AddRectify(Model.Technique_Rectify rectify)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Technique_Rectify newRectify = new Model.Technique_Rectify
            {
                RectifyId = rectify.RectifyId,
                RectifyType = rectify.RectifyType,
                RectifyCode = rectify.RectifyCode,
                RectifyName = rectify.RectifyName,
                SupRectifyId = rectify.SupRectifyId,
                IsEndLever = rectify.IsEndLever
            };
            newRectify.UpState = newRectify.UpState;
            db.Technique_Rectify.InsertOnSubmit(newRectify);
            db.SubmitChanges();
        }
        /// 
        /// 修改安全隐患
        /// 
        /// 
        public static void UpdateRectify(Model.Technique_Rectify rectify)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Technique_Rectify newRectify = db.Technique_Rectify.FirstOrDefault(e => e.RectifyId == rectify.RectifyId);
            if (newRectify != null)
            {
                newRectify.RectifyType = rectify.RectifyType;
                newRectify.RectifyCode = rectify.RectifyCode;
                newRectify.RectifyName = rectify.RectifyName;
                newRectify.SupRectifyId = rectify.SupRectifyId;
                newRectify.IsEndLever = rectify.IsEndLever;
                newRectify.UpState = newRectify.UpState;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除安全隐患
        /// 
        /// 
        public static void DeleteRectify(string rectifyId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Technique_Rectify rectify = db.Technique_Rectify.FirstOrDefault(e => e.RectifyId == rectifyId);
            if (rectify != null)
            {
                db.Technique_Rectify.DeleteOnSubmit(rectify);
                db.SubmitChanges();
            }
        }
        /// 
        /// 是否可删除资源节点
        /// 
        /// 
        /// true-可以,false-不可以
        public static bool IsDeleteRectify(string rectifyId)
        {
            bool isDelete = true;
            var rectify = BLL.RectifyService.GetRectifyById(rectifyId);
            if (rectify != null)
            {
                if (rectify.IsBuild == true)
                {
                    isDelete = false;
                }
                if (rectify.IsEndLever == true)
                {
                    var detailCout = Funs.DB.Technique_RectifyItem.FirstOrDefault(x => x.RectifyId == rectifyId);
                    if (detailCout != null)
                    {
                        isDelete = false;
                    }
                }
                else
                {
                    var supItemSetCount = BLL.RectifyService.GetRectifyBySupRectifyId(rectifyId, rectify.RectifyType);
                    if (supItemSetCount.Count() > 0)
                    {
                        isDelete = false;
                    }
                }
            }
            return isDelete;
        }
        public static List GetRectifyList()
        {
            var q = from x in Funs.DB.Technique_Rectify
                    where x.SupRectifyId == "0"
                    select x;
            return q.ToList();
        }
        public static void InitRectifyDropDownList(FineUIPro.DropDownList dropName, bool isShowPlease, string itemText)
        {
            dropName.DataValueField = "RectifyName";
            dropName.DataTextField = "RectifyName";
            dropName.DataSource = GetRectifyList();
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName, itemText);
            }
        }
    }
}