using System; using System.Collections.Generic; using System.Threading.Tasks; using Yitter.IdGenerator; using Znyc.Recruitment.Admin.Commons; using Znyc.Recruitment.Admin.Commons.Entitys; using Znyc.Recruitment.Admin.Commons.Enums; using Znyc.Recruitment.Admin.Commons.Extensions; using Znyc.Recruitment.Admin.Commons.Mapping; using Znyc.Recruitment.Admin.Commons.Pages; using Znyc.Recruitment.Admin.Commons.Services; using Znyc.Recruitment.Admin.Security.Dtos; using Znyc.Recruitment.Admin.Security.Entitys; using Znyc.Recruitment.Admin.Security.IRepositories; using Znyc.Recruitment.Admin.Security.IServices; namespace Znyc.Recruitment.Admin.Security.Services { /// /// 充值活动服务 /// public class RechargeService : BaseService, IRechargeService { private readonly IRechargeRepository _rechargeRepository; private readonly IRechargeIntroRepository _rechargeIntroRepository; public RechargeService( IRechargeRepository repository, IRechargeIntroRepository rechargeIntroRepository ) : base(repository) { _rechargeRepository = repository; _rechargeIntroRepository = rechargeIntroRepository; } /// /// 分页查询 /// /// /// public async Task> FindWithPagerSearchAsync(SearchRechargeModel search) { bool order = search.Order == "asc" ? false : true; string where = GetDataPrivilege(false); if (search.Status >= 0) { @where += $" and Status={search.Status}"; } if (!string.IsNullOrEmpty(search.StartTime)) { @where += $" and StartTime >='{search.StartTime} 00:00:00'"; } if (!string.IsNullOrEmpty(search.EndTime)) { @where += $" or EndTime <='{search.EndTime} 23:59:59'"; } PagerInfo pagerInfo = new PagerInfo { CurrenetPageIndex = search.CurrenetPageIndex, PageSize = search.PageSize }; List list = (await repository.FindWithPagerAsync(where, pagerInfo, search.Sort, order)).MapTo(); foreach (var item in list) { item.StatusName = EnumExtensions .ParseEnum(typeof(ActivityStatusEnum), item.Status.ToString()).ToDescription(); item.rechargeIntros = (await _rechargeIntroRepository.GetListWhereAsync($" ParentId={item.Id}")).MapTo(); } PageResult pageResult = new PageResult { CurrentPage = pagerInfo.CurrenetPageIndex, Items = list, ItemsPerPage = pagerInfo.PageSize, TotalItems = pagerInfo.RecordCount }; return pageResult; } /// /// 异步新增充值活动详情数据 /// /// /// /// 创建人 /// public async Task InsertRechargeIntroAsync(List input, long parentId, long userId) { //关闭其他充值活动 RechargeEntity rechargeEntity = await _rechargeRepository.GetWhereAsync($" IsDeleted=0 and Status=1 and Id!={parentId}"); if (rechargeEntity != null) { rechargeEntity.Status = (int)ActivityStatusEnum.End; await repository.UpdateAsync(rechargeEntity, rechargeEntity.Id).ConfigureAwait(false); } foreach (var item in input) { RechargeIntroEntity entity = new RechargeIntroEntity() { Id = YitIdHelper.NextId(), ParentId = parentId, ProductName = string.Format(CommonConst.RechargeIntro_Name, item.ProductValue), ProductValue = item.ProductValue, SendValue = item.SendValue, AllValue = item.ProductValue + item.SendValue, Price = item.Price, Description = string.Format(CommonConst.RechargeIntro_Description, item.ProductValue + item.SendValue), CreatedUserId = userId, CreatedTime = DateTime.Now }; await _rechargeIntroRepository.InsertAsync(entity); } return true; } /// /// 异步更新数据 /// /// /// /// public async Task UpdateAsync(RechargeUpdateInput input, long userId) { CommonResult result = new CommonResult(); RechargeEntity info = await repository.GetAsync(input.Id); if (!(info?.Id > 0)) { result.Success = false; result.ErrMsg = "充值活动信息不存在"; return result; } info.Name = input.Name; info.Status = (int)ActivityStatusEnum.Ongoing; info.ModifiedUserId = userId; info.ModifiedTime = DateTime.Now; foreach (var item in input.rechargeIntros) { RechargeIntroEntity entity = await _rechargeIntroRepository.GetAsync(item.Id); entity.ProductName = string.Format(CommonConst.RechargeIntro_Name, item.ProductValue); entity.ProductValue = item.ProductValue; entity.SendValue = item.SendValue; entity.AllValue = item.ProductValue + item.SendValue; entity.Price = item.Price; entity.Description = string.Format(CommonConst.RechargeIntro_Description, item.ProductValue + item.SendValue); entity.ModifiedUserId = userId; entity.ModifiedTime = DateTime.Now; await _rechargeIntroRepository.UpdateAsync(entity, entity.Id).ConfigureAwait(false); } await repository.UpdateAsync(info, input.Id).ConfigureAwait(false); //关闭其他充值活动 RechargeEntity rechargeEntity = await _rechargeRepository.GetWhereAsync($" IsDeleted=0 and Status=1 and Id!={input.Id}"); if (rechargeEntity != null) { rechargeEntity.Status = (int)ActivityStatusEnum.End; await repository.UpdateAsync(rechargeEntity, rechargeEntity.Id).ConfigureAwait(false); } result.Success = true; return result; } } }