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.
 
 

182 lines
7.9 KiB

using Znyc.CloudCar.Auth.HttpContextUser;
using Znyc.CloudCar.Configuration;
using Znyc.CloudCar.IRepository.Collection;
using Znyc.CloudCar.IRepository.Currency;
using Znyc.CloudCar.IRepository.Equipment;
using Znyc.CloudCar.IRepository.EquipmentPicture;
using Znyc.CloudCar.IRepository.User;
using Znyc.CloudCar.IServices.Collection;
using Znyc.CloudCar.Model.Dtos.Collection;
using Znyc.CloudCar.Model.Dtos.EquipmentPicture;
using Znyc.CloudCar.Model.Entities;
using Znyc.CloudCar.Model.ViewModels.ReportsCallBack;
using Znyc.CloudCar.Utility.Extensions;
using Znyc.CloudCar.Utility.Helper;
using static Znyc.CloudCar.Configuration.GlobalEnumVars;
namespace Znyc.CloudCar.Services.Collection
{
/// <summary>
/// 收藏服务
/// </summary>
public class CollectionService : ICollectionService
{
private readonly ICollectionRepository _collectionRepository;
private readonly IHttpContextUser _httpContextUser;
private readonly IEquipmentRepository _equipmentRepository;
private readonly ICurrencyRecordRepository _currencyRecordRepository;
private readonly IUserRepository _userRepository;
private readonly IEquipmentPictureRepository _equipmentPictureRepository;
public CollectionService(
ICollectionRepository collectionRepository,
IHttpContextUser httpContextUser,
IEquipmentRepository equipmentRepository,
ICurrencyRecordRepository currencyRecordRepository,
IUserRepository userRepository,
IEquipmentPictureRepository equipmentPictureRepository
)
{
_collectionRepository = collectionRepository;
_httpContextUser = httpContextUser;
_equipmentRepository = equipmentRepository;
_currencyRecordRepository = currencyRecordRepository;
_userRepository = userRepository;
_equipmentPictureRepository = equipmentPictureRepository;
}
/// <summary>
/// 分页查询收藏记录列表
/// </summary>
/// <param name="currentPage"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public async Task<ResponseOutput> PageAsync(int currentPage, int pageSize)
{
//收藏记录
List<long> collection = await _collectionRepository.Select
.Where(x => x.UserId == _httpContextUser.Id && x.State == 0)
.ToListAsync(x => x.EquipmentId);
long[] equipmentIds = collection.Distinct().ToArray();
List<CollectionListOutput> collectionList = await _equipmentRepository.Select
.Where(x => equipmentIds.Contains(x.Id))
.Count(out long total)
.OrderByDescending(x => x.IsTop)
.OrderByDescending(x => x.RefreshDate)
.Page(currentPage, pageSize)
.ToListAsync<CollectionListOutput>();
//积分记录
List<CurrencyRecordEntity> currencyRecordList = new List<CurrencyRecordEntity>();
if (_httpContextUser.Id > 0)
{
long[] cIds = collectionList.Select(x => x.Id).Distinct().ToArray();
currencyRecordList = await _currencyRecordRepository.Where(x =>
cIds.Contains(x.CurrencySoureObjectId) &&
x.CurrencyType == (int)CurrencyType.CallPhone).ToListAsync();
}
long[] uIds = collectionList.Select(x => x.UserId).Distinct().ToArray();
List<UserEntity> users = await _userRepository.Select.Where(x => uIds.Contains(x.Id)).ToListAsync();
//设备图片
long[] eIds = collectionList.Select(x => x.Id).Distinct().ToArray();
List<EquipmentPictureOutput> equipmentPictures = await _equipmentPictureRepository.Select
.Where(x => x.IsDeleted == false && eIds.Contains(x.EquipmentId) && x.PictureType == (int)PictureTypeEnum.Equipment)
.ToListAsync<EquipmentPictureOutput>();
foreach (var item in collectionList)
{
item.EquipmentPictures = equipmentPictures.FindAll(x => x.EquipmentId == item.Id);
UserEntity user = users.FirstOrDefault(x => x.Id == item.UserId);
item.UserName = user?.UserName ?? GlobalConstVars.User.Default_UserName;
item.AvatarUrl = GlobalConstVars.User.DefaultImagePrefix + (user?.AvatarUrl ?? GlobalConstVars.User.DefaultAvataUrl);
item.IsGetPhone = item.UserId == _httpContextUser.Id ? true : currencyRecordList.Exists(x => x.UserId == _httpContextUser.Id && x.CurrencySoureObjectId == item.Id);
item.ContactPhone = !item.IsGetPhone ? CommonHelper.ToHiddenPhone(item.ContactPhone) : item.ContactPhone;
item.CallPhoneCurrency = CommonHelper.GetCallPhoneCurrency(Convert.ToInt32(item.SellingPrice));
item.SellingPrice = item.SellingPrice / 10000;
}
PageOutput<CollectionListOutput> data = new PageOutput<CollectionListOutput>
{
List = collectionList,
Total = total
};
ResponseOutput response = new ResponseOutput()
{
Data = data,
Successed = true,
Code = 1
};
return response;
}
/// <summary>
/// 添加收藏
/// </summary>
/// <param name="equipmentId"></param>
/// <returns></returns>
public async Task<ResponseOutput> AddAsync(long equipmentId)
{
var collection = await _collectionRepository.Where(x =>
x.UserId == _httpContextUser.Id &&
x.EquipmentId == equipmentId && x.IsDeleted == false)
.OrderByDescending(x => x.ModifiedTime)
.FirstAsync();
if (collection.IsNull())
{
collection = await _collectionRepository.InsertAsync(new CollectionEntity()
{
UserId = _httpContextUser.Id,
EquipmentId = equipmentId,
State = 0
});
}
collection.ModifiedTime = DateTime.Now;
collection.State = 0;
await _collectionRepository.UpdateAsync(collection);
ResponseOutput response = new ResponseOutput()
{
Successed = true,
Msg = "收藏成功",
Code = 1
};
return response;
}
/// <summary>
/// 取消收藏
/// </summary>
/// <param name="equipmentId"></param>
/// <returns></returns>
public async Task<ResponseOutput> CancelAsync(long equipmentId)
{
ResponseOutput response = new ResponseOutput();
var collection = await _collectionRepository.GetAsync(x =>
x.EquipmentId == equipmentId &&
x.UserId == _httpContextUser.Id && x.IsDeleted == false);
if (collection.IsNull())
{
response.Successed = false;
response.Msg = "信息不存在!";
response.Code = 0;
return response;
}
collection.State = 1;
await _collectionRepository.UpdateAsync(collection);
response.Successed = true;
response.Msg = "取消收藏成功";
response.Code = 1;
return response;
}
/// <summary>
/// 是否收藏
/// </summary>
/// <param name="equipmentId"></param>
/// <returns></returns>
public async Task<bool> IsCollection(long equipmentId)
{
CollectionEntity entity =
await _collectionRepository.GetAsync(
x => x.EquipmentId == equipmentId && x.UserId == _httpContextUser.Id &&
x.State == 0 && x.IsDeleted == false);
return entity?.Id > 0;
}
}
}