emoticon_api/ARW.Service/Api/BusinessService/Custom/CustomerAddresses/CustomerAddressServiceApi.cs
2023-09-22 22:08:32 +08:00

176 lines
7.4 KiB
C#

using Infrastructure.Attribute;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ARW.Model;
using ARW.Repository;
using ARW.Repository.Business.Custom.CustomerAddresses;
using ARW.Service.Api.IBusinessService.Custom.CustomerAddresses;
using ARW.Model.Dto.Api.Custom.CustomerAddresses;
using ARW.Model.Models.Business.Custom.CustomerAddresses;
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
{
/// <summary>
/// 客户收货地址接口实现类Api
///
/// @author admin
/// @date 2023-06-09
/// </summary>
[AppService(ServiceType = typeof(ICustomerAddressServiceApi), ServiceLifetime = LifeTime.Transient)]
public class CustomerAddressServiceImplApi : BaseService<CustomerAddress>, ICustomerAddressServiceApi
{
private readonly CustomerAddressRepository _CustomerAddressRepository;
private readonly CustomerRepository _CustomerRepository;
public CustomerAddressServiceImplApi(CustomerAddressRepository CustomerAddressRepository, CustomerRepository customerRepository)
{
this._CustomerAddressRepository = CustomerAddressRepository;
_CustomerRepository = customerRepository;
}
#region Api接口代码
/// <summary>
/// 查询客户收货地址列表(Api)
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public async Task<List<CustomerAddressVoApi>> GetCustomerAddressListApi(CustomerAddressQueryDtoApi parm)
{
//开始拼装查询条件d
var predicate = Expressionable.Create<CustomerAddress>();
var query = _CustomerAddressRepository
.Queryable()
.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.CustomerAddressCustomerGuid == parm.CustomerAddressCustomerGuid)
.Where(predicate.ToExpression())
.OrderBy(s => s.Create_time, OrderByType.Asc)
.Select((s, c, d, f, g) => new CustomerAddressVoApi
{
CustomerAddressId = s.CustomerAddressId,
CustomerAddressGuid = s.CustomerAddressGuid,
Name = s.CustomerAddressName,
PhoneNumber = s.CustomerAddressPhone,
ProvinceName = c.RegionName,
CityName = d.RegionName,
DistrictName = f.RegionName,
Address = c.RegionName + d.RegionName + f.RegionName + s.CustomerAddressDetailed,
DetaiaddresslAddress = s.CustomerAddressDetailed,
IsDefault = s.CustomerAddressGuid == g.CustomerDefaultAddressGuid
});
return await query.ToListAsync();
}
/// <summary>
/// 查询客户收货地址详情(Api)
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public Task<string> GetCustomerAddressDetails(CustomerAddressDtoApi parm)
{
var query = _CustomerAddressRepository
.Queryable()
.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.CustomerAddressId == parm.CustomerAddressId)
.Select((s, c, d, f, g) => new CustomerAddressApiDetailsVo
{
CustomerAddressId = s.CustomerAddressId,
CustomerAddressGuid = s.CustomerAddressGuid,
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);
return query.ToJsonAsync();
}
/// <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
}
}