| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 10 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 11 | | using WinSolutions.Sveta.Server.Domain; |
| | | 12 | | using WinSolutions.Sveta.Common; |
| | | 13 | | using CrmVtbc; |
| | | 14 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 15 | | |
| | | 16 | | namespace WinSolutions.Sveta.Server.Services.Implements |
| | | 17 | | { |
| | | 18 | | public class CommonUserService : ICommonUserService |
| | | 19 | | { |
| | | 20 | | readonly SvetaDbContext _db; |
| | | 21 | | |
| | 186 | 22 | | public CommonUserService(SvetaDbContext db) |
| | 186 | 23 | | { |
| | 186 | 24 | | _db = db; |
| | 186 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | ///предварительная выборка юзеров |
| | | 29 | | /// </summary> |
| | | 30 | | /// <returns></returns> |
| | 0 | 31 | | private IQueryable<User> PrepareUsers() => _db.Users |
| | 0 | 32 | | .Include(d => d.Contragent) |
| | 0 | 33 | | .ThenInclude(Contragent => Contragent.ContragentsKind) |
| | 0 | 34 | | .Include(d => d.Contragent) |
| | 0 | 35 | | .ThenInclude(Contragent => Contragent.Owner) |
| | 0 | 36 | | .Include(d => d.RecState) |
| | 0 | 37 | | .Where(d => !d.IsDeleted) |
| | 0 | 38 | | .AsQueryable(); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// получает юзера по extrenalKey |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="tid">extrenalKey</param> |
| | | 44 | | /// <returns>юзер</returns> |
| | 0 | 45 | | public async Task<User> GetUser(Guid tid) => await PrepareUsers().FirstOrDefaultAsync(x => x.ExternalKey.Equals( |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// создает юзера |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="data">юзер</param> |
| | | 51 | | /// <param name="userId">id юзера, от кого выполняется</param> |
| | | 52 | | /// <returns></returns> |
| | | 53 | | public async Task Create(User data, long userId) |
| | 570 | 54 | | { |
| | 570 | 55 | | if (data.Contragent != null) |
| | 202 | 56 | | _db.Entry(data.Contragent).State = EntityState.Unchanged; |
| | 570 | 57 | | await _db.Users.AddAsync(data); |
| | 570 | 58 | | await _db.SaveChangesAsync(userId); |
| | 570 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Проверяет можно ли по данному хэштегу отправлять сообещния в телеграмм |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="hashtag">хэштег</param> |
| | | 65 | | /// <returns>значение true/false</returns> |
| | | 66 | | public async Task<bool> CanSendToTelegram(string hashtag) |
| | 0 | 67 | | { |
| | 0 | 68 | | var result = await _db.TelegramNotifications.Where(x=>!x.IsDeleted).FirstOrDefaultAsync(x => x.HashTag.ToLow |
| | 0 | 69 | | return result == null ? false : result.IsActive; |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// проверяет существование юзера |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="id">id юзера</param> |
| | | 76 | | /// <returns>значение true/false</returns> |
| | 0 | 77 | | public async Task<bool> UserExists(long id) => await _db.Users |
| | 0 | 78 | | .Where(e => e.Id == id) |
| | 0 | 79 | | .Where(d => !d.IsDeleted) |
| | 0 | 80 | | .AnyAsync(); |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// обновляет юзера |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="data">юзер</param> |
| | | 86 | | /// <param name="userId">id юзера, от кого выполняется</param> |
| | | 87 | | /// <returns></returns> |
| | | 88 | | public async Task Update(User data, long userId) |
| | 0 | 89 | | { |
| | 0 | 90 | | if (!(await UserExists(data.Id))) |
| | 0 | 91 | | { |
| | 0 | 92 | | throw new ArgumentException($"Record #{data.Id} not found"); |
| | | 93 | | } |
| | 0 | 94 | | _db.Users.Update(data); |
| | 0 | 95 | | await _db.SaveChangesAsync(userId); |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | ///получает список ролей |
| | | 100 | | /// </summary> |
| | | 101 | | /// <returns>список ролей</returns> |
| | 0 | 102 | | public async Task<List<Roles>> GetRoles() => await _db.Roles.Where(x => !x.IsDeleted).ToListAsync(); |
| | | 103 | | } |
| | | 104 | | } |