using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
    /// 
    /// 党员名册
    /// 
    public class PartyerService
    {
        /// 
        /// 根据主键获取党员名册
        /// 
        /// 
        /// 
        public static Model.Party_Partyer GetPartyerById(string partyerId)
        {
            return Funs.DB.Party_Partyer.FirstOrDefault(e => e.PartyerId == partyerId);
        }
        /// 
        /// 添加党员名册
        /// 
        /// 
        public static void AddPartyer(Model.Party_Partyer partyer)
        {
            Model.Party_Partyer newPartyer = new Model.Party_Partyer
            {
                PartyerId = partyer.PartyerId,
                Name = partyer.Name,
                Sex = partyer.Sex,
                BirthDate = partyer.BirthDate,
                Education = partyer.Education,
                Nation = partyer.Nation,
                JoinPartyDate = partyer.JoinPartyDate,
                JoinPostDate = partyer.JoinPostDate,
                Post = partyer.Post,
                Phone = partyer.Phone,
                PartyRelationInDate = partyer.PartyRelationInDate,
                PartyRelationOutDate = partyer.PartyRelationOutDate
            };
            Funs.DB.Party_Partyer.InsertOnSubmit(newPartyer);
            Funs.DB.SubmitChanges();
        }
        /// 
        /// 修改党员名册
        /// 
        /// 
        public static void UpdatePartyer(Model.Party_Partyer partyer)
        {
            Model.Party_Partyer newPartyer = Funs.DB.Party_Partyer.FirstOrDefault(e => e.PartyerId == partyer.PartyerId);
            if (newPartyer != null)
            {
                newPartyer.Name = partyer.Name;
                newPartyer.Sex = partyer.Sex;
                newPartyer.BirthDate = partyer.BirthDate;
                newPartyer.Education = partyer.Education;
                newPartyer.Nation = partyer.Nation;
                newPartyer.JoinPartyDate = partyer.JoinPartyDate;
                newPartyer.JoinPostDate = partyer.JoinPostDate;
                newPartyer.Post = partyer.Post;
                newPartyer.Phone = partyer.Phone;
                newPartyer.PartyRelationInDate = partyer.PartyRelationInDate;
                newPartyer.PartyRelationOutDate = partyer.PartyRelationOutDate;
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除党员名册
        /// 
        /// 
        public static void DeletePartyerById(string partyerId)
        {
            Model.Party_Partyer partyer = Funs.DB.Party_Partyer.FirstOrDefault(e => e.PartyerId == partyerId);
            if (partyer != null)
            {
                Funs.DB.Party_Partyer.DeleteOnSubmit(partyer);
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 获取党员下拉选项
        /// 
        /// 
        public static List GetPartyerList()
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                List list = new List();
                list = (from x in db.Party_Partyer
                        orderby x.Name
                        select x).ToList();
                return list;
            }
        }
        /// 
        /// 党员下拉框
        /// 
        /// 下拉框名字
        /// 是否显示请选择
        public static void InitPartyerDropDownList(FineUIPro.DropDownList dropName, bool isShowPlease)
        {
            dropName.DataValueField = "PartyerId";
            dropName.DataTextField = "Name";
            dropName.DataSource = GetPartyerList();
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }
        /// 
        /// 获取党员姓名
        /// 
        /// 
        public static string GetNameByPartyId(string partyId)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                string name = null;
                Model.Party_Partyer partyer = db.Party_Partyer.FirstOrDefault(x=>x.PartyerId== partyId);
                if (partyer != null)
                {
                    name = partyer.Name;
                }
                return name;
            }
        }
        #region 根据多党员ID得到党员名称字符串
        /// 
        /// 根据多党员ID得到党员名称字符串
        /// 
        /// 
        /// 
        public static string getNamesPartyIds(object partyIds)
        {
            string name = string.Empty;
            if (partyIds != null)
            {
                string[] ids = partyIds.ToString().Split(',');
                foreach (string id in ids)
                {
                    var q = GetNameByPartyId(id);
                    if (q != null)
                    {
                        name += q + ",";
                    }
                }
                if (name != string.Empty)
                {
                    name = name.Substring(0, name.Length - 1); ;
                }
            }
            return name;
        }
        #endregion
    }
}