using System;
using System.Collections.Generic;
using System.Linq;
namespace BLL
{
    public class ManPowerPlanService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 根据主键获取施工人力计划情况
        /// 
        /// 
        /// 
        public static Model.JDGL_SGManPower GetSGManPowerById(string Id)
        {
            return Funs.DB.JDGL_SGManPower.FirstOrDefault(x => x.Id == Id);
        }
        /// 
        /// 添加施工人力计划
        /// 
        /// 
        public static void AddSGManPower(Model.JDGL_SGManPower SGManPower)
        {
            Model.SGGLDB db = Funs.DB;
            Model.JDGL_SGManPower newSGManPower = new Model.JDGL_SGManPower
            {
                Id = SGManPower.Id,
                ProjectId = SGManPower.ProjectId,
                UnitId = SGManPower.UnitId,
                UnitWorkId = SGManPower.UnitWorkId,
                WorkPostId = SGManPower.WorkPostId,
                Version = SGManPower.Version,
                Quantity = SGManPower.Quantity,
                PlanDate = SGManPower.PlanDate,
                CompileMan = SGManPower.CompileMan,
                CompileTime = SGManPower.CompileTime,
                Remarks = SGManPower.Remarks,
            };
            db.JDGL_SGManPower.InsertOnSubmit(newSGManPower);
            db.SubmitChanges();
        }
        /// 
        /// 修改施工人力计划
        /// 
        /// 
        public static void UpdateSGManPower(Model.JDGL_SGManPower SGManPower)
        {
            Model.SGGLDB db = Funs.DB;
            Model.JDGL_SGManPower newSGManPower =
                db.JDGL_SGManPower.FirstOrDefault(x => x.Id == SGManPower.Id);
            if (newSGManPower != null)
            {
                newSGManPower.UnitId = SGManPower.UnitId;
                newSGManPower.UnitWorkId = SGManPower.UnitWorkId;
                newSGManPower.WorkPostId = SGManPower.WorkPostId;
                newSGManPower.Version = SGManPower.Version;
                newSGManPower.Quantity = SGManPower.Quantity;
                newSGManPower.PlanDate = SGManPower.PlanDate;
                newSGManPower.CompileMan = SGManPower.CompileMan;
                newSGManPower.CompileTime = SGManPower.CompileTime;
                newSGManPower.Remarks = SGManPower.Remarks;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除施工人力计划
        /// 
        /// 
        public static void DeleteSGManPowerById(string Id)
        {
            Model.SGGLDB db = Funs.DB;
            var q = (from x in db.JDGL_SGManPower where x.Id == Id select x).FirstOrDefault();
            if (q != null)
            {
                db.JDGL_SGManPower.DeleteOnSubmit(q);
                db.SubmitChanges();
            }
        }
        
        
        /// 
        /// 根据项目获取工力计划版本
        /// 
        /// 
        public static List getGroupVersion(string projectId)
        {
            var versions = Funs.DB.JDGL_SGManPower
                .Where(x => x.ProjectId == projectId)
                .GroupBy(x => x.Version)
                .Select(g => g.Key)
                .OrderByDescending(v => v)
                .ToList();
            
            return versions; 
        }
    }
}