DemandScheduleService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using Yitter.IdGenerator;
  2. namespace Admin.NET.Plugin.AiDOP.Supply;
  3. /// <summary>
  4. /// 物料需求计划服务
  5. /// </summary>
  6. [ApiDescriptionSettings(Order = 305, Description = "物料需求计划")]
  7. [Route("api/Supply")]
  8. [AllowAnonymous]
  9. [NonUnify]
  10. public class DemandScheduleService : IDynamicApiController, ITransient
  11. {
  12. private readonly ISqlSugarClient _db;
  13. private readonly SqlSugarRepository<DemandSchedule> _rep;
  14. private readonly UserManager _userManager;
  15. public DemandScheduleService(
  16. ISqlSugarClient db,
  17. SqlSugarRepository<DemandSchedule> rep,
  18. UserManager userManager)
  19. {
  20. _db = db;
  21. _rep = rep;
  22. _userManager = userManager;
  23. }
  24. [DisplayName("物料需求计划列表")]
  25. [HttpGet("demand-schedule/list")]
  26. public async Task<object> GetList([FromQuery] DemandScheduleListInput input)
  27. {
  28. var page = input.Page <= 0 ? 1 : input.Page;
  29. var pageSize = input.PageSize <= 0 ? 10 : input.PageSize;
  30. var offset = (page - 1) * pageSize;
  31. var pars = new List<SugarParameter>();
  32. var where = new List<string>
  33. {
  34. "(a.ishistoryversion = 'N' OR a.ishistoryversion IS NULL)",
  35. "IFNULL(a.IsDeleted,0)=0"
  36. };
  37. if (_userManager.TenantId > 0)
  38. {
  39. where.Add("a.tenant_id = @TenantId");
  40. pars.Add(new SugarParameter("@TenantId", _userManager.TenantId));
  41. }
  42. if (!string.IsNullOrWhiteSpace(input.ItemNum))
  43. {
  44. where.Add("a.itemnum LIKE @ItemNum");
  45. pars.Add(new SugarParameter("@ItemNum", $"%{input.ItemNum.Trim()}%"));
  46. }
  47. if (!string.IsNullOrWhiteSpace(input.Descr))
  48. {
  49. where.Add("b.Descr LIKE @Descr");
  50. pars.Add(new SugarParameter("@Descr", $"%{input.Descr.Trim()}%"));
  51. }
  52. if (!string.IsNullOrWhiteSpace(input.RequestDateFrom) && DateTime.TryParse(input.RequestDateFrom, out var reqFrom))
  53. {
  54. where.Add("a.requestdate >= @RequestDateFrom");
  55. pars.Add(new SugarParameter("@RequestDateFrom", reqFrom.Date));
  56. }
  57. var fromSql = $"""
  58. FROM ic_demandschedule a
  59. INNER JOIN ItemMaster b ON a.itemnum = b.ItemNum AND CAST(a.factory_id AS CHAR(64)) = CAST(b.Domain AS CHAR(64))
  60. WHERE {string.Join(" AND ", where)}
  61. """;
  62. var total = await _db.Ado.GetIntAsync($"SELECT COUNT(1) {fromSql}", pars);
  63. var list = await _db.Ado.SqlQueryAsync<DemandScheduleListRow>(
  64. $"""
  65. SELECT
  66. a.Id AS Id,
  67. a.itemnum AS ItemNum,
  68. b.Descr AS Descr,
  69. b.Descr1 AS Descr1,
  70. a.fversion AS FVersion,
  71. a.drawing AS Drawing,
  72. a.requestdate AS RequestDate,
  73. a.arrivaldate AS ArrivalDate,
  74. a.shortqty AS ShortQty,
  75. a.mesqty AS MesQty,
  76. a.locqty AS LocQty,
  77. a.sechedqty AS SechedQty,
  78. a.tosechedqty AS ToSechedQty,
  79. CASE IFNULL(a.status,'') WHEN 'P' THEN '已发布' ELSE '' END AS StatusText,
  80. CASE IFNULL(a.status,'') WHEN 'P' THEN DATE_FORMAT(a.update_time,'%Y-%m-%d') ELSE '' END AS PublishDate,
  81. IFNULL(a.status,'') AS Status,
  82. a.remarks AS Remarks,
  83. a.tenant_id AS TenantId,
  84. a.factory_id AS FactoryId,
  85. a.company_id AS CompanyId
  86. {fromSql}
  87. ORDER BY a.requestdate DESC, a.Id DESC
  88. LIMIT {pageSize} OFFSET {offset}
  89. """,
  90. pars);
  91. return new { total, page, pageSize, list };
  92. }
  93. [DisplayName("获取物料需求计划详情")]
  94. [HttpGet("demand-schedule/{id:long}")]
  95. public async Task<object> GetDetail(long id)
  96. {
  97. var row = await _rep.GetFirstAsync(x => x.Id == id && (x.IsDeleted == null || x.IsDeleted == 0));
  98. if (row == null) throw Oops.Oh("记录不存在");
  99. return row;
  100. }
  101. [DisplayName("保存物料需求计划")]
  102. [ApiDescriptionSettings(Name = "SaveDemandSchedule"), HttpPost("demand-schedule/save")]
  103. public async Task<object> Save([FromBody] DemandScheduleSaveInput input)
  104. {
  105. if (string.IsNullOrWhiteSpace(input.ItemNum)) throw Oops.Oh("物料编号不能为空");
  106. if (input.Id is null or 0)
  107. {
  108. var entity = input.Adapt<DemandSchedule>();
  109. entity.Id = YitIdHelper.NextId();
  110. entity.ItemNum = input.ItemNum?.Trim();
  111. entity.Status ??= string.Empty;
  112. entity.IsHistoryVersion ??= "N";
  113. entity.IsDeleted ??= 0;
  114. entity.TenantId ??= _userManager.TenantId;
  115. entity.CompanyId ??= 1000;
  116. entity.CreateBy = _userManager.UserId;
  117. entity.CreateByName = _userManager.Account;
  118. entity.CreateTime = DateTime.Now;
  119. entity.UpdateBy = _userManager.UserId;
  120. entity.UpdateByName = _userManager.Account;
  121. entity.UpdateTime = DateTime.Now;
  122. entity.RequestDate = ParseDate(input.RequestDate);
  123. entity.ArrivalDate = ParseDate(input.ArrivalDate);
  124. await _rep.InsertAsync(entity);
  125. return new { id = entity.Id, message = "新增成功" };
  126. }
  127. else
  128. {
  129. var entity = await _rep.GetFirstAsync(x => x.Id == input.Id.Value && (x.IsDeleted == null || x.IsDeleted == 0))
  130. ?? throw Oops.Oh("记录不存在");
  131. entity.ItemNum = input.ItemNum?.Trim();
  132. entity.FVersion = input.FVersion;
  133. entity.Drawing = input.Drawing;
  134. entity.RequestDate = ParseDate(input.RequestDate);
  135. entity.ArrivalDate = ParseDate(input.ArrivalDate);
  136. entity.ShortQty = input.ShortQty;
  137. entity.MesQty = input.MesQty;
  138. entity.LocQty = input.LocQty;
  139. entity.SechedQty = input.SechedQty;
  140. entity.ToSechedQty = input.ToSechedQty;
  141. entity.Remarks = input.Remarks;
  142. entity.CompanyId = input.CompanyId ?? entity.CompanyId ?? 1000;
  143. entity.FactoryId = input.FactoryId ?? entity.FactoryId;
  144. entity.UpdateBy = _userManager.UserId;
  145. entity.UpdateByName = _userManager.Account;
  146. entity.UpdateTime = DateTime.Now;
  147. await _rep.UpdateAsync(entity);
  148. return new { id = entity.Id, message = "编辑成功" };
  149. }
  150. }
  151. [DisplayName("删除物料需求计划")]
  152. [HttpPost("demand-schedule/delete/{id:long}")]
  153. public async Task<object> Delete(long id)
  154. {
  155. var entity = await _rep.GetFirstAsync(x => x.Id == id && (x.IsDeleted == null || x.IsDeleted == 0))
  156. ?? throw Oops.Oh("记录不存在");
  157. entity.IsDeleted = 1;
  158. entity.UpdateBy = _userManager.UserId;
  159. entity.UpdateByName = _userManager.Account;
  160. entity.UpdateTime = DateTime.Now;
  161. await _rep.UpdateAsync(entity);
  162. return new { message = "删除成功" };
  163. }
  164. [DisplayName("勾选发布")]
  165. [HttpPost("demand-schedule/publish-selected")]
  166. public async Task<object> PublishSelected([FromBody] DemandScheduleBatchIdsInput input)
  167. {
  168. var ids = ParseIds(input.Ids);
  169. if (!ids.Any()) throw Oops.Oh("请选择要发布的数据");
  170. var affected = await _db.Updateable<DemandSchedule>()
  171. .SetColumns(x => new DemandSchedule
  172. {
  173. Status = "P",
  174. UpdateBy = _userManager.UserId,
  175. UpdateByName = _userManager.Account,
  176. UpdateTime = DateTime.Now
  177. })
  178. .Where(x => ids.Contains(x.Id) && (x.IsDeleted == null || x.IsDeleted == 0) && (x.Status == null || x.Status == ""))
  179. .ExecuteCommandAsync();
  180. return new { affected, message = affected > 0 ? "发布成功" : "无可发布数据" };
  181. }
  182. [DisplayName("全部发布")]
  183. [HttpPost("demand-schedule/publish-all")]
  184. public async Task<object> PublishAll()
  185. {
  186. var query = _db.Updateable<DemandSchedule>()
  187. .SetColumns(x => new DemandSchedule
  188. {
  189. Status = "P",
  190. UpdateBy = _userManager.UserId,
  191. UpdateByName = _userManager.Account,
  192. UpdateTime = DateTime.Now
  193. })
  194. .Where(x => (x.IsDeleted == null || x.IsDeleted == 0) && (x.Status == null || x.Status == ""));
  195. if (_userManager.TenantId > 0)
  196. query = query.Where(x => x.TenantId == _userManager.TenantId);
  197. var affected = await query.ExecuteCommandAsync();
  198. return new { affected, message = affected > 0 ? "全部发布成功" : "无可发布数据" };
  199. }
  200. [DisplayName("取消发布")]
  201. [HttpPost("demand-schedule/unpublish-selected")]
  202. public async Task<object> UnPublishSelected([FromBody] DemandScheduleBatchIdsInput input)
  203. {
  204. var ids = ParseIds(input.Ids);
  205. if (!ids.Any()) throw Oops.Oh("请选择要取消发布的数据");
  206. var affected = await _db.Updateable<DemandSchedule>()
  207. .SetColumns(x => new DemandSchedule
  208. {
  209. Status = "",
  210. UpdateBy = _userManager.UserId,
  211. UpdateByName = _userManager.Account,
  212. UpdateTime = DateTime.Now
  213. })
  214. .Where(x => ids.Contains(x.Id) && (x.IsDeleted == null || x.IsDeleted == 0) && x.Status == "P")
  215. .ExecuteCommandAsync();
  216. return new { affected, message = affected > 0 ? "取消发布成功" : "无可取消发布数据" };
  217. }
  218. private static DateTime? ParseDate(string? value)
  219. {
  220. return DateTime.TryParse(value, out var dt) ? dt.Date : null;
  221. }
  222. private static List<long> ParseIds(string ids)
  223. {
  224. return ids.Split(',', StringSplitOptions.RemoveEmptyEntries)
  225. .Select(x => long.TryParse(x.Trim(), out var id) ? id : 0)
  226. .Where(x => x > 0)
  227. .Distinct()
  228. .ToList();
  229. }
  230. private sealed class DemandScheduleListRow
  231. {
  232. public long Id { get; set; }
  233. public string? ItemNum { get; set; }
  234. public string? Descr { get; set; }
  235. public string? Descr1 { get; set; }
  236. public string? FVersion { get; set; }
  237. public string? Drawing { get; set; }
  238. public DateTime? RequestDate { get; set; }
  239. public DateTime? ArrivalDate { get; set; }
  240. public decimal? ShortQty { get; set; }
  241. public decimal? MesQty { get; set; }
  242. public decimal? LocQty { get; set; }
  243. public decimal? SechedQty { get; set; }
  244. public decimal? ToSechedQty { get; set; }
  245. public string? Status { get; set; }
  246. public string? StatusText { get; set; }
  247. public string? PublishDate { get; set; }
  248. public string? Remarks { get; set; }
  249. public long? TenantId { get; set; }
  250. public long? FactoryId { get; set; }
  251. public long? CompanyId { get; set; }
  252. }
  253. }