using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BLL
{
    /// 
    /// HSE费用登记(新)
    /// 
    public class HseExpenseService
    {
        public static bool Insert(Model.CostGoods_HseExpense model)
        {
            try
            {
                Funs.DB.CostGoods_HseExpense.InsertOnSubmit(model);
                Funs.DB.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                ErrLogInfo.WriteLog($"插入数据失败,原因:{ex.Message}");
                return false;
            }
        }
        public static bool Update(Model.CostGoods_HseExpense model)
        {
            try
            {
                var result = Funs.DB.CostGoods_HseExpense.FirstOrDefault(a => a.Id == model.Id);
                if (result != null)
                {
                    result.UnitId = model.UnitId;
                    result.PayDate = model.PayDate;
                    result.PayMonth = model.PayMonth;
                    result.SMonthType1 = model.SMonthType1;
                    result.SMonthType2 = model.SMonthType2;
                    result.SMonthType3 = model.SMonthType3;
                    result.SMonthType4 = model.SMonthType4;
                    result.SMonthType5 = model.SMonthType5;
                    result.SMonthType6 = model.SMonthType6;
                    result.SMonthType7 = model.SMonthType7;
                    result.SMonthType8 = model.SMonthType8;
                    result.SMonthType9 = model.SMonthType9;
                    result.SMonthType10 = model.SMonthType10;
                    Funs.DB.SubmitChanges();
                }
                return true;
            }
            catch (Exception ex)
            {
                ErrLogInfo.WriteLog($"更新表数据失败,原因:{ex.Message}");
                return false;
            }
        }
        public static bool Delete(List newId)
        {
            try
            {
                var result = Funs.DB.CostGoods_HseExpense.Where(a => newId.Contains(a.Id)).ToList();
                if (result.Count > 0)
                {
                    Funs.DB.CostGoods_HseExpense.DeleteAllOnSubmit(result);
                    Funs.DB.SubmitChanges();
                }
                return true;
            }
            catch (Exception ex)
            {
                ErrLogInfo.WriteLog($"删除数据失败,原因:{ex.Message}");
                return false;
            }
        }
        public static bool Delete(string newId)
        {
            try
            {
                var result = Funs.DB.CostGoods_HseExpense.Where(a => a.Id == newId).ToList();
                if (result.Count > 0)
                {
                    Funs.DB.CostGoods_HseExpense.DeleteAllOnSubmit(result);
                    Funs.DB.SubmitChanges();
                }
                return true;
            }
            catch (Exception ex)
            {
                ErrLogInfo.WriteLog($"删除数据失败,原因:{ex.Message}");
                return false;
            }
        }
        public static Model.CostGoods_HseExpense Detail(string newId)
        {
            var result = Funs.DB.CostGoods_HseExpense.FirstOrDefault(a => a.Id == newId);
            return result;
        }
        /// 
        /// 根据时间、项目获取五环安全费用投入登记信息
        /// 
        /// 
        /// 
        /// 
        /// 
        public static List GetPayRegistrationByPayDate(DateTime startTime, DateTime endTime, string projectId, string unitId)
        {
            return (from x in Funs.DB.CostGoods_HseExpense where x.PayDate >= startTime && x.PayDate <= endTime && x.ProjectId == projectId && x.UnitId == unitId select x).ToList();
        }
        /// 
        ///  根据时间、项目获取施工分包商安全费用投入登记信息
        /// 
        /// 
        /// 
        /// 
        /// 
        public static List GetConPayRegistrationByPayDate(DateTime startTime, DateTime endTime, string projectId)
        {
            return (from x in Funs.DB.CostGoods_HseExpense
                    join y in Funs.DB.Base_Unit on x.UnitId equals y.UnitId
                    where x.PayDate >= startTime && x.PayDate <= endTime && x.ProjectId == projectId
                    && y.UnitTypeId == BLL.Const.UnitId_Con
                    select x).ToList();
        }
    }
}