using JNPF.Common.Core.Manager; using JNPF.Common.Dtos.VisualDev; using JNPF.Common.Enums; using JNPF.Common.Filter; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.Systems.Entitys.Permission; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; using JNPF.VisualDev.Entitys; using JNPF.VisualDev.Entitys.Dto.VisualDevModelData; using JNPF.VisualDev.Interfaces; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using NPOI.Util; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.EquipMgr.Entities; using Tnb.EquipMgr.Entities.Dto; using Tnb.EquipMgr.Interfaces; namespace Tnb.EquipMgr { /// /// 模具归还接口 /// [ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)] [Route("api/[area]/[controller]/[action]")] [OverideVisualDev(ModuleId)] public class ToolMoldReturnService : IOverideVisualDevService, IToolMoldReturnService, IDynamicApiController, ITransient { private const string ModuleId = "27275901607701"; private readonly ISqlSugarClient _db; private readonly IRunService _runService; private readonly IVisualDevService _visualDevService; private readonly IBillRullService _billRuleService; private readonly IUserManager _userManager; public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc(); public ToolMoldReturnService( ISqlSugarRepository repository, IRunService runService, IVisualDevService visualDevService, IBillRullService billRullService, IUserManager userManager ) { _db = repository.AsSugarClient(); _runService = runService; _visualDevService = visualDevService; OverideFuncs.CreateAsync = Create; _billRuleService= billRullService; _userManager = userManager; OverideFuncs.GetListAsync = GetList; } private async Task GetList(VisualDevModelListQueryInput input) { Dictionary? queryJson = new(); string? code = ""; DateTime[]? returntime = null; if (input != null && !string.IsNullOrEmpty(input.queryJson)) { queryJson = JsonConvert.DeserializeObject>(input?.queryJson ?? ""); } if (queryJson!.TryGetValue("code", out object? value)) { code = value.ToString(); } if (queryJson!.TryGetValue("return_time", out object? value1)) { returntime = value1.ToObject().Select(x => DateTimeOffset.FromUnixTimeSeconds(x / 1000).ToLocalTime().DateTime).ToArray(); } SqlSugarPagedList result = await _db.Queryable() .LeftJoin ((a, b) => a.location_id == b.id) .LeftJoin((a, b, c) => a.return_id == c.Id) .LeftJoin((a, b, c, d) => a.mold_id == d.id) .WhereIF(!string.IsNullOrEmpty(code), (a, b, c, d) => a.code.Contains(code)) .WhereIF(returntime != null, (a, b, c, d) => a.return_time >= returntime[0] && a.return_time <= returntime[1]) .Select((a, b, c, d) => new ToolMoldReturnListOutput { id = a.id, code = a.code, mold_id = d.mold_code, return_id = c.RealName, location_id = b.location_code, location_id_id = a.location_id, return_time = a.return_time == null ? "" : a.return_time.Value.ToString("yyyy-MM-dd"), remark = a.remark }).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(result); } private DateTime GetDateTime(long value) { DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); DateTime dateTime = unixStartTime.AddMilliseconds(value); return dateTime; } private async Task Create(VisualDevModelDataCrInput input) { string Code = await _billRuleService.GetBillNumber("moldReturn"); DbResult result = await _db.Ado.UseTranAsync(async () => { ToolMoldReturn toolMoldReturn = new ToolMoldReturn(); toolMoldReturn.code = Code; toolMoldReturn.mold_id = input.data[nameof(ToolMoldReturn.mold_id)].ToString(); toolMoldReturn.location_id = input.data[nameof(ToolMoldReturn.location_id)].ToString(); if (input.data[nameof(ToolMoldReturn.return_id)] != null) { toolMoldReturn.return_id = input.data[nameof(ToolMoldReturn.return_id)].ToString(); } if (input.data[nameof(ToolMoldReturn.return_time)] != null) { toolMoldReturn.return_time = GetDateTime(long.Parse(input.data[nameof(ToolMoldReturn.return_time)].ToString()!)); } if (input.data.ContainsKey(nameof(ToolMoldReturn.remark))) { toolMoldReturn.remark = input.data[nameof(ToolMoldReturn.remark)].ToString(); } toolMoldReturn.create_time = DateTime.Now; toolMoldReturn.create_id = _userManager.UserId; // VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleId, true); //await _runService.Create(templateEntity, input); await _db.Insertable(toolMoldReturn).ExecuteCommandAsync(); string ? locationId = ""; if (input.data.TryGetValue("location_id", out object? value)) { locationId = value.ToString(); } string? moldId = input.data.ContainsKey("mold_id") ? input.data["mold_id"].ToString() : ""; if (!string.IsNullOrEmpty(moldId)) { _ = await _db.Updateable() .SetColumns(x => x.mold_status == Tnb.BasicData.DictConst.ZKTypeId) .SetColumnsIF(!string.IsNullOrEmpty(locationId), x => x.location_id == locationId) .Where(X => X.id == moldId).ExecuteCommandAsync(); } }); return !result.IsSuccess ? throw Oops.Oh(ErrorCode.COM1008) : (dynamic)(result.IsSuccess ? "保存成功" : result.ErrorMessage); } } }