using ERP; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore.Storage; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Threading; using System.Threading.Tasks; using WMS.Web.Core.Dto; using WMS.Web.Core.Dto.Erp.Customer; using WMS.Web.Core.Internal.Results; using WMS.Web.Domain.Entitys; using WMS.Web.Domain.Infrastructure; using WMS.Web.Domain.IService; using WMS.Web.Domain.IService.Public; using Microsoft.Extensions.Hosting; using WMS.Web.Domain.Options; using Microsoft.Extensions.Options; using WMS.Web.Domain.Values; using WMS.Web.Core.Dto.Erp; using Microsoft.Extensions.Caching.Memory; using System.Reflection; using WMS.Web.Domain.Services; using SkiaSharp; using Google.Protobuf.Collections; using NPOI.SS.Formula.Functions; using System.Text.Json; using WMS.Web.Core.Dto.LingXing; using WMS.Web.Domain.Services.Public; namespace WMS.Web.Api.Controllers { [Route("api/[controller]")] [ApiController] public class TestController : ControllerBase { private readonly IErpService _erpService; private IInStockTaskService _inStockTaskService; private readonly IBasicsRepositories _basicsRepositories; private IBasicsRepositories _transactionRepositories; private readonly IOpsService _opsService; private readonly IBoxService _boxService; private readonly IOutStockTaskRepositories _outStockTaskRepositories; private readonly IOutStockRepositories _outStockRepositories; private readonly IOutStockTaskService _outStockTaskService; private readonly IBoxInventoryRepositories _boxInventoryRepositories; private readonly ITakeStockService _takeStockService; private readonly ITakeStockRepositories _takeStockRepositories; private readonly IInStockTaskRepositories _inStockTaskRepositories; private readonly IOutStockService _outStockService; private readonly IWebHostEnvironment _env; private readonly ILoginService _loginService; private readonly AppOptions _options; private readonly ISendMessageService _sendMessageService; private readonly IMaterialService _materialService; private readonly IMemoryCache _memoryCache; private readonly IRedisConcurrentProcessService _redisConcurrentProcessService; private IInStockService _inStockService; private readonly ILingXingService _lingXingService; private readonly IProductInventoryService _productInventoryService; private readonly IJuShuiTanService _juShuiTanService; private readonly IErpInventoryService _erpInventoryService; public TestController(IErpService erpService, IInStockTaskService inStockTaskService, IBoxInventoryRepositories boxInventoryRepositories, IRedisConcurrentProcessService redisConcurrentProcessService, IBasicsRepositories transactionRepositories, IOpsService opsService, IBoxService boxService, IBasicsRepositories basicsRepositories, IMemoryCache memoryCache, IOutStockTaskRepositories outStockTaskRepositories, IOutStockTaskService outStockTaskService, IOptions options, ILoginService loginService, ISendMessageService sendMessageService, ITakeStockService takeStockService, ITakeStockRepositories takeStockRepositories, IOutStockService outStockService, IInStockTaskRepositories inStockTaskRepositories, IWebHostEnvironment env, IMaterialService materialService, IOutStockRepositories outStockRepositories, IInStockService inStockService, ILingXingService lingXingService , IProductInventoryService productInventoryService, IJuShuiTanService juShuiTanService, IErpInventoryService erpInventoryService) { _inStockService = inStockService; _redisConcurrentProcessService = redisConcurrentProcessService; _memoryCache = memoryCache; _materialService = materialService; _loginService = loginService; _sendMessageService = sendMessageService; _inStockTaskRepositories = inStockTaskRepositories; _basicsRepositories = basicsRepositories; _options = options?.Value; _boxInventoryRepositories = boxInventoryRepositories; this._erpService = erpService; this._inStockTaskService = inStockTaskService; this._transactionRepositories = transactionRepositories; _opsService = opsService; _boxService = boxService; _outStockTaskRepositories = outStockTaskRepositories; _outStockTaskService = outStockTaskService; _takeStockService = takeStockService; _takeStockRepositories = takeStockRepositories; _outStockService = outStockService; _outStockRepositories = outStockRepositories; _env = env; _lingXingService = lingXingService; _productInventoryService = productInventoryService; _juShuiTanService = juShuiTanService; _erpInventoryService = erpInventoryService; } [HttpGet] [Route("hj")] public async Task TestHJ() { ////var myJson = "{\"name\":\"小明\",\"address\":\"威海\"}";//json字符串 //var myJson= "{\"code\":\"200\",\"msg\":\"OK\",\"data\":{\"access_token\":\"05895ad6-e318-41da-9b2c-579dedd18fd1\",\"refresh_token\":\"ad9145cf-8af9-4617-9dbe-187b0ff3edf9\",\"expires_in\":7162}}"; //var myObj = JsonConvert.DeserializeObject(myJson);//反序列化为 dynamic 对象 //var my2 = System.Text.Json.JsonSerializer.Deserialize(myJson); //string m = myObj.code; //var m2 = my2.Object; //var md = m2.code; //var b33= await _juShuiTanService.GetStock(); // var bc = await _lingXingService.GetStock(); //List<(string orgCode, string stockCode)> request = new List<(string orgCode, string stockCode)>(); //request.Add(("101", "CK001")); //request.Add(("101", "CK002")); //var list = await _erpService.BillQueryForInventory(request); //var b33 = await _juShuiTanService.GetStock(); //var b = b33.Data.Where(w => w.Id == 10923342).FirstOrDefault(); //await _juShuiTanService.GetTest(); //string codes = "AD"; //var b = await _erpInventoryService.GetInventory(codes); //var bcd=await _productInventoryService.Refresh(); //LingXingResponse r = new LingXingResponse(); //string str = "{\"code\":0,\"message\":\"success\",\"error_details\":[],\"request_id\":\"7A739491-8740-01AC-304E-0203AF887AE2\",\"response_time\":\"2024-10-16 17:58:57\",\"data\":[{\"wid\":437,\"type\":1,\"name\":\"ORICO_OD\",\"is_delete\":0}],\"total\":1}"; //var ss=JsonConvert.DeserializeObject>(str); //var b = System.Text.Json.JsonSerializer.Deserialize>(str); //await _productInventoryService.LingXing(); //var s = await _lingXingService.GetSeller(); //var ns = s.Data.Where(w =>w.Id== "2101").ToList(); //string str = string.Join(",", s.Data.Select(s => s.Id).ToList()); //var sd= await _lingXingService.GetInventory(new LingXingInventoryRequest() { wid = "437" }); //string sdsdf = sd.Message; //List list = new List(); //list.Add("GD-02-15"); //list.Add("GD-01-15"); //list.Add("AB-02-23"); //list.Add("AB-01-23"); //list.Add("AB-01"); //list.Add("AB-1-06"); //list.Add("AB-3-15"); //list.Add("C-3-15"); //List list2 = new List(); //list.ForEach(f => list2.Add(f.Replace("-0", "-"))); //var ss = list2.OrderBy(s => s).ToList(); //await _inStockTaskService.ErpDel(); //await _outStockTaskService.BillQueryForMisDeliveryOutStock(true, new List() { "QTCK026812" }); //await _inStockTaskService.SysnMiscellaneous(new List() { "QTRK028204" }, true); // List boxIds = new List(); // boxIds.Add(5324); //var b= await _inStockService.UnBind(boxIds, true); //var b= await _boxService.Sync(null); //var list = await _outStockTaskRepositories.GetEntityList(new List { 14321 }); //await _outStockRepositories.GetEntityListBySerialNumbers(new List() { "131VU-32C5A" }); //await _outStockRepositories.GetEntityListByBoxBillNos(new List() { "CTN0002187" }); //list[0].Details[0].ErpDetails[0].AccruedQty = 2; //list[0].Details[0].ErpDetails.Add(new OutStockTaskErpDetails() //{ // Erp_DetailId = 1 //}); //list[0].Details.Add(new OutStockTaskDetails() //{ // AccruedQty = 2 //}); //var b = await _outStockTaskRepositories.EditEntityList(list); //List s = new List(); //s.Add("XSCKD10629693"); //var st = await _outStockTaskService.BillQueryForSalOutStock(true, s); return ""; } /// /// 出库任务单手动同步金蝶数据 /// /// [HttpGet] [Route("Refresh")] public async Task Refresh() { if (!_env.IsDevelopment()) return ""; var result = await this._outStockTaskService.Sync(null); return ""; } /// /// 手动同步老ops箱信息 /// /// [HttpGet] [Route("RefreshOpsBox")] public async Task RefreshOpsBox() { if (!_env.IsDevelopment()) return ""; var result = await _boxService.Sync(); return ""; } /// /// 手动同步金蝶删单信息 /// /// [HttpGet] [Route("RefreshErpDelete")] public async Task RefreshErpDelete() { if (!_env.IsDevelopment()) return ""; await _inStockTaskService.ErpDel(); await _outStockTaskService.ErpDel(); return ""; } [HttpGet] [Route("tf")] public async Task TestTF() { if (!_env.IsDevelopment()) return ""; ////var result = await this._erpService.BillQueryForOrg(); //var result = await this._erpService.BillQueryForBillType(); var result = await this._erpService.BillQueryForMaterial(); //var list = new List(); //foreach (var item in result.Data.ToList()) //{ // var t = new TestMar(); // t.MaterialNumber = item.MaterialNumber; // t.BarCode = item.BarCode; // list.Add(t); //} var tt = JsonConvert.SerializeObject(result); return tt; } /// /// 根据物料Id获取物料编码 /// /// /// [HttpGet] [Route("GetMaterial")] public async Task> GetMaterial([FromQuery] List materialIds) { List<(int Id, string Number, string name)> mList = new List<(int Id, string Number, string name)>(); if (!_env.IsDevelopment()) return mList; var materials_result = await _erpService.BillQueryForMaterial(); var materials = materials_result.Data.ToList(); foreach (var mid in materialIds) { var m = materials.FirstOrDefault(f => f.MaterialId == mid); mList.Add((m.MaterialId, m.MaterialNumber, m.MaterialName)); } return mList; } /// /// 根据物料编码获取物料Id /// /// /// [HttpGet] [Route("GetMaterialId/{number}")] public async Task> GetMaterialId([FromRoute] string number) { List<(int Id, string Number, string name)> mList = new List<(int Id, string Number, string name)>(); if (!_env.IsDevelopment()) return mList; var materials_result = await _erpService.BillQueryForMaterial(); var materials = materials_result.Data.ToList(); var mL = materials.Where(f => f.MaterialNumber.Contains(number)) .Select(s => new { Id = s.MaterialId, Number = s.MaterialNumber, name = s.MaterialName }).ToList(); foreach (var m in mL) { mList.Add((m.Id, m.Number, m.name)); } return mList; } /// /// 自动生成箱信息 /// /// 箱前缀 /// 数量(明细条数) /// 物料编码 /// 组织编码 /// [HttpGet] [Route("SCBox/{str}/{number}")] public async Task SCBox([FromRoute] string str, [FromRoute] int number, [FromQuery] List materialNumbers, [FromQuery] string orgCode) { if (!_env.IsDevelopment()) return Result.ReSuccess(); if (materialNumbers == null) materialNumbers = new List(); var list = await GetList(str, number, materialNumbers, orgCode); await _boxService.Sync(list); return Result.ReSuccess(); } private async Task> GetList(string str, int number, List materialNumbers, string orgCode) { var materials_result = await _erpService.BillQueryForMaterial(); var materials = materials_result.Data.ToList(); List mIds = materials.Where(w => materialNumbers.Contains(w.MaterialNumber) && w.OrgCode == orgCode).Select(s => s.MaterialNumber).ToList(); int jn = 3; if (mIds.Count() <= 0) mIds = materials.Where(w => w.OrgCode == orgCode).Skip((new Random()).Next(1000, 10000)).Take(4).Select(s => s.MaterialNumber).ToList(); jn = mIds.Count() - 1; List list = new List(); for (int i = 0; i < number; i++) { List Details = new List(); for (int j = 0; j <= jn; j++) { var ran = (new Random()).Next(5, 10); List SerialNumbers = new List(); for (int g = 0; g < ran; g++) { OpsSerialNumbersResponse or = new OpsSerialNumbersResponse() { SerialNumber = await GetStr(), BarCereateUser = "01", BarCreateTime = DateTime.Now }; SerialNumbers.Add(or); } var mi = (new Random()).Next(1000, 20000); OpsBoxDetailsResponse d = new OpsBoxDetailsResponse() { MaterialNumber = mIds[j], Qty = ran, SerialNumbers = SerialNumbers }; Details.Add(d); } OpsBoxResponse response = new OpsBoxResponse() { BoxBillNo = str + ((new Random()).Next(500, 2000) + i).ToString(), OpsBoxId = 100 + i, CompleteCartonTime = DateTime.Now, CompleteCartonUser = "02", CreateTime = DateTime.Now, CreateUser = "01", OrgId = 0, SupplierId = 10897, Details = Details }; list.Add(response); } return list; } private Task GetStr() { var characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var Charsarr = new char[11]; var random = new Random(); for (int i = 0; i < Charsarr.Length; i++) { Charsarr[i] = characters[random.Next(characters.Length)]; } return Task.FromResult(new String(Charsarr)); } /// /// 测试:同步入库单 /// /// [HttpGet] [Route("InStock")] public async Task InStock() { if (!_env.IsDevelopment()) return Result.ReSuccess(); //3.同步数据 var result = await _inStockTaskService.Sysn(); return result; } /// /// 测试-接口是否正常 /// /// [HttpGet] [Route("lst")] public string Lst() { var successStr = "tongfei test lst->数据库链接地址:" + _options.DBConnectionString + ",单点项目链接地址:" + _options.SingleBaseUrl; return successStr; } /// /// 测试-单点接口是否正常 /// /// [HttpGet] [Route("lst1")] public async Task Lst1() { var result = await _basicsRepositories.GetUcStockAsync(ManagementSystemCode.GLXT0004.ToString(), "", 1); return JsonConvert.SerializeObject(result); } /// /// 测试-单点接口是否正常 /// /// [HttpGet] [Route("lst2")] public async Task Lst2() { var oldtoken = "3d046dc06a3f4f1ebcda93e921012c82"; var retoken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTEwNzIwNjksImlhdCI6MTcxMDk4NTY2OSwianRpIjoiMDA1NTFmM2ItNDQ5Ny00NzU1LTljNGQtNTNiODUxMDBjOTk5In0.XjHDfHhqvfap5MZPqTd45zi1L2_mgxu-lLnyqTKrhD0"; await _loginService.RefreshTokenNew(oldtoken, retoken); return ""; } /// /// 测试发送 /// /// [HttpGet] [Route("Send")] public async Task Send() { return await _sendMessageService.Execute(); } /// /// 同步新物料-和修改已启用批号管理的物料 /// /// [HttpGet] [Route("SyscMats")] public async Task SyscMats() { return await _materialService.SyncNewMaterials(); } /// /// /// /// [HttpGet] [Route("getmt")] public async Task GetMaterial() { var materials_result = await _erpService.BillQueryForMaterial(); var materials = materials_result.Data.ToList(); var tt = ""; return true; } /// /// 清空缓存 /// /// [HttpGet] [Route("ClearCache")] public Result ClearCache() { //通过反射调用清空缓存方法 PropertyInfo prop = _memoryCache.GetType().GetProperty("EntriesCollection", BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Public); object innerCache = prop.GetValue(_memoryCache); MethodInfo clearMethod = innerCache.GetType().GetMethod("Clear", BindingFlags.Instance | BindingFlags.Public); clearMethod.Invoke(innerCache, null); return Result.ReSuccess(); } /// /// /// /// [HttpGet] [Route("binfa")] public Result BinFa() { var cacheKey = "instock_shelf_" + 1; var IscanAccess = _redisConcurrentProcessService.GetRedisKeyValue(cacheKey); if (IscanAccess) return Result.ReFailure(ResultCodes.Concurrent_Instock); Console.WriteLine("process..." + IscanAccess); _redisConcurrentProcessService.UpdateAccessStatus(cacheKey, true); var tt2 = _redisConcurrentProcessService.GetRedisKeyValue(cacheKey); Console.WriteLine("redis1:" + tt2); return Result.ReSuccess(tt2); } [HttpGet] [Route("lst3")] public async Task lst3() { var data = await _erpService.BillQueryForPurchaseInStockBy("1541693", null); return true; } } }