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

KnowledgeController.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Common;
  4. using System.IO;
  5. using System.IRepositories;
  6. using System.Linq;
  7. using System.Model;
  8. using System.Security.Claims;
  9. using System.Threading.Tasks;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. using SqlSugar;
  13. namespace TVShoppingCallCenter_ZLJ.Controllers.knowledge
  14. {
  15. [Produces("application/json")]
  16. [Route("api/[controller]")]
  17. public class KnowledgeController : BaseController
  18. {
  19. private readonly IRepositoryCategoryRepository _repositorycategoryreposytory;
  20. private readonly IRepositoryInformationRepository _repositoryinformationrepository;
  21. private readonly ISys_AccessoriesRepository _sys_accessoriesrepository;
  22. private readonly ISys_SystemConfigRepository _sys_sysconfigRepository;
  23. public KnowledgeController(IRepositoryCategoryRepository repositorycategoryreposytory, IRepositoryInformationRepository repositoryinformationrepository, ISys_AccessoriesRepository sys_accessoriesrepository, ISys_SystemConfigRepository sys_sysconfigRepository)
  24. {
  25. _repositorycategoryreposytory = repositorycategoryreposytory;
  26. _repositoryinformationrepository = repositoryinformationrepository;
  27. _sys_accessoriesrepository = sys_accessoriesrepository;
  28. _sys_sysconfigRepository = sys_sysconfigRepository;
  29. }
  30. /// <summary>
  31. /// 添加知识库
  32. /// </summary>
  33. /// <param name="input"></param>
  34. /// <returns></returns>
  35. [HttpPost("add")]
  36. public async Task<IActionResult> AddAsync(T_RepositoryInformation input)
  37. {
  38. if (input.F_CategoryId <= 0)
  39. {
  40. return Error("请选择知识库分类");
  41. }
  42. input.F_Expand1 = _repositorycategoryreposytory.GetSingle(x => x.F_CategoryId == input.F_CategoryId)
  43. .Result != null ? _repositorycategoryreposytory.GetSingle(x => x.F_CategoryId == input.F_CategoryId)
  44. .Result.F_CategoryName : "";
  45. // string user = "8000";
  46. string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  47. input.F_CreateOn = DateTime.Now;
  48. input.F_CreateBy = user;
  49. input.F_DeleteFlag = 0;
  50. var res = await _repositoryinformationrepository.Add(input);
  51. if (res > 0)
  52. {
  53. return Success("添加成功");
  54. }
  55. else
  56. {
  57. return Error("添加失败");
  58. }
  59. }
  60. /// <summary>
  61. /// 修改知识库
  62. /// </summary>
  63. [HttpPost("update")]
  64. public async Task<IActionResult> UpdateAsync(T_RepositoryInformation input)
  65. {
  66. if (input.F_CategoryId <= 0)
  67. {
  68. return Error("请选择知识库分类");
  69. }
  70. input.F_Expand1 = _repositorycategoryreposytory.GetSingle(x => x.F_CategoryId == input.F_CategoryId)
  71. .Result != null ? _repositorycategoryreposytory.GetSingle(x => x.F_CategoryId == input.F_CategoryId)
  72. .Result.F_CategoryName : "";
  73. string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  74. var model = await _repositoryinformationrepository.GetSingle(x => x.F_RepositoryId == input.F_RepositoryId);
  75. if (model == null)
  76. return Error("操作失败");
  77. input.F_DeleteFlag = 0;
  78. input.F_CreateBy = model.F_CreateBy;
  79. input.F_CreateOn = model.F_CreateOn;
  80. input.F_ModifyBy = user;
  81. input.F_ModifyOn = DateTime.Now;
  82. var b = await _repositoryinformationrepository.Update(input);
  83. if (b)
  84. {
  85. return Success("修改成功");
  86. }
  87. return Error("修改失败");
  88. }
  89. [HttpPost("delete")]
  90. public async Task<IActionResult> Remove(int [] ids )
  91. {
  92. var res = 0;
  93. if (ids != null && ids.Length > 0)
  94. {
  95. foreach (var item in ids)
  96. {
  97. var model = await _repositoryinformationrepository.GetSingle(x => x.F_RepositoryId == item);
  98. model.F_DeleteFlag = (int)EnumUserCountState.Delete;
  99. if (_repositoryinformationrepository.Update(model).Result)
  100. res += 1;
  101. }
  102. if (res == ids.Length)
  103. return Success("删除成功");
  104. else if (res > 0 && res < ids.Length)
  105. return Error("部分删除失败,请查看后重新操作");
  106. else
  107. return Error("删除失败,请查看后重新操作");
  108. }
  109. else
  110. return Error("请选择要删除的记录");
  111. }
  112. /// <summary>
  113. /// 获取知识库详情
  114. /// </summary>
  115. /// <param name="id">id</param>
  116. /// <returns></returns>
  117. [HttpGet("getdetails")]
  118. public async Task<IActionResult> GetDetailsAsync(int id)
  119. {
  120. if (id <= 0)
  121. return Error("参数错误");
  122. var model = await _repositoryinformationrepository.GetSingle(x => x.F_RepositoryId == id);
  123. if (model == null)
  124. {
  125. return Error("获取失败");
  126. }
  127. var configfj = _sys_sysconfigRepository.GetSingle(x => x.F_ParamCode == "FileUrlPath").Result;
  128. List<T_Sys_Accessories> FileUrl = new List<T_Sys_Accessories>();
  129. FileUrl = GetFileData(model.F_Url , configfj.F_ParamValue);
  130. List<string> ParentId = new List<string>();
  131. List<string> ParentName = new List<string>();
  132. var obj = new
  133. {
  134. model.F_RepositoryId,
  135. model .F_Title ,
  136. model.F_Content,
  137. model.F_KeyWords,
  138. model.F_Description,
  139. model.F_Url,
  140. model.F_Expand1,
  141. ParentId = Category(model.F_CategoryId, 0, ParentId),
  142. ParentName = Category(model.F_CategoryId, 1, ParentName),
  143. FileUrl
  144. };
  145. return Success("获取成功!", obj);
  146. }
  147. public List<T_Sys_Accessories> GetFileData(string ids, string prefix)
  148. {
  149. List<T_Sys_Accessories> modellist = new List<T_Sys_Accessories>();
  150. List<IConditionalModel> conModels = new List<IConditionalModel>();
  151. conModels.Add(new ConditionalModel() { FieldName = "F_Id", ConditionalType = ConditionalType.In , FieldValue = ids });
  152. var model = _sys_accessoriesrepository.GetListALL(conModels, " F_CreateTime desc").Result ;
  153. foreach (var it in model)
  154. {
  155. it.F_Url = prefix + it.F_Url;
  156. modellist.Add(it);
  157. }
  158. return modellist;
  159. }
  160. private string CategoryId(int parentid)
  161. {
  162. string modelList = parentid+ "";
  163. var iv = _repositorycategoryreposytory.GetSingle (x => x.F_CategoryId == parentid && x.F_DeleteFlag == 0).Result;
  164. if (iv != null)
  165. {
  166. List<IConditionalModel> conModels = new List<IConditionalModel>();
  167. conModels.Add(new ConditionalModel() { FieldName = "F_DeleteFlag", ConditionalType = ConditionalType.Equal, FieldValue = "0" });
  168. conModels.Add(new ConditionalModel() { FieldName = "F_Expand2", ConditionalType = ConditionalType.Like, FieldValue = iv.F_Expand2 });
  169. var list = _repositorycategoryreposytory.GetListALL(conModels, " F_CreateOn desc").Result;
  170. if (list!=null )
  171. {
  172. foreach (var it in list)
  173. {
  174. modelList += "," + it.F_CategoryId;
  175. }
  176. }
  177. }
  178. return modelList;
  179. }
  180. public List<string> Category(int? CategoryId, int type, List<string> key)
  181. {
  182. var model = _repositorycategoryreposytory.GetSingle(x => x.F_CategoryId == CategoryId).Result;
  183. if (model != null)
  184. {
  185. if (type == 0)
  186. {
  187. key.Add("" + CategoryId);
  188. if (model.F_ParentId > 0)
  189. {
  190. key = Category((int)model.F_ParentId, 0, key);
  191. }
  192. }
  193. else
  194. {
  195. key.Add(model.F_CategoryName);
  196. if (model.F_ParentId > 0)
  197. {
  198. key = Category((int)model.F_ParentId, 1, key);
  199. }
  200. }
  201. }
  202. return key;
  203. }
  204. /// <summary>
  205. /// 上传附件
  206. /// </summary>
  207. /// <returns></returns>
  208. [HttpPost("upload")]
  209. public async Task<IActionResult> UploadFiles(List<IFormFile> files)
  210. {
  211. List<T_Sys_Accessories> acs = new List<T_Sys_Accessories>();
  212. //string user = "8000";
  213. string user = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
  214. string path = "\\Upload\\Files\\" + DateTime.Now.ToString("yyyy") + "\\" + DateTime.Now.ToString("MM") + "\\" + DateTime.Now.ToString("dd");
  215. string fliepath = Directory.GetCurrentDirectory() + path;
  216. //记录日志内容
  217. if (!Directory.Exists(fliepath))
  218. {
  219. Directory.CreateDirectory(fliepath);
  220. }
  221. if (files.Count > 0)
  222. {
  223. foreach (var item in files) //上传选定的文件列表
  224. {
  225. if (item.Length > 0) //文件大小 0 才上传
  226. {
  227. if (item.FileName.IndexOf ('#') != -1)
  228. {
  229. return Error ("附件名称不能包含#");
  230. }
  231. var thispath = fliepath + "\\" + item.FileName;
  232. //当前上传文件应存放的位置
  233. //上传文件
  234. using (var stream = new FileStream(thispath, FileMode.Create)) //创建特定名称的文件流
  235. {
  236. try
  237. {
  238. await item.CopyToAsync(stream); //上传文件
  239. T_Sys_Accessories model_T_Sys_Accessories = new T_Sys_Accessories();
  240. model_T_Sys_Accessories.F_CreateTime = DateTime.Now;//上传时间
  241. model_T_Sys_Accessories.F_Name = item.FileName;//附件名称
  242. model_T_Sys_Accessories.F_Type = item.FileName.Split ('.')[1];//附件类型
  243. model_T_Sys_Accessories.F_Url = path + "\\" + item.FileName;//附件地址
  244. model_T_Sys_Accessories.F_Size = item.Length;
  245. model_T_Sys_Accessories.F_CreateUser = user;//上传人
  246. var id = await _sys_accessoriesrepository.Add(model_T_Sys_Accessories);
  247. model_T_Sys_Accessories.F_Id = id;
  248. acs.Add(model_T_Sys_Accessories);
  249. }
  250. catch (Exception ex) //上传异常处理
  251. {
  252. }
  253. }
  254. }
  255. }
  256. return Success("成功", acs);
  257. }
  258. {
  259. return Error("请选择要上传的文件");
  260. }
  261. }
  262. /// <summary>
  263. /// 获取知识库列表
  264. /// </summary>
  265. /// <param name="keyword"></param>
  266. /// <param name="pageindex"></param>
  267. /// <param name="pagesize"></param>
  268. /// <returns></returns>
  269. [HttpGet("getlist")]
  270. public async Task<IActionResult> GetListMark(string keyword,int pid=-1, int pageindex = 0, int pagesize = 0)
  271. {
  272. // string user = "8000";
  273. List<IConditionalModel> conModels = new List<IConditionalModel>();
  274. #region 条件筛选
  275. conModels.Add(new ConditionalModel() { FieldName = "F_DeleteFlag", ConditionalType = ConditionalType.Equal, FieldValue = "0" });
  276. if (!string.IsNullOrEmpty(keyword))
  277. {
  278. conModels.Add(new ConditionalCollections()
  279. {
  280. ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
  281. {
  282. new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel() { FieldName = "F_Title", ConditionalType = ConditionalType.Like, FieldValue = keyword }),
  283. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_Content", ConditionalType = ConditionalType.Like, FieldValue = keyword }),
  284. new KeyValuePair<WhereType, ConditionalModel>( WhereType.Or , new ConditionalModel() { FieldName = "F_KeyWords", ConditionalType = ConditionalType.Like, FieldValue = keyword })
  285. }
  286. });
  287. }
  288. if (pid>-1)
  289. {
  290. conModels.Add(new ConditionalModel() { FieldName = "F_CategoryId", ConditionalType = ConditionalType.In , FieldValue = CategoryId(pid) });
  291. }
  292. #endregion
  293. int recordCount = 0;
  294. if (pageindex > 0 && pagesize > 0)
  295. {
  296. var list = await _repositoryinformationrepository.GetListByPage(conModels, new MyPageModel() { PageIndex = pageindex, PageSize = pagesize, PageCount = recordCount }, " F_CreateOn desc");
  297. var obj = new
  298. {
  299. state = "success",
  300. message = "成功",
  301. rows = list,
  302. total = list.Totals,
  303. };
  304. return Content(obj.ToJson());
  305. }
  306. else
  307. {
  308. var list = await _repositoryinformationrepository.GetListALL(conModels, " F_CreateOn desc");
  309. var obj = new
  310. {
  311. state = "success",
  312. message = "成功",
  313. rows = list,
  314. total = list.Count(),
  315. };
  316. return Content(obj.ToJson());
  317. }
  318. }
  319. }
  320. }