using System.Collections.Generic;
using System.Linq;
namespace BLL
{
    /// 
    /// 考试计划
    /// 
    public static class TestPlanService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 根据主键获取培训计划
        /// 
        /// 
        /// 
        public static Model.Training_TestPlan GetTestPlanById(string TestPlanId)
        {
            return Funs.DB.Training_TestPlan.FirstOrDefault(e => e.TestPlanId == TestPlanId);
        }
        /// 
        /// 添加培训计划
        /// 
        /// 
        public static void AddTestPlan(Model.Training_TestPlan testPlan)
        {
            Model.Training_TestPlan newTestPlan = new Model.Training_TestPlan
            {
                TestPlanId = testPlan.TestPlanId,
                ProjectId = testPlan.ProjectId,
                PlanCode = testPlan.PlanCode,
                PlanName = testPlan.PlanName,
                PlanManId = testPlan.PlanManId,
                PlanDate = testPlan.PlanDate,
                TestStartTime = testPlan.TestStartTime,
                TestEndTime = testPlan.TestEndTime,
                Duration = testPlan.Duration,
                SValue = testPlan.SValue,
                MValue = testPlan.MValue,
                JValue = testPlan.JValue,
                TotalScore = testPlan.TotalScore,
                QuestionCount = testPlan.QuestionCount,
                TestPalce = testPlan.TestPalce,
                UnitIds = testPlan.UnitIds,
                UnitNames = testPlan.UnitNames,
                DepartIds = testPlan.DepartIds,
                DepartNames = testPlan.DepartNames,
                WorkPostIds = testPlan.WorkPostIds,
                WorkPostNames = testPlan.WorkPostNames,
                PlanId= testPlan.PlanId,
                States = testPlan.States
            };
            db.Training_TestPlan.InsertOnSubmit(newTestPlan);
            db.SubmitChanges();
        }
        /// 
        /// 修改培训计划
        /// 
        /// 
        public static void UpdateTestPlan(Model.Training_TestPlan TestPlan)
        {
            Model.Training_TestPlan newTestPlan = db.Training_TestPlan.FirstOrDefault(e => e.TestPlanId == TestPlan.TestPlanId);
            if (newTestPlan != null)
            {
                newTestPlan.PlanCode = TestPlan.PlanCode;
                newTestPlan.PlanName = TestPlan.PlanName;
                newTestPlan.PlanManId = TestPlan.PlanManId;
                newTestPlan.PlanDate = TestPlan.PlanDate;
                newTestPlan.TestStartTime = TestPlan.TestStartTime;
                newTestPlan.TestEndTime = TestPlan.TestEndTime;
                newTestPlan.Duration = TestPlan.Duration;
                newTestPlan.TotalScore = TestPlan.TotalScore;
                newTestPlan.QuestionCount = TestPlan.QuestionCount;
                newTestPlan.TestPalce = TestPlan.TestPalce;
                newTestPlan.UnitIds = TestPlan.UnitIds;
                newTestPlan.UnitNames = TestPlan.UnitNames;
                newTestPlan.DepartIds = TestPlan.DepartIds;
                newTestPlan.DepartNames = TestPlan.DepartNames;
                newTestPlan.WorkPostIds = TestPlan.WorkPostIds;
                newTestPlan.WorkPostNames = TestPlan.WorkPostNames;
                newTestPlan.States = TestPlan.States;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除培训计划信息
        /// 
        /// 
        public static void DeleteTestPlanById(string TestPlanId)
        {
            var getTestPlan = GetTestPlanById(TestPlanId);
            if (getTestPlan != null)
            {
                var testPlanTrainings = from x in Funs.DB.Training_TestPlanTraining where x.TestPlanId == TestPlanId select x;
                if (testPlanTrainings.Count() > 0)
                {
                    Funs.DB.Training_TestPlanTraining.DeleteAllOnSubmit(testPlanTrainings);
                    Funs.DB.SubmitChanges();
                }
                if (getTestPlan.States == "3") //状态考试结束
                {
                    var updateTrainingPlan = TrainingPlanService.GetPlanById(getTestPlan.PlanId);
                    if (updateTrainingPlan != null)
                    {
                        updateTrainingPlan.States = "2";
                        TrainingPlanService.UpdatePlan(updateTrainingPlan);
                        var getTrainingTasks = from x in Funs.DB.Training_Task
                                               where x.PlanId == updateTrainingPlan.PlanId 
                                               select x;
                        foreach (var item in getTrainingTasks)
                        {
                            item.States = "1";
                            TrainingTaskService.UpdateTask(item);
                        }
                        ////删除归档的培训记录
                        var trainRecord = Funs.DB.EduTrain_TrainRecord.FirstOrDefault(x => x.PlanId == getTestPlan.PlanId);
                        if (trainRecord != null)
                        {
                            EduTrain_TrainRecordService.DeleteTrainingByTrainingId(trainRecord.TrainingId);
                        }
                    }
                }
                Funs.DB.Training_TestPlan.DeleteOnSubmit(getTestPlan);
                Funs.DB.SubmitChanges();              
            }
        }
        /// 
        /// 获取培训计划列
        /// 
        /// 
        public static List GetTestPlanList()
        {
            return (from x in db.Training_TestPlan orderby x.PlanCode select x).ToList();
        }
        /// 
        /// 获取培训计划列
        /// 
        /// 
        public static Model.Training_TestPlan GetTestPlanByPlanId(string planId)
        {
            return Funs.DB.Training_TestPlan.FirstOrDefault(x => x.PlanId == planId || x.TestPlanId ==planId);
        }
    }
}