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 { /// /// 客户收货地址接口实现类Api /// /// @author admin /// @date 2023-06-09 /// [AppService(ServiceType = typeof(ICustomerAddressServiceApi), ServiceLifetime = LifeTime.Transient)] public class CustomerAddressServiceImplApi : BaseService, ICustomerAddressServiceApi { private readonly CustomerAddressRepository _CustomerAddressRepository; private readonly CustomerRepository _CustomerRepository; public CustomerAddressServiceImplApi(CustomerAddressRepository CustomerAddressRepository, CustomerRepository customerRepository) { this._CustomerAddressRepository = CustomerAddressRepository; _CustomerRepository = customerRepository; } #region Api接口代码 /// /// 查询客户收货地址列表(Api) /// /// /// public async Task> GetCustomerAddressListApi(CustomerAddressQueryDtoApi parm) { //开始拼装查询条件d var predicate = Expressionable.Create(); var query = _CustomerAddressRepository .Queryable() .LeftJoin((s, c) => s.CustomerAddressProvinceId == c.RegionId) .LeftJoin((s, c, d) => s.CustomerAddressCityId == d.RegionId) .LeftJoin((s, c, d, f) => s.CustomerAddressAreaId == f.RegionId) .LeftJoin((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, Address = c.RegionName + d.RegionName + f.RegionName + s.CustomerAddressDetailed, IsDefault = s.CustomerAddressGuid == g.CustomerDefaultAddressGuid }); return await query.ToListAsync(); } /// /// 查询客户收货地址详情(Api) /// /// /// public Task GetCustomerAddressDetails(CustomerAddressDtoApi parm) { var query = _CustomerAddressRepository .Queryable() .LeftJoin((s, c) => s.CustomerAddressProvinceId == c.RegionId) .LeftJoin((s, c, d) => s.CustomerAddressCityId == d.RegionId) .LeftJoin((s, c, d, f) => s.CustomerAddressAreaId == f.RegionId) .LeftJoin((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(); } /// /// 添加或修改客户收货地址 /// public async Task 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 "添加成功!"; } } /// /// 默认收货地址处理 /// /// CustomerAddress模型 /// 是否为默认地址 /// 默认地址Guid /// 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 } }