Files
wcs/WCS.BLL/Services/Service/BatchBindMatDetailService.cs
hehaibing-1996 ee7ee7a672 批量导入
2025-01-20 17:45:05 +08:00

288 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using TouchSocket.Core;
using WCS.BLL.Config;
using WCS.BLL.DbModels;
using WCS.BLL.HardWare;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.DAL;
using WCS.DAL.Db;
using WCS.DAL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel;
using WCS.Model.ApiModel.BatchBindMatDetail;
using WCS.Model.ApiModel.InOutRecord;
using WCS.Model.ApiModel.MatBaseInfo;
using WCS.Model.ApiModel.MatDetailCurrentInfo;
using WCS.Model.ApiModel.StoreInfo;
using WCS.Model.ApiModel.User;
namespace WCS.BLL.Services.Service
{
public class BatchBindMatDetailService : IBatchBindMatDetailService
{
/// <summary>
/// 按照工位号获取绑定明细
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<PageQueryResponse<MatDetailCurrentInfoModel>> GetMatDetailCurrentInfosByStationCode(GetMatDetailCurrentInfosByStationCodeRequest request)
{
try
{
var recordsQueryable = DbHelp.db.Queryable<MatDetailCurrentInfo>()
.LeftJoin<ShelfInfo>((mci, si) => mci.ShlefId == si.Id)
.LeftJoin<LocationInfo>((mci, si, li) => (si.TransStatus == TransStatusEnum. && si.CurrentLocationId == li.Id)
|| (si.TransStatus == TransStatusEnum. && si.DestinationLocationId == li.Id))
.WhereIF(request.OrderTypeId != null && request.OrderTypeId != 0, (mci, si, li) => mci.OrderTypeId == request.OrderTypeId)
.WhereIF(!string.IsNullOrEmpty(request.ShelfCode), (mci, si, li) => si.ShelfCode.Contains(request.ShelfCode))
//.WhereIF(!string.IsNullOrEmpty(request.StationCode), (mci, si, li) => mci.StationCode.Contains(request.StationCode))
.Select((mci, si, li) => new MatDetailCurrentInfoModel()
{
Id = mci.Id,
ShlefId = mci.ShlefId,
ShelfCode = mci.ShelfCode,
ShelfType = mci.ShelfType,
LocationArea = li.LocationArea,
LocationCode = li.LocationCode,
MatCode = mci.MatCode,
MatName = mci.MatName,
MatSpec = mci.MatSpec,
MatUnit = mci.MatUnit,
MatCustomer = mci.MatCustomer,
MatQty = mci.MatQty,
MatSupplier = mci.MatSupplier,
OrderTypeId = mci.OrderTypeId,
OrderTypeName = mci.OrderTypeName,
OrderNumber = mci.OrderNumber,
StationCode = mci.StationCode,
ModifyUser = mci.ModifyUser,
ModifyTime = mci.ModifyTime
});
//分页
var totalCount = await recordsQueryable.CountAsync();
var records = await recordsQueryable
.OrderByDescending(mci => mci.Id)
.Skip((request.PageNumber - 1) * request.PageSize).Take(request.PageSize)
.ToListAsync();
//生成序号
for (int i = 0; i < records.Count; i++)
{
records[i].RowNumber = (request.PageNumber - 1) * request.PageSize + i + 1;
}
return new PageQueryResponse<MatDetailCurrentInfoModel>()
{
Code = 200,
Message = $"success",
Data = new PageQueryResponseData<MatDetailCurrentInfoModel>()
{
TotalCount = totalCount,
MaxPage = request.PageSize == 0 ? 0 : (int)Math.Ceiling((decimal)totalCount / request.PageSize),
Count = records.Count,
Lists = records.ToList()
}
};
}
catch (Exception ex)
{
return new PageQueryResponse<MatDetailCurrentInfoModel>()
{
Code = 300,
Message = $"操作失败:{ex.Message}",
};
}
}
public async Task<ResponseCommon<object>> updateMatDetailCurrentInfo(AddLocaionInfoRequest<MatDetailCurrentInfo> request)
{
try
{
var matDetailCurrentInfo = await DbHelp.db.Queryable<MatDetailCurrentInfo>() //.Where(t => t.MatDetailCurrentCode == request.MatDetailCurrentInfo.MatDetailCurrentCode)
.Where(t => t.Id == request.LocationInfo.Id)
.FirstAsync();
if (matDetailCurrentInfo == null)
{
return new ResponseCommon<Object>
{
Code = 205,
Message = $"更新位置信息失败:此条数据不存在,请确认!",
Data = null
};
}
matDetailCurrentInfo.MatQty = request.LocationInfo.MatQty;
matDetailCurrentInfo.ModifyUser = request.UserName;
matDetailCurrentInfo.ModifyTime = DateTime.Now;
var rowNum = await DbHelp.db.Updateable(matDetailCurrentInfo).ExecuteCommandAsync();
if (rowNum == 0)
{
return new ResponseCommon<Object>
{
Code = 201,
Message = $"更新货架存量信息失败:请重试!",
Data = null
};
}
else
{
return new ResponseCommon<Object>
{
Code = 200,
Message = $"更新货架存量信息成功!",
Data = null
};
}
}
catch (Exception ex)
{
var response = new ResponseCommon<Object>
{
Code = 300,
Message = $"操作失败:{ex.Message}",
Data = null
};
return response;
}
}
public async Task<ResponseCommon<object>> deleteMatDetailCurrentInfo(DeleteInfosRequest request)
{
//先查询出具体的Id
var MatDetailCurrentInfos = await DbHelp.db.Queryable<MatDetailCurrentInfo>()
.Where(t => request.needDeleteIds.Contains(t.Id))
.ToListAsync();
//执行删除
try
{
var deleteRows = await DbHelp.db.Deleteable(MatDetailCurrentInfos).ExecuteCommandAsync();
return new ResponseCommon<Object>
{
Code = 200,
Message = $"已删除{deleteRows}条数据!",
Data = null
};
}
catch (Exception ex)
{
var response = new ResponseCommon<Object>
{
Code = 300,
Message = $"操作失败:{ex.Message}",
Data = null
};
return response;
}
}
/// <summary>
/// 批量导入物料绑定关系
/// </summary>
/// <param name="lists"></param>
/// <param name="userName"></param>
/// <param name="deviceType"></param>
/// <returns></returns>
public async Task<ResponseCommon<List<string>>> importMatDetailCurrentInfo(List<MatDetailCurrentInfoImportModel> lists, string userName, string deviceType, string stationCode)
{
#region
//工位校验 工位是否有货架校验
var station = await DbHelp.db.Queryable<LocationInfo>()
.Where(t => t.LocationCode == stationCode || t.IsEnable == true)
.FirstAsync();
if (station == null)
{
return new ResponseCommon<List<string>>()
{
Code = 200,
Message = $"导入失败:工位[{stationCode}]不存在或已被禁用!",
Data = null,
};
}
//数量、物料编码等必填项的校验
foreach (var matDetailCurrentInfo in lists)
{
if (matDetailCurrentInfo. == null || matDetailCurrentInfo. <= 0)
{
return new ResponseCommon<List<string>>()
{
Code = 200,
Message = $"导入失败:数量应该大于0!",
Data = null,
};
}
if (string.IsNullOrEmpty(matDetailCurrentInfo.))
{
return new ResponseCommon<List<string>>()
{
Code = 200,
Message = $"导入失败:物料编码必填!",
Data = null,
};
}
}
//单据类型校验
//物料编码校验
#endregion
#region
try
{
await DbHelp.db.BeginTranAsync();
foreach (var mat in lists)
{
var matBaseInfo = new MatBaseInfo()
{
MatCode = mat.,
MatName = mat.,
MatSpec = mat.,
MatUnit = mat.,
MatCustomer = mat.,
ModifyUser = userName,
IsEnable = mat. == "启用" ? true : false,
};
await DbHelp.db.Insertable(matBaseInfo).ExecuteCommandAsync();
}
await DbHelp.db.CommitTranAsync();
return new ResponseCommon<List<string>>()
{
Code = 200,
Message = "导入数据成功",
Data = null
};
}
catch (Exception ex)
{
await DbHelp.db.RollbackTranAsync();
var ErrList = new List<string>
{
ex.Message
};
return new ResponseCommon<List<string>>()
{
Code = 200,
Message = "导入失败",
Data = ErrList
};
}
#endregion
}
}
}