You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
5.0 KiB

using AutoMapper;
using Common.Util;
using Entity.DbModel.System.App;
using Entity.Dto.Req.App;
using Entity.Dto.Resp;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity;
using Repository.System.App;
namespace Service.System.App;
[Scope]
public class AppCustomerService : BaseServices<AppCustomer>
{
private readonly AppCustomerRepository _appCustomerRepository;
public AppCustomerService(
AppCustomerRepository appCustomerRepository
)
{
_appCustomerRepository = appCustomerRepository;
}
/// <summary>
/// App端新增用户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<string> AddAppCustomer(AddAppCustomerReq input)
{
var existingUser =
await _appCustomerRepository.QueryByClauseAsync(u =>
u.LoginNo == input.LoginNo || u.IphoneNo == input.IphoneNo);
if (existingUser?.LoginNo == input.LoginNo)
{
return "账号已存在";
}
// 对密码进行加密
input.LoginPsd = CryptogramUtil.Encrypt(input.LoginPsd);
// 插入新用户
await _appCustomerRepository.InsertAsync(input);
// 查询新用户的ID
AppCustomer newAddUser = await _appCustomerRepository.QueryByClauseAsync(u => u.LoginNo == input.LoginNo);
return "新增账号:" + newAddUser.LoginNo;
}
/// <summary>
/// App用户基础信息
/// </summary>
/// <param name="loginNo"></param>
/// <returns></returns>
public virtual async Task<AppCustomerResp> GetBaseInfo(String loginNo)
{
AppCustomer appCustomer = await _appCustomerRepository.QueryByClauseAsync(u => u.LoginNo == loginNo);
var config = new MapperConfiguration(cfg => { cfg.CreateMap<AppCustomerResp, AppCustomer>().ReverseMap(); });
IMapper mapper = config.CreateMapper();
AppCustomerResp vo = mapper.Map<AppCustomerResp>(appCustomer);
return vo;
}
/// <summary>
/// 修改App用户基础信息
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual async Task<Result<string>> UpdateBaseInfo(UpdateAppCustomerReq user)
{
var existingUser =
await _appCustomerRepository.QueryByClauseAsync(u =>
u.LoginNo == user.LoginNo && u.Id != user.Id);
if (existingUser?.LoginNo == user.LoginNo)
{
return Result<string>.Fail("修改失败,账号已存在");
}
int updateAsync = await _appCustomerRepository.UpdateAsync(user, true, u => new { u.LoginPsd });
if (updateAsync > 0)
{
return Result<string>.Success("修改成功");
}
return Result<string>.Fail("修改失败");
}
/// <summary>
/// 绑定车队
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public virtual async Task<Result<string>> BindTeam(CustomerTeamReq user)
{
var existingUser = await _appCustomerRepository.QueryByClauseAsync(u => u.LoginNo == user.LoginNo);
if (existingUser == null)
{
return Result<string>.Fail("账户不存在");
}
// 绑定or解绑
return user.Bind ? await TeamBind(user, existingUser) : await UnbindTeam(existingUser);
}
/// <summary>
/// 绑定车队
/// </summary>
/// <param name="user"></param>
/// <param name="existingUser"></param>
/// <returns></returns>
private async Task<Result<string>> TeamBind(CustomerTeamReq user, AppCustomer existingUser)
{
existingUser.TeamId = user.TeamId;
var updateAsync = await _appCustomerRepository.UpdateAsync(existingUser, true);
return updateAsync > 0 ? Result<string>.Success("绑定成功") : Result<string>.Fail("绑定失败");
}
/// <summary>
/// 解绑车队
/// </summary>
/// <param name="existingUser"></param>
/// <returns></returns>
private async Task<Result<string>> UnbindTeam(AppCustomer existingUser)
{
existingUser.TeamId = null;
var updateAsync = await _appCustomerRepository.UpdateAsync(existingUser, false);
return updateAsync > 0 ? Result<string>.Success("解绑成功") : Result<string>.Fail("解绑失败");
}
/// <summary>
/// 修改密码
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<string> UpdateCustomerPsd(CustomerPwdReq input)
{
var user = await _appCustomerRepository.QueryByClauseAsync(u => u.LoginNo == input.LoginNo);
if (CryptogramUtil.Decrypt(user.LoginPsd) != input.PasswordOld)
return "旧密码不匹配";
if (input.PasswordOld == input.PasswordNew)
return "新密码不能与旧密码相同";
user.LoginPsd = CryptogramUtil.Encrypt(input.PasswordNew);
await _appCustomerRepository.UpdateAsync(user);
return "修改密码成功";
}
}