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

SetAreaController.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 pid=0, 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. if(pid>0)
  42. conModels.Add(new ConditionalModel() { FieldName = "F_ParentId", ConditionalType = ConditionalType.Equal, FieldValue = pid.ToString() });
  43. #endregion
  44. var list_ModuleInfo = await _sys_areaRepository.GetListALL(conModels, "F_Code asc");
  45. var treeList = new List<TreeModel>();
  46. foreach (var item in list_ModuleInfo)
  47. {
  48. TreeModel treeModel = new TreeModel();
  49. treeModel.id = item.F_Id;
  50. treeModel.code = item.F_Code;
  51. treeModel.iconcls = "";
  52. treeModel.text = item.F_AreaName;
  53. treeModel.parentid = item.F_ParentId.Value;
  54. treeList.Add(treeModel);
  55. }
  56. return Success("获取地区树成功", treeList.TreeRecursion(1));
  57. }
  58. /// <summary>
  59. /// 依据关键字获取相关菜单数据
  60. /// </summary>
  61. /// <param name="keyword"></param>
  62. /// <returns></returns>
  63. [HttpGet("gettreegridjson")]
  64. public async Task<ActionResult> GetTreeGridJson(string keyword, int level = 0)
  65. {
  66. #region 筛选
  67. List<IConditionalModel> conModels = new List<IConditionalModel>();
  68. conModels.Add(new ConditionalModel() { FieldName = "F_State", ConditionalType = ConditionalType.Equal, FieldValue = ((int)EnumDelState.Enabled).ToString() });
  69. if (level > 0)
  70. conModels.Add(new ConditionalModel() { FieldName = "F_Level", ConditionalType = ConditionalType.LessThanOrEqual, FieldValue = level.ToString() });
  71. #endregion
  72. var list_ModuleInfo = await _sys_areaRepository.GetListALL(conModels, "F_Code asc");
  73. if (!string.IsNullOrEmpty(keyword))
  74. {
  75. list_ModuleInfo = list_ModuleInfo.ToList().TreeWhere(t => t.F_AreaName.Contains(keyword) || t.F_Code.StartsWith(keyword) || t.F_Code=="0", "F_Id", "F_ParentId").ToList();
  76. }
  77. list_ModuleInfo = list_ModuleInfo.OrderBy(x => x.F_Sort).ThenByDescending(x => x.F_LastModifyOn).ToList();//排序
  78. var treeList = new List<TreeGridModel>();
  79. foreach (var item in list_ModuleInfo)
  80. {
  81. TreeGridModel treeModel = new TreeGridModel();
  82. bool hasChildren = list_ModuleInfo.Count(t => t.F_ParentId == item.F_Id) == 0 ? false : true;
  83. treeModel.id = item.F_Id.ToString();
  84. treeModel.isLeaf = hasChildren;
  85. treeModel.parentId = item.F_ParentId.ToString();
  86. treeModel.expanded = hasChildren;
  87. treeModel.entityJson = item.ToJson();
  88. treeList.Add(treeModel);
  89. }
  90. return Content(treeList.TreeGridJson());
  91. }
  92. /// <summary>
  93. /// 获取实体
  94. /// </summary>
  95. [HttpGet("getdetails")]
  96. public async Task<IActionResult> GetDetails(int id)
  97. {
  98. if (id > 0)
  99. {
  100. var dModel = await _sys_areaRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  101. if (dModel != null)
  102. {
  103. return Success("获取成功", dModel);
  104. }
  105. else
  106. {
  107. return Error("获取失败");
  108. }
  109. }
  110. else
  111. {
  112. return Error("获取参数失败");
  113. }
  114. }
  115. [HttpGet("getareasingle")]
  116. public async Task<IActionResult> GetAreasingle(int id)
  117. {
  118. var entity = await _sys_areaRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  119. if (entity == null)
  120. return Error("加载失败");
  121. #region 转换数据格式
  122. var ids = await GetTypeIDAsync(entity.F_Id);
  123. List<int> idds = new List<int>();
  124. foreach (var item in ids.Split(','))
  125. {
  126. idds.Add(int.Parse(item));
  127. }
  128. #endregion
  129. var obj = new
  130. {
  131. model = entity,
  132. levelid = idds.ToArray(),
  133. levelname = (await GetTypeNameAsync(entity.F_Id)).Split(',')
  134. };
  135. return Success("加载成功", obj);
  136. }
  137. /// <summary>
  138. /// 添加
  139. /// </summary>
  140. [HttpPost("add")]
  141. public async Task<IActionResult> Add(AreaInput input)
  142. {
  143. #region 验证
  144. if (string.IsNullOrWhiteSpace(input.areaname))
  145. {
  146. return Error("地区名称不能为空");
  147. }
  148. #endregion
  149. T_Sys_Area clmodel = new T_Sys_Area();
  150. clmodel.F_AreaName = input.areaname;
  151. clmodel.F_Code = input.code;
  152. clmodel.F_ParentCode = input.parentcode;
  153. clmodel.F_ParentId = input.parentid;
  154. clmodel.F_Level = input.level;
  155. clmodel.F_Sort = input.sort;
  156. clmodel.F_Remark = input.remark;
  157. clmodel.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
  158. clmodel.F_CreateOn = DateTime.Now;
  159. clmodel.F_State = (int)EnumDelState.Enabled;
  160. var res = await _sys_areaRepository.Add(clmodel);
  161. if (res > 0)
  162. return Success("保存成功");
  163. else
  164. {
  165. return Error("保存失败");
  166. }
  167. }
  168. /// <summary>
  169. /// 修改
  170. /// </summary>
  171. [HttpPost("update")]
  172. public async Task<IActionResult> Edit(AreaInput input)
  173. {
  174. #region
  175. if (input.id <= 0)
  176. return Error("请选择要编辑的数据");
  177. if (string.IsNullOrWhiteSpace(input.areaname))
  178. {
  179. return Error("地区名称不能为空");
  180. }
  181. #endregion
  182. var clmodel = await _sys_areaRepository.GetSingle(x => x.F_Id == input.id && x.F_State == (int)EnumDelState.Enabled);
  183. if (clmodel == null)
  184. return Error("信息获取失败");
  185. clmodel.F_AreaName = input.areaname;
  186. clmodel.F_Code = input.code;
  187. clmodel.F_ParentCode = input.parentcode;
  188. clmodel.F_ParentId = input.parentid;
  189. clmodel.F_Level = input.level;
  190. clmodel.F_Sort = input.sort;
  191. clmodel.F_Remark = input.remark;
  192. clmodel.F_LastModifyBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  193. clmodel.F_LastModifyOn = DateTime.Now;
  194. var res = await _sys_areaRepository.Update(clmodel);
  195. if (res)
  196. return Success("保存成功");
  197. else
  198. {
  199. return Error("保存失败");
  200. }
  201. }
  202. [HttpPost("delete")]
  203. public async Task<IActionResult> Remove(int[] ids)
  204. {
  205. //使用逻辑删除
  206. //物理删除的数据无法恢复
  207. var res = 0;
  208. if (ids != null && ids.Length > 0)
  209. {
  210. //获取选中的下所有级联子集id
  211. List<int> _ids = await GetTreeModuleId(new List<int>(ids));
  212. foreach (var item in _ids)
  213. {
  214. var ml = await _sys_areaRepository.GetSingle(x => x.F_Id == item);
  215. ml.F_State = (int)EnumDelState.Delete;
  216. ml.F_DeleteOn = DateTime.Now.ToLocalTime();
  217. ml.F_DeleteBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  218. if (_sys_areaRepository.Update(ml).Result)
  219. res += 1;
  220. }
  221. if (res == ids.Length)
  222. return Success("删除成功");
  223. else if (res > 0 && res < ids.Length)
  224. return Error("部分删除失败,请查看后重新操作");
  225. else
  226. return Error("删除失败,请查看后重新操作");
  227. }
  228. else
  229. return Error("请选择要删除的记录");
  230. }
  231. /// <summary>
  232. /// 导入地区
  233. /// </summary>
  234. /// <param name="input"></param>
  235. /// <returns></returns>
  236. [HttpPost("importarea")]
  237. public async Task<IActionResult> ImportArea()
  238. {
  239. Microsoft.AspNetCore.Http.IFormFile _upfile = Request.Form.Files[0];
  240. 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"))
  241. return Error($"请正确上传Excel文件:file.ContentType={_upfile.ContentType}");
  242. //将数据导入数据库
  243. int headrow = 0;
  244. NPOIHelper npoi = new NPOIHelper();
  245. var dtExcel = npoi.ExcelToTable1(_upfile, headrow);
  246. if (dtExcel.Rows.Count <= 0)
  247. return Error("请选择要导入的文件");
  248. IList<AreaInput> IDatas = ModelConvertHelper<AreaInput>.ConvertToModel(dtExcel);
  249. List<AreaInput> Datas = ModelConvertHelper<AreaInput>.ConvertIListToList(IDatas);
  250. int num = Datas.Count;
  251. List<AreaInput> inputlist = new List<AreaInput>();
  252. foreach (AreaInput input in Datas)
  253. {
  254. Expression<Func<T_Sys_Area, bool>> eq = a => a.F_Code == input.code;
  255. eq = eq.And(b => b.F_AreaName == input.areaname);
  256. if (await _sys_areaRepository.GetCount(eq) > 0)
  257. {
  258. return Error("地区 有重复:" + input.areaname);
  259. }
  260. inputlist.Add(input);
  261. }
  262. if (await AddForInput(inputlist))
  263. {
  264. return Success("已导入数据");
  265. }
  266. else
  267. {
  268. return Error("导入失败");
  269. }
  270. }
  271. #region 私有方法
  272. /// <summary>
  273. /// 获取分类
  274. /// </summary>
  275. /// <param name="typeid"></param>
  276. /// <returns></returns>
  277. public async Task<string> GetTypeIDAsync(int id)
  278. {
  279. var typeinfo = await _sys_areaRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  280. string type = string.Empty;
  281. if (typeinfo != null)
  282. {
  283. if (typeinfo.F_ParentId != null)
  284. {
  285. type = await GetTypeIDAsync(typeinfo.F_ParentId.Value);
  286. }
  287. if (string.IsNullOrEmpty(type))
  288. {
  289. type = typeinfo.F_Id.ToString();
  290. }
  291. else
  292. {
  293. type += "," + typeinfo.F_Id.ToString();
  294. }
  295. }
  296. return type;
  297. }
  298. public async Task<string> GetTypeNameAsync(int id)
  299. {
  300. var typeinfo = await _sys_areaRepository.GetSingle(x => x.F_Id == id && x.F_State == (int)EnumDelState.Enabled);
  301. string type = string.Empty;
  302. if (typeinfo != null)
  303. {
  304. if (typeinfo.F_ParentId != null)
  305. {
  306. type = await GetTypeNameAsync(typeinfo.F_ParentId.Value);
  307. }
  308. if (string.IsNullOrEmpty(type))
  309. {
  310. type = typeinfo.F_AreaName;
  311. }
  312. else
  313. {
  314. type += "," + typeinfo.F_AreaName;
  315. }
  316. }
  317. return type;
  318. }
  319. /// <summary>
  320. /// 获取所有子节点的Id
  321. /// </summary>
  322. /// <param name="ids"></param>
  323. /// <returns></returns>
  324. private async Task<List<int>> GetTreeModuleId(List<int> ids)
  325. {
  326. List<int> _moduleIdlist = null;
  327. List<int> _list = new List<int>(ids);
  328. var list_ModuleInfo = await _sys_areaRepository.GetListALL(x => x.F_State == (int)EnumDelState.Enabled);//DataSet 不在此处添加搜素条件,是因为计划改为缓存
  329. for (int i = 0; i < ids.Count; i++)
  330. {
  331. _moduleIdlist = list_ModuleInfo.Where(x => x.F_ParentId == ids[i]).Select(x => x.F_Id).ToList();
  332. if (_moduleIdlist.Count > 0)
  333. {
  334. _list.AddRange(_moduleIdlist);
  335. _list.AddRange(await GetTreeModuleId(_moduleIdlist));
  336. _list = _list.Distinct().ToList();
  337. }
  338. }
  339. return _list;
  340. }
  341. /// <summary>
  342. /// 导入
  343. /// </summary>
  344. /// <param name="inputs"></param>
  345. /// <returns></returns>
  346. private async Task<bool> AddForInput(List<AreaInput> inputs)
  347. {
  348. List<T_Sys_Area> inputlist = new List<T_Sys_Area>();
  349. foreach (var input in inputs)
  350. {
  351. T_Sys_Area clmodel = new T_Sys_Area();
  352. clmodel.F_AreaName = input.areaname;
  353. clmodel.F_Code = input.code;
  354. clmodel.F_ParentCode = input.parentcode;
  355. clmodel.F_ParentId = input.parentid;
  356. clmodel.F_Level = input.level;
  357. clmodel.F_Sort = input.sort;
  358. clmodel.F_Remark = input.remark;
  359. clmodel.F_CreateBy = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value; //"8000";
  360. clmodel.F_CreateOn = DateTime.Now;
  361. clmodel.F_State = (int)EnumDelState.Enabled;
  362. inputlist.Add(clmodel);
  363. }
  364. if (await _sys_areaRepository.AddMany(inputlist))
  365. {
  366. return true;
  367. }
  368. else
  369. {
  370. return false;
  371. }
  372. }
  373. #endregion
  374. }
  375. }