AdoS0QualityBaseDataControllers.cs 25 KB

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