using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using BLL;
using System.Collections;
using System.Web.UI.WebControls;
namespace BLL
{
    public class PersonManageService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 记录数
        /// 
        public static int count
        {
            get;
            set;
        }
        /// 
        /// 定义变量
        /// 
        public static IQueryable qq = from x in db.BS_Welder orderby x.WED_Unit, x.WED_Code select x;
        /// 
        /// 获取分页列表
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static IEnumerable getListData(string project, string drpUnitS, string isOnGuard, string txtCodeS, string txtNameS, string txtWorkCodeS, string txtClassS, int startRowIndex, int maximumRows)
        {
            IQueryable q = qq;
            if (!string.IsNullOrEmpty(project))
            {
                q = q.Where(e => e.ProjectId == project);
            }
            if (drpUnitS != "0"&&!string.IsNullOrEmpty(drpUnitS))
            {
                q = q.Where(e => e.WED_Unit == drpUnitS);
            }
            if (!string.IsNullOrEmpty(txtCodeS))
            {
                q = q.Where(e => e.WED_Code.Contains(txtCodeS));
            }
            if (!string.IsNullOrEmpty(txtNameS))
            {
                q = q.Where(e => e.WED_Name.Contains(txtNameS));
            }
            if (!string.IsNullOrEmpty(txtWorkCodeS))
            {
                q = q.Where(e => e.WED_WorkCode.Contains(txtWorkCodeS));
            }
            if (!string.IsNullOrEmpty(txtClassS))
            {
                q = q.Where(e => e.WED_Class.Contains(txtClassS));
            }
            if (isOnGuard!="0")
            {
                q = q.Where(e => e.WED_IfOnGuard == Convert.ToBoolean(isOnGuard));
            }
            count = q.Count();
            if (count == 0)
            {
                return new object[] { "" };
            }
            return from x in q.Skip(startRowIndex).Take(maximumRows)
                   select new
                   {
                       x.WED_ID,
                       x.WED_Code,
                       x.WED_Name,
                       WED_Unit = (from y in db.Base_Unit where y.UnitId == x.WED_Unit select y.UnitName).First(),
                       WED_Sex = (x.WED_Sex == "2" ? "女" : "男"),
                       WED_Birthday = string.Format("{0:yyyy-MM-dd}", x.WED_Birthday),
                       LeaveDate = string.Format("{0:yyyy-MM-dd}", x.LeaveDate),
                       PostDate = string.Format("{0:yyyy-MM-dd}", x.PostDate),
                       x.WED_WorkCode,
                       x.WED_Class,
                       WED_IfOnGuard = (x.WED_IfOnGuard == true ? "是" : "否"),
                       x.WED_Remark,
                       Thickness = (x.ThicknessMin == null ? "0.0000" : x.ThicknessMin.ToString()) + "-" + (x.ThicknessMax == null ? " " : x.ThicknessMax.ToString()),
                       Sizes = (x.SizesMin == null ? "0.0000" : x.SizesMin.ToString()) + "-" + (x.SizesMax == null ? " " : x.SizesMax.ToString()),
                   };
        }
        /// 
        /// 获取列表数
        /// 
        /// 
        /// 
        /// 
        /// 
        public static int GetListCount(string project, string drpUnitS, string isOnGuard, string txtCodeS, string txtNameS, string txtWorkCodeS, string txtClassS)
        {
            return count;
        }
        /// 
        /// 根据id查询人员信息
        /// 
        /// 
        /// 
        public static Model.BS_Welder GetBSWelderByTeamWEDID(string WED_ID)
        {
            return Funs.DB.BS_Welder.FirstOrDefault(e => e.WED_ID == WED_ID);
        }
      
