using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FineUIPro;
namespace BLL
{
    /// 
    /// 开车分包计划
    /// 
    public static class DriverSubPlanService
    {
        #region 获取列表
        /// 
        /// 记录数
        /// 
        public static int Count
        {
            get;
            set;
        }
        public static IQueryable GetDriverSub_DriverSubPlanByModle(Model.DriverSub_DriverSubPlan table)
        {
            var q = from x in Funs.DB.DriverSub_DriverSubPlan
                    where
                              (string.IsNullOrEmpty(table.DriverSubPlanId) || x.DriverSubPlanId.Contains(table.DriverSubPlanId)) &&
                              (string.IsNullOrEmpty(table.ProjectId) || x.ProjectId.Contains(table.ProjectId)) &&
                              (string.IsNullOrEmpty(table.Code) || x.Code.Contains(table.Code)) &&
                              (string.IsNullOrEmpty(table.SubUnitId) || x.SubUnitId.Contains(table.SubUnitId)) &&
                              (string.IsNullOrEmpty(table.Introductions) || x.Introductions.Contains(table.Introductions)) &&
                              (string.IsNullOrEmpty(table.Achievement) || x.Achievement.Contains(table.Achievement)) &&
                              (string.IsNullOrEmpty(table.Cooperation) || x.Cooperation.Contains(table.Cooperation)) &&
                              (string.IsNullOrEmpty(table.InstallationIds) || x.InstallationIds.Contains(table.InstallationIds)) &&
                              (string.IsNullOrEmpty(table.InstallationNames) || x.InstallationNames.Contains(table.InstallationNames)) &&
                              (string.IsNullOrEmpty(table.AttachUrl) || x.AttachUrl.Contains(table.AttachUrl)) &&
                              (string.IsNullOrEmpty(table.Remark) || x.Remark.Contains(table.Remark)) &&
                              (string.IsNullOrEmpty(table.SubcontractingTypes) || x.SubcontractingTypes.Contains(table.SubcontractingTypes)) 
                    select x
                  ;
            return q;
        }
        /// 
        /// 获取分页列表
        /// 
        /// 
        /// 
        /// 
        public static IQueryable GetListData(Model.DriverSub_DriverSubPlan table, Grid grid1)
        {
            var q = GetDriverSub_DriverSubPlanByModle(table);
            Count = q.Count() ;
            if (Count == 0)
            {
                return null;
            }
            q = q.Skip(grid1.PageSize * grid1.PageIndex).Take(grid1.PageSize);
            // q = SortConditionHelper.SortingAndPaging(q, Grid1.SortField, Grid1.SortDirection, Grid1.PageIndex, Grid1.PageSize);
            return from x in q
                   select new
                   {
                       x.DriverSubPlanId,
                       x.ProjectId,
                       x.Code,
                       x.SubUnitId,
                       x.Introductions,
                       x.Achievement,
                       x.Cooperation,
                       x.InstallationIds,
                       x.InstallationNames,
                       x.IsInvited,
                       x.AttachUrl,
                       x.Remark,
                       x.SubcontractingTypes,
                       StateName = GetSubStateName(x.State)
                   };
        }
        #endregion
        /// 
        /// 根据主键获取开车分包计划信息
        /// 
        /// 
        /// 
        public static Model.DriverSub_DriverSubPlan GetDriverSubPlanById(string DriverSubPlanId)
        {
            return Funs.DB.DriverSub_DriverSubPlan.FirstOrDefault(e => e.DriverSubPlanId == DriverSubPlanId);
        }
        public static List GetDriverSubPlanByProjectid(string projectId)
        {
            return Funs.DB.DriverSub_DriverSubPlan.Where(e => e.ProjectId == projectId).ToList();
        }
        /// 
        /// 添加开车分包计划信息
        /// 
        /// 
        public static void AddDriverSubPlan(Model.DriverSub_DriverSubPlan DriverSubPlan)
        {
            Model.DriverSub_DriverSubPlan newDriverSubPlan = new Model.DriverSub_DriverSubPlan();
            newDriverSubPlan.DriverSubPlanId = DriverSubPlan.DriverSubPlanId;
            newDriverSubPlan.ProjectId = DriverSubPlan.ProjectId;
            newDriverSubPlan.Code = DriverSubPlan.Code;
            newDriverSubPlan.SubUnitId = DriverSubPlan.SubUnitId;
            newDriverSubPlan.Introductions = DriverSubPlan.Introductions;
            newDriverSubPlan.Achievement = DriverSubPlan.Achievement;
            newDriverSubPlan.Cooperation = DriverSubPlan.Cooperation;
            newDriverSubPlan.InstallationIds = DriverSubPlan.InstallationIds;
            newDriverSubPlan.InstallationNames = DriverSubPlan.InstallationNames;
            newDriverSubPlan.IsInvited = DriverSubPlan.IsInvited;
            newDriverSubPlan.AttachUrl = DriverSubPlan.AttachUrl;
            newDriverSubPlan.Remark = DriverSubPlan.Remark;
            newDriverSubPlan.SubcontractingTypes= DriverSubPlan.SubcontractingTypes;
            newDriverSubPlan.State = DriverSubPlan.State;
            Funs.DB.DriverSub_DriverSubPlan.InsertOnSubmit(newDriverSubPlan);
            Funs.DB.SubmitChanges();
        }
        /// 
        /// 修改开车分包计划
        /// 
        /// 
        public static void UpdateDriverSubPlan(Model.DriverSub_DriverSubPlan DriverSubPlan)
        {
            Model.DriverSub_DriverSubPlan newDriverSubPlan = Funs.DB.DriverSub_DriverSubPlan.FirstOrDefault(e => e.DriverSubPlanId == DriverSubPlan.DriverSubPlanId);
            if (newDriverSubPlan != null)
            {
                newDriverSubPlan.Code = DriverSubPlan.Code;
                newDriverSubPlan.SubUnitId = DriverSubPlan.SubUnitId;
                newDriverSubPlan.Introductions = DriverSubPlan.Introductions;
                newDriverSubPlan.Achievement = DriverSubPlan.Achievement;
                newDriverSubPlan.Cooperation = DriverSubPlan.Cooperation;
                newDriverSubPlan.InstallationIds = DriverSubPlan.InstallationIds;
                newDriverSubPlan.InstallationNames = DriverSubPlan.InstallationNames;
                newDriverSubPlan.IsInvited = DriverSubPlan.IsInvited;
                newDriverSubPlan.AttachUrl = DriverSubPlan.AttachUrl;
                newDriverSubPlan.Remark = DriverSubPlan.Remark;
                newDriverSubPlan.SubcontractingTypes = DriverSubPlan.SubcontractingTypes;
                newDriverSubPlan.State = DriverSubPlan.State;
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除开车分包计划
        /// 
        /// 
        public static void DeleteDriverSubPlanById(string DriverSubPlanId)
        {
            Model.DriverSub_DriverSubPlan DriverSubPlan = Funs.DB.DriverSub_DriverSubPlan.FirstOrDefault(e => e.DriverSubPlanId == DriverSubPlanId);
            if (DriverSubPlan != null)
            {
                if (!string.IsNullOrEmpty(DriverSubPlan.AttachUrl))
                {
                    BLL.UploadAttachmentService.DeleteFile(Funs.RootPath, DriverSubPlan.AttachUrl);//删除附件
                }
                Funs.DB.DriverSub_DriverSubPlan.DeleteOnSubmit(DriverSubPlan);
                Funs.DB.SubmitChanges();
            }
        }
        public static void InitSubPlanDropDownList(FineUIPro.DropDownList dropName,string projectid, bool isShowPlease)
        {
            dropName.DataValueField = "DriverSubPlanId";
            dropName.DataTextField = "Code";
            dropName.DataSource = GetDriverSubPlanByProjectid(projectid);
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }
        public static  string GetSubcontractingTypeNames(object str)
        {
            string strName = "";
            if (str != null)
            {
                string[] strArr = str.ToString().Split(',');
                foreach (string s in strArr)
                {
                    foreach (System.Web.UI.WebControls.ListItem item in DropListService.drpDriverSubNameList())
                    {
                        if (item.Value == s)
                        {
                            strName += item.Text + ",";
                        }
                    }
                }
            }
            return strName.TrimEnd(',');
        }
        public static string GetSubStateName(int? str)
        {
            string strName = "";
            if (str != null)
            {
                string[] strArr = str.ToString().Split(',');
                foreach (string s in strArr)
                {
                    foreach (System.Web.UI.WebControls.ListItem item in DropListService.drpDriverSubPlanStateList())
                    {
                        if (item.Value == s)
                        {
                            strName += item.Text + ",";
                        }
                    }
                }
            }
            return strName.TrimEnd(',');
        }
    }
}