using FineUIPro;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace BLL
{
///
/// 包装管理服务类
///
public static class HJGLPackagingmanageService
{
#region Fields
///
/// 未到场
///
public static int state_0 = 0;
///
/// 已发货
///
public static int state_1 = 1;
///
/// 已到场
///
public static int state_2 = 2;
public static Dictionary TypeIntMap = new Dictionary
{
{ "预制组件" ,(int)TypeInt.预制组件},
{ "预制散件" ,(int)TypeInt.预制散件},
{ "其他材料" ,(int)TypeInt.其他材料},
};
public static Dictionary CategoryIntMap = new Dictionary
{
{ "打捆" ,(int)CategoryInt.打捆},
{ "装箱" ,(int)CategoryInt.装箱},
{ "散装" ,(int)CategoryInt.散装},
};
#endregion Fields
#region Enums
public enum TypeInt : int
{
预制组件 = 10,
预制散件 = 20,
其他材料 = 30,
}
public enum CategoryInt : int
{
打捆 = 10,
装箱 = 20,
散装 = 30,
}
#endregion Enums
#region Methods
///
/// 新增实体
///
///
public static void AddHJGL_PackagingManage(Model.HJGL_PackagingManage newtable)
{
Model.HJGL_PackagingManage table = new Model.HJGL_PackagingManage
{
PackagingManageId = newtable.PackagingManageId,
PackagingCode = newtable.PackagingCode,
ProjectId = newtable.ProjectId,
PipelineComponentId = newtable.PipelineComponentId,
StackingPosition = newtable.StackingPosition,
State = newtable.State,
ContactName = newtable.ContactName,
ContactPhone = newtable.ContactPhone,
Remark = newtable.Remark,
ReceiveDate = newtable.ReceiveDate,
ReceiveMan = newtable.ReceiveMan,
TrainNumber = newtable.TrainNumber,
TrainNumberId = newtable.TrainNumberId,
TypeInt = newtable.TypeInt,
CategoryInt = newtable.CategoryInt,
CompileMan = newtable.CompileMan,
CompileDate = newtable.CompileDate
};
var db1 = Funs.DB;
db1.HJGL_PackagingManage.InsertOnSubmit(table);
db1.SubmitChanges();
}
public static void DeleteHJGL_PackagingManageById(string PackagingManageId)
{
var db1 = Funs.DB;
Model.HJGL_PackagingManage table = db1.HJGL_PackagingManage.FirstOrDefault(x => x.PackagingManageId == PackagingManageId);
if (table != null)
{
db1.HJGL_PackagingManage.DeleteOnSubmit(table);
db1.SubmitChanges();
}
}
public static Model.HJGL_PackagingManage GetHJGL_PackagingManageById(string PackagingManageId)
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
return db.HJGL_PackagingManage.FirstOrDefault(x => x.PackagingManageId == PackagingManageId);
}
}
public static string GetMinPlanStartDate(string PackagingManageId)
{
string PlanStartDate = "";
var tb = GetPackagingDetailById(PackagingManageId);
if (tb == null || tb.Count == 0)
{
return PlanStartDate;
}
PlanStartDate = tb.OrderBy(x => x.PlanStartDate).FirstOrDefault()?.PlanStartDate.ToString();
return PlanStartDate;
}
public static string GetNewPackagingCode(string projectid)
{
string code = ProjectService.GetProjectCodeByProjectId(projectid) + "-" + string.Format("{0:yyyyMMdd}", DateTime.Now) + "-";
var q = GetPackagingManageList(projectid, code);
if (q.Total > 0)
{
code = code + (q.Total + 1).ToString().PadLeft(3, '0');
}
else
{
code = code + "001";
}
return code;
}
///
/// 根据项目号获取包装编号历史记录
///
///
///
public static List GetPackagingCode(string projectid)
{
Model.SGGLDB db = Funs.DB;
var q = (from x in db.HJGL_PackagingManage
where x.ProjectId.Contains(projectid)
select x.PackagingCode).Distinct().ToList();
return q;
}
///
/// 根据主键获取装箱明细
///
///
///
public static List GetPackagingDetailById(string PackagingManageId)
{
var model = GetHJGL_PackagingManageById(PackagingManageId);
var result = new List();
if (model.TypeInt == (int)HJGLPackagingmanageService.TypeInt.预制组件)
{
if (string.IsNullOrEmpty(model.PipelineComponentId))
return new List();
var PipelineComponentIds = model?.PipelineComponentId.Split(',');
var query = from com in Funs.DB.HJGL_Pipeline_Component
join pipe in Funs.DB.HJGL_Pipeline on com.PipelineId equals pipe.PipelineId into pipeGroup
from pipe in pipeGroup.DefaultIfEmpty()
join unitwork in Funs.DB.WBS_UnitWork on pipe.UnitWorkId equals unitwork.UnitWorkId into unitworkGroup
from unitwork in unitworkGroup.DefaultIfEmpty()
where PipelineComponentIds.Contains(com.PipelineComponentId)
orderby com.PipelineComponentCode
select new Model.PackagingManagePrintOutput
{
PipelineComponentId = com.PipelineComponentId,
PipelineComponentCode = com.PipelineComponentCode,
PlanStartDate = pipe != null && pipe.PlanStartDate != null ? pipe.PlanStartDate : DateTime.Now,
UnitWorkName = unitwork != null ? unitwork.UnitWorkName : "",
num = "1",
CU = "个",
FlowingSection = pipe != null ? pipe.FlowingSection : ""
};
result = query.ToList();
}
else
{
var query = HJGLPackagingmanagedetailService.GetPackagingData(PackagingManageId).ToList();
var detailList = from x in query
select new Model.PackagingManagePrintOutput
{
PipelineComponentId = x.Id,
PipelineComponentCode = x.MaterialCode,
num = x.Number.ToString(),
CU = x.MaterialUnit,
UnitWorkName = x.UnitWorkId == null ? "" : UnitWorkService.GetNameById(x.UnitWorkId),
FlowingSection = model.StackingPosition,
};
result = detailList.ToList();
}
return result;
}
public static List GetPackagingManage(string trainNumberId)
{
Model.SGGLDB db = Funs.DB;
var q = (from x in db.HJGL_PackagingManage
where x.TrainNumberId == trainNumberId
select x).Distinct().ToList();
return q;
}
public static (List Data, int Total) GetPackagingManageList(string projectId, string PackagingCode, int pageIndex = 0, int pageSize = 20)
{
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
{
var baseQuery = (from x in db.HJGL_PackagingManage
join n in db.Base_Project on x.ProjectId equals n.ProjectId
join m in db.Person_Persons on x.ReceiveMan equals m.PersonId into tt
from t in tt.DefaultIfEmpty()
join train in db.HJGL_TrainNumberManage on x.TrainNumberId equals train.Id into trains
from train in trains.DefaultIfEmpty()
where x.ProjectId == projectId
&& (string.IsNullOrEmpty(PackagingCode) || x.PackagingCode.Contains(PackagingCode))
select new PackagingManageItem
{
PackagingManageId = x.PackagingManageId,
PackagingCode = x.PackagingCode,
ProjectName = n.ProjectName,
ContactName = train.ContactName,
ContactPhone = train.ContactPhone,
DriverName = train.DriverName,
DriverPhone = train.DriverPhone,
LicensePlateNumber = train.LicensePlateNumber,
StackingPosition = x.StackingPosition,
State = x.State,
TypeInt = x.TypeInt,
CategoryInt = x.CategoryInt,
TypeString = GetTypeString(x.TypeInt),
CategoryString = GetCategoryString(x.CategoryInt),
ReceiveMan = train.ContactName,//t.PersonName,
ReceiveDate = x.ReceiveDate.HasValue ? string.Format("{0:g}", x.ReceiveDate) : "",
PlanStartDate = GetMinPlanStartDate(x.PackagingManageId),
TrainNumberOld = x.TrainNumber,
TrainNumber = train.TrainNumber,
Code = x.PackagingCode.Substring(0, x.PackagingCode.LastIndexOf("-")).Substring(x.PackagingCode.Substring(0, x.PackagingCode.LastIndexOf("-")).LastIndexOf("-") + 1),
}).Distinct();
var pagedData = baseQuery
.OrderByDescending(x => x.Code)
.Skip((pageIndex) * pageSize)
.Take(pageSize)
.ToList();
// 获取总记录数(延迟计数优化)
var totalCount = baseQuery.Count();
return (pagedData, totalCount);
}
}
public static ListItem[] GetState()
{
ListItem[] list = new ListItem[3];
list[0] = new ListItem("预出库", state_0.ToString());
list[1] = new ListItem("已出库", state_1.ToString());
list[2] = new ListItem("已到场", state_2.ToString());
return list;
}
public static string GetTypeString(int? TypeInt)
{
return TypeIntMap.FirstOrDefault(c => c.Value == TypeInt).Key;
}
public static string GetCategoryString(int? CategoryInt)
{
return CategoryIntMap.FirstOrDefault(c => c.Value == CategoryInt).Key;
}
///
/// 管线下拉框
///
/// 下拉框名字
/// 是否显示请选择
public static void InitPipelineDownList(FineUIPro.DropDownList dropName, string projectid, bool isShowPlease)
{
dropName.DataValueField = "string";
dropName.DataTextField = "string";
dropName.DataSource = GetPackagingCode(projectid);
dropName.DataBind();
if (isShowPlease)
{
Funs.FineUIPleaseSelect(dropName);
}
}
///
/// 形成出库单
///
///
public static void PutOutOrder(string PackagingManageId)
{
var model = GetHJGL_PackagingManageById(PackagingManageId);
if (model.State == state_0)
{
model.State = state_1;
UpdateHJGL_PackagingManage(model);
}
}
public static void UpdateHJGL_PackagingManage(Model.HJGL_PackagingManage newtable)
{
var db1 = Funs.DB;
Model.HJGL_PackagingManage table = db1.HJGL_PackagingManage.FirstOrDefault(x => x.PackagingManageId == newtable.PackagingManageId);
if (table != null)
{
table.PackagingManageId = newtable.PackagingManageId;
table.PackagingCode = newtable.PackagingCode;
table.ProjectId = newtable.ProjectId;
table.PipelineComponentId = newtable.PipelineComponentId;
table.StackingPosition = newtable.StackingPosition;
table.State = newtable.State;
table.ContactName = newtable.ContactName;
table.ContactPhone = newtable.ContactPhone;
table.Remark = newtable.Remark;
table.ReceiveMan = newtable.ReceiveMan;
table.ReceiveDate = newtable.ReceiveDate;
table.TrainNumber = newtable.TrainNumber;
table.TrainNumberId = newtable.TrainNumberId;
table.TypeInt = newtable.TypeInt;
table.CategoryInt = newtable.CategoryInt;
db1.SubmitChanges();
}
}
#endregion Methods
#region Classes
public class PackagingManageItem
{
#region Properties
public string Code { get; set; }
public string ContactName { get; set; }
public string ContactPhone { get; set; }
public string DriverName { get; set; }
public string DriverPhone { get; set; }
public string LicensePlateNumber { get; set; }
public string PackagingCode { get; set; }
public string PackagingManageId { get; set; }
public string PlanStartDate { get; set; }
public string ProjectId { get; set; }
public string ProjectName { get; set; }
public string ReceiveDate { get; set; }
public string ReceiveMan { get; set; }
public string StackingPosition { get; set; }
public int? State { get; set; }
public string TrainNumber { get; set; }
public string TrainNumberOld { get; set; }
public int? TypeInt { get; set; }
public string TypeString { get; set; }
public int? CategoryInt { get; set; }
public string CategoryString { get; set; }
#endregion Properties
}
#endregion Classes
}
}