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
{
///
/// 店铺接口实现类
///
/// @author 黎文豪
/// @date 2023-06-09
///
[AppService(ServiceType = typeof(IShopService), ServiceLifetime = LifeTime.Transient)]
public class ShopServiceImpl : BaseService, 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 业务逻辑代码
///
/// 查询店铺分页列表
///
public async Task> GetShopList(ShopQueryDto parm)
{
//开始拼装查询条件d
var predicate = Expressionable.Create();
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((s, c) => s.ShopUserId == c.UserId)
.LeftJoin((s, c, d) => s.ShopCustomerGuid == d.CustomerGuid)
.LeftJoin((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);
}
///
/// 添加或修改店铺
///
public async Task 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处理
///
/// Excel数据导出处理
///
public async Task> HandleExportData(List data)
{
return data;
}
#endregion
///
/// 获取经营类目列表(一级类目)
///
///
///
public async Task> 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();
}
///
/// 获取未绑定的用户列表
///
///
///
public async Task> GetUserWithOutBindList(UserWithOutBindQueryDto parm)
{
//开始拼装查询条件d
var predicate = Expressionable.Create();
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.UserName), s => s.UserName.Contains(parm.UserName));
var query = _SysUserRepository
.Queryable()
.Where(predicate.ToExpression())
.Where(s => SqlFunc.Subqueryable().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);
}
///
/// 获取未绑定的客户列表
///
///
///
public async Task> GetCustomerWithOutBindList(CustomerWithOutBindQueryDto parm)
{
//开始拼装查询条件d
var predicate = Expressionable.Create();
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.CustomerNickname), s => s.CustomerNickname.Contains(parm.CustomerNickname));
var query = _CustomerRepository
.Queryable()
.Where(predicate.ToExpression())
.Where(s => SqlFunc.Subqueryable().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);
}
///
/// 审核
///
public async Task 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}】已通过审核!
";
return errorRes;
}
if (res.ShopAuditStatus == 3)
{
var errorRes = $"店铺:【{res.ShopName}】已被驳回!
";
return errorRes;
}
else if (res.ShopAuditStatus == 1)
{
var addStr = $"店铺:【{res.ShopName}】审核通过!
";
// 分发后台用户账户
var customer = await _CustomerRepository.GetFirstAsync(s => s.CustomerGuid == res.ShopCustomerGuid);
long[] roleId = new long[] { 3 };
int[] postId = Array.Empty();
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;
}
}
///
/// 获取商户店铺详情
///
///
///
public Task 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
}
}