using Model;
using System;
using System.Collections.Generic;
using System.Linq;
namespace BLL
{
    /// 
    /// 周计划情况
    /// 
    public class WeekPlanService
    {
        //public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 根据主键获取周计划情况
        /// 
        /// 
        /// 
        public static Model.JDGL_WeekPlan GetWeekPlanById(string WeekPlanId)
        {
            Model.SGGLDB db = Funs.DB;
            return db.JDGL_WeekPlan.FirstOrDefault(e => e.WeekPlanId == WeekPlanId);
        }
        /// 
        /// 根据主键获取周计划情况
        ///  
        /// 
        /// 
        public static List GetWeekPlansByWeeks(string projectId, string weekNo)
        {
            Model.SGGLDB db = Funs.DB;
            return (from x in db.JDGL_WeekPlan where x.ProjectId == projectId && x.WeekNo == weekNo select x).ToList();
        }
        /// 
        /// 添加周计划情况
        /// 
        /// 
        public static void AddWeekPlan(Model.JDGL_WeekPlan WeekPlan)
        {
            Model.SGGLDB db = Funs.DB;
            Model.JDGL_WeekPlan newWeekPlan = new Model.JDGL_WeekPlan
            {
                WeekPlanId = WeekPlan.WeekPlanId,
                ProjectId = WeekPlan.ProjectId,
                WeekNo = WeekPlan.WeekNo,
                StartDate = WeekPlan.StartDate,
                EndDate = WeekPlan.EndDate,
                UnitWork = WeekPlan.UnitWork,
                Major = WeekPlan.Major,
                WorkContent = WeekPlan.WorkContent,
                UnitId = WeekPlan.UnitId,
                DutyPerson = WeekPlan.DutyPerson,
                PlanDate = WeekPlan.PlanDate,
                IsOK = WeekPlan.IsOK,
                Remark = WeekPlan.Remark,
                CompileMan = WeekPlan.CompileMan,
                CompileDate = WeekPlan.CompileDate,
                SortIndex = WeekPlan.SortIndex,
            };
            db.JDGL_WeekPlan.InsertOnSubmit(newWeekPlan);
            db.SubmitChanges();
        }
        /// 
        /// 修改周计划情况
        /// 
        /// 
        public static void UpdateWeekPlan(Model.JDGL_WeekPlan WeekPlan)
        {
            Model.SGGLDB db = Funs.DB;
            Model.JDGL_WeekPlan newWeekPlan = db.JDGL_WeekPlan.FirstOrDefault(e => e.WeekPlanId == WeekPlan.WeekPlanId);
            if (newWeekPlan != null)
            {
                newWeekPlan.UnitWork = WeekPlan.UnitWork;
                newWeekPlan.Major = WeekPlan.Major;
                newWeekPlan.WorkContent = WeekPlan.WorkContent;
                newWeekPlan.UnitId = WeekPlan.UnitId;
                newWeekPlan.DutyPerson = WeekPlan.DutyPerson;
                newWeekPlan.PlanDate = WeekPlan.PlanDate;
                newWeekPlan.IsOK = WeekPlan.IsOK;
                newWeekPlan.Remark = WeekPlan.Remark;
                newWeekPlan.CompileMan = WeekPlan.CompileMan;
                newWeekPlan.CompileDate = WeekPlan.CompileDate;
                newWeekPlan.SortIndex = WeekPlan.SortIndex;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除周计划情况
        /// 
        /// 
        public static void DeleteWeekPlanByWeekPlanId(string WeekPlanId)
        {
            Model.SGGLDB db = Funs.DB;
            var q = (from x in db.JDGL_WeekPlan where x.WeekPlanId == WeekPlanId select x).FirstOrDefault();
            if (q != null)
            {
                db.JDGL_WeekPlan.DeleteOnSubmit(q);
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据当前时间获取上一个周计划
        /// 
        /// 
        /// 
        public static JDGL_WeekPlan GetLastWeekPlan(string projectId)
        {
            Model.SGGLDB db = Funs.DB;
            return (from x in db.JDGL_WeekPlan where x.ProjectId == projectId && DateTime.Now > ((DateTime)x.EndDate).AddDays(1) orderby x.EndDate select x).FirstOrDefault();
        }
        /// 
        /// 根据主键删除周计划情况
        /// 
        /// 
        /// 
        public static void DeleteAllWeekPlan(string projectId, string weekNo)
        {
            Model.SGGLDB db = Funs.DB;
            var q = from x in db.JDGL_WeekPlan where x.ProjectId == projectId && x.WeekNo == weekNo select x;
            if (q != null)
            {
                db.JDGL_WeekPlan.DeleteAllOnSubmit(q);
                db.SubmitChanges();
            }
        }
        /// 
        /// 获取周计划下责任单位Id
        /// 
        /// 
        /// 
        public static List GetWeekPlanUnitIdListByProjectIdWeekNo(string projectId)
        {
            Model.SGGLDB db = Funs.DB;
            var lstUnit = (from x in db.JDGL_WeekPlan where x.ProjectId == projectId orderby x.UnitId select x.UnitId).Distinct().ToList();
            return lstUnit;
        }
    }
}