using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
    public class CostControlService
    {
        /// 
        /// 根据费控项Id获取费控项信息
        /// 
        /// 费控项Id
        /// 
        public static Model.WBS_CostControl GetCostControlByCostControlId(string costControlId)
        {
            return Funs.DB.WBS_CostControl.FirstOrDefault(e => e.CostControlId.ToString() == costControlId);
        }
        /// 
        /// 根据费控项编号集合获取费控项信息
        /// 
        /// 费控项编号集合
        /// 
        public static List GetCostControlsByCostControlCodes(List costControlCodes, string wbsSetId)
        {
            return (from x in Funs.DB.WBS_CostControl where costControlCodes.Contains(x.CostControlCode) && x.WbsSetId == wbsSetId select x).ToList();
        }
        /// 
        /// 根据费控项编号集合获取费控项信息
        /// 
        /// 费控项编号集合
        /// 
        public static List GetCostControlsByWbsSetId(string wbsSetId)
        {
            return (from x in Funs.DB.WBS_CostControl where x.WbsSetId == wbsSetId select x).ToList();
        }
        /// 
        /// 增加费控项
        /// 
        /// 费控项
        public static void AddCostControl(Model.WBS_CostControl costControl)
        {
            Model.SGGLDB db = Funs.DB;
            Model.WBS_CostControl newUP = new Model.WBS_CostControl();
            newUP.CostControlId = SQLHelper.GetNewID();
            newUP.ProjectId = costControl.ProjectId;
            newUP.WbsSetId = costControl.WbsSetId;
            newUP.CostControlCode = costControl.CostControlCode;
            newUP.CostControlName = costControl.CostControlName;
            newUP.Unit = costControl.Unit;
            newUP.TotalNum = costControl.TotalNum;
            newUP.RealPrice = costControl.RealPrice;
            db.WBS_CostControl.InsertOnSubmit(newUP);
            db.SubmitChanges();
        }
        /// 
        /// 修改费控项信息
        /// 
        /// 费控项信息
        public static void UpdateCostControl(Model.WBS_CostControl costControl)
        {
            Model.SGGLDB db = Funs.DB;
            Model.WBS_CostControl newUP = db.WBS_CostControl.First(e => e.CostControlId == costControl.CostControlId);
            newUP.CostControlCode = costControl.CostControlCode;
            newUP.CostControlName = costControl.CostControlName;
            newUP.Unit = costControl.Unit;
            newUP.TotalNum = costControl.TotalNum;
            newUP.RealPrice = costControl.RealPrice;
            newUP.PlanPrice = costControl.PlanPrice;
            db.SubmitChanges();
        }
        /// 
        /// 根据Id删除费控项信息
        /// 
        /// 
        public static void DeleteCostControl(string costControlId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.WBS_CostControl CnP = db.WBS_CostControl.First(e => e.CostControlId.ToString() == costControlId);
            db.WBS_CostControl.DeleteOnSubmit(CnP);
            db.SubmitChanges();
        }
        /// 
        /// 根据Id删除费控项信息
        /// 
        /// 
        public static void DeleteCostControlByWbsSetId(string wbsSetId)
        {
            Model.SGGLDB db = Funs.DB;
            var costControls = from x in db.WBS_CostControl where x.WbsSetId == wbsSetId select x;
            if (costControls.Count() > 0)
            {
                foreach (var costControl in costControls)
                {
                    BLL.CostControlDetailHistoryService.DeleteCostControlDetailHistoryByCostControlId(costControl.CostControlId);
                    BLL.CostControlDetailService.DeleteCostControlDetailByCostControlId(costControl.CostControlId);
                }
                db.WBS_CostControl.DeleteAllOnSubmit(costControls);
                db.SubmitChanges();
            }
        }
    }
}