颐和api

DictionaryController.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using MadRunFabric.Common;
  7. using Microsoft.Extensions.Logging;
  8. using ConfigurationApi.IRepositories;
  9. using MongoDB.Driver;
  10. using ConfigurationApi.Models.Input;
  11. using Microsoft.AspNetCore.Authorization;
  12. using MadRunFabric.Model;
  13. namespace ConfigurationApi.Controllers
  14. {
  15. /// <summary>
  16. /// 字典,字典项管理
  17. /// </summary>
  18. [Authorize]
  19. [ApiVersion("6.0")]
  20. [Produces("application/json")]
  21. [Route("api/[controller]")]
  22. public class DictionaryController : BaseController
  23. {
  24. private readonly ILogger<DictionaryController> _logger;
  25. private readonly ISys_DictionaryBaseRepository _sys_dictionarybaserepository;
  26. private readonly ISys_DictionaryValueRepository _sys_dictionaryValuerepository;
  27. public DictionaryController(
  28. ILogger<DictionaryController> logger,
  29. ISys_DictionaryBaseRepository sys_dictionarybaserepository,
  30. ISys_DictionaryValueRepository sys_dictionaryValuerepository
  31. )
  32. {
  33. _logger = logger;
  34. _sys_dictionarybaserepository = sys_dictionarybaserepository;
  35. _sys_dictionaryValuerepository = sys_dictionaryValuerepository;
  36. }
  37. #region 字典
  38. /// <summary>
  39. /// 获取字典下拉列表
  40. /// </summary>
  41. /// <returns></returns>
  42. [HttpGet("getlistdrop")]
  43. public async Task<IActionResult> GetListDropAsync()
  44. {
  45. var list = await _sys_dictionarybaserepository.Get(x => x.statetype);
  46. return Success("获取全部数据成功", list);
  47. }
  48. /// <summary>
  49. /// 获取字典列表 by page
  50. /// </summary>
  51. /// <param name="keyword"></param>
  52. /// <param name="pageindex"></param>
  53. /// <param name="pagesize"></param>
  54. /// <returns></returns>
  55. [HttpGet("getlistbypage")]
  56. public async Task<IActionResult> GetListsByPageAsync(string keyword, int pageindex = 1, int pagesize = 10)
  57. {
  58. #region 条件信息
  59. ////排序字段
  60. var sort = Builders<Sys_DictionaryBase>.Sort.Descending("sort");
  61. var list = await _sys_dictionarybaserepository.GetByPage(null, pageindex, pagesize, sort);
  62. var redCount = await _sys_dictionarybaserepository.CountAsync(null); //获取总数
  63. //根据条件查询集合
  64. var listfilter = new List<FilterDefinition<Sys_DictionaryBase>>();
  65. listfilter.Add(Builders<Sys_DictionaryBase>.Filter.Eq("statetype", true));
  66. //模糊查询
  67. if (!string.IsNullOrEmpty(keyword))
  68. listfilter.Add(Builders<Sys_DictionaryBase>.Filter.Where(s => s.dictionarycode.Contains(keyword) || s.dictionaryname.Contains(keyword)));
  69. #endregion
  70. int recordCount = 0;
  71. if (listfilter.Count > 0)
  72. {
  73. var filter = Builders<Sys_DictionaryBase>.Filter.And(listfilter);
  74. list = await _sys_dictionarybaserepository.GetByPage(filter, pageindex, pagesize, sort);
  75. redCount = await _sys_dictionarybaserepository.CountAsync(filter); //获取总数
  76. }
  77. recordCount = int.Parse(redCount.ToString());
  78. var obj = new
  79. {
  80. state = "success",
  81. message = "成功",
  82. rows = list,
  83. total = recordCount,
  84. };
  85. return Content(obj.ToJson());
  86. }
  87. /// <summary>
  88. /// 获取字典详情 by id
  89. /// </summary>
  90. /// <param name="id">id</param>
  91. /// <returns></returns>
  92. [HttpGet("getdetailes")]
  93. public async Task<IActionResult> GetDetailsAsync(string id)
  94. {
  95. if (string.IsNullOrEmpty(id))
  96. return Error("参数错误");
  97. var model = await _sys_dictionarybaserepository.GetSingle(id);
  98. if (model == null)
  99. return Success("获取失败", null);
  100. var result = new
  101. {
  102. id = model.id,
  103. dictionarycode = model.dictionarycode,
  104. dictionaryname = model.dictionaryname,
  105. sort = model.sort,
  106. describe = model.describe,
  107. statetype = model.statetype,
  108. };
  109. return Success("获取成功!", result);
  110. }
  111. /// <summary>
  112. /// 添加字典信息
  113. /// </summary>
  114. /// <param name="input"></param>
  115. /// <returns></returns>
  116. [HttpPost("add")]
  117. public async Task<IActionResult> AddAsync(DictionaryBaseInput input)
  118. {
  119. if (string.IsNullOrEmpty(input.dictionarycode))
  120. return Error("字典标志不能为空");
  121. if (string.IsNullOrEmpty(input.dictionaryname))
  122. return Error("字典名称不能为空");
  123. long n = await _sys_dictionarybaserepository.Count(x => x.dictionarycode == input.dictionarycode&&x.statetype==true);
  124. if (n > 0)
  125. return Error("字典标志已存在");
  126. var model = new Sys_DictionaryBase();
  127. model.dictionarycode = input.dictionarycode;
  128. model.dictionaryname = input.dictionaryname;
  129. model.sort = input.sort;
  130. model.describe = input.describe;
  131. model.statetype = true;
  132. //var model = new Sys_DictionaryBase()
  133. //{
  134. // dictionarycode = "BXJB",
  135. // dictionaryname = "报修级别",
  136. // sort = 1,
  137. // describe = "",
  138. // statetype = true,
  139. //};
  140. bool b = await _sys_dictionarybaserepository.Add(model);
  141. if (b)
  142. return Success("字典添加成功");
  143. return Error("字典添加失败");
  144. }
  145. /// <summary>
  146. /// 修改字典信息
  147. /// </summary>
  148. /// <param name="input"></param>
  149. /// <returns></returns>
  150. [HttpPost("update")]
  151. public async Task<IActionResult> UpdateAsync(DictionaryBaseInput input)
  152. {
  153. if (string.IsNullOrEmpty(input.dictionarycode))
  154. return Error("字典标志不能为空");
  155. if (string.IsNullOrEmpty(input.dictionaryname))
  156. return Error("字典名称不能为空");
  157. if (string.IsNullOrEmpty(input.id))
  158. return Error("参数错误");
  159. var model = new Sys_DictionaryBase();
  160. model = await _sys_dictionarybaserepository.GetSingle(input.id);
  161. if (model == null)
  162. return Error("操作失败");
  163. //model.dictionarycode = input.dictionarycode;
  164. model.dictionaryname = input.dictionaryname;
  165. model.sort = input.sort;
  166. model.describe = input.describe;
  167. //model.statetype = true;
  168. bool b = await _sys_dictionarybaserepository.Update(model);
  169. if (b)
  170. return Success("字典修改成功");
  171. return Error("字典修改失败");
  172. }
  173. /// <summary>
  174. /// 删除字典信息 by ids
  175. /// </summary>
  176. /// <param name="ids">string[] id</param>
  177. /// <returns></returns>
  178. [HttpPost("delete")]
  179. public async Task<IActionResult> DeleteAsync(string[] ids)
  180. {
  181. int num = 0;
  182. if (ids == null && ids.Length <= 0)
  183. return Error("请选择要删除的记录");
  184. foreach (var item in ids)
  185. {
  186. var model = await _sys_dictionarybaserepository.GetSingle(item);
  187. model.statetype = false;
  188. if (await _sys_dictionarybaserepository.Update(model))
  189. num += 1;
  190. }
  191. if (num == ids.Length)
  192. return Success("删除成功");
  193. if (num > 0 && num < ids.Length)
  194. return Error("部分删除失败,请查看后重新操作");
  195. return Error("删除失败,请查看后重新操作");
  196. }
  197. #endregion
  198. #region 字典项
  199. /// <summary>
  200. /// 获取字典下拉列表
  201. /// </summary>
  202. /// <returns></returns>
  203. [HttpGet("getdicvaluelistdrop")]
  204. public async Task<IActionResult> GetDicValueListDropAsync(string code,string projectname)
  205. {
  206. if (string.IsNullOrEmpty(code))
  207. return Success("获取全部数据成功", null); //return Error("参数错误");
  208. var list = await _sys_dictionaryValuerepository.Get(x => x.statetype == true && x.dictionarycode == code);
  209. if (!string.IsNullOrEmpty(projectname))
  210. list = list.Where(x => x.projectname == projectname);
  211. return Success("获取全部数据成功", list);
  212. }
  213. /// <summary>
  214. /// 获取字典项列表 by page
  215. /// </summary>
  216. /// <param name="keyword"></param>
  217. /// <param name="pageindex"></param>
  218. /// <param name="pagesize"></param>
  219. /// <returns></returns>
  220. [HttpGet("getdicvaluelistbypage")]
  221. public async Task<IActionResult> GetDicValueListsByPageAsync(string keyword, string dictionarycode, int pageindex = 1, int pagesize = 10)
  222. {
  223. #region 条件信息
  224. ////排序字段
  225. var sort = Builders<Sys_DictionaryValue>.Sort.Ascending("sort");
  226. var list = await _sys_dictionaryValuerepository.GetByPage(null, pageindex, pagesize, sort);
  227. var redCount = await _sys_dictionaryValuerepository.CountAsync(null); //获取总数
  228. //根据条件查询集合
  229. var listfilter = new List<FilterDefinition<Sys_DictionaryValue>>();
  230. listfilter.Add(Builders<Sys_DictionaryValue>.Filter.Eq("statetype", true));
  231. //模糊查询
  232. if (!string.IsNullOrEmpty(dictionarycode))
  233. listfilter.Add(Builders<Sys_DictionaryValue>.Filter.Eq("dictionarycode", dictionarycode));
  234. if (!string.IsNullOrEmpty(keyword))
  235. listfilter.Add(Builders<Sys_DictionaryValue>.Filter.Where(s => s.dictionarycode.Contains(keyword) || s.name.Contains(keyword)));
  236. #endregion
  237. int recordCount = 0;
  238. if (listfilter.Count > 0)
  239. {
  240. var filter = Builders<Sys_DictionaryValue>.Filter.And(listfilter);
  241. list = await _sys_dictionaryValuerepository.GetByPage(filter, pageindex, pagesize, sort);
  242. redCount = await _sys_dictionaryValuerepository.CountAsync(filter); //获取总数
  243. }
  244. recordCount = int.Parse(redCount.ToString());
  245. var obj = new
  246. {
  247. state = "success",
  248. message = "成功",
  249. rows = list,
  250. total = recordCount,
  251. };
  252. return Content(obj.ToJson());
  253. }
  254. /// <summary>
  255. /// 获取字典详情 by id
  256. /// </summary>
  257. /// <param name="id">id</param>
  258. /// <returns></returns>
  259. [HttpGet("getdicvaluedetailes")]
  260. public async Task<IActionResult> GetDicValueDetailsAsync(string id)
  261. {
  262. if (string.IsNullOrEmpty(id))
  263. return Error("参数错误");
  264. var model = await _sys_dictionaryValuerepository.GetSingle(id);
  265. if (model == null)
  266. return Success("获取失败", null);
  267. var result = new
  268. {
  269. id = model.id,
  270. dictionarycode = model.dictionarycode,
  271. valuecode = model.valuecode,
  272. name = model.name,
  273. sort = model.sort,
  274. describe = model.describe,
  275. statetype = model.statetype,
  276. projectname=model .projectname,
  277. system_image=model.system_image
  278. };
  279. return Success("获取成功!", result);
  280. }
  281. /// <summary>
  282. /// 添加字典信息
  283. /// </summary>
  284. /// <param name="input"></param>
  285. /// <returns></returns>
  286. [HttpPost("adddicvalue")]
  287. public async Task<IActionResult> AddDicValueAsync(DictionaryValueInput input)
  288. {
  289. if (string.IsNullOrEmpty(input.dictionarycode))
  290. return Error("字典标志不能为空");
  291. if (string.IsNullOrEmpty(input.name))
  292. return Error("字典项名称不能为空");
  293. var model = new Sys_DictionaryValue();
  294. //model.id = input.id;
  295. model.dictionarycode = input.dictionarycode;
  296. model.valuecode = input.valuecode;
  297. model.name = input.name;
  298. model.sort = input.sort;
  299. model.describe = input.describe;
  300. model.statetype = true;
  301. model.projectname = input.projectname;
  302. if (input.system_image != null)
  303. {
  304. model.system_image = input.system_image;
  305. }
  306. else
  307. {
  308. model.system_image = new List<FileBaseModel>();
  309. }
  310. //var model = new Sys_DictionaryValue()
  311. //{
  312. // dictionarycode = "BXJB",
  313. // valuecode = "",
  314. // name = "一般请求",
  315. // sort = 1,
  316. // describe = "",
  317. // statetype = true,
  318. //};
  319. bool b = await _sys_dictionaryValuerepository.Add(model);
  320. if (b)
  321. return Success("字典添加成功");
  322. return Error("字典添加失败");
  323. }
  324. /// <summary>
  325. /// 修改字典信息
  326. /// </summary>
  327. /// <param name="input"></param>
  328. /// <returns></returns>
  329. [HttpPost("updatedicvalue")]
  330. public async Task<IActionResult> UpdateDicValueAsync(DictionaryValueInput input)
  331. {
  332. if (string.IsNullOrEmpty(input.dictionarycode))
  333. return Error("字典标志不能为空");
  334. if (string.IsNullOrEmpty(input.name))
  335. return Error("字典项名称不能为空");
  336. if (string.IsNullOrEmpty(input.id))
  337. return Error("参数错误");
  338. var model = new Sys_DictionaryValue();
  339. model = await _sys_dictionaryValuerepository.GetSingle(input.id);
  340. if (model == null)
  341. return Error("操作失败");
  342. //model.dictionarycode = input.dictionarycode;
  343. model.name = input.name;
  344. model.sort = input.sort;
  345. model.describe = input.describe;
  346. model.projectname = input.projectname;
  347. if (input.system_image != null)
  348. {
  349. model.system_image = input.system_image;
  350. }
  351. else
  352. {
  353. model.system_image = new List<FileBaseModel>();
  354. }
  355. //model.statetype = true;
  356. bool b = await _sys_dictionaryValuerepository.Update(model);
  357. if (b)
  358. return Success("字典添加成功");
  359. return Error("字典添加失败");
  360. }
  361. /// <summary>
  362. /// 删除字典项信息 by ids
  363. /// </summary>
  364. /// <param name="ids">string[] id</param>
  365. /// <returns></returns>
  366. [HttpPost("deletedicvalue")]
  367. public async Task<IActionResult> DeleteDicValueAsync(string[] ids)
  368. {
  369. int num = 0;
  370. if (ids == null && ids.Length <= 0)
  371. return Error("请选择要删除的记录");
  372. foreach (var item in ids)
  373. {
  374. var model = await _sys_dictionaryValuerepository.GetSingle(item);
  375. model.statetype = false;
  376. if (await _sys_dictionaryValuerepository.Update(model))
  377. num += 1;
  378. }
  379. if (num == ids.Length)
  380. return Success("删除成功");
  381. if (num > 0 && num < ids.Length)
  382. return Error("部分删除失败,请查看后重新操作");
  383. return Error("删除失败,请查看后重新操作");
  384. }
  385. /// <summary>
  386. /// 获取字典详情 by id
  387. /// </summary>
  388. /// <param name="id">id</param>
  389. /// <returns></returns>
  390. [HttpGet("getdetailesbyname")]
  391. public async Task<IActionResult> GetDetailsByNameAsync(string name)
  392. {
  393. if (string.IsNullOrEmpty(name))
  394. return Error("参数错误");
  395. var model = await _sys_dictionaryValuerepository.GetSingle(x => x.name == name);
  396. if (model == null)
  397. return Success("获取失败", null);
  398. var result = new
  399. {
  400. id = model.id,
  401. dictionarycode = model.dictionarycode,
  402. valuecode = model.valuecode,
  403. name = model.name,
  404. sort = model.sort,
  405. describe = model.describe,
  406. statetype = model.statetype,
  407. projectname = model.projectname,
  408. system_image = model.system_image
  409. };
  410. return Success("获取成功!", result);
  411. }
  412. #endregion
  413. }
  414. }