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

SetAreaController.cs 15KB

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