足力健后端,使用.netcore版本,合并1个项目使用

SetAreaController.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Common;
  4. using System.Common.Helpers;
  5. using System.IRepositories;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Model;
  9. using System.Security.Claims;
  10. using System.Threading.Tasks;
  11. using Microsoft.AspNetCore.Authorization;
  12. using Microsoft.AspNetCore.Mvc;
  13. using SqlSugar;
  14. using TVShoppingCallCenter_ZLJ.Models.Inputs.Traffic;
  15. namespace TVShoppingCallCenter_ZLJ.Controllers.Traffic
  16. {
  17. [Authorize]
  18. [Produces("application/json")]
  19. [Route("api/[controller]")]
  20. public class SetAreaController : BaseController
  21. {
  22. private readonly ISys_AreaRepository _sys_areaRepository;
  23. public SetAreaController(ISys_AreaRepository sys_areaRepository)
  24. {
  25. _sys_areaRepository = sys_areaRepository;
  26. }
  27. /// <summary>
  28. /// 返回树形下拉框 菜单数据
  29. /// </summary>
  30. /// <returns></returns>
  31. [HttpGet("getall")]
  32. public async Task<IActionResult> GetALL(string code,int level = 0)
  33. {
  34. #region 筛选
  35. List<IConditionalModel> conModels = new List<IConditionalModel>();
  36. conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
  37. if (level > 0)
  38. conModels.Add(new ConditionalModel() { FieldName = "F_Level", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = level.ToString() });
  39. if (!string.IsNullOrWhiteSpace(code))
  40. conModels.Add(new ConditionalModel() { FieldName = "F_Code", ConditionalType = ConditionalType.LikeLeft, FieldValue = code });
  41. #endregion
  42. var list_ModuleInfo = await _sys_areaRepository.GetListALL(conModels, "F_Code asc");
  43. var treeList = new List<TreeModel>();
  44. foreach (var item in list_ModuleInfo)
  45. {
  46. TreeModel treeModel = new TreeModel();
  47. treeModel.id = item.F_Id;
  48. treeModel.code = item.F_Code;
  49. treeModel.iconcls = "";
  50. treeModel.text = item.F_AreaName;
  51. treeModel.parentid = item.F_ParentId.Value;
  52. treeList.Add(treeModel);
  53. }
  54. return Success("获取地区树成功", treeList.TreeRecursion(1));
  55. }
  56. [HttpGet("getallbypid")]
  57. public async Task<IActionResult> GetALLByPid(int pid = 1)
  58. {
  59. #region 筛选
  60. List<IConditionalModel> conModels = new List<IConditionalModel>();
  61. conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
  62. conModels.Add(new ConditionalModel() { FieldName = "F_ParentId", ConditionalType = ConditionalType.Equal, FieldValue = pid.ToString() });
  63. #endregion
  64. var list_ModuleInfo = await _sys_areaRepository.GetListALL(conModels, "F_Code asc");
  65. var treeList = new List<TreeModel>();
  66. foreach (var item in list_ModuleInfo)
  67. {
  68. TreeModel treeModel = new TreeModel();
  69. treeModel.id = item.F_Id;
  70. treeModel.code = item.F_Code;
  71. treeModel.iconcls = "";
  72. treeModel.text = item.F_AreaName;
  73. treeModel.parentid = item.F_ParentId.Value;
  74. treeList.Add(treeModel);
  75. }
  76. return Success("获取地区树子节点成功", treeList);
  77. }
  78. /// <summary>
  79. /// 依据关键字获取相关菜单数据
  80. /// </summary>
  81. /// <param name="keyword"></param>
  82. /// <returns></returns>
  83. [HttpGet("gettreegridjson")]
  84. public async Task<ActionResult> GetTreeGridJson(string keyword, int level = 0)
  85. {
  86. #region 筛选
  87. List<IConditionalModel> conModels = new List<IConditionalModel>();
  88. conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
  89. if (level > 0)
  90. conModels.Add(new ConditionalModel() { FieldName = "F_Level", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = level.ToString() });
  91. if (!string.IsNullOrEmpty(keyword))
  92. {
  93. conModels.Add(new ConditionalCollections()
  94. {
  95. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
  96. {
  97. new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Code", ConditionalType = ConditionalType.LikeLeft, FieldValue = keyword }),
  98. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Code", ConditionalType = ConditionalType.Equal, FieldValue = "0" })
  99. }
  100. });
  101. }
  102. #endregion
  103. var list_ModuleInfo = await _sys_areaRepository.GetListALL(conModels, "F_Code asc");
  104. var treeList = new List<TreeGridModel>();
  105. foreach (var item in list_ModuleInfo)
  106. {
  107. TreeGridModel treeModel = new TreeGridModel();
  108. bool hasChildren = list_ModuleInfo.Count(t => t.F_ParentId == item.F_Id) == 0 ? false : true;
  109. treeModel.id = item.F_Id.ToString();
  110. treeModel.isLeaf = hasChildren;
  111. treeModel.parentId = item.F_ParentId.ToString();
  112. treeModel.expanded = hasChildren;
  113. treeModel.entityJson = item.ToJson();
  114. treeList.Add(treeModel);
  115. }
  116. return Content(treeList.TreeGridJson());
  117. }
  118. /// <summary>
  119. /// 获取实体
  120. /// </summary>
  121. [HttpGet("getdetails")]
  122. public async Task<IActionResult> GetDetails(int id)
  123. {
  124. if (id > 0)
  125. {
  126. var dModel = await _sys_areaRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  127. if (dModel != null)
  128. {
  129. return Success("获取成功", dModel);
  130. }
  131. else
  132. {
  133. return Error("获取失败");
  134. }
  135. }
  136. else
  137. {
  138. return Error("获取参数失败");
  139. }
  140. }
  141. [HttpGet("getareasingle")]
  142. public async Task<IActionResult> GetAreasingle(int id)
  143. {
  144. var entity = await _sys_areaRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  145. if (entity == null)
  146. return Error("加载失败");
  147. #region 转换数据格式
  148. var ids = await GetTypeIDAsync(entity.F_Id);
  149. List<int> idds = new List<int>();
  150. foreach (var item in ids.Split(','))
  151. {
  152. idds.Add(int.Parse(item));
  153. }
  154. #endregion
  155. var obj = new
  156. {
  157. model = entity,
  158. levelid = idds.ToArray(),
  159. levelname = (await GetTypeNameAsync(entity.F_Id)).Split(',')
  160. };
  161. return Success("加载成功", obj);
  162. }
  163. /// <summary>
  164. /// 添加
  165. /// </summary>
  166. [HttpPost("add")]
  167. public async Task<IActionResult> Add(AreaInput input)
  168. {
  169. #region 验证
  170. if (string.IsNullOrWhiteSpace(input.areaname))
  171. {
  172. return Error("地区名称不能为空");
  173. }
  174. #endregion
  175. T_Sys_Area clmodel = new T_Sys_Area();
  176. clmodel.F_AreaName = input.areaname;
  177. clmodel.F_Code = input.code;
  178. clmodel.F_ParentId = input.parentid;
  179. #region
  180. var pmodel = new T_Sys_Area();
  181. if (input.parentid <= 0)
  182. {
  183. clmodel.F_ParentCode = "0";
  184. clmodel.F_Level = 1;
  185. }
  186. else
  187. {
  188. pmodel = await _sys_areaRepository.GetSingle(x => x.F_Id == input.parentid && x.F_State == (int)EnumDelState.Enabled);
  189. if (pmodel != null)
  190. {
  191. clmodel.F_ParentCode = pmodel.F_Code;
  192. clmodel.F_Level = pmodel.F_Level + 1;
  193. }
  194. else
  195. return Error("父级区域有误,请重新选择");
  196. }
  197. #endregion
  198. clmodel.F_Sort = input.sort;
  199. clmodel.F_Remark = input.remark;
  200. clmodel.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
  201. clmodel.F_CreateOn = DateTime.Now;
  202. clmodel.F_State = (int)EnumDelState.Enabled;
  203. var res = await _sys_areaRepository.Add(clmodel);
  204. if (res > 0)
  205. return Success("保存成功");
  206. else
  207. {
  208. return Error("保存失败");
  209. }
  210. }
  211. /// <summary>
  212. /// 修改
  213. /// </summary>
  214. [HttpPost("update")]
  215. public async Task<IActionResult> Edit(AreaInput input)
  216. {
  217. #region
  218. if (input.id <= 0)
  219. return Error("请选择要编辑的数据");
  220. if (string.IsNullOrWhiteSpace(input.areaname))
  221. {
  222. return Error("地区名称不能为空");
  223. }
  224. #endregion
  225. var clmodel = await _sys_areaRepository.GetSingle(x => x.F_Id == input.id && x.F_State == (int)EnumDelState.Enabled);
  226. if (clmodel == null)
  227. return Error("信息获取失败");
  228. clmodel.F_AreaName = input.areaname;
  229. clmodel.F_Code = input.code;
  230. clmodel.F_ParentId = input.parentid;
  231. #region
  232. var pmodel= new T_Sys_Area();
  233. if (input.parentid <= 0)
  234. {
  235. clmodel.F_ParentCode = "0";
  236. clmodel.F_Level = 1;
  237. }
  238. else
  239. {
  240. pmodel = await _sys_areaRepository.GetSingle(x => x.F_Id == input.parentid && x.F_State == (int)EnumDelState.Enabled);
  241. if (pmodel != null)
  242. {
  243. clmodel.F_ParentCode = pmodel.F_Code;
  244. clmodel.F_Level = pmodel.F_Level + 1;
  245. }
  246. else
  247. return Error("父级区域有误,请重新选择");
  248. }
  249. #endregion
  250. clmodel.F_Sort = input.sort;
  251. clmodel.F_Remark = input.remark;
  252. clmodel.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  253. clmodel.F_LastModifyOn = DateTime.Now;
  254. var res = await _sys_areaRepository.Update(clmodel);
  255. if (res)
  256. return Success("保存成功");
  257. else
  258. {
  259. return Error("保存失败");
  260. }
  261. }
  262. [HttpPost("delete")]
  263. public async Task<IActionResult> Remove(int[] ids)
  264. {
  265. //使用逻辑删除
  266. //物理删除的数据无法恢复
  267. var res = 0;
  268. if (ids != null && ids.Length > 0)
  269. {
  270. //获取选中的下所有级联子集id
  271. List<int> _ids = await GetTreeModuleId(new List<int>(ids));
  272. foreach (var item in _ids)
  273. {
  274. var ml = await _sys_areaRepository.GetSingle(x => x.F_Id == item);
  275. ml.F_State = (int)EnumDelState.Delete;
  276. ml.F_DeleteOn = DateTime.Now.ToLocalTime();
  277. ml.F_DeleteBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  278. if (_sys_areaRepository.Update(ml).Result)
  279. res += 1;
  280. }
  281. if (res == ids.Length)
  282. return Success("删除成功");
  283. else if (res > 0 && res < ids.Length)
  284. return Error("部分删除失败,请查看后重新操作");
  285. else
  286. return Error("删除失败,请查看后重新操作");
  287. }
  288. else
  289. return Error("请选择要删除的记录");
  290. }
  291. /// <summary>
  292. /// 导入地区
  293. /// </summary>
  294. /// <param name="input"></param>
  295. /// <returns></returns>
  296. [HttpPost("importarea")]
  297. public async Task<IActionResult> ImportArea()
  298. {
  299. Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
  300. if (!_upfile.ContentType.Equals("application/vnd.ms-excel") && !_upfile.ContentType.Equals("application/x-xls") && !_upfile.ContentType.Equals("application/x-xlsx") && !_upfile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") && !_upfile.ContentType.Equals("application/octet-stream"))
  301. return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
  302. //将数据导入数据库
  303. int headrow = 0;
  304. NPOIHelper npoi = new NPOIHelper();
  305. var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
  306. if (dtExcel.Rows.Count <= 0)
  307. return Error("请选择要导入的文件");
  308. IList<AreaInput> IDatas = ModelConvertHelper<AreaInput>.ConvertToModel(dtExcel);
  309. List<AreaInput> Datas = ModelConvertHelper<AreaInput>.ConvertIListToList(IDatas);
  310. int num = Datas.Count;
  311. List<AreaInput> inputlist = new List<AreaInput>();
  312. foreach (AreaInput input in Datas)
  313. {
  314. Expression<Func<T_Sys_Area, bool>> eq = a => a.F_Code == input.code;
  315. eq = eq.And(b => b.F_AreaName == input.areaname);
  316. if (await _sys_areaRepository.GetCount(eq) > 0)
  317. {
  318. return Error("地区 有重复:" + input.areaname);
  319. }
  320. inputlist.Add(input);
  321. }
  322. if (await AddForInput(inputlist))
  323. {
  324. return Success("已导入数据");
  325. }
  326. else
  327. {
  328. return Error("导入失败");
  329. }
  330. }
  331. #region 私有方法
  332. /// <summary>
  333. /// 获取分类
  334. /// </summary>
  335. /// <param name="typeid"></param>
  336. /// <returns></returns>
  337. public async Task<string> GetTypeIDAsync(int id)
  338. {
  339. var typeinfo = await _sys_areaRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  340. string type = string.Empty;
  341. if (typeinfo != null)
  342. {
  343. if (typeinfo.F_ParentId != null)
  344. {
  345. type = await GetTypeIDAsync(typeinfo.F_ParentId.Value);
  346. }
  347. if (string.IsNullOrEmpty(type))
  348. {
  349. type = typeinfo.F_Id.ToString();
  350. }
  351. else
  352. {
  353. type += "," + typeinfo.F_Id.ToString();
  354. }
  355. }
  356. return type;
  357. }
  358. public async Task<string> GetTypeNameAsync(int id)
  359. {
  360. var typeinfo = await _sys_areaRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  361. string type = string.Empty;
  362. if (typeinfo != null)
  363. {
  364. if (typeinfo.F_ParentId != null)
  365. {
  366. type = await GetTypeNameAsync(typeinfo.F_ParentId.Value);
  367. }
  368. if (string.IsNullOrEmpty(type))
  369. {
  370. type = typeinfo.F_AreaName;
  371. }
  372. else
  373. {
  374. type += "," + typeinfo.F_AreaName;
  375. }
  376. }
  377. return type;
  378. }
  379. /// <summary>
  380. /// 获取所有子节点的Id
  381. /// </summary>
  382. /// <param name="ids"></param>
  383. /// <returns></returns>
  384. private async Task<List<int>> GetTreeModuleId(List<int> ids)
  385. {
  386. List<int> _moduleIdlist = null;
  387. List<int> _list = new List<int>(ids);
  388. var list_ModuleInfo = await _sys_areaRepository.GetListALL(x => x.F_State == (int)EnumDelState.Enabled);//DataSet 不在此处添加搜素条件,是因为计划改为缓存
  389. for (int i = 0; i < ids.Count; i++)
  390. {
  391. _moduleIdlist = list_ModuleInfo.Where(x => x.F_ParentId == ids[i]).Select(x => x.F_Id).ToList();
  392. if (_moduleIdlist.Count > 0)
  393. {
  394. _list.AddRange(_moduleIdlist);
  395. _list.AddRange(await GetTreeModuleId(_moduleIdlist));
  396. _list = _list.Distinct().ToList();
  397. }
  398. }
  399. return _list;
  400. }
  401. /// <summary>
  402. /// 导入
  403. /// </summary>
  404. /// <param name="inputs"></param>
  405. /// <returns></returns>
  406. private async Task<bool> AddForInput(List<AreaInput> inputs)
  407. {
  408. List<T_Sys_Area> inputlist = new List<T_Sys_Area>();
  409. foreach (var input in inputs)
  410. {
  411. T_Sys_Area clmodel = new T_Sys_Area();
  412. clmodel.F_AreaName = input.areaname;
  413. clmodel.F_Code = input.code;
  414. clmodel.F_ParentCode = input.parentcode;
  415. clmodel.F_ParentId = input.parentid;
  416. clmodel.F_Level = input.level;
  417. clmodel.F_Sort = input.sort;
  418. clmodel.F_Remark = input.remark;
  419. clmodel.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
  420. clmodel.F_CreateOn = DateTime.Now;
  421. clmodel.F_State = (int)EnumDelState.Enabled;
  422. inputlist.Add(clmodel);
  423. }
  424. if (await _sys_areaRepository.AddMany(inputlist))
  425. {
  426. return true;
  427. }
  428. else
  429. {
  430. return false;
  431. }
  432. }
  433. #endregion
  434. }
  435. }