using FineUIPro;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;

namespace BLL
{
    public static class Project_HJGLData_DefectService
    {
        public static SGGLDB db = Funs.DB;


        #region 获取列表

        /// <summary>
        /// 记录数
        /// </summary>
        public static int count { get; set; }

        public static List<Project_HJGLData_Defect> GetProject_HJGLData_DefectByModle(Project_HJGLData_Defect table)
        {
            var q = from x in db.Project_HJGLData_Defect
                    where
                        (string.IsNullOrEmpty(table.Id) || x.Id.Contains(table.Id)) &&
                        (string.IsNullOrEmpty(table.ProjectId) || x.Id.Contains(table.ProjectId)) &&
                        (string.IsNullOrEmpty(table.UnitId) || x.UnitId.Contains(table.UnitId)) &&
                        (string.IsNullOrEmpty(table.CollCropCode) || x.CollCropCode.Contains(table.CollCropCode)) &&
                        (string.IsNullOrEmpty(table.UnitName) || x.UnitName.Contains(table.UnitName)) &&
                        (string.IsNullOrEmpty(table.DefectName) || x.DefectName.Contains(table.DefectName))
                    select x
                ;

            return q.ToList();
        }

        /// 获取分页列表
        /// </summary>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">每页数量</param>
        /// <returns></returns>
        public static IEnumerable getListData(Project_HJGLData_Defect table, Grid Grid1)
        {
            var q = GetProject_HJGLData_DefectByModle(table);
            count = q.Count();
            if (count == 0) return null;
            // q = SortConditionHelper.SortingAndPaging(q, Grid1.SortField, Grid1.SortDirection, Grid1.PageIndex, Grid1.PageSize);
            return from x in q
                select new
                {
                    x.Id,
                    x.ProjectId,
                    x.UnitId,
                    x.CollCropCode,
                    x.UnitName,
                    x.ReportDate,
                    x.DefectName,
                    x.DefectNum
                };
        }

        #endregion

        public static Project_HJGLData_Defect GetProject_HJGLData_DefectById(string Id)
        {
            return db.Project_HJGLData_Defect.FirstOrDefault(x => x.Id == Id);
        }

        /// <summary>
        /// 获取项目该日期的缺陷分析数据
        /// </summary>
        /// <param name="reportDate"></param>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static List<Project_HJGLData_Defect> GetProject_HJGLData_DefectByDate(DateTime? reportDate,
            string projectid)
        {
            var q = from x in db.Project_HJGLData_Defect
                where x.ReportDate.Value.Date.CompareTo(reportDate.Value.Date) == 0 && x.ProjectId == projectid
                select x;
            return q.ToList();
        }

        public static void AddProject_HJGLData_Defect(Project_HJGLData_Defect newtable)
        {
            var table = new Project_HJGLData_Defect
            {
                Id = newtable.Id,
                ProjectId = newtable.ProjectId,
                UnitId = newtable.UnitId,
                CollCropCode = newtable.CollCropCode,
                UnitName = newtable.UnitName,
                ReportDate = newtable.ReportDate,
                DefectName = newtable.DefectName,
                DefectNum = newtable.DefectNum
            };
            db.Project_HJGLData_Defect.InsertOnSubmit(table);
            db.SubmitChanges();
        }

        public static void AddBulkProject_HJGLData_Defect(List<Project_HJGLData_Defect> newtables)
        {
            db.Project_HJGLData_Defect.InsertAllOnSubmit(newtables);
            db.SubmitChanges();
        }

        public static void UpdateProject_HJGLData_Defect(Project_HJGLData_Defect newtable)
        {
            var table = db.Project_HJGLData_Defect.FirstOrDefault(x => x.Id == newtable.Id);
            if (table != null)
            {
                table.Id = newtable.Id;
                table.ProjectId = newtable.ProjectId;
                table.UnitId = newtable.UnitId;
                table.CollCropCode = newtable.CollCropCode;
                table.UnitName = newtable.UnitName;
                table.ReportDate = newtable.ReportDate;
                table.DefectName = newtable.DefectName;
                table.DefectNum = newtable.DefectNum;
                db.SubmitChanges();
            }
        }

        public static void DeleteProject_HJGLData_DefectById(string Id)
        {
            var table = db.Project_HJGLData_Defect.FirstOrDefault(x => x.Id == Id);
            if (table != null)
            {
                db.Project_HJGLData_Defect.DeleteOnSubmit(table);
                db.SubmitChanges();
            }
        }

        public static void DeleteProject_HJGLData_DefectByDate(DateTime? reportDate)
        {
            var table = db.Project_HJGLData_Defect.Where(x =>
                x.ReportDate.Value.Date.CompareTo(reportDate.Value.Date) == 0);
            if (table != null)
            {
                db.Project_HJGLData_Defect.DeleteAllOnSubmit(table);
                db.SubmitChanges();
            }
        }

