增加同步物料接口
This commit is contained in:
Binary file not shown.
122
src/WMS.Web.Api/Controllers/MaterialController.cs
Normal file
122
src/WMS.Web.Api/Controllers/MaterialController.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using AutoMapper;
|
||||
using WMS.Web.Core.Dto.Erp;
|
||||
using WMS.Web.Core.Help;
|
||||
using WMS.Web.Core.Internal.Results;
|
||||
using WMS.Web.Domain.Entitys;
|
||||
using WMS.Web.Domain.Infrastructure;
|
||||
using WMS.Web.Domain.IService.Public;
|
||||
using WMS.Web.Domain.Options;
|
||||
using WMS.Web.Domain.Services;
|
||||
using WMS.Web.Domain.Services.Public;
|
||||
using WMS.Web.Domain.Values;
|
||||
using WMS.Web.Repositories;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace WMS.Web.Api.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// 物料
|
||||
/// </summary>
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class MaterialController : ControllerBase
|
||||
{
|
||||
private IMapper _mapper;
|
||||
private ILogger<MaterialController> _logger;
|
||||
private IErpService _erpService;
|
||||
private IMaterialsRepositories _materialsRepositories;
|
||||
private ErpOptions _erpOptions;
|
||||
private readonly IMemoryCache _memoryCache;
|
||||
public MaterialController(IMapper mapper, ILogger<MaterialController> logger, IErpService erpService,
|
||||
IMaterialsRepositories materialsRepositories, IOptions<ErpOptions> erpOptions, IMemoryCache memoryCache)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_logger = logger;
|
||||
_erpService = erpService;
|
||||
_materialsRepositories = materialsRepositories;
|
||||
this._erpOptions = erpOptions.Value;
|
||||
this._memoryCache = memoryCache;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 刷新物料 69条码或物料编码 名字
|
||||
/// </summary>
|
||||
/// <param name="number">物料编码</param>
|
||||
/// <param name="type">69 条码,other 名字和规格型号</param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Route("Refresh/{number}/{type}")]
|
||||
public async Task<Result> Refresh([FromRoute] string number, [FromRoute] string type)
|
||||
{
|
||||
if (string.IsNullOrEmpty(number))
|
||||
return Result.ReFailure("物料编码不能为空", 556677);
|
||||
if (string.IsNullOrEmpty(type) || (!type.Equals("69") && !type.Equals("other")))
|
||||
return Result.ReFailure("类型只能是69或other", 556677);
|
||||
|
||||
List<string> numbers = new List<string>();
|
||||
numbers.Add(number);
|
||||
|
||||
var list = await _materialsRepositories.GetEntityList(numbers);
|
||||
if (list.Count() <= 0) return Result.ReFailure(ResultCodes.MateriaNoData);
|
||||
|
||||
var mDtos = await _erpService.BillQueryForMaterialByNumbers(numbers);
|
||||
if (!mDtos.IsSuccess) return Result.ReFailure(mDtos.Message, mDtos.Status);
|
||||
if (mDtos.Data.Count() <= 0) return Result.ReFailure(ResultCodes.MateriaNoData);
|
||||
|
||||
var materials = _memoryCache.Get<List<ErpMaterialDto>>(_erpOptions.cache_materail_key);
|
||||
|
||||
List<Materials> update_list = new List<Materials>();
|
||||
foreach (var m in mDtos.Data)
|
||||
{
|
||||
var entity = list.FirstOrDefault(f => f.MaterialNumber.Equals(m.MaterialNumber));
|
||||
if (entity == null) continue;
|
||||
if (type.Equals("69"))
|
||||
{
|
||||
entity.BarCode = m.BarCode.Trim();
|
||||
update_list.Add(entity);
|
||||
}
|
||||
//if (type.Equals("id"))
|
||||
//{
|
||||
// entity.IdConvertBar = m.IdConvertBar.Trim();
|
||||
// update_list.Add(entity);
|
||||
//}
|
||||
if (type.Equals("other"))
|
||||
{
|
||||
entity.MaterialName = m.MaterialName;
|
||||
entity.Specifications = m.Specifications;
|
||||
update_list.Add(entity);
|
||||
}
|
||||
|
||||
//修改缓存
|
||||
if (materials == null) continue;
|
||||
var entity_cache = materials.FirstOrDefault(f => f.MaterialNumber.Equals(m.MaterialNumber));
|
||||
if (entity_cache == null) continue;
|
||||
if (type.Equals("69"))
|
||||
entity_cache.BarCode = m.BarCode.Trim();
|
||||
//if (type.Equals("id"))
|
||||
// entity_cache.IdConvertBar = m.IdConvertBar.Trim();
|
||||
if (type.Equals("other"))
|
||||
{
|
||||
entity_cache.MaterialName = m.MaterialName;
|
||||
entity_cache.Specifications = m.Specifications;
|
||||
}
|
||||
|
||||
}
|
||||
//修改缓存
|
||||
if (materials != null && materials.Count() > 0)
|
||||
{
|
||||
_memoryCache.Set(_erpOptions.cache_materail_key, materials, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(DateTimeUtil.GetTotalMinutesTimeSpan())));
|
||||
}
|
||||
await _materialsRepositories.UpdateRange(update_list);
|
||||
return Result.ReSuccess();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6269,6 +6269,14 @@
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:WMS.Web.Domain.Services.Public.ErpService.BillQueryForMaterialByNumbers(System.Collections.Generic.List{System.String})">
|
||||
<summary>
|
||||
获取物料id32进制
|
||||
</summary>
|
||||
<param name="materialNumbers"></param>
|
||||
<returns></returns>
|
||||
<exception cref="T:System.NotImplementedException"></exception>
|
||||
</member>
|
||||
<member name="M:WMS.Web.Domain.Services.Public.ErpService.BillQueryForMaterialById(System.Int32)">
|
||||
<summary>
|
||||
erp:基础数据-物料-ids查询
|
||||
|
||||
@@ -134,7 +134,7 @@ namespace WMS.Web.Domain.IService.Public
|
||||
/// <param name="number"></param>
|
||||
/// <returns></returns>
|
||||
Task<Result<ErpMaterialDto>> BillQueryForMaterial(string number);
|
||||
|
||||
Task<ResultList<ErpMaterialDto>> BillQueryForMaterialByNumbers(List<string> materialNumbers);
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
|
||||
@@ -61,5 +61,6 @@ namespace WMS.Web.Domain.Infrastructure
|
||||
/// <param name="isTransaction"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> UpdateRange(List<Materials> entitys, bool isTransaction = true);
|
||||
Task<List<Materials>> GetEntityList(List<string> materNumbers);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1034,7 +1034,89 @@ namespace WMS.Web.Domain.Services.Public
|
||||
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取物料id32进制
|
||||
/// </summary>
|
||||
/// <param name="materialNumbers"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public async Task<ResultList<ErpMaterialDto>> BillQueryForMaterialByNumbers(List<string> materialNumbers)
|
||||
{
|
||||
//2.先登录金蝶-拿到token
|
||||
var token_result = await this.Init();
|
||||
if (!token_result.IsSuccess)
|
||||
return ResultList<ErpMaterialDto>.ReFailure(token_result);
|
||||
|
||||
|
||||
//3.获取金蝶物料:拼接参数和条件
|
||||
var query = new ErpBillQueryDto(token_result.Data);
|
||||
var param = new ErpBillQueryParamDto(FormIdParam.BD_MATERIAL.ToString());
|
||||
param.FieldKeys = "FMATERIALID,FName,FNumber,FSpecification,FBaseUnitId,FBaseUnitId.FName,FBaseUnitId.FNumber,FBARCODE,FUseOrgId,FUseOrgId.FNumber,FIsBatchManage,FProductIDS";
|
||||
param.Limit = 10000;
|
||||
|
||||
//查询条件:备注其中的条件值以金蝶的值为准!!!
|
||||
//1.审核状态:已审核
|
||||
//2.禁用状态:A否,B是
|
||||
//3.使用组织:只查询“深圳市元创时代科技有限公司”这个组织;组织ID=100008--固定值查询(测试端和正式是一样的)
|
||||
//param.FilterString = "FDocumentStatus='C' and FForbidStatus='A' and (FUseOrgId=19162897 or FUseOrgId=100008)";
|
||||
|
||||
|
||||
param.OrderString = "FCreateDate ASC";
|
||||
|
||||
//4.循环拿金蝶数据
|
||||
var erp_materials_list = new List<ErpMaterialDto>();
|
||||
var error_josn = "";
|
||||
decimal d_count = Convert.ToDecimal(materialNumbers.Count()) / Convert.ToDecimal(100);
|
||||
int count = (int)Math.Ceiling(d_count);
|
||||
try
|
||||
{
|
||||
for (int i = 1; i <= count; i++)
|
||||
{
|
||||
param.FilterString = "FDocumentStatus='C' and FForbidStatus='A' and FUseOrgId=100008";
|
||||
|
||||
var list = materialNumbers.Skip((i - 1) * 100).Take(100);
|
||||
string numbers = JsonConvert.SerializeObject(list);
|
||||
numbers = numbers.Replace("[", "").Replace("]", "").Replace("\"", "'");
|
||||
param.FilterString += $" and FNumber in ({numbers}) and (FProductIDS<>'' or FProductIDS<>' ')";
|
||||
|
||||
|
||||
//4.2.参数json化
|
||||
query.Data = JsonConvert.SerializeObject(param);
|
||||
var json = JsonConvert.SerializeObject(query);
|
||||
|
||||
//4.3.请求查询接口并返回数据
|
||||
var result_json = await _client.ExecuteBillQueryAsync(json);
|
||||
error_josn = result_json;
|
||||
var result = JsonConvert.DeserializeObject<List<List<string>>>(result_json);
|
||||
//4.4.判断数据是否全部拉取完成:并停止循环的条件
|
||||
if (result == null || result.Count == 0)
|
||||
break;
|
||||
//4.5.拼装
|
||||
foreach (var item in result)
|
||||
{
|
||||
var lis = new ErpMaterialDto();
|
||||
lis.MaterialId = Convert.ToInt32(item[0]);
|
||||
lis.MaterialName = item[1];
|
||||
lis.MaterialNumber = item[2];
|
||||
lis.Specifications = item[3];
|
||||
lis.BaseUnitId = Convert.ToInt32(item[4]);
|
||||
lis.BaseUnitName = item[5];
|
||||
lis.BaseUnitNumber = item[6];
|
||||
lis.BarCode = item[7];
|
||||
lis.OrgId = Convert.ToInt32(item[8]);
|
||||
lis.OrgCode = item[9];
|
||||
lis.IsBatchManage = Convert.ToBoolean(item[10]);
|
||||
erp_materials_list.Add(lis);
|
||||
}
|
||||
}
|
||||
return ResultList<ErpMaterialDto>.ReSuccess(erp_materials_list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogInformation("物料拉取->失败:" + ex.Message + " " + error_josn);
|
||||
return ResultList<ErpMaterialDto>.ReFailure(ResultCodes.ErpSynsError);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// erp:基础数据-物料-ids查询
|
||||
/// </summary>
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace WMS.Web.Repositories.DependencyInjection
|
||||
string[] pathlist = path.Split('/');
|
||||
bool isLogin = pathlist.Where(x => x == "login" || x.ToLower() == "heart"
|
||||
|| x.ToLower() == "test" || x.ToLower() == "serialnumber" || x.ToLower() == "barcode" ||
|
||||
x.ToLower() == "swagger" || x.ToLower() == "productinventory").Any();
|
||||
x.ToLower() == "swagger" || x.ToLower() == "productinventory" || x.ToLower() == "material").Any();
|
||||
if (isLogin)
|
||||
{
|
||||
context.Response.StatusCode = 200;
|
||||
|
||||
@@ -160,5 +160,10 @@ namespace WMS.Web.Repositories
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public async Task<List<Materials>> GetEntityList(List<string> materNumbers)
|
||||
{
|
||||
var entitys = await _context.Materials.Where(x => materNumbers.Contains(x.MaterialNumber)).ToListAsync();
|
||||
return entitys;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user