using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
namespace BLL
{
    /// 
    /// 开车保运管理
    /// 
    public static class DriverRunService
    {
        /// 
        /// 根据主键获取开车保运管理
        /// 
        /// 
        /// 
        public static Model.DriverRun_DriverRun GetDriverRunById(string driverRunId)
        {
            return Funs.DB.DriverRun_DriverRun.FirstOrDefault(e => e.DriverRunId == driverRunId);
        }
        /// 
        /// 添加开车保运管理
        /// 
        /// 
        public static void AddDriverRun(Model.DriverRun_DriverRun driverRun)
        {
            Model.DriverRun_DriverRun newDriverRun = new Model.DriverRun_DriverRun();
            newDriverRun.DriverRunId = driverRun.DriverRunId;
            newDriverRun.ProjectId = driverRun.ProjectId;
            newDriverRun.Code = driverRun.Code;
            newDriverRun.UnitId = driverRun.UnitId;
            newDriverRun.Implement = driverRun.Implement;
            newDriverRun.Descriptions = driverRun.Descriptions;
            newDriverRun.InstallationId = driverRun.InstallationId;
            newDriverRun.InstallationNames = driverRun.InstallationNames;
            newDriverRun.Objective = driverRun.Objective;
            newDriverRun.NeedCompletedDate = driverRun.NeedCompletedDate;
            newDriverRun.CompileMan = driverRun.CompileMan;
            newDriverRun.CompileDate = driverRun.CompileDate;
            newDriverRun.AttachUrl = driverRun.AttachUrl;
            newDriverRun.Remark = driverRun.Remark;
            newDriverRun.Status = driverRun.Status;
            newDriverRun.Priority = driverRun.Priority;
            newDriverRun.State = driverRun.State;
            Funs.DB.DriverRun_DriverRun.InsertOnSubmit(newDriverRun);
            Funs.DB.SubmitChanges();
        }
        public static void AddDriverRunForApi(Model.DriverRun_DriverRun driverRun)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                Model.DriverRun_DriverRun newDriverRun = new Model.DriverRun_DriverRun();
                newDriverRun.DriverRunId = driverRun.DriverRunId;
                newDriverRun.ProjectId = driverRun.ProjectId;
                newDriverRun.Code = driverRun.Code;
                newDriverRun.UnitId = driverRun.UnitId;
                newDriverRun.Implement = driverRun.Implement;
                newDriverRun.Descriptions = driverRun.Descriptions;
                newDriverRun.InstallationId = driverRun.InstallationId;
                newDriverRun.InstallationNames = driverRun.InstallationNames;
                newDriverRun.Objective = driverRun.Objective;
                newDriverRun.NeedCompletedDate = driverRun.NeedCompletedDate;
                newDriverRun.CompileMan = driverRun.CompileMan;
                newDriverRun.CompileDate = driverRun.CompileDate;
                newDriverRun.AttachUrl = driverRun.AttachUrl;
                newDriverRun.Remark = driverRun.Remark;
                newDriverRun.Status = driverRun.Status;
                newDriverRun.Priority = driverRun.Priority;
                newDriverRun.State = driverRun.State;
                db.DriverRun_DriverRun.InsertOnSubmit(newDriverRun);
                db.SubmitChanges();
            }
        }
        /// 
        /// 修改开车保运管理
        /// 
        /// 
        public static void UpdateDriverRun(Model.DriverRun_DriverRun driverRun)
        {
            Model.DriverRun_DriverRun newDriverRun = Funs.DB.DriverRun_DriverRun.FirstOrDefault(e => e.DriverRunId == driverRun.DriverRunId);
            if (newDriverRun != null)
            {
                newDriverRun.ProjectId = driverRun.ProjectId;
                newDriverRun.Code = driverRun.Code;
                newDriverRun.UnitId = driverRun.UnitId;
                newDriverRun.Implement = driverRun.Implement;
                newDriverRun.Descriptions = driverRun.Descriptions;
                newDriverRun.InstallationId = driverRun.InstallationId;
                newDriverRun.InstallationNames = driverRun.InstallationNames;
                newDriverRun.Objective = driverRun.Objective;
                newDriverRun.NeedCompletedDate = driverRun.NeedCompletedDate;
                newDriverRun.RealCompletedDate = driverRun.RealCompletedDate;
                newDriverRun.AttachUrl = driverRun.AttachUrl;
                newDriverRun.Remark = driverRun.Remark;
                newDriverRun.State = driverRun.State;
                Funs.DB.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除开车保运管理
        /// 
        /// 
        public static void DeleteDriverRunById(string driverRunId)
        {
            Model.DriverRun_DriverRun newDriverRun = Funs.DB.DriverRun_DriverRun.FirstOrDefault(e => e.DriverRunId == driverRunId);
            if (newDriverRun != null)
            {
                if (!string.IsNullOrEmpty(newDriverRun.AttachUrl))
                {
                    UploadAttachmentService.DeleteFile(Funs.RootPath, newDriverRun.AttachUrl);
                }
                Funs.DB.DriverRun_DriverRun.DeleteOnSubmit(newDriverRun);
                Funs.DB.SubmitChanges();
            }
        }
        public static Model.DriverRun_DriverRun GetDriverRunForApi(string driverRunId)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                Model.DriverRun_DriverRun x = db.DriverRun_DriverRun.FirstOrDefault(e => e.DriverRunId == driverRunId);
                return x;
            }
        }
        public static void Init(FineUIPro.DropDownList dropName, string state, bool isShowPlease)
        {
            dropName.DataValueField = "Value";
            dropName.DataTextField = "Text";
            dropName.DataSource = GetDHandleTypeByState(state);
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }
        /// 
        /// 根据状态选择下一步办理类型
        /// 
        /// 
        /// 
        public static ListItem[] GetDHandleTypeByState(string state)
        {
            if (state == Const.DriverRun_Compile)  //无是否同意
            {
                ListItem[] lis = new ListItem[1];
                lis[0] = new ListItem("开车负责人审批", Const.DriverRun_Audit1);
                return lis;
            }
            else if (state == Const.DriverRun_Audit1)//有是否同意
            {
                ListItem[] lis = new ListItem[1];
                lis[0] = new ListItem("保运主任审批", Const.DriverRun_Audit2);//是 加载
                return lis;
            }
            else if (state == Const.DriverRun_Audit2)//无是否同意
            {
                ListItem[] lis = new ListItem[1];
                lis[0] = new ListItem("保运主任确认完成", Const.DriverRun_Audit3);
                return lis;
            }
            else if (state == Const.DriverRun_Audit3)//无是否同意
            {
                ListItem[] lis = new ListItem[1];
                lis[0] = new ListItem("开车负责人确认完成", Const.DriverRun_Audit4);
                return lis;
            }
            else if (state == Const.DriverRun_Audit4)//有是否同意
            {
                ListItem[] lis = new ListItem[1];
                lis[0] = new ListItem("流程闭环", Const.DriverRun_Complete);//是 加载
                return lis;
            }
            else
                return null;
        }
        public static List GetListDataForApi(string state, string projectId, int index, int page)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                IQueryable q = db.DriverRun_DriverRun;
                List ids = new List();
                if (!string.IsNullOrEmpty(projectId))
                {
                    q = q.Where(e => e.ProjectId == projectId);
                }
                if (!string.IsNullOrEmpty(state))
                {
                    q = q.Where(e => e.State == state);
                }
                var qq1 = from x in q
                          orderby x.Code descending
                          select new
                          {
                              x.DriverRunId,
                              x.ProjectId,
                              x.UnitId,
                              x.Code,
                              x.Descriptions,
                              x.InstallationId,
                              x.InstallationNames,
                              x.State,
                              x.Objective,
                              x.NeedCompletedDate,
                              x.AttachUrl,
                          };
                var list = qq1.Skip(index * page).Take(page).ToList();
                List listRes = new List();
                for (int i = 0; i < list.Count; i++)
                {
                    Model.DriverRun_DriverRun x = new Model.DriverRun_DriverRun();
                    x.DriverRunId = list[i].DriverRunId;
                    x.ProjectId = list[i].ProjectId;
                    x.Code = list[i].Code;
                    x.UnitId = list[i].UnitId + "$" + UnitService.GetUnitNameByUnitId(list[i].UnitId);
                    x.Descriptions = list[i].Descriptions;
                    x.State = list[i].State;
                    x.InstallationId = list[i].InstallationId;
                    x.InstallationNames = list[i].InstallationNames;
                    x.Objective = list[i].Objective;
                    x.NeedCompletedDate = list[i].NeedCompletedDate;
                    x.AttachUrl = list[i].AttachUrl;
                    listRes.Add(x);
                }
                return listRes;
            }
        }
        public static void UpdateDriverRunForApi(Model.DriverRun_DriverRun DriverRun)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                Model.DriverRun_DriverRun newDriverRun = db.DriverRun_DriverRun.First(e => e.DriverRunId == DriverRun.DriverRunId);
                if (!string.IsNullOrEmpty(DriverRun.Code))
                    newDriverRun.Code = DriverRun.Code;
                if (!string.IsNullOrEmpty(DriverRun.UnitId))
                    newDriverRun.UnitId = DriverRun.UnitId;
                if (!string.IsNullOrEmpty(DriverRun.Descriptions))
                    newDriverRun.Descriptions = DriverRun.Descriptions;
                if (!string.IsNullOrEmpty(DriverRun.State))
                    newDriverRun.State = DriverRun.State;
                if (DriverRun.NeedCompletedDate.HasValue)
                    newDriverRun.NeedCompletedDate = DriverRun.NeedCompletedDate;
                if (DriverRun.RealCompletedDate.HasValue)
                    newDriverRun.RealCompletedDate = DriverRun.RealCompletedDate;
                if (!string.IsNullOrEmpty(DriverRun.InstallationId))
                    newDriverRun.InstallationId = DriverRun.InstallationId;
                if (!string.IsNullOrEmpty(DriverRun.InstallationNames))
                    newDriverRun.InstallationNames = DriverRun.InstallationNames;
                if (!string.IsNullOrEmpty(DriverRun.Objective))
                    newDriverRun.Objective = DriverRun.Objective;
                db.SubmitChanges();
            }
        }
        public static int GetListCount(string projectId)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                IQueryable q = db.DriverRun_DriverRun;
                if (!string.IsNullOrEmpty(projectId))
                {
                    q = q.Where(e => e.ProjectId == projectId);
                }
                return q.Count();
            }
        }
    }
}