using System;
using System.Collections.Generic;
using System.Linq;
namespace BLL
{
    public class SitePerson_DayReportService
    {
        /// 
        /// 获取工作日报信息
        /// 
        /// 工作日报Id
        /// 
        public static Model.SitePerson_DayReport GetDayReportByDayReportId(string dayReportId)
        {
            return Funs.DB.SitePerson_DayReport.FirstOrDefault(x => x.DayReportId == dayReportId);
        }
        /// 
        /// 增加工作日报信息
        /// 
        /// 工作日报实体
        public static void AddDayReport(Model.SitePerson_DayReport dayReport)
        {
            Model.SGGLDB db = Funs.DB;
            Model.SitePerson_DayReport newDayReport = new Model.SitePerson_DayReport
            {
                DayReportId = dayReport.DayReportId,
                ProjectId = dayReport.ProjectId,
                CompileMan = dayReport.CompileMan,
                CompileDate = dayReport.CompileDate,
                States = dayReport.States
            };
            db.SitePerson_DayReport.InsertOnSubmit(newDayReport);
            db.SubmitChanges();
            ////增加一条编码记录
            BLL.CodeRecordsService.InsertCodeRecordsByMenuIdProjectIdUnitId(BLL.Const.DayReportMenuId, dayReport.ProjectId, null, dayReport.DayReportId, dayReport.CompileDate);
        }
        /// 
        /// 修改工作日报信息
        /// 
        /// 工作日报实体
        public static void UpdateDayReport(Model.SitePerson_DayReport dayReport)
        {
            Model.SGGLDB db = Funs.DB;
            Model.SitePerson_DayReport newDayReport = db.SitePerson_DayReport.First(e => e.DayReportId == dayReport.DayReportId);
            newDayReport.ProjectId = dayReport.ProjectId;
            newDayReport.CompileMan = dayReport.CompileMan;
            newDayReport.CompileDate = dayReport.CompileDate;
            newDayReport.States = dayReport.States;
            db.SubmitChanges();
        }
        /// 
        /// 根据工作日报主键删除一个工作日报信息
        /// 
        /// 工作日报主键
        public static void DeleteDayReportByDayReportId(string dayReportId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.SitePerson_DayReport dayReport = db.SitePerson_DayReport.FirstOrDefault(e => e.DayReportId == dayReportId);
            if (dayReport != null)
            {
                ///删除编码表记录
                BLL.CodeRecordsService.DeleteCodeRecordsByDataId(dayReportId);
                BLL.CommonService.DeleteFlowOperateByID(dayReportId);
                db.SitePerson_DayReport.DeleteOnSubmit(dayReport);
                db.SubmitChanges();
            }
        }
        /// 
        /// 判断人工月报是否存在
        /// 
        /// 
        /// 
        /// true 存在;false:不存在
        public static bool IsExistDayReport(DateTime compileDate, string projectId)
        {
            var q = from x in Funs.DB.SitePerson_DayReport
                    where x.CompileDate == compileDate && x.ProjectId == projectId
                    select x;
            if (q.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 根据时间获取工作日报信息
        /// 
        /// 开始时间
        /// 结束时间
        /// 工作日报信息
        public static List GetDayReportsByCompileDate(DateTime startTime, DateTime endTime, string projectId)
        {
            return (from x in Funs.DB.SitePerson_DayReport where x.CompileDate >= startTime && x.CompileDate < endTime && x.ProjectId == projectId && x.States == BLL.Const.State_2 select x).ToList();
        }
        /// 
        /// 根据时间获取工作日报信息
        /// 
        /// 开始时间
        /// 结束时间
        /// 工作日报信息
        public static List GetDayReportsByCompileDate2(DateTime startTime, string projectId)
        {
            return (from x in Funs.DB.SitePerson_DayReport where x.CompileDate >= startTime && x.ProjectId == projectId && x.States == BLL.Const.State_2 select x).ToList();
        }
        /// 
        /// 获取工作日报信息
        /// 
        /// 开始时间
        /// 结束时间
        /// 工作日报信息
        public static List GetDayReports(string projectId)
        {
            return (from x in Funs.DB.SitePerson_DayReport where x.ProjectId == projectId && x.States == BLL.Const.State_2 select x).ToList();
        }
        /// 
        /// 根据时间获取工作日报信息
        /// 
        /// 开始时间
        /// 结束时间
        /// 工作日报信息
        public static List GetDayReportsByCompileDateAndUnitId(DateTime startTime, DateTime endTime, string projectId, string unitId)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                return (from x in db.SitePerson_DayReport
                        join y in db.SitePerson_DayReportDetail
                        on x.DayReportId equals y.DayReportId
                        where x.CompileDate >= startTime && x.CompileDate < endTime && x.ProjectId == projectId && x.States == BLL.Const.State_2
                        && y.UnitId == unitId
                        select x).Distinct().ToList();
            }
        }
        /// 
        ///  获取出入记录人工时
        /// 
        /// 
        public static List getDayReports(string projectId, DateTime? sDate)
        {
            Model.SGGLDB db = Funs.DB;
            List reports = new List();
            var getAllPersonInOutList = from x in db.SitePerson_PersonInOutNumber
                                        where x.ProjectId == projectId 
                                        select x;
            if (getAllPersonInOutList.Count() > 0)
            {
                var getInOutDates = getAllPersonInOutList.Select(x => x.InOutDate);
                if (sDate.HasValue)
                {
                    getInOutDates = getInOutDates.Where(x => x.Year == sDate.Value.Year && x.Month == sDate.Value.Month && x.Day == sDate.Value.Day);
                }
                foreach (var item in getInOutDates)
                {
                    var getNow = getAllPersonInOutList.FirstOrDefault(x => x.InOutDate.Year == item.Year && x.InOutDate.Month == item.Month && x.InOutDate.Day == item.Day);
                    if (getNow != null)
                    {
                        Model.SitePerson_DayReport reportItem = new Model.SitePerson_DayReport
                        {
                            DayReportId = SQLHelper.GetNewID(),
                            ProjectId = projectId,
                            CompileDate = item,
                            TotalPersonWorkTime = getNow.WorkHours,
                        };
                        var getMax = getAllPersonInOutList.Where(x => x.InOutDate < item).Max(x => x.WorkHours) ?? 0;
                        reportItem.DayWorkTime = (getNow.WorkHours ?? 0) - getMax;
                        reports.Add(reportItem);
                    }
                }
            }
            return reports;
        }
    }
}