Files
SGGL_SHJ/SGGL/BLL/CLGL/TwAntiCorrosionTrustService.cs
T

460 lines
21 KiB
C#

using FineUIPro;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace BLL
{
/// <summary>
/// 防腐委托单服务。
/// </summary>
public static class TwAntiCorrosionTrustService
{
public static int Count { get; set; }
public static List<AntiCorrosionTrustOutput> GetListData(AntiCorrosionTrustOutput filter)
{
var query = from t in Funs.DB.Tw_AntiCorrosionTrust
join uw in Funs.DB.WBS_UnitWork on t.UnitWorkId equals uw.UnitWorkId into uwJoin
from uw in uwJoin.DefaultIfEmpty()
join om in Funs.DB.Tw_OutputMaster on t.OutputMasterId equals om.Id into omJoin
from om in omJoin.DefaultIfEmpty()
join p in Funs.DB.Person_Persons on t.CreateMan equals p.PersonId into pJoin
from p in pJoin.DefaultIfEmpty()
where (string.IsNullOrEmpty(filter.Id) || t.Id == filter.Id)
&& (string.IsNullOrEmpty(filter.ProjectId) || t.ProjectId == filter.ProjectId)
&& (string.IsNullOrEmpty(filter.UnitWorkId) || t.UnitWorkId == filter.UnitWorkId)
&& (string.IsNullOrEmpty(filter.TrustCode) || t.TrustCode.Contains(filter.TrustCode))
&& (string.IsNullOrEmpty(filter.OutputMasterCode) || om.CusBillCode.Contains(filter.OutputMasterCode))
orderby t.CreateDate descending, t.TrustCode descending
select new AntiCorrosionTrustOutput
{
Id = t.Id,
ProjectId = t.ProjectId,
UnitWorkId = t.UnitWorkId,
UnitWorkName = uw.UnitWorkName,
ConstructionPart = t.ConstructionPart,
ConstructionProfessional = t.ConstructionProfessional,
TrustCode = t.TrustCode,
OutputMasterId = t.OutputMasterId,
OutputMasterCode = om.CusBillCode,
DemandDate = t.DemandDate,
CompleteDate = t.CompleteDate,
CreateMan = t.CreateMan,
CreateManName = p.PersonName,
CreateDate = t.CreateDate,
Remark = t.Remark
};
return query.ToList();
}
public static IEnumerable<AntiCorrosionTrustOutput> GetListData(AntiCorrosionTrustOutput filter, Grid grid)
{
var list = GetListData(filter);
Count = list.Count;
return list.Skip(grid.PageIndex * grid.PageSize).Take(grid.PageSize).ToList();
}
public static AntiCorrosionTrustOutput GetById(string id)
{
if (string.IsNullOrEmpty(id))
{
return null;
}
return GetListData(new AntiCorrosionTrustOutput { Id = id })
.FirstOrDefault(x => x.Id == id);
}
public static bool IsExistTrustCode(string trustCode, string id, string projectId)
{
return Funs.DB.Tw_AntiCorrosionTrust.Any(x => x.TrustCode == trustCode
&& x.ProjectId == projectId
&& (string.IsNullOrEmpty(id) || x.Id != id));
}
public static string GenerateTrustCode(string unitWorkId)
{
var unitWork = UnitWorkService.GetUnitWorkByUnitWorkId(unitWorkId);
if (unitWork == null)
{
return string.Empty;
}
var prefix = "SXHJ-ECFF-" + unitWork.UnitWorkName + "-";
var maxSerial = Funs.DB.Tw_AntiCorrosionTrust
.Where(x => x.UnitWorkId == unitWorkId && x.TrustCode.StartsWith(prefix))
.Select(x => x.TrustCode)
.ToList()
.Select(x => GetTrustCodeSerial(x))
.DefaultIfEmpty(0)
.Max();
return prefix + (maxSerial + 1).ToString("000");
}
public static List<Tw_InOutMasterOutput> GetAvailableOutputMasters(string projectId, string unitWorkId, string currentTrustId)
{
var usedOutputIds = Funs.DB.Tw_AntiCorrosionTrust
.Where(x => x.ProjectId == projectId && (string.IsNullOrEmpty(currentTrustId) || x.Id != currentTrustId))
.Select(x => x.OutputMasterId)
.ToList();
var query = from om in Funs.DB.Tw_OutputMaster
join plan in Funs.DB.Tw_InOutPlanMaster on om.InOutPlanMasterId equals plan.Id into planJoin
from plan in planJoin.DefaultIfEmpty()
join createPerson in Funs.DB.Person_Persons on om.CreateMan equals createPerson.PersonId into createJoin
from createPerson in createJoin.DefaultIfEmpty()
join reqUnit in Funs.DB.Base_Unit on om.ReqUnitId equals reqUnit.UnitId into unitJoin
from reqUnit in unitJoin.DefaultIfEmpty()
where om.ProjectId == projectId
&& !usedOutputIds.Contains(om.Id)
&& (string.IsNullOrEmpty(unitWorkId) || (plan.WeldTaskId != null && plan.WeldTaskId.Contains(unitWorkId)))
orderby om.CreateDate descending, om.CusBillCode descending
select new Tw_InOutMasterOutput
{
Id = om.Id,
ProjectId = om.ProjectId,
CusBillCode = om.CusBillCode,
InOutPlanMasterId = om.InOutPlanMasterId,
WarehouseId = om.WarehouseId,
WarehouseCode = om.WarehouseCode,
WarehouseName = om.WarehouseCode,
Source = om.Source,
TypeInt = om.TypeInt,
State = om.State,
CreateMan = om.CreateMan,
CreateManName = createPerson.PersonName,
CreateDate = om.CreateDate,
ReqUnitId = om.ReqUnitId,
ReqUnitName = reqUnit.UnitName,
Category = om.Category,
AuditMan = om.AuditMan,
AuditDate = om.AuditDate,
AuditMan2 = om.AuditMan2,
AuditDate2 = om.AuditDate2,
WarehouseMan = om.WarehouseMan,
WarehouseDate = om.WarehouseDate,
WeldTaskId = plan.WeldTaskId,
Remark = plan.Remark
};
return query.ToList();
}
public static Tw_InOutMasterOutput GetOutputMasterById(string id)
{
if (string.IsNullOrEmpty(id))
{
return null;
}
var query = from om in Funs.DB.Tw_OutputMaster
join plan in Funs.DB.Tw_InOutPlanMaster on om.InOutPlanMasterId equals plan.Id into planJoin
from plan in planJoin.DefaultIfEmpty()
join reqUnit in Funs.DB.Base_Unit on om.ReqUnitId equals reqUnit.UnitId into unitJoin
from reqUnit in unitJoin.DefaultIfEmpty()
where om.Id == id
select new Tw_InOutMasterOutput
{
Id = om.Id,
ProjectId = om.ProjectId,
CusBillCode = om.CusBillCode,
InOutPlanMasterId = om.InOutPlanMasterId,
WarehouseId = om.WarehouseId,
WarehouseCode = om.WarehouseCode,
WarehouseName = om.WarehouseCode,
Source = om.Source,
TypeInt = om.TypeInt,
State = om.State,
CreateMan = om.CreateMan,
CreateDate = om.CreateDate,
ReqUnitId = om.ReqUnitId,
ReqUnitName = reqUnit.UnitName,
Category = om.Category,
AuditMan = om.AuditMan,
AuditDate = om.AuditDate,
AuditMan2 = om.AuditMan2,
AuditDate2 = om.AuditDate2,
WarehouseMan = om.WarehouseMan,
WarehouseDate = om.WarehouseDate,
WeldTaskId = plan.WeldTaskId,
Remark = plan.Remark
};
return query.FirstOrDefault();
}
public static List<AntiCorrosionTrustDetailOutput> GetDetailList(string trustId)
{
var query = from d in Funs.DB.Tw_AntiCorrosionTrustDetail
join m in Funs.DB.HJGL_MaterialCodeLib on d.MaterialCode equals m.MaterialCode into materialJoin
from m in materialJoin.DefaultIfEmpty()
where d.TrustId == trustId
orderby d.SortIndex
select new AntiCorrosionTrustDetailOutput
{
Id = d.Id,
TrustId = d.TrustId,
SortIndex = d.SortIndex,
MaterialCode = d.MaterialCode,
Code = m.Code,
MaterialName = m.MaterialName,
MaterialSpec = m.MaterialSpec,
MaterialDef = m.MaterialDef,
MaterialUnit = m.MaterialUnit,
Quantity = d.Quantity,
PaintCode = d.PaintCode,
Primer = d.Primer,
IntermediatePaint = d.IntermediatePaint,
Topcoat = d.Topcoat,
ColorCode = d.ColorCode,
Remark = d.Remark
};
return query.ToList();
}
public static List<AntiCorrosionTrustDetailOutput> GetDetailsByOutputMasterId(string outputMasterId)
{
var outputDetails = Funs.DB.Tw_OutputDetail
.Where(x => x.OutputMasterId == outputMasterId)
.OrderBy(x => x.Id)
.ToList();
var materialCodes = outputDetails.Select(x => x.MaterialCode).Where(x => !string.IsNullOrEmpty(x)).Distinct().ToList();
var materials = Funs.DB.HJGL_MaterialCodeLib
.Where(x => materialCodes.Contains(x.MaterialCode))
.ToDictionary(x => x.MaterialCode, x => x);
var index = 0;
var result = new List<AntiCorrosionTrustDetailOutput>();
foreach (var detail in outputDetails)
{
index++;
HJGL_MaterialCodeLib material = null;
if (!string.IsNullOrEmpty(detail.MaterialCode) && materials.ContainsKey(detail.MaterialCode))
{
material = materials[detail.MaterialCode];
}
// 防腐明细只保存材料主编码和数量,展示字段统一由材料编码库补齐。
result.Add(new AntiCorrosionTrustDetailOutput
{
Id = SQLHelper.GetNewID(),
SortIndex = index,
MaterialCode = detail.MaterialCode,
Code = material == null ? string.Empty : material.Code,
MaterialName = material == null ? string.Empty : material.MaterialName,
MaterialSpec = material == null ? string.Empty : material.MaterialSpec,
MaterialDef = material == null ? string.Empty : material.MaterialDef,
MaterialUnit = material == null ? string.Empty : material.MaterialUnit,
Quantity = detail.ActNum ?? detail.PlanNum
});
}
return result;
}
public static List<PaintCodeDictOutput> GetPaintCodeList()
{
return Funs.DB.Tw_PaintCodeDict
.Where(x => x.IsUsed)
.OrderBy(x => x.SortIndex)
.ThenBy(x => x.PaintCode)
.Select(x => new PaintCodeDictOutput
{
Id = x.Id,
PaintCode = x.PaintCode,
Primer = x.Primer,
IntermediatePaint = x.IntermediatePaint,
Topcoat = x.Topcoat,
ColorCode = x.ColorCode,
SortIndex = x.SortIndex
})
.ToList();
}
public static void Save(AntiCorrosionTrustOutput trust, List<AntiCorrosionTrustDetailOutput> details)
{
var isNew = string.IsNullOrEmpty(trust.Id);
Tw_AntiCorrosionTrust entity;
if (isNew)
{
entity = new Tw_AntiCorrosionTrust
{
Id = SQLHelper.GetNewID(),
ProjectId = trust.ProjectId,
CreateMan = trust.CreateMan,
CreateDate = DateTime.Now
};
Funs.DB.Tw_AntiCorrosionTrust.InsertOnSubmit(entity);
}
else
{
entity = Funs.DB.Tw_AntiCorrosionTrust.FirstOrDefault(x => x.Id == trust.Id);
if (entity == null)
{
throw new Exception("防腐委托单不存在或已删除!");
}
}
entity.UnitWorkId = trust.UnitWorkId;
entity.ConstructionPart = trust.ConstructionPart;
entity.ConstructionProfessional = trust.ConstructionProfessional;
entity.TrustCode = trust.TrustCode;
entity.OutputMasterId = trust.OutputMasterId;
entity.DemandDate = trust.DemandDate;
entity.CompleteDate = trust.CompleteDate;
entity.Remark = trust.Remark;
var oldDetails = Funs.DB.Tw_AntiCorrosionTrustDetail.Where(x => x.TrustId == entity.Id).ToList();
Funs.DB.Tw_AntiCorrosionTrustDetail.DeleteAllOnSubmit(oldDetails);
var paintMap = GetPaintCodeList().ToDictionary(x => x.PaintCode ?? string.Empty, x => x);
var index = 0;
foreach (var detail in details.Where(x => !string.IsNullOrEmpty(x.MaterialCode)))
{
index++;
if (!string.IsNullOrEmpty(detail.PaintCode) && paintMap.ContainsKey(detail.PaintCode))
{
var paint = paintMap[detail.PaintCode];
detail.Primer = paint.Primer;
detail.IntermediatePaint = paint.IntermediatePaint;
detail.Topcoat = paint.Topcoat;
detail.ColorCode = paint.ColorCode;
}
// 涂漆代码保存时按字典补齐,避免前端联动遗漏造成打印字段为空。
Funs.DB.Tw_AntiCorrosionTrustDetail.InsertOnSubmit(new Tw_AntiCorrosionTrustDetail
{
Id = SQLHelper.GetNewID(),
TrustId = entity.Id,
SortIndex = detail.SortIndex ?? index,
MaterialCode = detail.MaterialCode,
Quantity = detail.Quantity,
PaintCode = detail.PaintCode,
Primer = detail.Primer,
IntermediatePaint = detail.IntermediatePaint,
Topcoat = detail.Topcoat,
ColorCode = detail.ColorCode,
Remark = detail.Remark
});
}
Funs.DB.SubmitChanges();
trust.Id = entity.Id;
}
public static void DeleteById(string id)
{
var details = Funs.DB.Tw_AntiCorrosionTrustDetail.Where(x => x.TrustId == id).ToList();
Funs.DB.Tw_AntiCorrosionTrustDetail.DeleteAllOnSubmit(details);
var trust = Funs.DB.Tw_AntiCorrosionTrust.FirstOrDefault(x => x.Id == id);
if (trust != null)
{
Funs.DB.Tw_AntiCorrosionTrust.DeleteOnSubmit(trust);
}
Funs.DB.SubmitChanges();
}
public static DataTable GetPrintMasterTable(string id)
{
var query = (from t in Funs.DB.Tw_AntiCorrosionTrust
join p in Funs.DB.Base_Project on t.ProjectId equals p.ProjectId into projectJoin
from p in projectJoin.DefaultIfEmpty()
join uw in Funs.DB.WBS_UnitWork on t.UnitWorkId equals uw.UnitWorkId into uwJoin
from uw in uwJoin.DefaultIfEmpty()
join om in Funs.DB.Tw_OutputMaster on t.OutputMasterId equals om.Id into omJoin
from om in omJoin.DefaultIfEmpty()
join createPerson in Funs.DB.Person_Persons on t.CreateMan equals createPerson.PersonId into personJoin
from createPerson in personJoin.DefaultIfEmpty()
where t.Id == id
select new
{
p.ProjectName,
uw.UnitWorkName,
uw.UnitWorkCode,
t.ConstructionPart,
t.ConstructionProfessional,
t.TrustCode,
OutputMasterCode = om.CusBillCode,
t.DemandDate,
t.CompleteDate,
CreateManName = createPerson.PersonName
})
.ToList()
.Select(x => new
{
x.ProjectName,
x.UnitWorkName,
x.UnitWorkCode,
x.ConstructionPart,
x.ConstructionProfessional,
x.TrustCode,
x.OutputMasterCode,
DemandDate = x.DemandDate.HasValue ? x.DemandDate.Value.ToString("yyyy-MM-dd") : string.Empty,
CompleteDate = x.CompleteDate.HasValue ? x.CompleteDate.Value.ToString("yyyy-MM-dd") : string.Empty,
x.CreateManName
});
return ToDataTable(query, "Table1");
}
public static DataTable GetPrintDetailTable(string id)
{
var query = from d in Funs.DB.Tw_AntiCorrosionTrustDetail
join m in Funs.DB.HJGL_MaterialCodeLib on d.MaterialCode equals m.MaterialCode into materialJoin
from m in materialJoin.DefaultIfEmpty()
where d.TrustId == id
orderby d.SortIndex
select new
{
d.SortIndex,
m.MaterialName,
m.Code,
m.MaterialSpec,
m.MaterialDef,
m.MaterialUnit,
d.Quantity,
d.PaintCode,
d.Primer,
d.IntermediatePaint,
d.Topcoat,
d.ColorCode,
d.Remark
};
return ToDataTable(query.ToList(), "Data");
}
private static int GetTrustCodeSerial(string trustCode)
{
if (string.IsNullOrEmpty(trustCode) || trustCode.Length < 3)
{
return 0;
}
int serial;
return int.TryParse(trustCode.Substring(trustCode.Length - 3), out serial) ? serial : 0;
}
private static DataTable ToDataTable<T>(IEnumerable<T> data, string tableName)
{
var table = new DataTable(tableName);
var properties = typeof(T).GetProperties();
foreach (var property in properties)
{
var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
table.Columns.Add(property.Name, propertyType);
}
foreach (var item in data)
{
var row = table.NewRow();
foreach (var property in properties)
{
row[property.Name] = property.GetValue(item, null) ?? DBNull.Value;
}
table.Rows.Add(row);
}
return table;
}
}
}