        public static void DeleteProject_HJGLData_DefectByDate(DateTime? reportDate, string projectid)
        {
            var table = db.Project_HJGLData_Defect.Where(x =>
                x.ReportDate.Value.Date.CompareTo(reportDate.Value.Date) == 0 && x.ProjectId == projectid);
            if (table != null)
            {
                db.Project_HJGLData_Defect.DeleteAllOnSubmit(table);
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 判断当天是否已统计数据
        /// </summary>
        /// <returns></returns>
        public static bool IsReportByToday(string projectid, string type)
        {
            var result = false;
            var q = (from x in Funs.DB.Project_HJGLData_Defect
                where x.ReportDate < DateTime.Now.AddDays(1).Date && x.ReportDate >= DateTime.Now.Date &&
                      x.ProjectId == projectid && x.DefectName == type
                select x).ToList();
            if (q != null && q.Count > 0) result = true;
            return result;
        }

        /// <summary>
        /// 根据projectid,获取当天的统计后的数据
        /// </summary>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public static Project_HJGLData_Defect getTodayProject_HJGLData_Defect(string projectid, string type)
        {
            var q = (from x in Funs.DB.Project_HJGLData_Defect
                where x.ReportDate < DateTime.Now.AddDays(1).Date && x.ReportDate >= DateTime.Now.Date &&
                      x.ProjectId == projectid && x.DefectName == type
                select x).FirstOrDefault();
            return q;
        }

        public static List<HJGLDataDefectItems> getTodayProject_HJGLData_Defect()
        {
            var q = (from x in Funs.DB.Project_HJGLData_Defect
                where x.ReportDate < DateTime.Now.AddDays(1).Date && x.ReportDate >= DateTime.Now.Date
                group x by x.DefectName
                into g
                select new HJGLDataDefectItems
                {
                    Id = SQLHelper.GetNewID(),
                    DefectName = g.Key,
                    DefectNum = g.Sum(p => p.DefectNum)
                }).ToList();
            return q;
        }

        /// <summary>
        /// 统计所有在建项目数据
        /// </summary>
        public static void StatisticalAllProjectData()
        {
            var db = Funs.DB;

            var projectids = ProjectService.GetProjectWorkList().Select(x => x.ProjectId).ToList();
            var thisUnitId = string.Empty;
            var thisUnit = CommonService.GetIsThisUnit();
            if (thisUnit != null) thisUnitId = thisUnit.UnitId;
            var baseUnit = UnitService.GetUnitByUnitId(thisUnitId);
            var data = (from x in db.CH_CheckItem
                        join y in db.CH_Check on x.CHT_CheckID equals y.CHT_CheckID
                where projectids.Contains(y.ProjectId)
                group x by new { x.Defects_Definition, y.ProjectId }
                into g
                select new
                {
                    UnitId = thisUnitId,
                    CollCropCode = baseUnit.CollCropCode,
                    UnitName = baseUnit.UnitName,
                    ProjectId = g.Key.ProjectId,
                    ReportDate = DateTime.Now.Date,
                    DefectName = g.Key.Defects_Definition,
                    DefectNum = g.Count(x => x.Defects_Definition == g.Key.Defects_Definition)
                }).ToList();
            DeleteProject_HJGLData_DefectByDate(DateTime.Now.Date); //删除当前所有
            var projectHjglDataDefect = new List<Project_HJGLData_Defect>();
            foreach (var item in data
                    )
            {
                var table = new Project_HJGLData_Defect
                {
                    Id = SQLHelper.GetNewID(),
                    UnitId = item.UnitId,
                    CollCropCode = item.CollCropCode,
                    UnitName = item.UnitName,
                    ProjectId = item.ProjectId,
                    ReportDate = item.ReportDate,
                    DefectName = item.DefectName,
                    DefectNum = item.DefectNum
                };
                projectHjglDataDefect.Add(table);
            }

            AddBulkProject_HJGLData_Defect(projectHjglDataDefect); //批量增加
        }

        /// <summary>
        /// 统计数据
        /// </summary>
        /// <param name="projectid"></param>
        public static void StatisticalData(string projectid)
        {
            var thisUnitId = string.Empty;
            var thisUnit = CommonService.GetIsThisUnit();
            if (thisUnit != null) thisUnitId = thisUnit.UnitId;
            var base_Unit = UnitService.GetUnitByUnitId(thisUnitId);
            var list = from x in db.CH_CheckItem
                join y in db.CH_Check on x.CHT_CheckID equals y.CHT_CheckID
                where y.ProjectId == projectid
                select x;
            var types = (from x in list select x.Defects_Definition).Distinct().ToList();
            foreach (var t in types)
                if (!string.IsNullOrEmpty(t))
                {
                    var table = new Project_HJGLData_Defect
                    {
                        UnitId = thisUnitId,
                        CollCropCode = base_Unit.CollCropCode,
                        UnitName = base_Unit.UnitName,
                        ProjectId = projectid,
                        ReportDate = DateTime.Now.Date,
                        DefectName = t,
                        DefectNum = list.Count(x => x.Defects_Definition == t)
                    };
                    if (IsReportByToday(projectid, t))
                    {
                        table.Id = getTodayProject_HJGLData_Defect(projectid, t).Id;
                        UpdateProject_HJGLData_Defect(table);
                    }
                    else
                    {
                        table.Id = SQLHelper.GetNewID();
                        AddProject_HJGLData_Defect(table);
                    }
                }
        }

        /// <summary>
        /// 获取缺陷名称
        /// </summary>
        /// <returns></returns>
        public static string GetDefectName(string projectid)
        {
            var result = "";
            return result;
        }

        /// <summary>
        /// 获取缺陷数量
        /// </summary>
        /// <returns></returns>
        public static int GetDefectNum(string projectid)
        {
            var result = 0;
            return result;
        }
    }
}