using BLL;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FineUIPro.Web.common
{
    public partial class mainMenu_PDigData : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getEmployInOutRecords = Funs.DB.T_d_EmployInOutRecord.Where(x => x.ProjectId == this.CurrUser.LoginProjectId && x.RecordDate.Value == DateTime.Now.Date).ToList();
                getAllPersons = Funs.DB.SitePerson_Person.Where(x => x.ProjectId == this.CurrUser.LoginProjectId).ToList();
                getPersons = getAllPersons.Where(x =>x.IsUsed == true && x.InTime <= DateTime.Now && (!x.OutTime.HasValue || x.OutTime > DateTime.Now)).ToList();
                ///当前现场总人数
                getSitePerson();             
            }
        }
        #region 当前现场总人数
        /// 
        ///  当前现场总人数
        /// 
        private void getSitePerson()
        {
            var AllCount = getEmployInOutRecords.Count();
            if (AllCount > 0)
            {
                ////总人数
                this.divperson.InnerHtml = ((AllCount % 10000) / 1000).ToString();
                this.person00.InnerHtml = ((AllCount % 1000) / 100).ToString();
                this.person01.InnerHtml = ((AllCount % 100) / 10).ToString();
                this.person02.InnerHtml = (AllCount % 10).ToString();
            }
        }
        #endregion
        #region 项目安全人工时
        /// 
        ///  项目安全人工时
        /// 
        protected string Two
        {
            get
            {
                List series = new List();
                Model.BusinessColumn businessColumn = new Model.BusinessColumn();
                List listCategories = new List();
                businessColumn.title = "项目安全人工时";
                Model.SingleSerie s = new Model.SingleSerie();
                List listdata = new List();
                Model.SingleSerie s2 = new Model.SingleSerie();
                List listdata2 = new List();
                var getMonts = SitePerson_MonthReportService.getMonthReports(CurrUser.LoginProjectId, null);
                foreach (var month in getMonts.OrderBy(x => x.CompileDate))
                {
                    listCategories.Add(string.Format("{0:yyyy-MM}", month.CompileDate));
                    listdata.Add(double.Parse((month.TotalPersonWorkTime ?? 0).ToString()));
                    listdata2.Add(double.Parse((month.DayWorkTime ?? 0).ToString()));
                }
                s.data = listdata;
                series.Add(s);
                s2.data = listdata2;
                series.Add(s2);
                businessColumn.categories = listCategories;
                businessColumn.series = series;
                return JsonConvert.SerializeObject(businessColumn);
            }
        }
        #endregion
                
        #region  现场人员
        /// 
        /// 
        /// 
        public static List getEmployInOutRecords;
        /// 
        /// 
        /// 
        public static List getAllPersons;
        /// 
        /// 
        /// 
        public static List getPersons;
        /// 
        /// 按单位统计
        /// 
        protected string Four1
        {
            get
            {
                List series = new List();
                Model.BusinessColumn businessColumn = new Model.BusinessColumn();
                List listCategories = new List();
                Model.SingleSerie s = new Model.SingleSerie();
                Model.SingleSerie s2 = new Model.SingleSerie();
                List listdata = new List();
                List listdata2 = new List();
                businessColumn.title = "现场人员";
                int manCount = 0;
                var units = getAllPersons.Select(x => x.UnitId).Distinct();
                if (units.Count() > 0)
                {                 
                    foreach (var unitId in units)
                    {                       
                        var unitRords = from x in getEmployInOutRecords
                                        join y in getAllPersons on x.IDCardNo equals y.IdentityCard
                                        where y.UnitId == unitId && y.PersonId != null && x.UnitId== unitId
                                        select x;
                        var unitPersons = getPersons.Where(x => x.UnitId == unitId);
                        int rcount = unitRords.Distinct().Count();
                        if (rcount > 0)
                        {
                            manCount += rcount;
                            listCategories.Add(UnitService.GetShortUnitNameByUnitId(unitId));
                            listdata.Add(rcount);
                            listdata2.Add(unitPersons.Count());
                        }
                    }
                }
                int nullCount = getEmployInOutRecords.Count() - manCount;
                if (nullCount > 0)
                {
                    listCategories.Add("未知");
                    listdata.Add(nullCount);
                    listdata2.Add(0);
                }
                s.data = listdata;
                series.Add(s);
                s2.data = listdata2;
                series.Add(s2);
                businessColumn.categories = listCategories;
                businessColumn.series = series;
                return JsonConvert.SerializeObject(businessColumn);
            }
        }
        /// 
        /// 按岗位统计
        /// 
        protected string Four2
        {
            get
            {
                List series = new List();
                Model.BusinessColumn businessColumn = new Model.BusinessColumn();
                List listCategories = new List();
                Model.SingleSerie s = new Model.SingleSerie();
                List listdata = new List();
                Model.SingleSerie s2 = new Model.SingleSerie();
                List listdata2 = new List();
                businessColumn.title = "现场人员";
                var getTypes = (from x in getEmployInOutRecords
                                join y in getAllPersons on x.IDCardNo equals y.IdentityCard
                                join z in Funs.DB.Base_WorkPost on y.WorkPostId equals z.WorkPostId                    
                                select new { y.WorkPostId,z.WorkPostName }).Distinct();
                if (getTypes.Count() > 0)
                {
                    foreach (var item in getTypes)
                    {
                        listCategories.Add(item.WorkPostName ?? "未知");
                        var unitRords = getEmployInOutRecords.Where(x => x.PostId == item.WorkPostId);
                        var unitPersons = getPersons.Where(x => x.WorkPostId == item.WorkPostId);
                        listdata.Add(unitRords.Count());
                        listdata2.Add(unitPersons.Count());
                    }
                }
                else
                {
                    var getpTypes = getPersons.Select(x => x.WorkPostId).Distinct();
                    foreach (var item in getpTypes)
                    {
                        listCategories.Add(WorkPostService.getWorkPostNameById(item));
                        var unitRords = getEmployInOutRecords.Where(x => x.PostId == item);
                        var unitPersons = getPersons.Where(x => x.WorkPostId == item);
                        listdata.Add(unitRords.Count());
                        listdata2.Add(unitPersons.Count());
                    }
                }
                s.data = listdata;
                series.Add(s);
                s2.data = listdata2;
                series.Add(s2);
                businessColumn.categories = listCategories;
                businessColumn.series = series;
                return JsonConvert.SerializeObject(businessColumn);
            }
        }
        #endregion
                
    }
}