AdoS0QualityBaseDataControllers.cs 23 KB

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