ProjectPriceService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. using Microsoft.AspNetCore.Mvc;
  2. using NPOI.SS.Formula.Functions;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using ZhonTai.Admin.Core.Consts;
  9. using ZhonTai.Admin.Core.Dto;
  10. using ZhonTai.Admin.Domain.Org;
  11. using ZhonTai.Admin.Domain.Project;
  12. using ZhonTai.Admin.Domain.Tenant;
  13. using ZhonTai.Admin.Services.Project.Dto;
  14. using ZhonTai.Common.Extensions;
  15. using ZhonTai.DynamicApi;
  16. using ZhonTai.DynamicApi.Attributes;
  17. namespace ZhonTai.Admin.Services.Project
  18. {
  19. /// <summary>
  20. /// 项目价格设置服务
  21. /// </summary>
  22. [Order(10)]
  23. [DynamicApi(Area = AdminConsts.AreaName)]
  24. public partial class ProjectPriceService : BaseService, IProjectService, IDynamicApi
  25. {
  26. private readonly IProjectRepository _projectRepository;
  27. private readonly IProjectPriceRepository _projectPriceRepository;
  28. private readonly IProjectConfigRepository _projectConfigRepository;
  29. private readonly IOrgRepository _orgRepository;
  30. private readonly ITenantRepository _tenantRepository;
  31. public ProjectPriceService(IProjectRepository projectRepository, IProjectPriceRepository projectPriceRepository, IProjectConfigRepository projectConfigRepository, IOrgRepository orgRepository, ITenantRepository tenantRepository)
  32. {
  33. _projectRepository = projectRepository;
  34. _projectPriceRepository = projectPriceRepository;
  35. _projectConfigRepository = projectConfigRepository;
  36. _orgRepository = orgRepository;
  37. _tenantRepository = tenantRepository;
  38. }
  39. #region 公司默认抽成比例
  40. /// <summary>
  41. /// 查询公司抽成比例
  42. /// </summary>
  43. /// <returns></returns>
  44. [HttpGet]
  45. public async Task<CompanyDrawOutput> GetCompanyDrawAsync()
  46. {
  47. await ProjectPriceInitAsync();
  48. var list = await _projectConfigRepository.Select
  49. .Where(m => m.ProjectId == 0 && m.ProjectPriceId == 0 && m.Status == 1)
  50. .OrderByDescending(m=>m.EffectDate)
  51. .ToListAsync(a => new
  52. {
  53. a.Id,
  54. a.DrawRatio,
  55. a.EffectDate
  56. });
  57. CompanyDrawOutput output = new CompanyDrawOutput();
  58. if (list.Count > 1)
  59. {
  60. DateTime dtnow = DateTime.Today;
  61. var next = list[0];
  62. var current = list[1];
  63. output.CurrentRatio = current.DrawRatio;
  64. output.NextRatio = next.DrawRatio;
  65. output.NextEffectDate = next.EffectDate;
  66. }
  67. else
  68. {
  69. output.CurrentRatio = list[0].DrawRatio;
  70. }
  71. return output;
  72. }
  73. /// <summary>
  74. /// 修改公司抽成比例
  75. /// </summary>
  76. /// <returns></returns>
  77. [HttpPost]
  78. public async Task EditCompanyDrawAsync(CompanyDrawInput input)
  79. {
  80. CheckDrawRatio(input.DrawRatio);
  81. CheckDrawRatioEffect(input.EffectDate);
  82. //删除旧的未生效记录
  83. var listDeleteId = await _projectConfigRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.DrawType == 1 && m.ProjectId == 0 && m.ProjectPriceId == 0 && m.Status == 1 && m.EffectDate > DateTime.Today)
  84. .ToListAsync(m => m.Id);
  85. if (listDeleteId.Count > 0)
  86. {
  87. await _projectConfigRepository.SoftDeleteAsync(m => listDeleteId.Contains(m.Id));
  88. }
  89. //添加新的比例
  90. await _projectConfigRepository.InsertAsync(new ProjectConfigEntity()
  91. {
  92. ProjectId = 0,
  93. ProjectPriceId = 0,
  94. DrawRatio = input.DrawRatio,
  95. EffectDate = input.EffectDate,
  96. Status = 1,
  97. DrawType = 1,
  98. DrawWay = 1,
  99. TenantId = 0
  100. });
  101. }
  102. #endregion
  103. #region 公司设置项目抽成比例
  104. /// <summary>
  105. /// 查询项目分页
  106. /// </summary>
  107. /// <param name="input"></param>
  108. /// <returns></returns>
  109. [HttpPost]
  110. public async Task<PageOutput<GetProjectPricePageOutput>> GetProjectPageAsync(PageInput<GetProjectPricePageInput> input)
  111. {
  112. var list = await _projectRepository.Select
  113. .Count(out var total)
  114. .OrderByDescending(true, a => a.Id)
  115. .Page(input.CurrentPage, input.PageSize)
  116. .ToListAsync(a => new GetProjectPricePageOutput()
  117. {
  118. Id = a.Id,
  119. Name = a.Name,
  120. Logo = a.Logo,
  121. Prices = _projectPriceRepository.Select.Where(m => m.ProjectId == a.Id).ToList<PriceGetPageOutput_Price>()
  122. });
  123. var data = new PageOutput<GetProjectPricePageOutput>()
  124. {
  125. List = Mapper.Map<List<GetProjectPricePageOutput>>(list),
  126. Total = total
  127. };
  128. return data;
  129. }
  130. /// <summary>
  131. /// 项目价格设价
  132. /// </summary>
  133. /// <param name="input"></param>
  134. /// <returns></returns>
  135. [HttpPost]
  136. public async Task<long> SetProjectPriceAsync(ProjectPriceSetInput input)
  137. {
  138. //验证项目价格信息是否存在
  139. var price = await _projectPriceRepository.GetAsync(input.Id);
  140. if (!(price?.Id > 0))
  141. {
  142. throw ResultOutput.Exception("信息不存在,请刷新后重试!");
  143. }
  144. if (!new int[] { 1, 2 }.Contains(input.DrawPriceWay))
  145. {
  146. throw ResultOutput.Exception("无效的设价方式!");
  147. }
  148. decimal amount = input.DrawPrice;
  149. if (input.DrawPriceWay == 1)
  150. {
  151. CheckDrawRatio(input.DrawRatio);
  152. amount = GetDrawAmount(price.Price, input.DrawRatio);
  153. }
  154. else if (input.DrawPriceWay == 2)
  155. {
  156. //抽成金额
  157. if (input.DrawPrice > price.Price || input.DrawPrice < 0)
  158. {
  159. throw ResultOutput.Exception("设置有效的抽成金额");
  160. }
  161. }
  162. //覆盖已经存在未生效的
  163. var listDeleteId = await _projectConfigRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.ProjectId == price.ProjectId && m.ProjectPriceId == price.Id && m.Status == 1 && m.EffectDate > DateTime.Today && m.DrawType == 2)
  164. .ToListAsync(m => m.Id);
  165. if (listDeleteId.Count > 0)
  166. {
  167. await _projectConfigRepository.SoftDeleteAsync(m => listDeleteId.Contains(m.Id));
  168. }
  169. //新增
  170. var effectDate = input.EffectDate.HasValue ? input.EffectDate : DateTime.Today.AddDays(1);
  171. await _projectConfigRepository.InsertAsync(new ProjectConfigEntity()
  172. {
  173. ProjectId = price.ProjectId,
  174. ProjectPriceId = price.Id,
  175. Status = 1,
  176. DrawWay = input.DrawPriceWay,
  177. DrawRatio = input.DrawRatio,
  178. DrawAmount = amount,
  179. EffectDate = effectDate,
  180. DrawType = 2,
  181. TenantId = 0
  182. });
  183. return 1;
  184. }
  185. #endregion
  186. #region 公司设置平台抽成比例
  187. /// <summary>
  188. /// 查询平台单独设价分页
  189. /// </summary>
  190. /// <param name="input"></param>
  191. /// <returns></returns>
  192. [HttpPost]
  193. public async Task<PageOutput<TenantPriceGetPageOutput>> GetTenantDrawPageAsync(PageInput<TenantPriceGetPageInput> input)
  194. {
  195. var projectId = input?.Filter?.ProjectId;
  196. if (!projectId.HasValue)
  197. {
  198. projectId = 0;
  199. }
  200. //项目价格信息
  201. var projectprice = _projectPriceRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.ProjectId == projectId).ToList(m => new
  202. {
  203. m.Id,
  204. m.Name,
  205. m.Price
  206. });
  207. //平台信息
  208. var list = await _orgRepository.Select.DisableGlobalFilter(FilterNames.Tenant)
  209. .Count(out var total)
  210. .OrderByDescending(true, a => a.Id)
  211. .Page(input.CurrentPage, input.PageSize)
  212. .ToListAsync(a => new TenantPriceGetPageOutput()
  213. {
  214. Id = a.TenantId.Value,
  215. Name = a.Name,
  216. prices = _projectConfigRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.ProjectId == projectId && m.TenantId == a.TenantId && m.Status == 1 & m.DrawType == 3).ToList<TenantPriceGetPageOutput_Price>()
  217. });
  218. //项目信息同平台信息合并
  219. foreach (var item in list)
  220. {
  221. List<TenantPriceGetPageOutput_Price> prices = new List<TenantPriceGetPageOutput_Price>();
  222. foreach (var it in projectprice)
  223. {
  224. var tprice = item.prices.Where(m => m.ProjectPriceId == it.Id).FirstOrDefault();
  225. if (tprice != null)
  226. {
  227. prices.Add(new TenantPriceGetPageOutput_Price()
  228. {
  229. ProjectId = tprice.ProjectId,
  230. ProjectPriceId = tprice.ProjectPriceId,
  231. Name = it.Name,
  232. Price = it.Price,
  233. DrawPriceWay = tprice.DrawPriceWay,
  234. DrawRatio = tprice.DrawRatio,
  235. DrawPrice = tprice.DrawPrice
  236. });
  237. }
  238. else
  239. {
  240. prices.Add(new TenantPriceGetPageOutput_Price()
  241. {
  242. ProjectId = projectId.Value,
  243. ProjectPriceId = it.Id,
  244. Name = it.Name,
  245. Price = it.Price,
  246. DrawPriceWay = 0
  247. });
  248. }
  249. }
  250. item.prices = prices;
  251. }
  252. var data = new PageOutput<TenantPriceGetPageOutput>()
  253. {
  254. List = Mapper.Map<List<TenantPriceGetPageOutput>>(list),
  255. Total = total
  256. };
  257. return data;
  258. }
  259. /// <summary>
  260. /// 平台项目设置价格
  261. /// </summary>
  262. /// <param name="input"></param>
  263. /// <returns></returns>
  264. [HttpPost]
  265. public async Task<long> SetTenantDrawAsync(TenantDrawSetInput input)
  266. {
  267. if (!new[] { 1, 2 }.Contains(input.DrawPriceWay))
  268. {
  269. throw ResultOutput.Exception("无效的设价方式!");
  270. }
  271. //验证平台是否存在
  272. var tenant = await _tenantRepository.GetAsync(input.TenatntId);
  273. if (!(tenant?.Id > 0))
  274. {
  275. throw ResultOutput.Exception("平台不存在,请刷新后重试!");
  276. }
  277. //验证项目价格信息是否存在
  278. var price = await _projectPriceRepository.GetAsync(input.ProjectPriceId);
  279. if (!(price?.Id > 0))
  280. {
  281. throw ResultOutput.Exception("项目信息不存在,请刷新后重试!");
  282. }
  283. var amount = input.DrawPrice;
  284. if (input.DrawPriceWay == 1)
  285. {
  286. //抽成比例
  287. CheckDrawRatio(input.DrawRatio);
  288. amount = GetDrawAmount(price.Price, input.DrawRatio);
  289. }
  290. else if (input.DrawPriceWay == 2)
  291. {
  292. //抽成金额
  293. if (input.DrawPrice > price.Price || input.DrawPrice < 0)
  294. {
  295. throw ResultOutput.Exception("设置有效的抽成金额");
  296. }
  297. }
  298. //覆盖已经存在未生效的
  299. var listDeleteId = await _projectConfigRepository.Select.DisableGlobalFilter(FilterNames.Tenant).Where(m => m.ProjectId == price.ProjectId && m.ProjectPriceId == price.Id && m.Status == 1 && m.EffectDate > DateTime.Today && m.DrawType == 3)
  300. .ToListAsync(m => m.Id);
  301. if (listDeleteId.Count > 0)
  302. {
  303. await _projectConfigRepository.SoftDeleteAsync(m => listDeleteId.Contains(m.Id));
  304. }
  305. //新增
  306. var effectDate = input.EffectDate.HasValue ? input.EffectDate : DateTime.Today.AddDays(1);
  307. await _projectConfigRepository.InsertAsync(new ProjectConfigEntity()
  308. {
  309. ProjectId = price.ProjectId,
  310. ProjectPriceId = price.Id,
  311. Status = 1,
  312. DrawWay = input.DrawPriceWay,
  313. DrawRatio = input.DrawRatio,
  314. DrawAmount = amount,
  315. EffectDate = effectDate,
  316. DrawType = 3,
  317. TenantId = input.TenatntId
  318. });
  319. return 1;
  320. }
  321. #endregion
  322. #region 校验
  323. /// <summary>
  324. /// 校验抽成比例
  325. /// </summary>
  326. /// <param name="drawRatio"></param>
  327. private void CheckDrawRatio(decimal drawRatio)
  328. {
  329. //抽成比例
  330. if (drawRatio < 0 || drawRatio > 100)
  331. {
  332. throw ResultOutput.Exception("抽成比例有效范围在0-100");
  333. }
  334. }
  335. /// <summary>
  336. /// 校验抽成比例生效时间
  337. /// </summary>
  338. /// <param name="effectDate"></param>
  339. private void CheckDrawRatioEffect(DateTime effectDate)
  340. {
  341. //生效时间
  342. if (effectDate <= DateTime.Today)
  343. {
  344. throw ResultOutput.Exception("生效日期,最短为次日生效");
  345. }
  346. }
  347. /// <summary>
  348. /// 抽成金额
  349. /// </summary>
  350. public static decimal GetDrawAmount(decimal price, decimal ratio)
  351. {
  352. var amount = UtilConvertExtension.ToDecimalCutWithN(price * ratio * 0.01m, 0, 1);
  353. return amount;
  354. }
  355. /// <summary>
  356. /// 初始化公司抽成信息
  357. /// </summary>
  358. /// <returns></returns>
  359. private async Task ProjectPriceInitAsync()
  360. {
  361. var count = await _projectConfigRepository.Select.Where(m => m.ProjectId == 0 && m.ProjectPriceId == 0).CountAsync();
  362. if (count <= 0)
  363. {
  364. await _projectConfigRepository.InsertAsync(new ProjectConfigEntity()
  365. {
  366. ProjectId = 0,
  367. ProjectPriceId = 0,
  368. DrawRatio = 5,
  369. EffectDate = DateTime.Today,
  370. Status = 1
  371. });
  372. }
  373. }
  374. #endregion
  375. }
  376. }