using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading;
namespace FineUIPro.Web.HSSE.SitePerson
{
    public partial class PersonInfoClock : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UnitService.GetUnit(this.drpUnit, this.CurrUser.LoginProjectId, true);
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if ((this.ckIn.Checked && !string.IsNullOrEmpty(this.txtInTime.Text)) || (this.ckOut.Checked && !string.IsNullOrEmpty(this.txtOutTime.Text)))
            {
                var getPersons = from x in Funs.DB.SitePerson_Person
                                 join y in Funs.DB.Project_ProjectUnit on new { x.ProjectId, x.UnitId } equals new { y.ProjectId, y.UnitId }
                                 where x.ProjectId == this.CurrUser.LoginProjectId && (!x.OutTime.HasValue || x.OutTime > DateTime.Now)
                                 && x.IsCardNoOK == true && x.InTime <= DateTime.Now && x.HeadImage != null
                                 && y.IsSynchro == true && x.RealNameAddTime.HasValue
                                 select x;
                if (this.drpUnit.SelectedValueArray.Length == 1 && this.drpUnit.SelectedValue == BLL.Const._Null)
                {
                }
                else
                {
                    string[] strs = this.drpUnit.SelectedValueArray;
                    getPersons = getPersons.Where(x => strs.Contains(x.UnitId));
                }
                var units = from x in Funs.DB.Base_Unit
                            select x;
                var workPosts = from x in Funs.DB.Base_WorkPost
                                select x;
                var unitWorks = from x in Funs.DB.WBS_UnitWork
                                select x;
                Dictionary unitNameDic = new Dictionary();
                Dictionary workPostNameDic = new Dictionary();
                Dictionary workPostTypeDic = new Dictionary();
                Dictionary unitWorkDic = new Dictionary();
                foreach (var unit in units)
                {
                    unitNameDic.Add(unit.UnitId, unit.UnitName);
                }
                foreach (var workPost in workPosts)
                {
                    workPostNameDic.Add(workPost.WorkPostId, workPost.WorkPostName);
                    workPostTypeDic.Add(workPost.WorkPostId, workPost.PostType);
                }
                foreach (var unitWork in unitWorks)
                {
                    unitWorkDic.Add(unitWork.UnitWorkId, unitWork.UnitWorkName + UnitWorkService.GetProjectType(unitWork.ProjectType));
                }
                if (getPersons.Count() > 0)
                {
                    List list = new List();
                    foreach (var item in getPersons)
                    {
                        if (this.ckIn.Checked)
                        {
                            Model.SitePerson_PersonInOut newPersonInfo = new Model.SitePerson_PersonInOut
                            {
                                PersonInOutId = SQLHelper.GetNewID(),
                                PersonId = item.PersonId,
                                ProjectId = item.ProjectId,
                                WorkAreaId = item.WorkAreaId,
                                InOutWay = Const.InOutWay_2,
                            };
                            newPersonInfo.ProjectId = item.ProjectId;
                            newPersonInfo.PersonId = item.PersonId;
                            newPersonInfo.PersonName = item.PersonName;
                            newPersonInfo.IdentityCard = item.IdentityCard;
                            newPersonInfo.UnitId = item.UnitId;
                            newPersonInfo.UnitName = unitNameDic.ContainsKey(item.UnitId) ? unitNameDic[item.UnitId] : "";
                            newPersonInfo.WorkPostId = item.WorkPostId;
                            if (!string.IsNullOrEmpty(item.WorkPostId))
                            {
                                newPersonInfo.WorkPostName = workPostNameDic.ContainsKey(item.WorkPostId) ? workPostNameDic[item.WorkPostId] : "";
                                newPersonInfo.PostType = workPostTypeDic.ContainsKey(item.WorkPostId) ? workPostTypeDic[item.WorkPostId] : "";
                            }
                            if (string.IsNullOrEmpty(newPersonInfo.WorkAreaName) && !string.IsNullOrEmpty(newPersonInfo.WorkAreaId))
                            {
                                string unitWorkName = string.Empty;
                                if (!string.IsNullOrEmpty(newPersonInfo.WorkAreaId))
                                {
                                    string[] strs = newPersonInfo.WorkAreaId.Split(',');
                                    foreach (var i in strs)
                                    {
                                        unitWorkName += unitWorkDic.ContainsKey(i) ? unitWorkDic[i] + "," : "";
                                    }
                                    if (!string.IsNullOrEmpty(unitWorkName))
                                    {
                                        unitWorkName = unitWorkName.Substring(0, unitWorkName.LastIndexOf(","));
                                    }
                                }
                                newPersonInfo.WorkAreaName = unitWorkName;
                            }
                            newPersonInfo.IsIn = true;
                            newPersonInfo.ChangeTime = Funs.GetNewDateTimeOrNow(this.txtInTime.Text);
                            list.Add(newPersonInfo);
                        }
                        if (this.ckOut.Checked)
                        {
                            Model.SitePerson_PersonInOut newPersonInfo = new Model.SitePerson_PersonInOut
                            {
                                PersonInOutId = SQLHelper.GetNewID(),
                                PersonId = item.PersonId,
                                ProjectId = item.ProjectId,
                                WorkAreaId = item.WorkAreaId,
                                InOutWay = Const.InOutWay_2,
                            };
                            newPersonInfo.ProjectId = item.ProjectId;
                            newPersonInfo.PersonId = item.PersonId;
                            newPersonInfo.PersonName = item.PersonName;
                            newPersonInfo.IdentityCard = item.IdentityCard;
                            newPersonInfo.UnitId = item.UnitId;
                            newPersonInfo.UnitName = unitNameDic.ContainsKey(item.UnitId) ? unitNameDic[item.UnitId] : "";
                            newPersonInfo.WorkPostId = item.WorkPostId;
                            if (!string.IsNullOrEmpty(item.WorkPostId))
                            {
                                newPersonInfo.WorkPostName = workPostNameDic.ContainsKey(item.WorkPostId) ? workPostNameDic[item.WorkPostId] : "";
                                newPersonInfo.PostType = workPostTypeDic.ContainsKey(item.WorkPostId) ? workPostTypeDic[item.WorkPostId] : "";
                            }
                            if (string.IsNullOrEmpty(newPersonInfo.WorkAreaName) && !string.IsNullOrEmpty(newPersonInfo.WorkAreaId))
                            {
                                string unitWorkName = string.Empty;
                                if (!string.IsNullOrEmpty(newPersonInfo.WorkAreaId))
                                {
                                    string[] strs = newPersonInfo.WorkAreaId.Split(',');
                                    foreach (var i in strs)
                                    {
                                        unitWorkName += unitWorkDic.ContainsKey(i) ? unitWorkDic[i] + "," : "";
                                    }
                                    if (!string.IsNullOrEmpty(unitWorkName))
                                    {
                                        unitWorkName = unitWorkName.Substring(0, unitWorkName.LastIndexOf(","));
                                    }
                                }
                                newPersonInfo.WorkAreaName = unitWorkName;
                            }
                            newPersonInfo.IsIn = false;
                            newPersonInfo.ChangeTime = Funs.GetNewDateTimeOrNow(this.txtOutTime.Text);
                            list.Add(newPersonInfo);
                        }
                    }
                    Thread t = new Thread(new ThreadStart(() =>
                    {
                        BLL.PersonInOutService.AddPersonInOut(list);
                    }));
                    t.Start();
                }
                else
                {
                    Alert.ShowInParent("当前项目没有身份证合法的在岗人员!", MessageBoxIcon.Warning);
                    return;
                }
            }
            else
            {
                Alert.ShowInParent("请选择进场或出场!", MessageBoxIcon.Warning);
                return;
            }
            ShowNotify("提交成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        protected void ck_CheckedChanged(object sender, CheckedEventArgs e)
        {
            this.txtInTime.Text = string.Empty;
            if (this.ckIn.Checked)
            {
                this.txtInTime.Text = string.Format("{0:yyyy-MM-dd HH:mm}", System.DateTime.Now);
            }
            this.txtOutTime.Text = string.Empty;
            if (this.ckOut.Checked)
            {
                this.txtOutTime.Text = string.Format("{0:yyyy-MM-dd HH:mm}", System.DateTime.Now);
            }
        }
    }
}