AdoS0QualityBaseDataControllers.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Quality;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Quality;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. using System.Linq.Expressions;
  5. using static Admin.NET.Plugin.AiDOP.Controllers.S0.Quality.AdoS0QmsControllerHelpers;
  6. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Quality;
  7. [ApiController]
  8. [Route("api/s0/quality/raw-whitelists")]
  9. [AllowAnonymous]
  10. [NonUnify]
  11. public class AdoS0QmsRawWhitelistsController : ControllerBase
  12. {
  13. private readonly SqlSugarRepository<AdoS0QmsRawWhitelist> _rep;
  14. public AdoS0QmsRawWhitelistsController(SqlSugarRepository<AdoS0QmsRawWhitelist> rep)
  15. {
  16. _rep = rep;
  17. }
  18. [HttpGet]
  19. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  20. {
  21. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  22. var query = _rep.AsQueryable()
  23. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  24. (x.SupplierCode != null && x.SupplierCode.Contains(q.Keyword!)) ||
  25. (x.SupplierName != null && x.SupplierName.Contains(q.Keyword!)));
  26. var total = await query.CountAsync();
  27. var list = await query.OrderBy(x => x.SupplierCode).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  28. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  29. }
  30. [HttpGet("{id:long}")]
  31. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  32. [HttpGet("options")]
  33. public async Task<IActionResult> GetOptionsAsync()
  34. {
  35. var list = await _rep.AsQueryable()
  36. .OrderBy(x => x.SupplierCode)
  37. .Select(x => new AdoS0QualitySimpleOptionDto { Value = x.Id, Label = (x.SupplierCode ?? "") + " / " + (x.SupplierName ?? "") })
  38. .ToListAsync();
  39. return Ok(list);
  40. }
  41. [HttpPost]
  42. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsRawWhitelistUpsertDto dto)
  43. {
  44. var entity = new AdoS0QmsRawWhitelist
  45. {
  46. SupplierCode = dto.SupplierCode.Trim(),
  47. SupplierName = dto.SupplierName.Trim()
  48. };
  49. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  50. return Ok(entity);
  51. }
  52. [HttpPut("{id:long}")]
  53. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsRawWhitelistUpsertDto dto)
  54. {
  55. var entity = await _rep.GetByIdAsync(id);
  56. if (entity == null) return NotFound();
  57. entity.SupplierCode = dto.SupplierCode.Trim();
  58. entity.SupplierName = dto.SupplierName.Trim();
  59. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  60. return Ok(entity);
  61. }
  62. [HttpDelete("{id:long}")]
  63. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  64. }
  65. [ApiController]
  66. [Route("api/s0/quality/sampling-schemes")]
  67. [AllowAnonymous]
  68. [NonUnify]
  69. public class AdoS0QmsSamplingSchemesController : ControllerBase
  70. {
  71. private readonly SqlSugarRepository<AdoS0QmsSamplingScheme> _rep;
  72. public AdoS0QmsSamplingSchemesController(SqlSugarRepository<AdoS0QmsSamplingScheme> rep)
  73. {
  74. _rep = rep;
  75. }
  76. [HttpGet]
  77. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  78. {
  79. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  80. var query = _rep.AsQueryable()
  81. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  82. (x.Number != null && x.Number.Contains(q.Keyword!)) ||
  83. (x.Name != null && x.Name.Contains(q.Keyword!)));
  84. var total = await query.CountAsync();
  85. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  86. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  87. }
  88. [HttpGet("{id:long}")]
  89. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  90. [HttpGet("options")]
  91. public async Task<IActionResult> GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}"));
  92. [HttpPost]
  93. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsSamplingSchemeUpsertDto dto)
  94. {
  95. var entity = new AdoS0QmsSamplingScheme();
  96. ApplySamplingScheme(entity, dto, true);
  97. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  98. return Ok(entity);
  99. }
  100. [HttpPut("{id:long}")]
  101. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsSamplingSchemeUpsertDto dto)
  102. {
  103. var entity = await _rep.GetByIdAsync(id);
  104. if (entity == null) return NotFound();
  105. ApplySamplingScheme(entity, dto, false);
  106. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  107. return Ok(entity);
  108. }
  109. [HttpDelete("{id:long}")]
  110. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  111. private static void ApplySamplingScheme(AdoS0QmsSamplingScheme entity, AdoS0QmsSamplingSchemeUpsertDto dto, bool isCreate)
  112. {
  113. entity.Number = dto.Number.Trim();
  114. entity.Name = dto.Name.Trim();
  115. entity.SamplingType = NullIfWhiteSpace(dto.SamplingType);
  116. entity.InspectionLevel = NullIfWhiteSpace(dto.InspectionLevel);
  117. entity.Strictness = NullIfWhiteSpace(dto.Strictness);
  118. entity.AqlValue = NullIfWhiteSpace(dto.AqlValue);
  119. entity.InspectionType = NullIfWhiteSpace(dto.InspectionType);
  120. entity.InspectOrgId = dto.InspectOrgId;
  121. entity.InspectUserId = NullIfWhiteSpace(dto.InspectUserId);
  122. entity.Status = NullIfWhiteSpace(dto.Status);
  123. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  124. entity.Comment = NullIfWhiteSpace(dto.Comment);
  125. if (isCreate) entity.CreateTime = DateTime.Now;
  126. entity.ModifyTime = DateTime.Now;
  127. }
  128. }
  129. [ApiController]
  130. [Route("api/s0/quality/instruments")]
  131. [AllowAnonymous]
  132. [NonUnify]
  133. public class AdoS0QmsInspectionInstrumentsController : ControllerBase
  134. {
  135. private readonly SqlSugarRepository<AdoS0QmsInspectionInstrument> _rep;
  136. public AdoS0QmsInspectionInstrumentsController(SqlSugarRepository<AdoS0QmsInspectionInstrument> rep)
  137. {
  138. _rep = rep;
  139. }
  140. [HttpGet]
  141. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  142. {
  143. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  144. var query = _rep.AsQueryable()
  145. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  146. (x.Number != null && x.Number.Contains(q.Keyword!)) ||
  147. (x.Name != null && x.Name.Contains(q.Keyword!)) ||
  148. (x.Model != null && x.Model.Contains(q.Keyword!)));
  149. var total = await query.CountAsync();
  150. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  151. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  152. }
  153. [HttpGet("{id:long}")]
  154. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  155. [HttpGet("options")]
  156. public async Task<IActionResult> GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}"));
  157. [HttpPost]
  158. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsInspectionInstrumentUpsertDto dto)
  159. {
  160. var entity = new AdoS0QmsInspectionInstrument();
  161. ApplyInspectionInstrument(entity, dto, true);
  162. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  163. return Ok(entity);
  164. }
  165. [HttpPut("{id:long}")]
  166. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsInspectionInstrumentUpsertDto dto)
  167. {
  168. var entity = await _rep.GetByIdAsync(id);
  169. if (entity == null) return NotFound();
  170. ApplyInspectionInstrument(entity, dto, false);
  171. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  172. return Ok(entity);
  173. }
  174. [HttpDelete("{id:long}")]
  175. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  176. private static void ApplyInspectionInstrument(AdoS0QmsInspectionInstrument entity, AdoS0QmsInspectionInstrumentUpsertDto dto, bool isCreate)
  177. {
  178. entity.Number = dto.Number.Trim();
  179. entity.Name = dto.Name.Trim();
  180. entity.Model = NullIfWhiteSpace(dto.Model);
  181. entity.Specification = NullIfWhiteSpace(dto.Specification);
  182. entity.Manufacturer = NullIfWhiteSpace(dto.Manufacturer);
  183. entity.Status = NullIfWhiteSpace(dto.Status);
  184. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  185. entity.Comment = NullIfWhiteSpace(dto.Comment);
  186. if (isCreate) entity.CreateTime = DateTime.Now;
  187. entity.ModifyTime = DateTime.Now;
  188. }
  189. }
  190. [ApiController]
  191. [Route("api/s0/quality/inspection-methods")]
  192. [AllowAnonymous]
  193. [NonUnify]
  194. public class AdoS0QmsInspectionMethodsController : ControllerBase
  195. {
  196. private readonly SqlSugarRepository<AdoS0QmsInspectionMethod> _rep;
  197. public AdoS0QmsInspectionMethodsController(SqlSugarRepository<AdoS0QmsInspectionMethod> rep)
  198. {
  199. _rep = rep;
  200. }
  201. [HttpGet]
  202. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  203. {
  204. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  205. var query = _rep.AsQueryable()
  206. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  207. (x.Number != null && x.Number.Contains(q.Keyword!)) ||
  208. (x.Name != null && x.Name.Contains(q.Keyword!)));
  209. var total = await query.CountAsync();
  210. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  211. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  212. }
  213. [HttpGet("{id:long}")]
  214. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  215. [HttpGet("options")]
  216. public async Task<IActionResult> GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}"));
  217. [HttpPost]
  218. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsInspectionMethodUpsertDto dto)
  219. {
  220. var entity = new AdoS0QmsInspectionMethod();
  221. ApplyInspectionMethod(entity, dto, true);
  222. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  223. return Ok(entity);
  224. }
  225. [HttpPut("{id:long}")]
  226. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsInspectionMethodUpsertDto dto)
  227. {
  228. var entity = await _rep.GetByIdAsync(id);
  229. if (entity == null) return NotFound();
  230. ApplyInspectionMethod(entity, dto, false);
  231. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  232. return Ok(entity);
  233. }
  234. [HttpDelete("{id:long}")]
  235. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  236. private static void ApplyInspectionMethod(AdoS0QmsInspectionMethod entity, AdoS0QmsInspectionMethodUpsertDto dto, bool isCreate)
  237. {
  238. entity.Number = dto.Number.Trim();
  239. entity.Name = dto.Name.Trim();
  240. entity.ControlStrategy = NullIfWhiteSpace(dto.ControlStrategy);
  241. entity.Status = NullIfWhiteSpace(dto.Status);
  242. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  243. entity.Comment = NullIfWhiteSpace(dto.Comment);
  244. if (isCreate) entity.CreateTime = DateTime.Now;
  245. entity.ModifyTime = DateTime.Now;
  246. }
  247. }
  248. [ApiController]
  249. [Route("api/s0/quality/inspection-items")]
  250. [AllowAnonymous]
  251. [NonUnify]
  252. public class AdoS0QmsInspectionItemsController : ControllerBase
  253. {
  254. private readonly SqlSugarRepository<AdoS0QmsInspectionItem> _rep;
  255. public AdoS0QmsInspectionItemsController(SqlSugarRepository<AdoS0QmsInspectionItem> rep)
  256. {
  257. _rep = rep;
  258. }
  259. [HttpGet]
  260. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  261. {
  262. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  263. var query = _rep.AsQueryable()
  264. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  265. (x.Number != null && x.Number.Contains(q.Keyword!)) ||
  266. (x.Name != null && x.Name.Contains(q.Keyword!)));
  267. var total = await query.CountAsync();
  268. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  269. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  270. }
  271. [HttpGet("{id:long}")]
  272. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  273. [HttpGet("options")]
  274. public async Task<IActionResult> GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}"));
  275. [HttpPost]
  276. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsInspectionItemUpsertDto dto)
  277. {
  278. var entity = new AdoS0QmsInspectionItem();
  279. ApplyInspectionItem(entity, dto, true);
  280. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  281. return Ok(entity);
  282. }
  283. [HttpPut("{id:long}")]
  284. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsInspectionItemUpsertDto dto)
  285. {
  286. var entity = await _rep.GetByIdAsync(id);
  287. if (entity == null) return NotFound();
  288. ApplyInspectionItem(entity, dto, false);
  289. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  290. return Ok(entity);
  291. }
  292. [HttpDelete("{id:long}")]
  293. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  294. private static void ApplyInspectionItem(AdoS0QmsInspectionItem entity, AdoS0QmsInspectionItemUpsertDto dto, bool isCreate)
  295. {
  296. entity.Number = dto.Number.Trim();
  297. entity.Name = dto.Name.Trim();
  298. entity.CheckMethodId = dto.CheckMethodId;
  299. entity.CheckBasisId = dto.CheckBasisId;
  300. entity.CheckInstructId = dto.CheckInstructId;
  301. entity.RadioGroupField = NullIfWhiteSpace(dto.RadioGroupField);
  302. entity.RadioGroupField1 = NullIfWhiteSpace(dto.RadioGroupField1);
  303. entity.MetricType = dto.MetricType;
  304. entity.Status = NullIfWhiteSpace(dto.Status);
  305. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  306. entity.Comment = NullIfWhiteSpace(dto.Comment);
  307. if (isCreate) entity.CreateTime = DateTime.Now;
  308. entity.ModifyTime = DateTime.Now;
  309. }
  310. }
  311. [ApiController]
  312. [Route("api/s0/quality/inspection-frequencies")]
  313. [AllowAnonymous]
  314. [NonUnify]
  315. public class AdoS0QmsInspectionFrequenciesController : ControllerBase
  316. {
  317. private readonly SqlSugarRepository<AdoS0QmsInspectionFrequency> _rep;
  318. public AdoS0QmsInspectionFrequenciesController(SqlSugarRepository<AdoS0QmsInspectionFrequency> rep)
  319. {
  320. _rep = rep;
  321. }
  322. [HttpGet]
  323. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  324. {
  325. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  326. var query = _rep.AsQueryable()
  327. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  328. (x.Number != null && x.Number.Contains(q.Keyword!)) ||
  329. (x.Name != null && x.Name.Contains(q.Keyword!)));
  330. var total = await query.CountAsync();
  331. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  332. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  333. }
  334. [HttpGet("{id:long}")]
  335. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  336. [HttpGet("options")]
  337. public async Task<IActionResult> GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}"));
  338. [HttpPost]
  339. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsInspectionFrequencyUpsertDto dto)
  340. {
  341. var entity = new AdoS0QmsInspectionFrequency();
  342. ApplyInspectionFrequency(entity, dto, true);
  343. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  344. return Ok(entity);
  345. }
  346. [HttpPut("{id:long}")]
  347. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsInspectionFrequencyUpsertDto dto)
  348. {
  349. var entity = await _rep.GetByIdAsync(id);
  350. if (entity == null) return NotFound();
  351. ApplyInspectionFrequency(entity, dto, false);
  352. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  353. return Ok(entity);
  354. }
  355. [HttpDelete("{id:long}")]
  356. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  357. private static void ApplyInspectionFrequency(AdoS0QmsInspectionFrequency entity, AdoS0QmsInspectionFrequencyUpsertDto dto, bool isCreate)
  358. {
  359. entity.Number = dto.Number.Trim();
  360. entity.Name = dto.Name.Trim();
  361. entity.Remark = NullIfWhiteSpace(dto.Remark);
  362. entity.Status = NullIfWhiteSpace(dto.Status);
  363. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  364. if (isCreate) entity.CreateTime = DateTime.Now;
  365. entity.ModifyTime = DateTime.Now;
  366. }
  367. }
  368. internal static partial class AdoS0QmsControllerHelpers
  369. {
  370. internal static async Task<IActionResult> DeleteByIdAsync<TEntity>(SqlSugarRepository<TEntity> rep, long id) where TEntity : class, new()
  371. {
  372. var item = await rep.GetByIdAsync(id);
  373. if (item == null) return new NotFoundResult();
  374. await rep.DeleteAsync(item);
  375. return new OkObjectResult(new { message = "删除成功" });
  376. }
  377. internal static IActionResult OkOrNotFound<TEntity>(TEntity? entity) where TEntity : class => entity == null ? new NotFoundResult() : new OkObjectResult(entity);
  378. internal static string? NullIfWhiteSpace(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim();
  379. internal static async Task<List<AdoS0QualitySimpleOptionDto>> BuildOptionsAsync<TEntity>(
  380. ISugarQueryable<TEntity> query,
  381. Expression<Func<TEntity, long>> valueExp,
  382. Expression<Func<TEntity, string>> labelExp) where TEntity : class, new()
  383. {
  384. var rows = await query.ToListAsync();
  385. var valueGetter = valueExp.Compile();
  386. var labelGetter = labelExp.Compile();
  387. return rows.Select(x => new AdoS0QualitySimpleOptionDto
  388. {
  389. Value = valueGetter(x),
  390. Label = labelGetter(x)
  391. }).OrderBy(x => x.Label).ToList();
  392. }
  393. }