using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using BLL;


namespace WebAPI.Controllers.JGZL
{
    public class JGZLController : ApiController
    {
        #region 用户基本数据,单位,项目,单位工程,签审岗位(无),机构(无)

        /// <summary>
        /// 用户基本数据,单位,项目,单位工程,签审岗位(无),机构(无)
        /// </summary>
        /// <returns></returns>
        public Model.ResponeData getUserList(string UserName,int? PageIndex,int? PageSize)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var responeData = new Model.ResponeData();
                try
                {
                    var getUserListQuery = from x in db.Sys_User
                                           join u in db.Base_Unit on x.UnitId equals u.UnitId into uGrouped
                                           from ug in uGrouped.DefaultIfEmpty()
                                           select new
                                           {
                                               x.UserId,
                                               x.Account,
                                               x.UserName,
                                               x.Telephone,
                                               x.IdentityCard,
                                               x.BirthDay,
                                               x.Email,
                                               x.IsPost,
                                               x.Sex,
                                               ug.UnitId,
                                               ug.UnitName,
                                               ProjectItems = from proj in db.Base_Project
                                                              join pu in db.Project_ProjectUser on proj.ProjectId equals pu.ProjectId
                                                              where pu.UserId == x.UserId
                                                              select new
                                                              {
                                                                  proj.ProjectId,
                                                                  proj.ProjectName,
                                                                  pu.WorkAreaId // 假设 WorkAreaId 是 UnitWorkIds 的来源
                                                              }
                                           };

                    var getList = getUserListQuery.AsEnumerable()
                        .Select(user => new UserItem
                        {
                            UserId = user.UserId,
                            Account = user.Account,
                            UserName = user.UserName,
                            Telephone = user.Telephone,
                            IdentityCard = user.IdentityCard,
                            BirthDay = user.BirthDay,
                            Email = user.Email,
                            IsPost = user.IsPost,
                            Sex = user.Sex,
                            UnitId = user.UnitId,
                            UnitName = user.UnitName,
                            ProjectItemList = user.ProjectItems.Select(projectItem => new ProjectItem
                            {
                                ProjectId = projectItem.ProjectId,
                                ProjectName = projectItem.ProjectName,
                                UnitWorkItemList = string.IsNullOrEmpty(projectItem.WorkAreaId)
                                    ? new List<UnitWorkItem>()
                                    : db.WBS_UnitWork
                                        .Where(uw => projectItem.WorkAreaId.Split(',')
                                            .Contains(uw.UnitWorkId))
                                        .Select(uw => new UnitWorkItem
                                        {
                                            UnitWorkId = uw.UnitWorkId,
                                            UnitWorkName = uw.UnitWorkName
                                        })
                                        .ToList()
                            }).ToList()
                        }).ToList();

                    if (!string.IsNullOrEmpty(UserName))
                    {
                        getList = getList.Where(x => x.UserName.Contains(UserName)).ToList();
                    }

                    if (PageIndex > 0 && PageSize > 0)
                    {
                        getList = getList.Skip(((int)PageIndex - 1) * (int)PageSize).Take((int)PageSize).ToList();
                    }

                    responeData.data = new { getList };
                }
                catch (Exception ex)
                {
                    responeData.code = 0;
                    responeData.message = ex.Message;
                }
                return responeData;
            }
        }

        #endregion
        
        #region 项目数据

        /// <summary>
        /// 项目数据
        /// </summary>
        /// <returns></returns>
        public Model.ResponeData getProjedtList()
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var responeData = new Model.ResponeData();
                try
                {
                    var getList = (from x in db.Base_Project
                        select new
                        {
                            x.ProjectId,
                            x.ProjectCode,
                            x.ProjectName,
                            x.StartDate,
                        }).ToList();

                    responeData.data = new { getList };
                }
                catch (Exception ex)
                {
                    responeData.code = 0;
                    responeData.message = ex.Message;
                }
                return responeData;
            }
        }

        #endregion
        
        
        #region 根据项目获取单位工程数据

        /// <summary>
        /// 根据项目获取单位工程数据
        /// </summary>
        /// <returns></returns>
        public Model.ResponeData getUnitWorkListByProjectId(string ProjectId)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var responeData = new Model.ResponeData();
                try
                {
                    var getList = (from x in db.WBS_UnitWork
                        where x.ProjectId==ProjectId
                        select new
                        {
                            x.UnitWorkId,
                            x.UnitWorkName,
                            x.UnitWorkCode,
                        }).ToList();

                    responeData.data = new { getList };
                }
                catch (Exception ex)
                {
                    responeData.code = 0;
                    responeData.message = ex.Message;
                }
                return responeData;
            }
        }

        #endregion
        
        
        #region 根据项目获取装置数据

        /// <summary>
        /// 根据项目获取装置数据
        /// </summary>
        /// <returns></returns>
        public Model.ResponeData getInstallationListByProjectId(string ProjectId)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var responeData = new Model.ResponeData();
                try
                {
                    var getList = (from x in db.Project_Installation
                        where x.ProjectId==ProjectId
                        select new
                        {
                            x.InstallationId,
                            x.InstallationName,
                            x.InstallationCode,
                        }).ToList();

                    responeData.data = new { getList };
                }
                catch (Exception ex)
                {
                    responeData.code = 0;
                    responeData.message = ex.Message;
                }
                return responeData;
            }
        }

        #endregion
        
        
        #region 单位数据

        /// <summary>
        /// 单位数据
        /// </summary>
        /// <returns></returns>
        public Model.ResponeData getUnitList()
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var responeData = new Model.ResponeData();
                try
                {
                    var getList = (from x in db.Base_Unit
                        select new
                        {
                            x.UnitId,
                            x.UnitCode,
                            x.UnitName,
                        }).ToList();

                    responeData.data = new { getList };
                }
                catch (Exception ex)
                {
                    responeData.code = 0;
                    responeData.message = ex.Message;
                }
                return responeData;
            }
        }

        #endregion

        public class UserItem
        {
            public string UserId { get; set; }
            public string Account { get; set; }
            public string UserName { get; set; }
            public string Telephone { get; set; }
            public string IdentityCard { get; set; }
            public DateTime? BirthDay { get; set; }
            public string Email { get; set; }
            public System.Nullable<bool> IsPost { get; set; }
            public string Sex { get; set; }
            public string UnitId { get; set; }
            public string UnitName { get; set; }
            public List<ProjectItem> ProjectItemList { get; set; }

        }

        public class ProjectItem
        {
            public string ProjectId { get; set; }
            public string ProjectName { get; set; }
            public List<UnitWorkItem> UnitWorkItemList { get; set; }
        }

        public class UnitWorkItem
        {
            public string UnitWorkId { get; set; }
            public string UnitWorkName { get; set; }
        }
    }
}