AdoS0QualityBaseDataControllers.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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/base-types")]
  9. [AllowAnonymous]
  10. [NonUnify]
  11. public class AdoS0QmsQualityBaseTypesController : ControllerBase
  12. {
  13. private readonly SqlSugarRepository<AdoS0QmsQualityBaseType> _rep;
  14. public AdoS0QmsQualityBaseTypesController(SqlSugarRepository<AdoS0QmsQualityBaseType> 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.TypeCategory.Contains(q.Keyword!)
  25. || x.TypeCode.Contains(q.Keyword!)
  26. || x.ShortName.Contains(q.Keyword!)
  27. || (x.FullName != null && x.FullName.Contains(q.Keyword!)));
  28. var total = await query.CountAsync();
  29. var list = await query.OrderBy(x => x.TypeCategory).OrderBy(x => x.TypeCode)
  30. .Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  31. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  32. }
  33. [HttpGet("{id:long}")]
  34. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  35. [HttpPost]
  36. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsQualityBaseTypeUpsertDto dto)
  37. {
  38. // C4 唯一性 pre-check:(TypeCategory, TypeCode) 全局唯一
  39. var category = dto.TypeCategory.Trim();
  40. var code = dto.TypeCode.Trim();
  41. if (await _rep.AsQueryable().AnyAsync(x => x.TypeCategory == category && x.TypeCode == code))
  42. {
  43. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.QualityBaseTypeDuplicate,
  44. $"质量基础类型 '{category} / {code}' 已存在");
  45. }
  46. var entity = new AdoS0QmsQualityBaseType();
  47. ApplyQualityBaseType(entity, dto, true);
  48. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  49. return Ok(entity);
  50. }
  51. [HttpPut("{id:long}")]
  52. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsQualityBaseTypeUpsertDto dto)
  53. {
  54. var entity = await _rep.GetByIdAsync(id);
  55. if (entity == null) return NotFound();
  56. // C4 唯一性 pre-check:(TypeCategory, TypeCode) 全局唯一,排除自身
  57. var category = dto.TypeCategory.Trim();
  58. var code = dto.TypeCode.Trim();
  59. if (await _rep.AsQueryable().AnyAsync(x => x.TypeCategory == category && x.TypeCode == code && x.Id != id))
  60. {
  61. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.QualityBaseTypeDuplicate,
  62. $"质量基础类型 '{category} / {code}' 已存在");
  63. }
  64. ApplyQualityBaseType(entity, dto, false);
  65. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  66. return Ok(entity);
  67. }
  68. [HttpDelete("{id:long}")]
  69. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  70. private static void ApplyQualityBaseType(AdoS0QmsQualityBaseType entity, AdoS0QmsQualityBaseTypeUpsertDto dto, bool isCreate)
  71. {
  72. entity.TypeCategory = dto.TypeCategory.Trim();
  73. entity.TypeCode = dto.TypeCode.Trim();
  74. entity.ShortName = dto.ShortName.Trim();
  75. entity.FullName = NullIfWhiteSpace(dto.FullName);
  76. entity.IsActive = dto.IsActive;
  77. entity.Remark = NullIfWhiteSpace(dto.Remark);
  78. if (isCreate) entity.CreateTime = DateTime.Now;
  79. entity.ModifyTime = DateTime.Now;
  80. }
  81. }
  82. [ApiController]
  83. [Route("api/s0/quality/raw-whitelists")]
  84. [AllowAnonymous]
  85. [NonUnify]
  86. public class AdoS0QmsRawWhitelistsController : ControllerBase
  87. {
  88. private readonly SqlSugarRepository<AdoS0QmsRawWhitelist> _rep;
  89. public AdoS0QmsRawWhitelistsController(SqlSugarRepository<AdoS0QmsRawWhitelist> rep)
  90. {
  91. _rep = rep;
  92. }
  93. [HttpGet]
  94. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  95. {
  96. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  97. var query = _rep.AsQueryable()
  98. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  99. (x.SupplierCode != null && x.SupplierCode.Contains(q.Keyword!)) ||
  100. (x.SupplierName != null && x.SupplierName.Contains(q.Keyword!)) ||
  101. (x.MaterialCode != null && x.MaterialCode.Contains(q.Keyword!)) ||
  102. (x.MaterialName != null && x.MaterialName.Contains(q.Keyword!)));
  103. var total = await query.CountAsync();
  104. var list = await query.OrderBy(x => x.SupplierCode).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  105. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  106. }
  107. [HttpGet("{id:long}")]
  108. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  109. [HttpGet("options")]
  110. public async Task<IActionResult> GetOptionsAsync()
  111. {
  112. var list = await _rep.AsQueryable()
  113. .OrderBy(x => x.SupplierCode)
  114. .Select(x => new AdoS0QualitySimpleOptionDto { Value = x.Id, Label = (x.SupplierCode ?? "") + " / " + (x.SupplierName ?? "") })
  115. .ToListAsync();
  116. return Ok(list);
  117. }
  118. [HttpPost]
  119. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsRawWhitelistUpsertDto dto)
  120. {
  121. var err = ValidateRawWhitelist(dto, out var dimensionType);
  122. if (err != null) return AdoS0ApiErrors.InvalidRequest(err);
  123. var entity = new AdoS0QmsRawWhitelist
  124. {
  125. SupplierCode = NullIfWhiteSpace(dto.SupplierCode),
  126. SupplierName = NullIfWhiteSpace(dto.SupplierName),
  127. MaterialCode = NullIfWhiteSpace(dto.MaterialCode),
  128. MaterialName = NullIfWhiteSpace(dto.MaterialName),
  129. DimensionType = dimensionType
  130. };
  131. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  132. return Ok(entity);
  133. }
  134. [HttpPut("{id:long}")]
  135. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsRawWhitelistUpsertDto dto)
  136. {
  137. var entity = await _rep.GetByIdAsync(id);
  138. if (entity == null) return NotFound();
  139. var err = ValidateRawWhitelist(dto, out var dimensionType);
  140. if (err != null) return AdoS0ApiErrors.InvalidRequest(err);
  141. entity.SupplierCode = NullIfWhiteSpace(dto.SupplierCode);
  142. entity.SupplierName = NullIfWhiteSpace(dto.SupplierName);
  143. entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode);
  144. entity.MaterialName = NullIfWhiteSpace(dto.MaterialName);
  145. entity.DimensionType = dimensionType;
  146. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  147. return Ok(entity);
  148. }
  149. [HttpDelete("{id:long}")]
  150. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  151. private static string? ValidateRawWhitelist(AdoS0QmsRawWhitelistUpsertDto dto, out string dimensionType)
  152. {
  153. dimensionType = string.IsNullOrWhiteSpace(dto.DimensionType)
  154. ? "supplier"
  155. : dto.DimensionType.Trim().ToLowerInvariant();
  156. if (dimensionType is not ("supplier" or "material" or "material_supplier"))
  157. return "维度类型仅支持 supplier/material/material_supplier";
  158. if ((dimensionType == "supplier" || dimensionType == "material_supplier")
  159. && string.IsNullOrWhiteSpace(dto.SupplierCode))
  160. return "供应商维度下,供应商编码不能为空";
  161. if ((dimensionType == "supplier" || dimensionType == "material_supplier")
  162. && string.IsNullOrWhiteSpace(dto.SupplierName))
  163. return "供应商维度下,供应商名称不能为空";
  164. if ((dimensionType == "material" || dimensionType == "material_supplier")
  165. && string.IsNullOrWhiteSpace(dto.MaterialCode))
  166. return "物料维度下,物料编码不能为空";
  167. return null;
  168. }
  169. }
  170. [ApiController]
  171. [Route("api/s0/quality/sampling-schemes")]
  172. [AllowAnonymous]
  173. [NonUnify]
  174. public class AdoS0QmsSamplingSchemesController : ControllerBase
  175. {
  176. private readonly SqlSugarRepository<AdoS0QmsSamplingScheme> _rep;
  177. public AdoS0QmsSamplingSchemesController(SqlSugarRepository<AdoS0QmsSamplingScheme> rep)
  178. {
  179. _rep = rep;
  180. }
  181. [HttpGet]
  182. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  183. {
  184. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  185. var query = _rep.AsQueryable()
  186. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  187. (x.Number != null && x.Number.Contains(q.Keyword!)) ||
  188. (x.Name != null && x.Name.Contains(q.Keyword!)));
  189. var total = await query.CountAsync();
  190. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  191. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  192. }
  193. [HttpGet("{id:long}")]
  194. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  195. [HttpGet("options")]
  196. public async Task<IActionResult> GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}"));
  197. [HttpPost]
  198. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsSamplingSchemeUpsertDto dto)
  199. {
  200. var entity = new AdoS0QmsSamplingScheme();
  201. ApplySamplingScheme(entity, dto, true);
  202. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  203. return Ok(entity);
  204. }
  205. [HttpPut("{id:long}")]
  206. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsSamplingSchemeUpsertDto dto)
  207. {
  208. var entity = await _rep.GetByIdAsync(id);
  209. if (entity == null) return NotFound();
  210. ApplySamplingScheme(entity, dto, false);
  211. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  212. return Ok(entity);
  213. }
  214. [HttpDelete("{id:long}")]
  215. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  216. private static void ApplySamplingScheme(AdoS0QmsSamplingScheme entity, AdoS0QmsSamplingSchemeUpsertDto dto, bool isCreate)
  217. {
  218. entity.Number = dto.Number.Trim();
  219. entity.Name = dto.Name.Trim();
  220. entity.SamplingType = NullIfWhiteSpace(dto.SamplingType);
  221. entity.InspectionLevel = NullIfWhiteSpace(dto.InspectionLevel);
  222. entity.Strictness = NullIfWhiteSpace(dto.Strictness);
  223. entity.AqlValue = NullIfWhiteSpace(dto.AqlValue);
  224. entity.InspectionType = NullIfWhiteSpace(dto.InspectionType);
  225. entity.InspectOrgId = dto.InspectOrgId;
  226. entity.InspectUserId = NullIfWhiteSpace(dto.InspectUserId);
  227. entity.Status = NullIfWhiteSpace(dto.Status);
  228. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  229. entity.Comment = NullIfWhiteSpace(dto.Comment);
  230. entity.FixedSamplingRate = dto.FixedSamplingRate;
  231. if (isCreate) entity.CreateTime = DateTime.Now;
  232. entity.ModifyTime = DateTime.Now;
  233. }
  234. }
  235. [ApiController]
  236. [Route("api/s0/quality/instruments")]
  237. [AllowAnonymous]
  238. [NonUnify]
  239. public class AdoS0QmsInspectionInstrumentsController : ControllerBase
  240. {
  241. private readonly SqlSugarRepository<AdoS0QmsInspectionInstrument> _rep;
  242. public AdoS0QmsInspectionInstrumentsController(SqlSugarRepository<AdoS0QmsInspectionInstrument> rep)
  243. {
  244. _rep = rep;
  245. }
  246. [HttpGet]
  247. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  248. {
  249. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  250. var query = _rep.AsQueryable()
  251. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  252. (x.Number != null && x.Number.Contains(q.Keyword!)) ||
  253. (x.Name != null && x.Name.Contains(q.Keyword!)) ||
  254. (x.Model != null && x.Model.Contains(q.Keyword!)));
  255. var total = await query.CountAsync();
  256. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  257. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  258. }
  259. [HttpGet("{id:long}")]
  260. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  261. [HttpGet("options")]
  262. public async Task<IActionResult> GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}"));
  263. [HttpPost]
  264. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsInspectionInstrumentUpsertDto dto)
  265. {
  266. var entity = new AdoS0QmsInspectionInstrument();
  267. ApplyInspectionInstrument(entity, dto, true);
  268. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  269. return Ok(entity);
  270. }
  271. [HttpPut("{id:long}")]
  272. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsInspectionInstrumentUpsertDto dto)
  273. {
  274. var entity = await _rep.GetByIdAsync(id);
  275. if (entity == null) return NotFound();
  276. ApplyInspectionInstrument(entity, dto, false);
  277. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  278. return Ok(entity);
  279. }
  280. [HttpDelete("{id:long}")]
  281. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  282. private static void ApplyInspectionInstrument(AdoS0QmsInspectionInstrument entity, AdoS0QmsInspectionInstrumentUpsertDto dto, bool isCreate)
  283. {
  284. entity.Number = dto.Number.Trim();
  285. entity.Name = dto.Name.Trim();
  286. entity.Model = NullIfWhiteSpace(dto.Model);
  287. entity.Specification = NullIfWhiteSpace(dto.Specification);
  288. entity.Manufacturer = NullIfWhiteSpace(dto.Manufacturer);
  289. entity.Status = NullIfWhiteSpace(dto.Status);
  290. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  291. entity.Comment = NullIfWhiteSpace(dto.Comment);
  292. entity.GaugeCategory = NullIfWhiteSpace(dto.GaugeCategory);
  293. entity.CalibrationCycleDays = dto.CalibrationCycleDays;
  294. entity.NextCalibrationDate = NullIfWhiteSpace(dto.NextCalibrationDate);
  295. if (isCreate) entity.CreateTime = DateTime.Now;
  296. entity.ModifyTime = DateTime.Now;
  297. }
  298. }
  299. [ApiController]
  300. [Route("api/s0/quality/inspection-methods")]
  301. [AllowAnonymous]
  302. [NonUnify]
  303. public class AdoS0QmsInspectionMethodsController : ControllerBase
  304. {
  305. private readonly SqlSugarRepository<AdoS0QmsInspectionMethod> _rep;
  306. public AdoS0QmsInspectionMethodsController(SqlSugarRepository<AdoS0QmsInspectionMethod> rep)
  307. {
  308. _rep = rep;
  309. }
  310. [HttpGet]
  311. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  312. {
  313. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  314. var query = _rep.AsQueryable()
  315. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  316. (x.Number != null && x.Number.Contains(q.Keyword!)) ||
  317. (x.Name != null && x.Name.Contains(q.Keyword!)));
  318. var total = await query.CountAsync();
  319. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  320. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  321. }
  322. [HttpGet("{id:long}")]
  323. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  324. [HttpGet("options")]
  325. public async Task<IActionResult> GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}"));
  326. [HttpPost]
  327. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsInspectionMethodUpsertDto dto)
  328. {
  329. var entity = new AdoS0QmsInspectionMethod();
  330. ApplyInspectionMethod(entity, dto, true);
  331. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  332. return Ok(entity);
  333. }
  334. [HttpPut("{id:long}")]
  335. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsInspectionMethodUpsertDto dto)
  336. {
  337. var entity = await _rep.GetByIdAsync(id);
  338. if (entity == null) return NotFound();
  339. ApplyInspectionMethod(entity, dto, false);
  340. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  341. return Ok(entity);
  342. }
  343. [HttpDelete("{id:long}")]
  344. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  345. private static void ApplyInspectionMethod(AdoS0QmsInspectionMethod entity, AdoS0QmsInspectionMethodUpsertDto dto, bool isCreate)
  346. {
  347. entity.Number = dto.Number.Trim();
  348. entity.Name = dto.Name.Trim();
  349. entity.ControlStrategy = NullIfWhiteSpace(dto.ControlStrategy);
  350. entity.Status = NullIfWhiteSpace(dto.Status);
  351. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  352. entity.Comment = NullIfWhiteSpace(dto.Comment);
  353. if (isCreate) entity.CreateTime = DateTime.Now;
  354. entity.ModifyTime = DateTime.Now;
  355. }
  356. }
  357. [ApiController]
  358. [Route("api/s0/quality/inspection-items")]
  359. [AllowAnonymous]
  360. [NonUnify]
  361. public class AdoS0QmsInspectionItemsController : ControllerBase
  362. {
  363. private readonly SqlSugarRepository<AdoS0QmsInspectionItem> _rep;
  364. public AdoS0QmsInspectionItemsController(SqlSugarRepository<AdoS0QmsInspectionItem> rep)
  365. {
  366. _rep = rep;
  367. }
  368. [HttpGet]
  369. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  370. {
  371. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  372. var query = _rep.AsQueryable()
  373. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  374. (x.Number != null && x.Number.Contains(q.Keyword!)) ||
  375. (x.Name != null && x.Name.Contains(q.Keyword!)));
  376. var total = await query.CountAsync();
  377. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  378. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  379. }
  380. [HttpGet("{id:long}")]
  381. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  382. [HttpGet("options")]
  383. public async Task<IActionResult> GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}"));
  384. [HttpPost]
  385. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsInspectionItemUpsertDto dto)
  386. {
  387. var entity = new AdoS0QmsInspectionItem();
  388. ApplyInspectionItem(entity, dto, true);
  389. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  390. return Ok(entity);
  391. }
  392. [HttpPut("{id:long}")]
  393. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsInspectionItemUpsertDto dto)
  394. {
  395. var entity = await _rep.GetByIdAsync(id);
  396. if (entity == null) return NotFound();
  397. ApplyInspectionItem(entity, dto, false);
  398. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  399. return Ok(entity);
  400. }
  401. [HttpDelete("{id:long}")]
  402. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  403. private static void ApplyInspectionItem(AdoS0QmsInspectionItem entity, AdoS0QmsInspectionItemUpsertDto dto, bool isCreate)
  404. {
  405. entity.Number = dto.Number.Trim();
  406. entity.Name = dto.Name.Trim();
  407. entity.CheckMethodId = dto.CheckMethodId;
  408. entity.CheckBasisId = dto.CheckBasisId;
  409. entity.CheckInstructId = dto.CheckInstructId;
  410. entity.RadioGroupField = NullIfWhiteSpace(dto.RadioGroupField);
  411. entity.RadioGroupField1 = NullIfWhiteSpace(dto.RadioGroupField1);
  412. entity.MetricType = dto.MetricType;
  413. entity.Status = NullIfWhiteSpace(dto.Status);
  414. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  415. entity.Comment = NullIfWhiteSpace(dto.Comment);
  416. if (isCreate) entity.CreateTime = DateTime.Now;
  417. entity.ModifyTime = DateTime.Now;
  418. }
  419. }
  420. [ApiController]
  421. [Route("api/s0/quality/inspection-frequencies")]
  422. [AllowAnonymous]
  423. [NonUnify]
  424. public class AdoS0QmsInspectionFrequenciesController : ControllerBase
  425. {
  426. private readonly SqlSugarRepository<AdoS0QmsInspectionFrequency> _rep;
  427. public AdoS0QmsInspectionFrequenciesController(SqlSugarRepository<AdoS0QmsInspectionFrequency> rep)
  428. {
  429. _rep = rep;
  430. }
  431. [HttpGet]
  432. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  433. {
  434. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  435. var query = _rep.AsQueryable()
  436. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  437. (x.Number != null && x.Number.Contains(q.Keyword!)) ||
  438. (x.Name != null && x.Name.Contains(q.Keyword!)));
  439. var total = await query.CountAsync();
  440. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  441. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  442. }
  443. [HttpGet("{id:long}")]
  444. public async Task<IActionResult> GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id));
  445. [HttpGet("options")]
  446. public async Task<IActionResult> GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}"));
  447. [HttpPost]
  448. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsInspectionFrequencyUpsertDto dto)
  449. {
  450. var entity = new AdoS0QmsInspectionFrequency();
  451. ApplyInspectionFrequency(entity, dto, true);
  452. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  453. return Ok(entity);
  454. }
  455. [HttpPut("{id:long}")]
  456. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsInspectionFrequencyUpsertDto dto)
  457. {
  458. var entity = await _rep.GetByIdAsync(id);
  459. if (entity == null) return NotFound();
  460. ApplyInspectionFrequency(entity, dto, false);
  461. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  462. return Ok(entity);
  463. }
  464. [HttpDelete("{id:long}")]
  465. public async Task<IActionResult> DeleteAsync(long id) => await DeleteByIdAsync(_rep, id);
  466. private static void ApplyInspectionFrequency(AdoS0QmsInspectionFrequency entity, AdoS0QmsInspectionFrequencyUpsertDto dto, bool isCreate)
  467. {
  468. entity.Number = dto.Number.Trim();
  469. entity.Name = dto.Name.Trim();
  470. entity.Remark = NullIfWhiteSpace(dto.Remark);
  471. entity.Status = NullIfWhiteSpace(dto.Status);
  472. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  473. if (isCreate) entity.CreateTime = DateTime.Now;
  474. entity.ModifyTime = DateTime.Now;
  475. }
  476. }
  477. internal static partial class AdoS0QmsControllerHelpers
  478. {
  479. internal static async Task<IActionResult> DeleteByIdAsync<TEntity>(SqlSugarRepository<TEntity> rep, long id) where TEntity : class, new()
  480. {
  481. var item = await rep.GetByIdAsync(id);
  482. if (item == null) return new NotFoundResult();
  483. await rep.DeleteAsync(item);
  484. return new OkObjectResult(new { message = "删除成功" });
  485. }
  486. internal static IActionResult OkOrNotFound<TEntity>(TEntity? entity) where TEntity : class => entity == null ? new NotFoundResult() : new OkObjectResult(entity);
  487. internal static string? NullIfWhiteSpace(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim();
  488. internal static async Task<List<AdoS0QualitySimpleOptionDto>> BuildOptionsAsync<TEntity>(
  489. ISugarQueryable<TEntity> query,
  490. Expression<Func<TEntity, long>> valueExp,
  491. Expression<Func<TEntity, string>> labelExp) where TEntity : class, new()
  492. {
  493. var rows = await query.ToListAsync();
  494. var valueGetter = valueExp.Compile();
  495. var labelGetter = labelExp.Compile();
  496. return rows.Select(x => new AdoS0QualitySimpleOptionDto
  497. {
  498. Value = valueGetter(x),
  499. Label = labelGetter(x)
  500. }).OrderBy(x => x.Label).ToList();
  501. }
  502. }