        /// 
        /// 添加人员信息
        /// 
        /// 
        public static void AddBSWelder(Model.BS_Welder welder)
        {
            Model.SGGLDB db = Funs.DB;
            string newKeyID = SQLHelper.GetNewID(typeof(Model.BS_Welder));
            Model.BS_Welder newWelder = new Model.BS_Welder();
            newWelder.WED_ID = newKeyID;
            newWelder.WED_Code = welder.WED_Code;
            newWelder.WED_Name = welder.WED_Name;
            newWelder.WED_Unit = welder.WED_Unit;
            newWelder.WED_Sex = welder.WED_Sex;
            newWelder.WED_Birthday = welder.WED_Birthday;
            newWelder.LimitDate = welder.LimitDate;
            newWelder.LeaveDate = welder.LeaveDate;
            newWelder.PostDate = welder.PostDate;
            newWelder.WED_WorkCode = welder.WED_WorkCode;
            newWelder.WED_Class = welder.WED_Class;
            newWelder.WED_IfOnGuard = welder.WED_IfOnGuard;
            newWelder.WED_Remark = welder.WED_Remark;
            newWelder.ThicknessMin = welder.ThicknessMin;
            newWelder.ThicknessMax = welder.ThicknessMax;
            newWelder.SizesMax = welder.SizesMax;
            newWelder.SizesMin = welder.SizesMin;
            newWelder.ProjectId = welder.ProjectId;
            newWelder.IdentityCard = welder.IdentityCard;
            newWelder.PostDate = welder.PostDate;
            db.BS_Welder.InsertOnSubmit(newWelder);
            db.SubmitChanges();
        }
        /// 
        /// 修改人员信息
        /// 
        /// 
        public static void UpdateBSWelder(Model.BS_Welder welder)
        {
            Model.SGGLDB db = Funs.DB;
            Model.BS_Welder newWelder = db.BS_Welder.First(e => e.WED_ID == welder.WED_ID);           
            newWelder.WED_Code = welder.WED_Code;
            newWelder.WED_Name = welder.WED_Name;
            newWelder.WED_Unit = welder.WED_Unit;
            newWelder.WED_Sex = welder.WED_Sex;
            newWelder.WED_Birthday = welder.WED_Birthday;
            newWelder.LimitDate = welder.LimitDate;
            newWelder.LeaveDate = welder.LeaveDate;
            newWelder.PostDate = welder.PostDate;
            newWelder.WED_WorkCode = welder.WED_WorkCode;
            newWelder.WED_Class = welder.WED_Class;
            newWelder.WED_IfOnGuard = welder.WED_IfOnGuard;
            newWelder.WED_Remark = welder.WED_Remark;
            newWelder.ThicknessMin = welder.ThicknessMin;
            newWelder.ThicknessMax = welder.ThicknessMax;
            newWelder.SizesMax = welder.SizesMax;
            newWelder.SizesMin = welder.SizesMin;
            newWelder.IdentityCard = welder.IdentityCard;
            newWelder.PostDate = welder.PostDate;
            db.SubmitChanges();
        }
        /// 
        /// 修改人员信息
        /// 
        /// 
        public static void UpdateBSWelderItem(Model.BS_Welder welder)
        {
            Model.SGGLDB db = Funs.DB;
            Model.BS_Welder newWelder = db.BS_Welder.First(e => e.WED_ID == welder.WED_ID);       
            newWelder.ThicknessMin = welder.ThicknessMin;
            newWelder.ThicknessMax = welder.ThicknessMax;
            newWelder.SizesMax = welder.SizesMax;
            newWelder.SizesMin = welder.SizesMin;
            db.SubmitChanges();
        }
        /// 
        /// 是否存在人员编号
        /// 
        /// 
        /// true-存在,false-不存在
        public static bool IsExistWEDName(string WED_Name)
        {
            var q = from x in Funs.DB.BS_Welder where x.WED_Name == WED_Name select x;
            if (q.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 删除人员信息
        /// 
        /// 
        public static void DeleteBSWelder(string WED_ID)
        {
            Model.SGGLDB db = Funs.DB;
            Model.BS_Welder teamGroup = db.BS_Welder.First(e => e.WED_ID == WED_ID);
            db.BS_Welder.DeleteOnSubmit(teamGroup);
            db.SubmitChanges();
        }
      
        /// 
        /// 根据人员Id获取一条人员信息
        /// 
        /// 
        /// 
        public static Model.BS_Welder GetWelderByWenId(string wenid)
        {
            return Funs.DB.BS_Welder.FirstOrDefault(x => x.WED_ID == wenid);
        }
        /// 
        /// 获取人员
        /// 
        /// 
        public static ListItem[] GetWelderListByUnit(string projectId, string unitId)
        {
            var q = (from x in Funs.DB.BS_Welder where x.ProjectId == projectId && x.WED_Unit == unitId orderby x.WED_Code select x).ToList();
            ListItem[] list = new ListItem[q.Count()];
            for (int i = 0; i < q.Count(); i++)
            {
                list[i] = new ListItem(q[i].WED_Name ?? "", q[i].WED_ID.ToString());
            }
            return list;
        }
        /// 
        /// 根据单位获取焊工人员数
        /// 
        /// 
        /// 
        public static int GetPersonByUnitId(string unitId)
        {
            var q = (from x in Funs.DB.BS_Welder where x.WED_Unit == unitId select x).ToList();
            return q.Count();
        }
        /// 
        /// 根据项目Id、单位id获取焊工集合
        /// 
        /// 
        /// 
        /// 
        public static List GetWelderListByUnitId(string projectId, string unitId)
        {
            List welderList = (from x in Funs.DB.BS_Welder where x.ProjectId == projectId && x.WED_Unit == unitId  select x).ToList();
            return welderList;
        }
        /// 
        /// 根据项目Id、单位id、开始时间、接收时间获取焊工集合
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static List GetWelderListByUnitIdAndDate(string projectId, string unitId, DateTime startDate, DateTime endDate)
        {
            List welderList = (from x in Funs.DB.BS_Welder where x.ProjectId == projectId && x.WED_Unit == unitId && (x.PostDate >= Convert.ToDateTime(startDate) && x.PostDate <= Convert.ToDateTime(endDate)) select x).ToList();
            return welderList;
        }
        
    }
}