Commit d28e370a by zxw

修复装备统计内部装备数量查询错误

parent 765538cf
using Google.Protobuf.WellKnownTypes;
using JmpCommon;
using JmpCommon;
using JunmpPoliceStation.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace JunmpPoliceStation.App_Start
{
public class ActionLimitAttribute : ActionFilterAttribute
{
private ILogger<ActionLimitAttribute> _logger;
public static MemoryCache _cache = new MemoryCache(new MemoryCacheOptions());
public ActionLimitAttribute(ILogger<ActionLimitAttribute> logger)
{
_logger = logger;
......@@ -38,9 +34,9 @@ namespace JunmpPoliceStation.App_Start
}
var key = userHostAddress + url;
if (Exists(key))
if (MemoryCacheHelper.Exists(key))
{
int value = int.Parse(Get(key).ToString() ?? string.Empty);
int value = int.Parse(MemoryCacheHelper.Get(key).ToString() ?? string.Empty);
if (value > 25) //每分钟上限访问25次
{
filterContext.Result = new JsonResult(ResultCode.ACTION_LIMIT);
......@@ -50,11 +46,11 @@ namespace JunmpPoliceStation.App_Start
{
value += 1;
}
AddMemoryCache(key, value);
MemoryCacheHelper.AddMemoryCache(key, value);
}
else
{
AddMemoryCache(key, 1);
MemoryCacheHelper.AddMemoryCache(key, 1);
}
}
}
......@@ -76,59 +72,5 @@ namespace JunmpPoliceStation.App_Start
return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
/// <summary>
/// 验证缓存项是否存在
/// </summary>
/// <param name="key">缓存Key</param>
/// <returns></returns>
public static bool Exists(string key)
{
if (key == null)
{
return false;
}
return _cache.TryGetValue(key, out _);
}
/// <summary>
/// 获取缓存
/// </summary>
/// <param name="key">缓存Key</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static object Get(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (!Exists(key))
throw new ArgumentNullException(nameof(key));
return _cache.Get(key);
}
/// <summary>
/// 添加缓存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static bool AddMemoryCache(string key, object value)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
DateTimeOffset time = DateTimeOffset.Now.AddMinutes(1); //一分钟后过期
_cache.Set(key, value, time);
return Exists(key);
}
}
}
......@@ -732,7 +732,7 @@ namespace JunmpPoliceStation.Controllers
/// "detailList": [
/// {
/// "quantity": 1,
/// "equipmentId": "b9f83e07-a559-45d9-869f-22fbc777723c",
/// "equipmentDetailId": "b9f83e07-a559-45d9-869f-22fbc777723c",
/// }]
/// }
/// </remarks>
......
......@@ -8391,11 +8391,11 @@ namespace JunmpPoliceStation.Controllers
List<ViewEquipment> equipmentList = _unitOfWork.ViewEquipmentRepository.GetList(expressionPoliceman).OrderBy(c => c.FindCode.Length).ToList();
var data = equipmentList.GroupBy(t => new { t.WarehouseCode, t.EquipmentSizecode }).Select(c => new
var data = equipmentList.GroupBy(t => new { t.OrgId, t.WarehouseCode, t.EquipmentSizecode }).Select(c => new
{
c.FirstOrDefault().OrgId,
c.FirstOrDefault().WarehouseCode,
c.FirstOrDefault().WarehouseName,
WarehouseCode=c.FirstOrDefault(x => x.WarehouseCode != null)?.WarehouseCode ?? "00000000-0000-0000-0000-000000000000",
WarehouseName=c.FirstOrDefault(x => x.WarehouseCode != null)?.WarehouseName ?? "",
c.FirstOrDefault().EquipmentCode,
c.FirstOrDefault().EquipmentName,
c.FirstOrDefault().EquipmentSizecode,
......@@ -8404,7 +8404,7 @@ namespace JunmpPoliceStation.Controllers
zkCount = c.Where(f => f.CurrentState.Equals(0) || f.CurrentState.Equals(3)).Count(),
ckCount = c.Where(f => !(f.CurrentState == null || f.CurrentState.Equals(0) || f.CurrentState.Equals(3) || f.CurrentState.Equals(6))).Count(),
bfCount = c.Where(f => f.CurrentState.Equals(6)).Count()
}).Where(c => c.WarehouseCode != null).ToList();
}).Where(c => c.OrgId != null).ToList();
return JsonManager.ReturnSuccessResponse(data);
}
else
......@@ -8839,21 +8839,30 @@ namespace JunmpPoliceStation.Controllers
"EquipmentSizecodeNavigation" ,
"EquipmentCodeNavigation" ,
"WarehouseCodeNavigation" ,
"WarehouseCodeNavigation.Orgization",
"Org",
"CommonJpEquipmentStates",
"SupplierCodeNavigation",
"Cabinet",
"Shelf"
};
Expression<Func<CommonJpEquipmentInventory, bool>> expression = t => OrgId.Equals(t.WarehouseCodeNavigation.OrgizationId);
Expression<Func<CommonJpEquipmentInventory, bool>> expression = t => OrgId.Equals(t.OrgId);
expression = LambdaExtensions.AndAlso(expression, t => t.InventoryState != "loss");
//orgList.Add(orgInfo.Id);//查询所有组织机构仓库库存信息
if (!String.IsNullOrEmpty(entity.warehouseId))
{
string warehouseId = entity.warehouseId;
expression = LambdaExtensions.AndAlso(expression, t => t.WarehouseCode.Equals(warehouseId));
if (warehouseId == "00000000-0000-0000-0000-000000000000")
{
//无归属仓库
expression = LambdaExtensions.AndAlso(expression,
t => string.IsNullOrEmpty(t.WarehouseCode));
}
else
{
expression = LambdaExtensions.AndAlso(expression, t => t.WarehouseCode.Equals(warehouseId));
}
}
//装备型号
......@@ -8923,8 +8932,8 @@ namespace JunmpPoliceStation.Controllers
equipment.EquipmentCode,
warehouseName = equipment.WarehouseCodeNavigation?.Name,
equipment.WarehouseCode,
equipment.WarehouseCodeNavigation?.Orgization?.Name,
equipment.WarehouseCodeNavigation?.OrgizationId,
equipment.Org.Name,
equipment.OrgId,
supplierName = equipment.SupplierCodeNavigation?.Name,
equipment.SupplierCode,
cabinetID = equipment.Cabinet?.Id,
......
......@@ -19,6 +19,7 @@ using JmpCommon.Tools;
using System.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using JunmpPoliceStation.Extensions;
namespace JunmpPoliceStation.Controllers
{
......@@ -879,6 +880,8 @@ namespace JunmpPoliceStation.Controllers
if (result)
{
//清除组织机构缓存
MemoryCacheHelper.RemoveMemoryCache("orgModelList");
return JsonManager.SimpleStatusResponse(ResultCode.OPERATE_SUCCESS);
}
else
......
......@@ -20,6 +20,7 @@ using System.Net.WebSockets;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Internal;
using JmpModel.TempModel;
using JunmpPoliceStation.Extensions;
namespace JunmpPoliceStation.Controllers
{
......@@ -590,23 +591,40 @@ namespace JunmpPoliceStation.Controllers
size = 10;
}
Expression<Func<BaseJpOrganization, bool>> expression = t => t.State == 1;
ListEq = _unitOfWork.OrganizationRepository.GetList(expression, t => t.Code).ToList();
string orgizationCode = Guid.Empty.ToString();
List<BaseJpOrganization> dataList = null;
if (!String.IsNullOrEmpty(entity.orgId))
string orgId = entity.orgId;
if (string.IsNullOrEmpty(orgId))
{
orgizationCode = entity.orgId;
return JsonManager.SimpleCustResponse($"orgId is require");
}
var org = _unitOfWork.DbContext.BaseJpOrganizations.FirstOrDefault(x => x.Id == orgId);
if (org == null)
{
return JsonManager.SimpleCustResponse($"orgId is require");
}
var findcode = ListEq.Where(c => c.Id.Equals(orgizationCode)).FirstOrDefault().FindCode;
var orgModelList = new List<OrgTreeModel>();
if (MemoryCacheHelper.Exists("orgModelList"))
{
orgModelList = (List<OrgTreeModel>)MemoryCacheHelper.Get("orgModelList");
}
else
{
orgModelList = _unitOfWork.OrganizationRepository
.Entities
.Select(x => new OrgTreeModel
{
Id = x.Id,
State = x.State == 1,
Name = x.Name,
Code = x.Code,
ParentId = x.ParentId
})
.ToList();
dataList = GetClassID(orgizationCode);
//默认过期时间1天
MemoryCacheHelper.AddMemoryCache("orgModelList", orgModelList, 1440);
}
dataList = dataList.ToList().Concat(ListEq.Where(c => c.Id.Equals(orgizationCode))).ToList();
Expression<Func<ViewEquipment, bool>> expressionPoliceman = null;
......@@ -614,9 +632,8 @@ namespace JunmpPoliceStation.Controllers
string equipmentCode = entity.equipmentCode;
string equipmentSizecode = entity.equipmentSizecode;
List<string> IdList = dataList.Select(c => c.Id).ToList();
expressionPoliceman = t => IdList.Contains(t.OrgId);
var idList = OrgTreeModel.GetOrgIdList(orgModelList, org.Id);
expressionPoliceman = t => idList.Contains(t.OrgId);
if (!String.IsNullOrEmpty(typeId))
{
......@@ -635,7 +652,7 @@ namespace JunmpPoliceStation.Controllers
List<ViewEquipment> equipmentList = _unitOfWork.ViewEquipmentRepository.GetList(expressionPoliceman).OrderBy(c => c.FindCode.Length).ToList();
if (findcode.Length == 6)
if (org.FindCode.Length == 6)
{
var data = equipmentList.GroupBy(t => t.OrgName).Select(c => new
{
......@@ -679,7 +696,7 @@ namespace JunmpPoliceStation.Controllers
}
else
{
var codeLen = findcode.Length + 2;
var codeLen = org.FindCode.Length + 2;
var data = equipmentList.GroupBy(t => t.FindCode.PadLeft(codeLen, '0').Substring(0, codeLen)).Select(c => new
{
OrgName = c.OrderBy(f => f.OrgCode).FirstOrDefault().AreaName,
......
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using JmpModel.Model;
namespace JunmpPoliceStation.Extensions
{
public class MemoryCacheHelper
{
public static MemoryCache _cache = new MemoryCache(new MemoryCacheOptions());
/// <summary>
/// 验证缓存项是否存在
/// </summary>
/// <param name="key">缓存Key</param>
/// <returns></returns>
public static bool Exists(string key)
{
if (key == null)
{
return false;
}
return _cache.TryGetValue(key, out _);
}
/// <summary>
/// 获取缓存
/// </summary>
/// <param name="key">缓存Key</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static object Get(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (!Exists(key))
throw new ArgumentNullException(nameof(key));
return _cache.Get(key);
}
/// <summary>
/// 添加缓存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static bool AddMemoryCache(string key, object value, int minutes = 1)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
DateTimeOffset time = DateTimeOffset.Now.AddMinutes(minutes); //一分钟后过期
_cache.Set(key, value, time);
return Exists(key);
}
/// <summary>
/// 添加缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public static bool RemoveMemoryCache(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key));
}
_cache.Remove(key);
return !Exists(key);
}
}
public class OrgTreeModel
{
public OrgTreeModel()
{
Children = new List<OrgTreeModel>();
}
public string Id { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public bool State { get; set; }
public string ParentId { get; set; }
public List<OrgTreeModel> Children { get; set; }
/// <summary>
/// 获取组织机构本级及下级idList
/// </summary>
/// <param name="allList"></param>
/// <returns></returns>
public static List<string> GetOrgIdList(List<OrgTreeModel> allList, string id)
{
var top = allList.FirstOrDefault(x => x.Id == id);
if (top == null)
{
return new List<string>();
}
var strList = new List<string>();
if (top.State)
{
strList.Add(top.Id);
}
TraversalOrgTree(allList, allList.Where(x => x.ParentId == top.Id).ToList(), ref strList);
return strList;
}
/// <summary>
/// 遍历
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
private static void TraversalOrgTree(List<OrgTreeModel> allList, List<OrgTreeModel> list, ref List<string> strList)
{
if (list.Count == 0)
{
return;
}
foreach (var org in list)
{
if (org.State)
{
strList.Add(org.Id);
}
TraversalOrgTree(allList, allList.Where(x => x.ParentId == org.Id).ToList(), ref strList);
}
return;
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论