using DOAN.Common;
using DOAN.Model.BZFM;
using DOAN.Model.BZFM.Dto;
using DOAN.Model.System;
using DOAN.Model.System.Dto;
using DOAN.Repository;
using DOAN.Service.BZFM.IBZFMService;
using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
namespace DOAN.Service.BZFM
{
///
/// 物料表Service业务层处理
///
[AppService(ServiceType = typeof(IMmMaterialService), ServiceLifetime = LifeTime.Transient)]
public class MmMaterialService : BaseService, IMmMaterialService
{
///
/// 查询物料表列表
///
///
///
public PagedInfo GetList(MmMaterialQueryDto parm)
{
var predicate = QueryExp(parm);
var response = Queryable()
.Where(predicate.ToExpression())
.OrderBy(it => it.Type)
.ToPage(parm);
return response;
}
///
/// 获取详情
///
///
///
public MmMaterial GetInfo(int Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
return response;
}
///
/// 添加物料表
///
///
///
public MmMaterial AddMmMaterial(MmMaterial model)
{
return Insertable(model).ExecuteReturnEntity();
}
///
/// 修改物料表
///
///
///
public int UpdateMmMaterial(MmMaterial model)
{
return Update(model, true);
}
///
/// 查询导出表达式
///
///
///
private static Expressionable QueryExp(MmMaterialQueryDto parm)
{
var predicate = Expressionable.Create()
.AndIF(!string.IsNullOrEmpty(parm.CategoryCode), m => m.CategoryCode.Contains(parm.CategoryCode))
.AndIF(!string.IsNullOrEmpty(parm.MaterialName), m => m.MaterialName.Contains(parm.MaterialName))
.AndIF(!string.IsNullOrEmpty(parm.MaterialCode), m => m.MaterialCode.Contains(parm.MaterialCode))
.AndIF(!string.IsNullOrEmpty(parm.Specification), m => m.Specification.Contains(parm.Specification))
.AndIF(!string.IsNullOrEmpty(parm.Type), m => m.Type.Contains(parm.Type))
.AndIF(!string.IsNullOrEmpty(parm.Status), m => m.Status == parm.Status);
return predicate;
}
///
/// 获取物料类别下拉框
///
///
///
///
public List GetMmMaterialCategoryOptions(MmMaterialCategoryDto parm)
{
try
{
return Context.Queryable()
.WhereIF(!string.IsNullOrEmpty(parm.CategoryCode),it => it.CategoryCode.Contains(parm.CategoryCode))
.WhereIF(!string.IsNullOrEmpty(parm.CategoryName), it => it.CategoryName.Contains(parm.CategoryName))
.Select(it => new MmMaterialCategoryOptionsDto
{
Label = it.CategoryName,
Value = it.CategoryCode
}
).ToList();
}
catch (Exception)
{
// TODO 处理错误日志
throw;
}
}
///
/// 导入数据
///
///
///
public ImportResultDto Importmaterial(List material)
{
// normalize and set defaults, do not overwrite provided values
material.ForEach(x =>
{
if (x.CreatedTime == null)
{
x.CreatedTime = DateTime.Now;
}
if (string.IsNullOrWhiteSpace(x.Status))
{
x.Status = "启用";
}
if (!string.IsNullOrWhiteSpace(x.MaterialCode))
{
x.MaterialCode = x.MaterialCode.Trim();
}
if (!string.IsNullOrWhiteSpace(x.MaterialName))
{
x.MaterialName = x.MaterialName.Trim();
}
x.Description = x.Description.IsEmpty() ? "数据导入" : x.Description;
});
var x = Context.Storageable(material)
.SplitInsert(it => !it.Any())
.WhereColumns(it => new { it.Id, it.MaterialCode })//如果不是主键可以这样实现(多字段it=>new{it.x1,it.x2})
.ToStorage();
var result = x.AsInsertable.ExecuteCommand();//插入可插入部分;
var importResult = new ImportResultDto
{
Message = "导入完成",
Inserted = x.InsertList.Count,
Updated = x.UpdateList.Count,
ErrorCount = x.ErrorList.Count,
IgnoredCount = x.IgnoreList.Count,
Deleted = x.DeleteList.Count,
Total = x.TotalList.Count
};
//输出统计
Console.WriteLine(importResult);
// 收集错误与忽略信息
foreach (var item in x.ErrorList)
{
importResult.Errors.Add(new ImportErrorDto { MaterialCode = item.Item.MaterialCode, Message = item.StorageMessage });
}
foreach (var item in x.IgnoreList)
{
importResult.Ignored.Add(new ImportErrorDto { MaterialCode = item.Item.MaterialCode, Message = item.StorageMessage });
}
return importResult;
}
///
/// 导出物料表列表
///
///
public PagedInfo SelectMaterialList(MmMaterialQueryDto material, PagerInfo pager)
{
// Use the same predicate builder as GetList to support consistent filtering
var predicate = QueryExp(material);
var query = Queryable()
.Where(predicate.ToExpression())
.Select(it => new MmMaterialExcelDto;
;
return query.ToPage(pager);
}
}
}