390 lines
13 KiB
C#
390 lines
13 KiB
C#
using Atomx.Admin.Client.Models;
|
|
using Atomx.Admin.Services;
|
|
using Atomx.Common.Constants;
|
|
using Atomx.Common.Entities;
|
|
using Atomx.Common.Models;
|
|
using Atomx.Core.Jos;
|
|
using Atomx.Data;
|
|
using Atomx.Data.CacheServices;
|
|
using Atomx.Data.Services;
|
|
using FluentValidation;
|
|
using MapsterMapper;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Localization;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Atomx.Admin.Controllers
|
|
{
|
|
/// <summary>
|
|
/// 国家地区省份API
|
|
/// </summary>
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
[Authorize]
|
|
public class AreaController : ControllerBase
|
|
{
|
|
private readonly ILogger<AreaController> _logger;
|
|
private readonly DataContext _dbContext;
|
|
private readonly IIdCreatorService _idCreator;
|
|
private readonly IIdentityService _identityService;
|
|
private readonly IMapper _mapper;
|
|
private readonly ICacheService _cacheService;
|
|
private readonly IBackgroundJobService _backgroundService;
|
|
readonly IValidator<AreaModel> _validator;
|
|
readonly IStringLocalizer<AreaController> _localizer;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="logger"></param>
|
|
/// <param name="idCreator"></param>
|
|
/// <param name="identityService"></param>
|
|
/// <param name="dbContext"></param>
|
|
/// <param name="mapper"></param>
|
|
/// <param name="cacheService"></param>
|
|
/// <param name="backgroundJobService"></param>
|
|
/// <param name="validator"></param>
|
|
/// <param name="localizer"></param>
|
|
public AreaController(ILogger<AreaController> logger, IIdCreatorService idCreator, IIdentityService identityService, DataContext dbContext, IMapper mapper,
|
|
ICacheService cacheService, IBackgroundJobService backgroundJobService, IValidator<AreaModel> validator, IStringLocalizer<AreaController> localizer)
|
|
{
|
|
_logger = logger;
|
|
_idCreator = idCreator;
|
|
_identityService = identityService;
|
|
_dbContext = dbContext;
|
|
_mapper = mapper;
|
|
_cacheService = cacheService;
|
|
_backgroundService = backgroundJobService;
|
|
_validator = validator;
|
|
_localizer = localizer;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取省份-城市树形数据
|
|
/// </summary>
|
|
/// <param name="search"></param>
|
|
/// <param name="page"></param>
|
|
/// <param name="size"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("tree/{countryId}")]
|
|
public async Task<IActionResult> GetAreatree(long countryId)
|
|
{
|
|
var result = new ApiResult<List<KeyValueTree>>();
|
|
var list = await _cacheService.GetAreaTree(countryId);
|
|
return new JsonResult(result.IsSuccess(list));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据查询
|
|
/// </summary>
|
|
/// <param name="search"></param>
|
|
/// <param name="page"></param>
|
|
/// <param name="size"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("stateprovince/search")]
|
|
public async Task<IActionResult> StateProvinceSearch(AreaSearch search, int page, int size = 20)
|
|
{
|
|
if (page < 1)
|
|
{
|
|
page = 1;
|
|
}
|
|
var result = new ApiResult<PagingList<AreaItem>>();
|
|
var list = new PagingList<AreaItem>() { Index = page, Size = size };
|
|
var countries = new List<Country>();
|
|
|
|
var query = from p in _dbContext.Areas
|
|
where p.StateProvinceId == 0
|
|
select p;
|
|
|
|
if (!string.IsNullOrEmpty(search.Name))
|
|
{
|
|
query = from p in query
|
|
where p.Name.Contains(search.Name)
|
|
select p;
|
|
}
|
|
|
|
if (search.CountryId > 0)
|
|
{
|
|
query = from p in query
|
|
where p.CountryId == search.CountryId
|
|
select p;
|
|
}
|
|
|
|
list.Count = query.Count();
|
|
var data = query.OrderByDescending(p => p.DisplayOrder).Skip((page - 1) * size).Take(size).ToList();
|
|
|
|
if (search.CountryId > 0)
|
|
{
|
|
var country = _dbContext.Countries.SingleOrDefault(p => p.Id == search.CountryId);
|
|
if (country != null)
|
|
{
|
|
countries.Add(country);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var countryIds = data.Select(p => p.CountryId).Distinct().ToList();
|
|
countries = _dbContext.Countries.Where(p => countryIds.Contains(p.Id)).ToList();
|
|
}
|
|
|
|
foreach (var item in data)
|
|
{
|
|
var model = _mapper.Map<AreaItem>(item);
|
|
var country = countries.SingleOrDefault(p => p.Id == item.CountryId);
|
|
if (country != null)
|
|
{
|
|
model.CountryName = country.Name;
|
|
}
|
|
var state = await _cacheService.GetStateProvince(item.CountryId, item.ParentId);
|
|
if (state != null)
|
|
{
|
|
model.StateProvinceName = state.Name;
|
|
}
|
|
list.Items.Add(model);
|
|
}
|
|
|
|
result = result.IsSuccess(list);
|
|
|
|
return new JsonResult(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据查询
|
|
/// </summary>
|
|
/// <param name="search"></param>
|
|
/// <param name="page"></param>
|
|
/// <param name="size"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("search")]
|
|
public async Task<IActionResult> SearchAsync(AreaSearch search, int page, int size = 20)
|
|
{
|
|
if (page < 1)
|
|
{
|
|
page = 1;
|
|
}
|
|
var result = new ApiResult<PagingList<AreaItem>>();
|
|
var list = new PagingList<AreaItem>() { Index = page, Size = size };
|
|
var countries = new List<Country>();
|
|
|
|
var query = from p in _dbContext.Areas
|
|
where p.StateProvinceId != 0
|
|
select p;
|
|
|
|
if (!string.IsNullOrEmpty(search.Name))
|
|
{
|
|
query = from p in query
|
|
where p.Name.Contains(search.Name)
|
|
select p;
|
|
}
|
|
|
|
if (search.CountryId > 0)
|
|
{
|
|
query = from p in query
|
|
where p.CountryId == search.CountryId
|
|
select p;
|
|
}
|
|
if (search.StateProvinceId > 0)
|
|
{
|
|
query = from p in query
|
|
where p.ParentId == search.StateProvinceId
|
|
select p;
|
|
}
|
|
list.Count = query.Count();
|
|
var data = query.OrderByDescending(p => p.DisplayOrder).Skip((page - 1) * size).Take(size).ToList();
|
|
|
|
if (search.CountryId > 0)
|
|
{
|
|
var country = _dbContext.Countries.SingleOrDefault(p => p.Id == search.CountryId);
|
|
if (country != null)
|
|
{
|
|
countries.Add(country);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var countryIds = data.Select(p => p.CountryId).Distinct().ToList();
|
|
countries = _dbContext.Countries.Where(p => countryIds.Contains(p.Id)).ToList();
|
|
}
|
|
|
|
foreach (var item in data)
|
|
{
|
|
var model = _mapper.Map<AreaItem>(item);
|
|
var country = countries.SingleOrDefault(p => p.Id == item.CountryId);
|
|
if (country != null)
|
|
{
|
|
model.CountryName = country.Name;
|
|
}
|
|
var state = await _cacheService.GetStateProvince(item.CountryId, item.ParentId);
|
|
if (state != null)
|
|
{
|
|
model.StateProvinceName = state.Name;
|
|
}
|
|
list.Items.Add(model);
|
|
}
|
|
|
|
result = result.IsSuccess(list);
|
|
|
|
return new JsonResult(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过ID获取数据
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{id:long}")]
|
|
public IActionResult Get(long id)
|
|
{
|
|
var result = new ApiResult<Area>();
|
|
var data = _dbContext.Areas.SingleOrDefault(p => p.Id == id);
|
|
if (data == null)
|
|
{
|
|
return new JsonResult(new ApiResult<string>().IsFail("数据不存在", null));
|
|
}
|
|
|
|
result = result.IsSuccess(data);
|
|
return new JsonResult(result);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 通过ID获取详情,含多语言
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("detail")]
|
|
public IActionResult Detail(long id)
|
|
{
|
|
var result = new ApiResult<AreaLocalizedModel>();
|
|
var data = _dbContext.Areas.SingleOrDefault(p => p.Id == id);
|
|
if (data == null)
|
|
{
|
|
return new JsonResult(new ApiResult<string>().IsFail("数据不存在", null));
|
|
}
|
|
var localizedList = _dbContext.LocalizedProperties.Where(p => p.EntityId == id).ToList();
|
|
var model = _mapper.Map<AreaLocalizedModel>(data);
|
|
model.Locales = localizedList;
|
|
|
|
result = result.IsSuccess(model);
|
|
|
|
return new JsonResult(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增编辑数据
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("save")]
|
|
public IActionResult Edit(AreaModel model)
|
|
{
|
|
var result = new ApiResult<bool>();
|
|
var validation = _validator.Validate(model);
|
|
if (!validation.IsValid)
|
|
{
|
|
var message = validation.Errors.FirstOrDefault()?.ErrorMessage ?? string.Empty;
|
|
return new JsonResult(new ApiResult<string>().IsFail(message, null));
|
|
}
|
|
var data = _dbContext.Areas.SingleOrDefault(p => p.CountryId == model.CountryId && p.ParentId == model.ParentId && p.Name == model.Name && p.Id != model.Id);
|
|
if (data != null)
|
|
{
|
|
return new JsonResult(new ApiResult<string>().IsFail("当前站点语言下已经存在这个配置,请认真检查", null));
|
|
}
|
|
if (model.Id > 0)
|
|
{
|
|
data = _dbContext.Areas.SingleOrDefault(p => p.Id == model.Id);
|
|
if (data == null)
|
|
{
|
|
return new JsonResult(new ApiResult<string>().IsFail("数据不存在", null));
|
|
}
|
|
|
|
data = _mapper.Map(model, data);
|
|
|
|
var parent = _dbContext.Areas.Where(p => p.Id == model.ParentId).SingleOrDefault();
|
|
if (parent == null)
|
|
{
|
|
data.Depth = 0;
|
|
data.Path = model.Id.ToString();
|
|
}
|
|
else
|
|
{
|
|
if (parent.StateProvinceId == 0)
|
|
{
|
|
data.StateProvinceId = parent.Id;
|
|
}
|
|
else
|
|
{
|
|
data.StateProvinceId = parent.StateProvinceId;
|
|
}
|
|
|
|
data.Depth = parent.Depth + 1;
|
|
data.Path = $"{parent.Path},{data.Id}";
|
|
}
|
|
|
|
_dbContext.SaveChanges();
|
|
|
|
}
|
|
else
|
|
{
|
|
data = _mapper.Map<Area>(model);
|
|
data.Id = _idCreator.CreateId();
|
|
|
|
var parent = _dbContext.Areas.Where(p => p.Id == data.ParentId).SingleOrDefault();
|
|
if (parent == null)
|
|
{
|
|
data.Depth = 0;
|
|
data.Path = data.Id.ToString();
|
|
}
|
|
else
|
|
{
|
|
if (parent.StateProvinceId == 0)
|
|
{
|
|
data.StateProvinceId = parent.Id;
|
|
}
|
|
else
|
|
{
|
|
data.StateProvinceId = parent.StateProvinceId;
|
|
}
|
|
data.Depth = parent.Depth + 1;
|
|
data.Path = $"{parent.Path},{data.Id}";
|
|
}
|
|
|
|
_dbContext.Areas.Add(data);
|
|
_dbContext.SaveChanges();
|
|
}
|
|
|
|
_backgroundService.ResetStateProvinceAndAreaTree(model.CountryId);
|
|
|
|
|
|
return new JsonResult(new ApiResult<string>().IsSuccess("操作成功"));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除数据
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
|
|
[HttpPost("delete")]
|
|
public async Task<IActionResult> DeleteAsync(long id)
|
|
{
|
|
var result = new ApiResult<string>();
|
|
try
|
|
{
|
|
Console.WriteLine($"{id} deleted");
|
|
var count = _dbContext.Areas.Where(p => p.Id == id).ExecuteDelete();
|
|
result = result.IsSuccess(count.ToString());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result = result.IsFail(ex.Message);
|
|
_logger.LogError(ex.Message);
|
|
}
|
|
return new JsonResult(result);
|
|
}
|
|
|
|
}
|
|
}
|