using BLL;
using FastReport.Data;
using FineUIPro.Web.HSSE.SitePerson;
using FineUIPro.Web.ProjectData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HSSE.KqShowScreen
{
    public partial class KqShowScreen : PageBase
    {
        #region 项目主键
        /// 
        /// 项目主键
        /// 
        public string ProjectId
        {
            get
            {
                return (string)ViewState["ProjectId"];
            }
            set
            {
                ViewState["ProjectId"] = value;
            }
        }
        #endregion
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ProjectId = this.CurrUser.LoginProjectId;
            }
        }
        #endregion
        [WebMethod]
        public static object getData(string projectId)
        {
            int total = 0;
            HashSet ids = new HashSet();
            HashSet idsIn = new HashSet();
            Dictionary inperson = new Dictionary();
            Dictionary outPerson = new Dictionary();
            DateTime dateTime = DateTime.Now.AddDays(-1);
            var inout = Funs.DB.SitePerson_PersonInOut.Where(x => x.ProjectId == projectId && x.ChangeTime > dateTime).OrderByDescending(x => x.ChangeTime);
            Dictionary company = new Dictionary();
            Dictionary workPost = new Dictionary();
            foreach (var io in inout)
            {
                ids.Add(io.PersonId);
                if (io.IsIn.HasValue && io.IsIn.Value) //24小时内最晚的入场
                {
                    if (inperson.ContainsKey(io.PersonId))
                    {
                        if (inperson[io.PersonId] < io.ChangeTime.Value)
                        {
                            inperson[io.PersonId] = io.ChangeTime.Value;
                        }
                    }
                    else
                    {
                        if (io.ChangeTime.HasValue)
                        {
                            inperson.Add(io.PersonId, io.ChangeTime.Value);
                        }
                    }
                }
                else  //24小时内最晚的出场
                {
                    if (outPerson.ContainsKey(io.PersonId))
                    {
                        if (outPerson[io.PersonId] < io.ChangeTime.Value)
                        {
                            outPerson[io.PersonId] = io.ChangeTime.Value;
                        }
                    }
                    else
                    {
                        if (io.ChangeTime.HasValue)
                        {
                            outPerson.Add(io.PersonId, io.ChangeTime.Value);
                        }
                    }
                }
            }
            foreach (var id in ids)
            {
                if (inperson.ContainsKey(id) && !outPerson.ContainsKey(id))//只有入场没有出场  在场
                {
                    idsIn.Add(id);
                    total++;
                }
                else if (inperson.ContainsKey(id) && outPerson.ContainsKey(id))
                {
                    if (inperson[id] > outPerson[id])//最晚入场时间 小于出场时间   在场
                    {
                        idsIn.Add(id);
                        total++;
                    }
                }
            }
            HashSet keys1 = new HashSet();
            HashSet keys2 = new HashSet();
            foreach (var io in inout)
            {
                if (idsIn.Contains(io.PersonId))
                {
                    if (!string.IsNullOrEmpty(io.WorkPostName))
                    {
                        if (!keys1.Contains(io.PersonId))
                        {
                            keys1.Add(io.PersonId);
                            if (workPost.ContainsKey(io.WorkPostName))
                            {
                                workPost[io.WorkPostName] = workPost[io.WorkPostName] + 1;
                            }
                            else
                            {
                                workPost[io.WorkPostName] = 1;
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(io.UnitName))
                    {
                        if (!keys2.Contains(io.PersonId))
                        {
                            keys2.Add(io.PersonId);
                            if (company.ContainsKey(io.UnitName))
                            {
                                company[io.UnitName] = company[io.UnitName] + 1;
                            }
                            else
                            {
                                company[io.UnitName] = 1;
                            }
                        }
                    }
                }
            }
            var personInOut = inout.FirstOrDefault();
            string teamGroupName = "";
            string inOut = "出场";
            string PhotoUrl = "";
            string PersonName = "";
            string UnitName = "";
            string WorkPostName = "";
            string ChangeTime = "";
            if (personInOut != null)
            {
                var person = Funs.DB.SitePerson_Person.FirstOrDefault(x => x.PersonId == personInOut.PersonId);
                PersonName = personInOut.PersonName;
                UnitName = personInOut.UnitName;
                WorkPostName = personInOut.WorkPostName;
                ChangeTime = personInOut.ChangeTime.Value.ToString("yyyy-MM-dd HH:mm");
                PhotoUrl = "../../" + person.PhotoUrl;
                if (personInOut.IsIn == true)
                {
                    inOut = "入场";
                }
                if (!string.IsNullOrEmpty(person.TeamGroupId))
                {
                    var teamGroup = Funs.DB.ProjectData_TeamGroup.FirstOrDefault(x => x.TeamGroupId == person.TeamGroupId);
                    if (teamGroup != null)
                    {
                        teamGroupName = teamGroup.TeamGroupName;
                    }
                }
            }
            List