fixed 完善收货地址逻辑
This commit is contained in:
parent
a65cfcded6
commit
bb5b4d0b8a
@ -28,8 +28,42 @@ namespace ARW.Model.Dto.Api.Custom.CustomerAddresses
|
||||
/// </summary>
|
||||
public class CustomerAddressDtoApi
|
||||
{
|
||||
[Required(ErrorMessage = "CustomerAddressGuid不能为空")]
|
||||
[Required(ErrorMessage = "CustomerAddressId不能为空")]
|
||||
public long CustomerAddressId { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class CustomerAddressAddDtoApi
|
||||
{
|
||||
|
||||
public int CustomerAddressId { get; set; }
|
||||
|
||||
public long CustomerAddressGuid { get; set; }
|
||||
|
||||
public long CustomerAddressCustomerGuid { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "省编码不能为空")]
|
||||
public string ProvinceCode { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "市编码不能为空")]
|
||||
public string CityCode { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "区编码不能为空")]
|
||||
public string DistrictCode { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "收货人名称不能为空")]
|
||||
public string CustomerAddressName { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "收货电话不能为空")]
|
||||
public string CustomerAddressPhone { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "详细地址不能为空")]
|
||||
public string CustomerAddressDetailed { get; set; }
|
||||
|
||||
[Required(ErrorMessage = "是否默认地址不能为空")]
|
||||
public int IsDefault { get; set; }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,7 +14,6 @@ namespace ARW.Model.Dto.Api.Custom.Regions
|
||||
/// </summary>
|
||||
public class RegionQueryDtoApi : PagerInfo
|
||||
{
|
||||
public string RegionName { get; set; }
|
||||
}
|
||||
|
||||
|
||||
|
@ -25,7 +25,7 @@ namespace ARW.Model.Vo.Api.Custom.CustomerAddresses
|
||||
/// 描述 : guid
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(ValueToStringConverter))]
|
||||
[EpplusIgnore]
|
||||
[EpplusIgnore]
|
||||
public long CustomerAddressGuid { get; set; }
|
||||
|
||||
|
||||
@ -54,31 +54,69 @@ namespace ARW.Model.Vo.Api.Custom.CustomerAddresses
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// 客户收货地址详情展示对象Api
|
||||
/// </summary>
|
||||
public class CustomerAddressApiDetailsVo
|
||||
{
|
||||
[EpplusIgnore]
|
||||
public int CustomerAddressId { get; set; }
|
||||
[JsonConverter(typeof(ValueToStringConverter))]
|
||||
[EpplusIgnore]
|
||||
|
||||
[JsonConverter(typeof(ValueToStringConverter))]
|
||||
public long CustomerAddressGuid { get; set; }
|
||||
[JsonConverter(typeof(ValueToStringConverter))]
|
||||
[EpplusTableColumn(Header = "客户guid")]
|
||||
public long CustomerAddressCustomerGuid { get; set; }
|
||||
[EpplusTableColumn(Header = "省id")]
|
||||
public int CustomerAddressProvinceId { get; set; }
|
||||
[EpplusTableColumn(Header = "市id")]
|
||||
public int CustomerAddressCityId { get; set; }
|
||||
[EpplusTableColumn(Header = "区id")]
|
||||
public int CustomerAddressAreaId { get; set; }
|
||||
[EpplusTableColumn(Header = "收货人名称")]
|
||||
public string CustomerAddressName { get; set; }
|
||||
[EpplusTableColumn(Header = "收货电话")]
|
||||
public string CustomerAddressPhone { get; set; }
|
||||
[EpplusTableColumn(Header = "详细地址")]
|
||||
public string CustomerAddressDetailed { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 省名称
|
||||
/// </summary>
|
||||
public string ProvinceName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 省编码
|
||||
/// </summary>
|
||||
public string ProvinceCode { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 市名称
|
||||
/// </summary>
|
||||
public string CityName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 市编码
|
||||
/// </summary>
|
||||
public string CityCode { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 区名称
|
||||
/// </summary>
|
||||
public string DistrictName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 区编码
|
||||
/// </summary>
|
||||
public string DistrictCode { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 收货人名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 收货电话
|
||||
/// </summary>
|
||||
public string Phone { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 详细地址
|
||||
/// </summary>
|
||||
public string DetailAddress { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 是否为默认地址
|
||||
/// </summary>
|
||||
public bool IsDefault { get; set; }
|
||||
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@ using ARW.Model.Vo.Api.Custom.CustomerAddresses;
|
||||
using ARW.Model.Models.Business.Custom.Regions;
|
||||
using ARW.Model.Models.Business.Custom.Customers;
|
||||
using Microsoft.AspNetCore.Server.IISIntegration;
|
||||
using ARW.Repository.Business.Custom.Customers;
|
||||
|
||||
namespace ARW.Service.Api.BusinessService.Custom.CustomerAddresses
|
||||
{
|
||||
@ -29,10 +30,12 @@ namespace ARW.Service.Api.BusinessService.Custom.CustomerAddresses
|
||||
public class CustomerAddressServiceImplApi : BaseService<CustomerAddress>, ICustomerAddressServiceApi
|
||||
{
|
||||
private readonly CustomerAddressRepository _CustomerAddressRepository;
|
||||
private readonly CustomerRepository _CustomerRepository;
|
||||
|
||||
public CustomerAddressServiceImplApi(CustomerAddressRepository CustomerAddressRepository)
|
||||
public CustomerAddressServiceImplApi(CustomerAddressRepository CustomerAddressRepository, CustomerRepository customerRepository)
|
||||
{
|
||||
this._CustomerAddressRepository = CustomerAddressRepository;
|
||||
_CustomerRepository = customerRepository;
|
||||
}
|
||||
|
||||
#region Api接口代码
|
||||
@ -81,22 +84,25 @@ namespace ARW.Service.Api.BusinessService.Custom.CustomerAddresses
|
||||
|
||||
var query = _CustomerAddressRepository
|
||||
.Queryable()
|
||||
.LeftJoin<Region>((s, c) => s.CustomerAddressProvinceId == c.RegionPid)
|
||||
.LeftJoin<Region>((s, c, d) => s.CustomerAddressCityId == d.RegionPid)
|
||||
.LeftJoin<Region>((s, c, d, f) => s.CustomerAddressAreaId == f.RegionPid)
|
||||
.LeftJoin<Region>((s, c) => s.CustomerAddressProvinceId == c.RegionId)
|
||||
.LeftJoin<Region>((s, c, d) => s.CustomerAddressCityId == d.RegionId)
|
||||
.LeftJoin<Region>((s, c, d, f) => s.CustomerAddressAreaId == f.RegionId)
|
||||
.LeftJoin<Customer>((s, c, d, f, g) => s.CustomerAddressCustomerGuid == g.CustomerGuid)
|
||||
.Where(s => s.CustomerAddressGuid == parm.CustomerAddressGuid)
|
||||
.Select(s => new CustomerAddressApiDetailsVo
|
||||
.Where(s => s.CustomerAddressId == parm.CustomerAddressId)
|
||||
.Select((s, c, d, f, g) => new CustomerAddressApiDetailsVo
|
||||
{
|
||||
CustomerAddressId = s.CustomerAddressId,
|
||||
CustomerAddressGuid = s.CustomerAddressGuid,
|
||||
CustomerAddressCustomerGuid = s.CustomerAddressCustomerGuid,
|
||||
CustomerAddressProvinceId = s.CustomerAddressProvinceId,
|
||||
CustomerAddressCityId = s.CustomerAddressCityId,
|
||||
CustomerAddressAreaId = s.CustomerAddressAreaId,
|
||||
CustomerAddressName = s.CustomerAddressName,
|
||||
CustomerAddressPhone = s.CustomerAddressPhone,
|
||||
CustomerAddressDetailed = s.CustomerAddressDetailed,
|
||||
ProvinceName = c.RegionName,
|
||||
ProvinceCode = c.RegionCode,
|
||||
CityName = d.RegionName,
|
||||
CityCode = d.RegionCode,
|
||||
DistrictName = f.RegionName,
|
||||
DistrictCode = f.RegionCode,
|
||||
Name = s.CustomerAddressName,
|
||||
Phone = s.CustomerAddressPhone,
|
||||
DetailAddress = s.CustomerAddressDetailed,
|
||||
IsDefault = s.CustomerAddressGuid == g.CustomerDefaultAddressGuid
|
||||
}).Take(1);
|
||||
|
||||
|
||||
@ -104,6 +110,61 @@ namespace ARW.Service.Api.BusinessService.Custom.CustomerAddresses
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加或修改客户收货地址
|
||||
/// </summary>
|
||||
public async Task<string> AddOrUpdateCustomerAddress(CustomerAddress model, int IsDefault)
|
||||
{
|
||||
if (model.CustomerAddressId != 0)
|
||||
{
|
||||
var response = await _CustomerAddressRepository.UpdateAsync(model);
|
||||
await HandleDefaultAddress(model, IsDefault, model.CustomerAddressGuid);
|
||||
return "修改成功!";
|
||||
}
|
||||
else
|
||||
{
|
||||
var response = await _CustomerAddressRepository.InsertReturnSnowflakeIdAsync(model);
|
||||
await HandleDefaultAddress(model, IsDefault, response);
|
||||
return "添加成功!";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 默认收货地址处理
|
||||
/// </summary>
|
||||
/// <param name="model">CustomerAddress模型</param>
|
||||
/// <param name="IsDefault">是否为默认地址</param>
|
||||
/// <param name="defaultAddressGuid">默认地址Guid</param>
|
||||
/// <returns></returns>
|
||||
public async Task HandleDefaultAddress(CustomerAddress model, int IsDefault, long defaultAddressGuid)
|
||||
{
|
||||
// 修改客户的默认地址guid
|
||||
if (IsDefault == 1)
|
||||
{
|
||||
await _CustomerRepository.UpdateAsync(f => new Customer
|
||||
{
|
||||
CustomerDefaultAddressGuid = defaultAddressGuid
|
||||
}, s => s.CustomerGuid == model.CustomerAddressCustomerGuid);
|
||||
}
|
||||
else if (IsDefault == 0)
|
||||
{
|
||||
// 找到当前客户的默认收货地址
|
||||
var customerDefaultAddress = await _CustomerRepository.GetFirstAsync(s => s.CustomerGuid == model.CustomerAddressCustomerGuid);
|
||||
var customerDefaultAddressGuid = customerDefaultAddress.CustomerDefaultAddressGuid;
|
||||
// 当前的收货地址guid如果与客户的默认收货地址相同
|
||||
if (customerDefaultAddressGuid != 0 && model.CustomerAddressGuid == customerDefaultAddressGuid)
|
||||
{
|
||||
// 重新设置为Null值
|
||||
await _CustomerRepository.UpdateAsync(f => new Customer
|
||||
{
|
||||
CustomerDefaultAddressGuid = null
|
||||
}, s => s.CustomerGuid == model.CustomerAddressCustomerGuid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
@ -32,5 +32,13 @@ namespace ARW.Service.Api.IBusinessService.Custom.CustomerAddresses
|
||||
/// <returns></returns>
|
||||
Task<string> GetCustomerAddressDetails(CustomerAddressDtoApi parm);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加或修改客户收货地址
|
||||
/// </summary>
|
||||
/// <param name="parm"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> AddOrUpdateCustomerAddress(CustomerAddress parm, int IsDefault);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -127,7 +127,7 @@ namespace ARW.Service.Business.BusinessService.Custom.Regions
|
||||
public async Task<int> GetRegionId(string RegionCode, int level)
|
||||
{
|
||||
var region = await _RegionRepository.GetFirstAsync(s => s.RegionCode == RegionCode && s.RegionLevel == level);
|
||||
if (region == null) throw new Exception("编码不正确");
|
||||
if (region == null) throw new Exception(RegionCode + "编码不正确");
|
||||
return region.RegionId;
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,10 @@ using ARW.Model.Vo.Api.Custom.CustomerAddresses;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Geocoding;
|
||||
using ARW.Admin.WebApi.Framework;
|
||||
using ARW.Model.Dto.Business.Custom.CustomerAddresses;
|
||||
using ARW.Service.Business.IBusinessService.Custom.CustomerAddresses;
|
||||
using Microsoft.AspNetCore.Server.IISIntegration;
|
||||
using ARW.Service.Business.IBusinessService.Custom.Regions;
|
||||
|
||||
namespace ARW.WebApi.Controllers.Api.Custom.CustomerAddresses
|
||||
{
|
||||
@ -30,14 +34,17 @@ namespace ARW.WebApi.Controllers.Api.Custom.CustomerAddresses
|
||||
public class CustomerAddressApiController : BaseController
|
||||
{
|
||||
private readonly ICustomerAddressServiceApi _CustomerAddressServiceApi;
|
||||
private readonly IRegionService _RegionService;
|
||||
|
||||
/// <summary>
|
||||
/// 依赖注入
|
||||
/// </summary>
|
||||
/// <param name="CustomerAddressServiceApi">客户收货地址客户收货地址Api服务</param>
|
||||
public CustomerAddressApiController(ICustomerAddressServiceApi CustomerAddressServiceApi)
|
||||
/// <param name="regionService">省市区服务</param>
|
||||
public CustomerAddressApiController(ICustomerAddressServiceApi CustomerAddressServiceApi, IRegionService regionService)
|
||||
{
|
||||
_CustomerAddressServiceApi = CustomerAddressServiceApi;
|
||||
_RegionService = regionService;
|
||||
}
|
||||
|
||||
|
||||
@ -80,5 +87,34 @@ namespace ARW.WebApi.Controllers.Api.Custom.CustomerAddresses
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加或修改客户收货地址
|
||||
/// </summary>
|
||||
/// <param name="parm"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("addOrUpdateCustomerAddress")]
|
||||
[Log(Title = "添加或修改客户收货地址", BusinessType = BusinessType.ADDORUPDATE)]
|
||||
public async Task<IActionResult> AddOrUpdateCustomerAddress([FromBody] CustomerAddressAddDtoApi parm)
|
||||
{
|
||||
if (parm == null) { throw new CustomException("请求参数错误"); }
|
||||
|
||||
var modal = new CustomerAddress();
|
||||
if (parm.CustomerAddressId != 0) modal = parm.Adapt<CustomerAddress>().ToUpdate(HttpContext);
|
||||
else modal = parm.Adapt<CustomerAddress>().ToCreate(HttpContext);
|
||||
|
||||
// 字段转换
|
||||
var user = JwtUtil.GetLoginUser(App.HttpContext);
|
||||
modal.CustomerAddressCustomerGuid = user.UserId;
|
||||
|
||||
modal.CustomerAddressProvinceId = await _RegionService.GetRegionId(parm.ProvinceCode,1);
|
||||
modal.CustomerAddressCityId = await _RegionService.GetRegionId(parm.CityCode,2);
|
||||
modal.CustomerAddressAreaId = await _RegionService.GetRegionId(parm.DistrictCode,3);
|
||||
|
||||
|
||||
var res = await _CustomerAddressServiceApi.AddOrUpdateCustomerAddress(modal,parm.IsDefault);
|
||||
return SUCCESS(res);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user