emoticon_api/ARW.Service/Business/BusinessService/ShopManager/Shops/ShopService.cs

321 lines
12 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 Infrastructure;
using ARW.Model;
using ARW.Repository;
using ARW.Repository.Business.ShopManager.Shops;
using ARW.Service.Business.IBusinessService.ShopManager.Shops;
using ARW.Model.Dto.Business.ShopManager.Shops;
using ARW.Model.Models.Business.ShopManager.Shops;
using ARW.Model.Vo.Business.ShopManager.Shops;
using MimeKit;
using ARW.Model.Models.Business.Custom.Customers;
using ARW.Repository.Business.Custom.Customers;
using ARW.Model.System;
using ARW.Repository.System;
using ARW.Model.Vo.Business.GoodsManager.GoodsCategorys;
using ARW.Repository.Business.GoodsManager.GoodsCategorys;
using ARW.Model.Models.Business.GoodsManager.GoodsCategorys;
using ARW.Service.System.IService;
using ARW.Service.System;
using MimeKit.Encodings;
using ARW.Model.Dto.Api.ShopManager.Shops;
using ARW.Model.Vo.Api.ShopManager.Shops;
namespace ARW.Service.Business.BusinessService.ShopManager.Shops
{
/// <summary>
/// 店铺接口实现类
///
/// @author 黎文豪
/// @date 2023-06-09
/// </summary>
[AppService(ServiceType = typeof(IShopService), ServiceLifetime = LifeTime.Transient)]
public class ShopServiceImpl : BaseService<Shop>, IShopService
{
private readonly ShopRepository _ShopRepository;
private readonly CustomerRepository _CustomerRepository;
private readonly SysUserRepository _SysUserRepository;
private readonly GoodsCategoryRepository _GoodsCategoryRepository;
private readonly ISysUserService UserService;
private readonly ISysConfigService _SysConfigService;
public ShopServiceImpl(ShopRepository ShopRepository, CustomerRepository customerRepository, SysUserRepository sysUserRepository, GoodsCategoryRepository goodsCategoryRepository, ISysUserService userService, ISysConfigService sysConfigService)
{
this._ShopRepository = ShopRepository;
_CustomerRepository = customerRepository;
_SysUserRepository = sysUserRepository;
_GoodsCategoryRepository = goodsCategoryRepository;
UserService = userService;
_SysConfigService = sysConfigService;
}
#region
/// <summary>
/// 查询店铺分页列表
/// </summary>
public async Task<PagedInfo<ShopVo>> GetShopList(ShopQueryDto parm)
{
//开始拼装查询条件d
var predicate = Expressionable.Create<Shop>();
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.ShopName), s => s.ShopName.Contains(parm.ShopName));
predicate = predicate.AndIF(parm.ShopAuditStatus != null, s => s.ShopAuditStatus == parm.ShopAuditStatus);
var query = _ShopRepository
.Queryable()
.LeftJoin<SysUser>((s, c) => s.ShopUserId == c.UserId)
.LeftJoin<Customer>((s, c, d) => s.ShopCustomerGuid == d.CustomerGuid)
.LeftJoin<GoodsCategory>((s, c, d, f) => s.ShopBusinessCategoryGuid == f.GoodsCategoryGuid)
.Where(predicate.ToExpression())
.OrderBy(s => s.ShopSort, OrderByType.Asc)
.Select((s, c, d, f) => new ShopVo
{
UserName = c.UserName,
CustomerNickname = d.CustomerNickname,
ShopBusinessCategoryName = f.GoodsCategoryName,
ShopId = s.ShopId,
ShopGuid = s.ShopGuid,
ShopUserId = s.ShopUserId,
ShopCustomerGuid = s.ShopCustomerGuid,
ShopBusinessCategoryGuid = s.ShopBusinessCategoryGuid,
ShopLogo = s.ShopLogo,
ShopBusinessLicense = s.ShopBusinessLicense,
ShopName = s.ShopName,
ShopIntro = s.ShopIntro,
ShopSort = s.ShopSort,
ShopSalesOrderCount = s.ShopSalesOrderCount,
ShopAuditStatus = s.ShopAuditStatus,
ShopAuditUserGuid = s.ShopAuditUserGuid,
CreateTime = s.Create_time.ToString("yyyy-MM-dd HH:mm")
});
return await query.ToPageAsync(parm);
}
/// <summary>
/// 添加或修改店铺
/// </summary>
public async Task<string> AddOrUpdateShop(Shop model)
{
if (model.ShopId != 0)
{
var response = await _ShopRepository.UpdateAsync(model);
return "修改成功!";
}
else
{
model.ShopAuditStatus = 2;
model.ShopAuditUserGuid = 1;
var response = await _ShopRepository.InsertReturnSnowflakeIdAsync(model);
return "添加成功!";
}
}
#region Excel处理
/// <summary>
/// Excel数据导出处理
/// </summary>
public async Task<List<ShopVo>> HandleExportData(List<ShopVo> data)
{
return data;
}
#endregion
/// <summary>
/// 获取经营类目列表(一级类目)
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public async Task<List<GoodsCategoryVo>> GetFirstGoodsCategoryList()
{
var query = _GoodsCategoryRepository
.Queryable()
.Where(s => s.GoodsCategoryParentGuid == 0)
.OrderBy(s => s.Update_time, OrderByType.Desc)
.Select((s) => new GoodsCategoryVo
{
GoodsCategoryId = s.GoodsCategoryId,
GoodsCategoryGuid = s.GoodsCategoryGuid,
GoodsCategoryParentGuid = s.GoodsCategoryParentGuid,
GoodsCategoryAncestralGuid = s.GoodsCategoryAncestralGuid,
GoodsCategoryName = s.GoodsCategoryName,
GoodsCategoryImg = s.GoodsCategoryImg,
GoodsCategoryDisplayStatus = s.GoodsCategoryDisplayStatus,
GoodsCategoryIsPopular = s.GoodsCategoryIsPopular,
ProductCategorySort = s.ProductCategorySort,
});
return await query.ToListAsync();
}
/// <summary>
/// 获取未绑定的用户列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public async Task<PagedInfo<UserWithOutBindVo>> GetUserWithOutBindList(UserWithOutBindQueryDto parm)
{
//开始拼装查询条件d
var predicate = Expressionable.Create<SysUser>();
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.UserName), s => s.UserName.Contains(parm.UserName));
var query = _SysUserRepository
.Queryable()
.Where(predicate.ToExpression())
.Where(s => SqlFunc.Subqueryable<Shop>().Where(it => it.ShopUserId == s.UserId).NotAny())
.Where(s => s.UserId != 1)
.OrderBy(s => s.Update_time, OrderByType.Desc)
.Select((s) => new UserWithOutBindVo
{
UserId = s.UserId,
UserName = s.UserName,
});
return await query.ToPageAsync(parm);
}
/// <summary>
/// 获取未绑定的客户列表
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public async Task<PagedInfo<CustomerWithOutBindVo>> GetCustomerWithOutBindList(CustomerWithOutBindQueryDto parm)
{
//开始拼装查询条件d
var predicate = Expressionable.Create<Customer>();
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.CustomerNickname), s => s.CustomerNickname.Contains(parm.CustomerNickname));
var query = _CustomerRepository
.Queryable()
.Where(predicate.ToExpression())
.Where(s => SqlFunc.Subqueryable<Shop>().Where(it => it.ShopCustomerGuid == s.CustomerGuid).NotAny())
.OrderBy(s => s.Update_time, OrderByType.Desc)
.Select((s) => new CustomerWithOutBindVo
{
CustomerGuid = s.CustomerGuid,
CustomerNickname = s.CustomerNickname,
});
return await query.ToPageAsync(parm);
}
/// <summary>
/// 审核
/// </summary>
public async Task<string> Audit(int id, int status, long userGuid)
{
try
{
var res = await _ShopRepository.GetFirstAsync(s => s.ShopId == id);
await UseTranAsync(async () =>
{
await _ShopRepository.UpdateAsync(f => new Shop { ShopAuditStatus = status, ShopAuditUserGuid = userGuid, Update_time = DateTime.Now, Update_by = userGuid.ToString() }, s => s.ShopId == id);
});
if (res.ShopAuditStatus == 2)
{
var errorRes = $"店铺:【{res.ShopName}】<span style='color:red'>已通过审核!</span><br>";
return errorRes;
}
if (res.ShopAuditStatus == 3)
{
var errorRes = $"店铺:【{res.ShopName}】<span style='color:red'>已被驳回!</span><br>";
return errorRes;
}
else if (res.ShopAuditStatus == 1)
{
var addStr = $"店铺:【{res.ShopName}】<span style='color:#27af49'>审核通过!</span><br>";
// 分发后台用户账户
var customer = await _CustomerRepository.GetFirstAsync(s => s.CustomerGuid == res.ShopCustomerGuid);
long[] roleId = new long[] { 3 };
int[] postId = Array.Empty<int>();
if (UserConstants.NOT_UNIQUE.Equals(UserService.CheckUserNameUnique(customer.CustomerMobilePhoneNumber)))
{
throw new Exception($"新增用户 '{customer.CustomerMobilePhoneNumber}'失败,登录账号已存在");
}
var initPassword = _SysConfigService.Queryable().First(f => f.ConfigKey == "sys.user.initPassword");
var password = NETCore.Encrypt.EncryptProvider.Md5(initPassword.ConfigValue);
var user = new SysUser
{
UserName = customer.CustomerMobilePhoneNumber,
NickName = customer.CustomerNickname,
Password = password,
Avatar = customer.CustomerAvatar,
Phonenumber = customer.CustomerMobilePhoneNumber,
Sex = customer.CustomerGender.ToString(),
Status = "0",
RoleIds = roleId,
PostIds = postId
};
var userId = UserService.InsertUser(user);
if (userId == 0) throw new Exception($"新增用户 '{customer.CustomerMobilePhoneNumber}'失败");
await _ShopRepository.UpdateAsync(f => new Shop
{
ShopUserId = (int?)userId,
}, s => s.ShopId == id);
return addStr;
}
return "";
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 获取商户店铺详情
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public Task<string> GetShopOperatorDetail(ShopOperatorQueryDto parm)
{
var query = _ShopRepository
.Queryable()
.Where(s => s.ShopGuid == parm.ShopGuid)
.Select(s => new ShopDetailVo
{
ShopId = s.ShopId,
ShopGuid = s.ShopGuid,
ShopBusinessCategoryGuid = s.ShopBusinessCategoryGuid,
ShopLogo = s.ShopLogo,
ShopBusinessLicense = s.ShopBusinessLicense,
ShopName = s.ShopName,
ShopIntro = s.ShopIntro,
ShopSalesOrderCount = s.ShopSalesOrderCount,
}).Take(1);
return query.ToJsonAsync();
}
#endregion
}
}