using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
    /// 
    /// 关键事项明细
    /// 
    public static class GJSXItemService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 根据关键事项ID查询所有关键事项进展
        /// 
        /// 关键事项ID
        /// 
        public static List GetGJSXDetailByGJSXID(string GJSXID)
        {
            return (from x in Funs.DB.GJSX_detail where x.GJSXID == GJSXID orderby x.Sort select x).ToList();
        }
        /// 
        /// 根据关键事项ID查询所有事项进展
        /// 
        /// 关键事项ID
        /// 
        public static System.Collections.IEnumerable GetIEnumerableByGJSXID(string GJSXID)
        {
            return from x in db.GJSX_detail
                   where x.GJSXID == GJSXID
                   orderby x.Sort
                   select new
                   {
                       x.Cuid,
                       x.GJSXID,
                       x.Progress_user,
                       x.Date,
                       x.Progress_detail,
                       x.FilePath
                   };
        }
        /// 
        /// 增加关键事项明细
        /// 
        /// 
        public static void AddGJSXMX(Model.GJSX_detail gjsxmx)
        {
            Model.SGGLDB db = Funs.DB;
            string newKeyID = SQLHelper.GetNewID(typeof(Model.GJSX_detail));
            Model.GJSX_detail newGJSXMX = new Model.GJSX_detail();
            newGJSXMX.Cuid = gjsxmx.Cuid;
            newGJSXMX.GJSXID = gjsxmx.GJSXID;
            newGJSXMX.Progress_user = gjsxmx.Progress_user;
            newGJSXMX.Progress_detail = gjsxmx.Progress_detail;
            newGJSXMX.Date = gjsxmx.Date;
            newGJSXMX.Sort = gjsxmx.Sort;
            newGJSXMX.FilePath = gjsxmx.FilePath;
            newGJSXMX.ProgressStatus = gjsxmx.ProgressStatus;
            db.GJSX_detail.InsertOnSubmit(newGJSXMX);
            db.SubmitChanges();
        }
        /// 
        /// 根据关键事项Id删除所对应的所有事项进展
        /// 
        /// 关键事项ID
        public static void DeleteGJSXMXByGJSXID(string GJSXID)
        {
            Model.SGGLDB db = Funs.DB;
            var q = (from x in db.GJSX_detail where x.GJSXID == GJSXID select x).ToList();
            if (q != null)
            {
                db.GJSX_detail.DeleteAllOnSubmit(q);
                db.SubmitChanges();
            }
        }
        /// 
        /// 添加操作人员
        /// 
        /// 
        public static void AddItemUser(Model.GJSX_detail item)
        {
            Model.SGGLDB db = Funs.DB;
            Model.GJSX_detail newitem = new Model.GJSX_detail();
            string newKeyID = SQLHelper.GetNewID(typeof(Model.GJSX_detail));
            newitem.Cuid = newKeyID;
            newitem.GJSXID = item.GJSXID;
            newitem.Progress_user = item.Progress_user;
            newitem.Sort = item.Sort;
            newitem.FilePath = item.FilePath;
            newitem.Progress_detail = item.Progress_detail;
            db.GJSX_detail.InsertOnSubmit(newitem);
            db.SubmitChanges();
        }
        public static void UpdateGJSXdetail(Model.GJSX_detail _GJSX_detail)
        {
            Model.SGGLDB db = Funs.DB;
            Model.GJSX_detail newGJSX_detail = db.GJSX_detail.FirstOrDefault(e => e.Cuid == _GJSX_detail.Cuid);
            if (newGJSX_detail != null)
            {
                newGJSX_detail.Cuid = _GJSX_detail.Cuid;
                newGJSX_detail.GJSXID = _GJSX_detail.GJSXID;
                newGJSX_detail.Progress_user = _GJSX_detail.Progress_user;
                newGJSX_detail.Progress_detail = _GJSX_detail.Progress_detail;
                newGJSX_detail.Sort = _GJSX_detail.Sort;
                newGJSX_detail.FilePath = _GJSX_detail.FilePath;
                newGJSX_detail.ProgressStatus = newGJSX_detail.ProgressStatus;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据关键事项编号获取关键事项进展信息
        /// 
        /// 
        /// 
        public static Model.GJSX_detail GetGJSXItemByGJSXID(string GJSXID)
        {
            return (from x in Funs.DB.GJSX_detail where x.GJSXID == GJSXID select x).FirstOrDefault();
        }
        public static Model.GJSX_detail GetGJSXItemByGJSXID(string GJSXID,string userid)
        {
            return (from x in Funs.DB.GJSX_detail where x.GJSXID == GJSXID  && x.Progress_user==userid select x).FirstOrDefault();
        }
        #region 删除外键引用
        /// 
        /// 根据事项进展操作人获取事项进展信息
        /// 
        /// 
        /// 
        public static int GetGJSXMXByUserId(string userId)
        {
            return (from x in Funs.DB.GJSX_detail where x.Progress_user == userId select x).Count();
        }
        #endregion
        /// 
        /// 关键事项明细是否存在该用户
        /// 
        /// 
        /// 
        /// 
        public static bool IsExistOperateionId(string GJSXID, string operationId)
        {
            var q = from x in db.GJSX_detail where x.GJSXID == GJSXID && x.Progress_user == operationId select x;
            if (q.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 根据编号和负责人获取明细数量
        /// 
        /// 
        /// 
        /// 
        public static int GetGJSXMXByGJSXIDAndUserId(string GJSXID, string userId)
        {
            return (from x in Funs.DB.GJSX_detail where x.GJSXID == GJSXID && x.Progress_user == userId select x).Count();
        }
        public static Model.GJSX_detail GetGJSXMXById(string id)
        {
            return Funs.DB.GJSX_detail.FirstOrDefault(e => e.Cuid == id);
        }
    }
}