ProjectService.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using ZhonTai.Admin.Core.Consts;
  7. using ZhonTai.Admin.Services.User;
  8. using ZhonTai.DynamicApi.Attributes;
  9. using ZhonTai.DynamicApi;
  10. using Microsoft.AspNetCore.Mvc;
  11. using ZhonTai.Admin.Core.Dto;
  12. using ZhonTai.Admin.Domain.Role;
  13. using ZhonTai.Admin.Domain.User;
  14. using ZhonTai.Admin.Services.User.Dto;
  15. using ZhonTai.Admin.Domain.UserRole;
  16. using ZhonTai.Admin.Domain.Project;
  17. using ZhonTai.Admin.Services.Project.Dto;
  18. using ZhonTai.Admin.Core.Attributes;
  19. using ZhonTai.Admin.Domain.Tenant;
  20. using ZhonTai.Admin.Domain.TenantPkg;
  21. using ZhonTai.Admin.Domain.Pkg;
  22. using ZhonTai.Admin.Repositories;
  23. using ZhonTai.Admin.Services.Tenant.Dto;
  24. using System.Linq.Expressions;
  25. using ZhonTai.Admin.Domain.Org;
  26. using ZhonTai.Admin.Services.Pkg;
  27. using ZhonTai.Admin.Domain.ProjectLink;
  28. namespace ZhonTai.Admin.Services.Project
  29. {
  30. /// <summary>
  31. /// 项目服务
  32. /// </summary>
  33. [Order(10)]
  34. [DynamicApi(Area = AdminConsts.AreaName)]
  35. public partial class ProjectService : BaseService, IProjectService, IDynamicApi
  36. {
  37. private readonly IProjectRepository _projectRepository;
  38. private readonly IProjectPriceRepository _projectPriceRepository;
  39. private readonly IProjectRecordRepository _projectRecordRepository;
  40. private readonly IProjectLinkRepository _projectLinkRepository;
  41. public ProjectService(IProjectRepository projectRepository, IProjectPriceRepository projectPriceRepository, IProjectRecordRepository projectRecordRepository,IProjectLinkRepository projectLinkRepository)
  42. {
  43. _projectRepository = projectRepository;
  44. _projectPriceRepository = projectPriceRepository;
  45. _projectRecordRepository = projectRecordRepository;
  46. _projectLinkRepository = projectLinkRepository;
  47. }
  48. /// <summary>
  49. /// 查询
  50. /// </summary>
  51. /// <param name="id"></param>
  52. /// <returns></returns>
  53. public async Task<ProjectGetOutput> GetAsync(long id)
  54. {
  55. var project = await _projectRepository.Select
  56. .WhereDynamic(id)
  57. .FirstAsync(a => new ProjectGetOutput
  58. {
  59. Name = a.Name,
  60. Logo = a.Logo,
  61. Tips = a.Tips,
  62. SettleDay = a.SettleDay,
  63. MaxPrice = a.MaxPrice,
  64. Works = a.Works,
  65. VideoUrl = a.VideoUrl,
  66. Detail = a.Detail,
  67. Id = a.Id,
  68. prices = _projectPriceRepository.Select.Where(m => m.ProjectId == a.Id).ToList<ProjectPriceAddInput>()
  69. });
  70. return project;
  71. }
  72. /// <summary>
  73. /// 查询分页
  74. /// </summary>
  75. /// <param name="input"></param>
  76. /// <returns></returns>
  77. [HttpPost]
  78. public async Task<PageOutput<ProjectGetPageOutput>> GetPageAsync(PageInput<ProjectGetPageInput> input)
  79. {
  80. string keywords = input?.Filter.Keywrods;
  81. int? status = input?.Filter?.Status;
  82. var list = await _projectRepository.Select
  83. .WhereIf(keywords.NotNull(), (m) => m.Name.Contains(keywords))
  84. .WhereIf(status != null && status > 0, (m) => m.Status == status)
  85. .Count(out var total)
  86. .OrderByDescending(true, a => a.Id)
  87. .Page(input.CurrentPage, input.PageSize)
  88. .ToListAsync<ProjectGetPageOutput>();
  89. var data = new PageOutput<ProjectGetPageOutput>()
  90. {
  91. List = Mapper.Map<List<ProjectGetPageOutput>>(list),
  92. Total = total
  93. };
  94. return data;
  95. }
  96. /// <summary>
  97. /// 添加
  98. /// </summary>
  99. /// <param name="input"></param>
  100. /// <returns></returns>
  101. [HttpPost]
  102. [AdminTransaction]
  103. public async Task<long> AddAsync(ProjectAddInput input)
  104. {
  105. //添加项目
  106. ProjectEntity entity = Mapper.Map<ProjectEntity>(input);
  107. entity.Status = 1;
  108. ProjectEntity project = await _projectRepository.InsertAsync(entity);
  109. long projectId = project.Id;
  110. //添加项目价格
  111. var prices = input.prices.Select(m => new ProjectPriceEntity
  112. {
  113. ProjectId = projectId,
  114. Name = m.Name,
  115. Price = m.Price
  116. }).ToList();
  117. await _projectPriceRepository.InsertAsync(prices);
  118. return 1;
  119. }
  120. /// <summary>
  121. /// 修改
  122. /// </summary>
  123. /// <param name="input"></param>
  124. /// <returns></returns>
  125. public async Task UpdateAsync(ProjectUpdateInput input)
  126. {
  127. throw ResultOutput.Exception("暂未开发");
  128. var project = await _projectRepository.GetAsync(input.Id);
  129. if (!(project?.Id > 0))
  130. {
  131. throw ResultOutput.Exception("项目不存在");
  132. }
  133. //更新项目
  134. await _projectRepository.UpdateDiy.SetSource(
  135. new ProjectEntity()
  136. {
  137. Id = project.Id,
  138. Name = input.Name,
  139. Logo = input.Logo,
  140. Tips = input.Tips,
  141. SettleDay = input.SettleDay,
  142. MaxPrice = input.MaxPrice,
  143. Works = input.Works,
  144. VideoUrl = input.VideoUrl,
  145. Detail = input.Detail,
  146. })
  147. .UpdateColumns(a => new { a.Name, a.Logo, a.Tips, a.SettleDay, a.MaxPrice, a.VideoUrl, a.Detail, a.ModifiedTime }).ExecuteAffrowsAsync();
  148. //删除项目价格
  149. var PriceIds = _projectPriceRepository.Select.Where(m => m.ProjectId == project.Id).ToList(m => m.Id);
  150. var inputPrice = input.prices.Where(m => m.Id > 0).ToList();
  151. var inputPriceIds = inputPrice.Select(m => m.Id).ToList();
  152. var deletePriceIds = PriceIds.Except(inputPriceIds).ToList();
  153. if (deletePriceIds.Count() > 0)
  154. {
  155. await _projectPriceRepository.SoftDeleteAsync(a => deletePriceIds.Contains(a.Id));
  156. }
  157. //新增项目价格
  158. var addPriceIds = input.prices.Where(m => m.Id <= 0).ToList();
  159. if (addPriceIds.Count() > 0)
  160. {
  161. var list = addPriceIds.Select(m => new ProjectPriceEntity
  162. {
  163. ProjectId = project.Id,
  164. Name = m.Name,
  165. Price = m.Price
  166. }).ToList();
  167. await _projectPriceRepository.InsertAsync(list);
  168. }
  169. //更新项目价格
  170. foreach (var item in inputPrice)
  171. {
  172. await _projectPriceRepository.UpdateDiy.SetSource(
  173. new ProjectPriceEntity()
  174. {
  175. Id = item.Id,
  176. Name = item.Name,
  177. Price = item.Price
  178. })
  179. .UpdateColumns(a => new { a.Name, a.Price, a.ModifiedTime }).ExecuteAffrowsAsync();
  180. }
  181. }
  182. /// <summary>
  183. /// 更新状态
  184. /// </summary>
  185. ///<param name="Id"></param>
  186. ///<param name="status">状态 2上架 3下架 4暂停</param>
  187. /// <returns></returns>
  188. public async Task UpdateStatusAsync(long Id, int status)
  189. {
  190. var project = await _projectRepository.GetAsync(Id);
  191. if (!(project?.Id > 0))
  192. {
  193. throw ResultOutput.Exception("项目不存在");
  194. }
  195. if (!new int[] { 2, 3, 4, 5 }.Contains(status))
  196. {
  197. throw ResultOutput.Exception("请做出有效操作");
  198. }
  199. //上架项目需要验证推广码是否大于0
  200. if (status == 2) {
  201. var count= await _projectLinkRepository.Where(m=>m.ProjectId==Id&&m.IsUse==0).CountAsync();
  202. if (count <= 0) {
  203. throw ResultOutput.Exception("请去上传推广码");
  204. }
  205. }
  206. string statusText = GetStatusText(project.Status);
  207. string statusText2 = GetStatusText(status);
  208. string remark = $"{project.Name}从{statusText}变更为{statusText2}";
  209. //更新项目
  210. await _projectRepository.UpdateDiy.SetSource(
  211. new ProjectEntity()
  212. {
  213. Id = project.Id,
  214. Status = status,
  215. })
  216. .UpdateColumns(a => new { a.Status }).ExecuteAffrowsAsync();
  217. //项目记录
  218. await _projectRecordRepository.InsertAsync(new ProjectRecordEntity()
  219. {
  220. ProjectId = Id,
  221. Remark = remark,
  222. Type = status,
  223. });
  224. }
  225. #region 私有方法
  226. /// <summary>
  227. /// 获取项目状态
  228. /// </summary>
  229. /// <param name="status"></param>
  230. /// <returns></returns>
  231. private static string GetStatusText(int status) => status switch
  232. {
  233. 1 => "待上架",
  234. 2 => "上架",
  235. 3 => "下架",
  236. 4 => "暂停",
  237. 5 => "名额已满",
  238. _ => "未知状态"
  239. };
  240. #endregion
  241. }
  242. }