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 { private readonly AppCustomerRepository _appCustomerRepository; public AppCustomerService( AppCustomerRepository appCustomerRepository ) { _appCustomerRepository = appCustomerRepository; } /// /// App端新增用户 /// /// /// public virtual async Task 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; } /// /// App用户基础信息 /// /// /// public virtual async Task GetBaseInfo(String loginNo) { AppCustomer appCustomer = await _appCustomerRepository.QueryByClauseAsync(u => u.LoginNo == loginNo); var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); }); IMapper mapper = config.CreateMapper(); AppCustomerResp vo = mapper.Map(appCustomer); return vo; } /// /// 修改App用户基础信息 /// /// /// public virtual async Task> UpdateBaseInfo(UpdateAppCustomerReq user) { var existingUser = await _appCustomerRepository.QueryByClauseAsync(u => u.LoginNo == user.LoginNo && u.Id != user.Id); if (existingUser?.LoginNo == user.LoginNo) { return Result.Fail("修改失败,账号已存在"); } int updateAsync = await _appCustomerRepository.UpdateAsync(user, true, u => new { u.LoginPsd }); if (updateAsync > 0) { return Result.Success("修改成功"); } return Result.Fail("修改失败"); } /// /// 绑定车队 /// /// /// public virtual async Task> BindTeam(CustomerTeamReq user) { var existingUser = await _appCustomerRepository.QueryByClauseAsync(u => u.LoginNo == user.LoginNo); if (existingUser == null) { return Result.Fail("账户不存在"); } // 绑定or解绑 return user.Bind ? await TeamBind(user, existingUser) : await UnbindTeam(existingUser); } /// /// 绑定车队 /// /// /// /// private async Task> TeamBind(CustomerTeamReq user, AppCustomer existingUser) { existingUser.TeamId = user.TeamId; var updateAsync = await _appCustomerRepository.UpdateAsync(existingUser, true); return updateAsync > 0 ? Result.Success("绑定成功") : Result.Fail("绑定失败"); } /// /// 解绑车队 /// /// /// private async Task> UnbindTeam(AppCustomer existingUser) { existingUser.TeamId = null; var updateAsync = await _appCustomerRepository.UpdateAsync(existingUser, false); return updateAsync > 0 ? Result.Success("解绑成功") : Result.Fail("解绑失败"); } /// /// 修改密码 /// /// /// public virtual async Task 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 "修改密码成功"; } }