ColController.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * 分类设置
  3. */
  4. using System;
  5. using System.Web;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using Microsoft.AspNetCore.Mvc;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.Extensions.Logging;
  11. using Microsoft.Extensions.Options;
  12. using MySystem.BsModels;
  13. using Library;
  14. using LitJson;
  15. using MySystemLib;
  16. namespace MySystem.Areas.Admin.Controllers
  17. {
  18. [Area("Admin")]
  19. [Route("Admin/[controller]/[action]")]
  20. public class ColController : BaseController
  21. {
  22. public ColController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  23. {
  24. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["BsSqlConnStr"].ToString();
  25. }
  26. #region 分类设置列表
  27. /// <summary>
  28. /// 根据条件查询分类设置列表
  29. /// </summary>
  30. /// <returns></returns>
  31. public IActionResult Index(Col data, string right, string CurColId)
  32. {
  33. ViewBag.SysUserName = SysUserName;
  34. ViewBag.RightInfo = RightInfo;
  35. ViewBag.CurColId = CurColId;
  36. ViewBag.right = right;
  37. string Condition = "";
  38. Condition += "ColName:\"" + data.ColName + "\",";
  39. Condition += "ColEnName:\"" + data.ColEnName + "\",";
  40. if (!string.IsNullOrEmpty(Condition))
  41. {
  42. Condition = Condition.TrimEnd(',');
  43. Condition = ", where: {" + Condition + "}";
  44. }
  45. ViewBag.Condition = Condition;
  46. return View();
  47. }
  48. #endregion
  49. #region 根据条件查询分类设置列表
  50. /// <summary>
  51. /// 分类设置列表
  52. /// </summary>
  53. /// <returns></returns>
  54. public JsonResult IndexData(Col data, string CurColId, int page = 1, int limit = 30)
  55. {
  56. Dictionary<string, string> Fields = new Dictionary<string, string>();
  57. Fields.Add("ColName", "2"); //名称
  58. Fields.Add("ColEnName", "2"); //英文名
  59. string condition = " and ColId like '" + CurColId + "%'";
  60. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("Col", Fields, "Sort", "False", page, limit, condition);
  61. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  62. List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
  63. foreach (Dictionary<string, object> dic in diclist)
  64. {
  65. string ColId = dic["ColId"].ToString();
  66. string UrlString = dic["Url"].ToString();
  67. if (string.IsNullOrEmpty(UrlString))
  68. {
  69. UrlString = "";
  70. for (int i = ColId.Length; i >= 6; i -= 3)
  71. {
  72. string subcolid = ColId.Substring(0, i);
  73. Col query = bsdb.Col.FirstOrDefault(m => m.ColId == subcolid);
  74. if (query != null)
  75. {
  76. UrlString = "/" + query.ColEnName + UrlString;
  77. }
  78. }
  79. }
  80. Dictionary<string, object> item = new Dictionary<string, object>();
  81. item.Add("id", int.Parse(dic["Id"].ToString()));
  82. item.Add("pid", int.Parse(dic["ParentId"].ToString()));
  83. item.Add("title", dic["ColName"].ToString());
  84. item.Add("url", UrlString);
  85. item.Add("ColId", ColId);
  86. item.Add("Sort", int.Parse(dic["Sort"].ToString()));
  87. item.Add("Status", dic["Status"].ToString() == "1" ? "正常" : "关闭");
  88. result.Add(item);
  89. }
  90. return Json(result);
  91. }
  92. #endregion
  93. #region 增加分类设置
  94. /// <summary>
  95. /// 增加或修改分类设置信息
  96. /// </summary>
  97. /// <returns></returns>
  98. public IActionResult Add(string right, string PColId, string CurColId)
  99. {
  100. ViewBag.SysUserName = SysUserName;
  101. ViewBag.right = right;
  102. ViewBag.CurColId = CurColId;
  103. if (string.IsNullOrEmpty(PColId))
  104. {
  105. PColId = "001";
  106. }
  107. ViewBag.PColId = PColId;
  108. return View();
  109. }
  110. #endregion
  111. #region 增加分类设置
  112. /// <summary>
  113. /// 增加或修改分类设置信息
  114. /// </summary>
  115. /// <returns></returns>
  116. [HttpPost]
  117. public string Add(Col col, string PColId = "", string CurColId = "")
  118. {
  119. //构造ColId
  120. if (string.IsNullOrEmpty(PColId) && string.IsNullOrEmpty(CurColId))
  121. {
  122. return "请选择上级栏目!";
  123. }
  124. string code = "";
  125. int len = PColId.Length + 3;
  126. Col model = bsdb.Col.Where(m => m.ColId.StartsWith(PColId) && m.ColId.Length == len).OrderByDescending(m => m.ColId).FirstOrDefault();
  127. if (model != null)
  128. {
  129. string CurCode = model.ColId;
  130. int num = int.Parse(CurCode.Substring(CurCode.Length - 3)) + 1;
  131. code = num.ToString();
  132. for (int i = 0; i < 3 - num.ToString().Length; i++)
  133. {
  134. code = "0" + code;
  135. }
  136. code = PColId + code;
  137. }
  138. else
  139. {
  140. code = PColId + "001";
  141. }
  142. //获取父级数据
  143. Col parentcol = bsdb.Col.FirstOrDefault(m => m.ColId == PColId) ?? new Col();
  144. //获取父级ID
  145. int ParentId = 0;
  146. if (!string.IsNullOrEmpty(PColId))
  147. {
  148. ParentId = parentcol.Id;
  149. }
  150. //获取英文名,为空则自动翻译
  151. string EnName = col.ColEnName;
  152. if (string.IsNullOrEmpty(EnName))
  153. {
  154. EnName = new PublicFunction().TranslateZHToEn(col.ColName);
  155. EnName = EnName.Replace(" ", "").Replace("'", "").ToLower();
  156. }
  157. //判断中文名是否存在
  158. string CheckCol = code.Substring(0, 3);
  159. Col check = bsdb.Col.FirstOrDefault(m => m.ColId.StartsWith(CheckCol) && m.ColName == col.ColName);
  160. if (check != null)
  161. {
  162. return "中文名已存在";
  163. }
  164. Col add = bsdb.Col.Add(new Col()
  165. {
  166. ColId = code,
  167. Sort = col.Sort,
  168. ColName = col.ColName,
  169. ColEnName = EnName,
  170. ColPicPath = col.ColPicPath,
  171. ColDetail = col.ColDetail, //简介
  172. Recommend = col.Recommend,
  173. Url = col.Url,
  174. Contents = col.Contents,
  175. CreateDate = DateTime.Now,
  176. SeoTitle = col.SeoTitle,
  177. SeoKeyword = col.SeoKeyword,
  178. SeoDescription = col.SeoDescription,
  179. ParentId = ParentId,
  180. Status = 1,
  181. }).Entity;
  182. bsdb.SaveChanges();
  183. AddSysLog(add.Id.ToString(), "Col", "add");
  184. SetRedis(PColId);
  185. return "success";
  186. }
  187. #endregion
  188. #region 修改分类设置
  189. /// <summary>
  190. /// 增加或修改分类设置信息
  191. /// </summary>
  192. /// <returns></returns>
  193. public IActionResult Edit(string right, string ColId, string PColId, string CurColId, int Id = 0)
  194. {
  195. ViewBag.SysUserName = SysUserName;
  196. ViewBag.right = right;
  197. ViewBag.CurColId = CurColId;
  198. if (string.IsNullOrEmpty(PColId))
  199. {
  200. PColId = "001";
  201. }
  202. ViewBag.PColId = PColId;
  203. Col editData = bsdb.Col.FirstOrDefault(m => m.ColId == ColId) ?? new Col();
  204. ViewBag.data = editData;
  205. return View();
  206. }
  207. #endregion
  208. #region 修改分类设置
  209. /// <summary>
  210. /// 增加或修改分类设置信息
  211. /// </summary>
  212. /// <returns></returns>
  213. [HttpPost]
  214. public string Edit(Col col, string OldEnName, string PColId = "", string CurColId = "")
  215. {
  216. //构造ColId
  217. string code = "";
  218. int len = PColId.Length + 3;
  219. Col model = bsdb.Col.Where(m => m.ColId.StartsWith(PColId) && m.ColId.Length == len).OrderByDescending(m => m.ColId).FirstOrDefault();
  220. if (model != null)
  221. {
  222. string CurCode = model.ColId;
  223. int num = int.Parse(CurCode.Substring(CurCode.Length - 3)) + 1;
  224. code = num.ToString();
  225. for (int i = 0; i < 3 - num.ToString().Length; i++)
  226. {
  227. code = "0" + code;
  228. }
  229. code = PColId + code;
  230. }
  231. else
  232. {
  233. code = PColId + "001";
  234. }
  235. //获取英文名,为空则自动翻译
  236. string EnName = col.ColEnName;
  237. if (string.IsNullOrEmpty(EnName))
  238. {
  239. EnName = new PublicFunction().TranslateZHToEn(col.ColName);
  240. EnName = EnName.Replace(" ", "").Replace("'", "").ToLower();
  241. }
  242. // //判断英文名是否存在
  243. // string CheckCol = PColId.Substring(0, 3);
  244. // Col check = bsdb.Col.FirstOrDefault(m => m.ColId.StartsWith(CheckCol) && m.ColEnName == EnName && m.ColEnName != OldEnName);
  245. // if (check != null)
  246. // {
  247. // return "英文名已存在";
  248. // }
  249. Col editData = bsdb.Col.FirstOrDefault(c => c.ColId == col.ColId);
  250. if (editData != null)
  251. {
  252. //修改时,上级有变更,则同步更新同步更新数据表相关的ColId
  253. if (PColId != col.ColId.Substring(0, PColId.Length))
  254. {
  255. if (col.ColId.Substring(0, 3) == "001")
  256. {
  257. dbconn.op("update Articles set ColId='" + code + "' where ColId='" + col.ColId + "'");
  258. }
  259. else if (col.ColId.Substring(0, 3) == "003")
  260. {
  261. dbconn.op("update Advertisment set ColId='" + code + "' where ColId='" + col.ColId + "'");
  262. }
  263. editData.ColId = code;
  264. if (!string.IsNullOrEmpty(PColId))
  265. {
  266. Col parentcol = bsdb.Col.FirstOrDefault(m => m.ColId == PColId) ?? new Col();
  267. editData.ParentId = parentcol.Id;
  268. }
  269. }
  270. editData.ColName = col.ColName;
  271. editData.Sort = col.Sort;
  272. editData.ColEnName = EnName;
  273. editData.ColPicPath = col.ColPicPath;
  274. editData.ColDetail = col.ColDetail; //简介
  275. editData.Recommend = col.Recommend;
  276. editData.Url = col.Url;
  277. editData.Contents = col.Contents;
  278. editData.UpdateDate = DateTime.Now;
  279. editData.SeoTitle = col.SeoTitle;
  280. editData.SeoKeyword = col.SeoKeyword;
  281. editData.SeoDescription = col.SeoDescription;
  282. bsdb.SaveChanges();
  283. }
  284. else
  285. {
  286. int ParentId = 0;
  287. if (!string.IsNullOrEmpty(PColId))
  288. {
  289. Col parentcol = bsdb.Col.FirstOrDefault(m => m.ColId == PColId) ?? new Col();
  290. ParentId = parentcol.Id;
  291. }
  292. Col add = bsdb.Col.Add(new Col()
  293. {
  294. ColId = col.ColId,
  295. ColName = col.ColName,
  296. ColEnName = EnName,
  297. ColPicPath = col.ColPicPath,
  298. ColDetail = col.ColDetail, //简介
  299. Recommend = col.Recommend,
  300. Url = col.Url,
  301. Contents = col.Contents,
  302. CreateDate = DateTime.Now,
  303. SeoTitle = col.SeoTitle,
  304. SeoKeyword = col.SeoKeyword,
  305. SeoDescription = col.SeoDescription,
  306. ParentId = ParentId,
  307. Status = 1,
  308. }).Entity;
  309. bsdb.SaveChanges();
  310. }
  311. AddSysLog(col.Id.ToString(), "Col", "update");
  312. SetRedis(PColId);
  313. return "success";
  314. }
  315. #endregion
  316. #region 删除分类设置信息
  317. /// <summary>
  318. /// 删除分类设置信息
  319. /// </summary>
  320. /// <returns></returns>
  321. public string Delete(string Id)
  322. {
  323. string[] idlist = Id.Split(new char[] { ',' });
  324. List<string> ColIds = new List<string>();
  325. AddSysLog(Id, "Col", "del");
  326. foreach (string subid in idlist)
  327. {
  328. int id = int.Parse(subid);
  329. Col item = bsdb.Col.FirstOrDefault(m => m.Id == id) ?? new Col();
  330. if (!ColIds.Contains(item.ColId))
  331. {
  332. ColIds.Add(item.ColId);
  333. }
  334. var check = bsdb.Col.Any(m => m.ParentId == item.Id);
  335. if (check)
  336. {
  337. var cols = bsdb.Col.Where(m => m.ParentId == item.Id).ToList();
  338. foreach (var col in cols)
  339. {
  340. var dcol = cols.FirstOrDefault(m => m.Id == col.Id);
  341. int colId = int.Parse(dcol.Id.ToString());
  342. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Delete("Col", colId);
  343. }
  344. }
  345. var studys = db.SchoolMakerStudy.Where(m => m.SeoKeyword.StartsWith(item.ColId)).ToList();
  346. foreach (var items in studys)
  347. {
  348. var study = studys.FirstOrDefault(m => m.Id == items.Id);
  349. study.SeoKeyword = null;
  350. }
  351. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Delete("Col", id);
  352. }
  353. db.SaveChanges();
  354. bsdb.SaveChanges();
  355. foreach (string CurColId in ColIds)
  356. {
  357. SetRedis(CurColId);
  358. }
  359. return "success";
  360. }
  361. #endregion
  362. #region 开启
  363. /// <summary>
  364. /// 开启
  365. /// </summary>
  366. /// <returns></returns>
  367. public string Open(string Id)
  368. {
  369. string[] idlist = Id.Split(new char[] { ',' });
  370. AddSysLog(Id, "Col", "open");
  371. foreach (string subid in idlist)
  372. {
  373. int id = int.Parse(subid);
  374. Dictionary<string, object> Fields = new Dictionary<string, object>();
  375. Fields.Add("Status", 1);
  376. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("Col", Fields, id);
  377. }
  378. bsdb.SaveChanges();
  379. return "success";
  380. }
  381. #endregion
  382. #region 关闭
  383. /// <summary>
  384. /// 关闭
  385. /// </summary>
  386. /// <returns></returns>
  387. public string Close(string Id)
  388. {
  389. string[] idlist = Id.Split(new char[] { ',' });
  390. AddSysLog(Id, "Col", "close");
  391. foreach (string subid in idlist)
  392. {
  393. int id = int.Parse(subid);
  394. Dictionary<string, object> Fields = new Dictionary<string, object>();
  395. Fields.Add("Status", 0);
  396. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("Col", Fields, id);
  397. }
  398. bsdb.SaveChanges();
  399. return "success";
  400. }
  401. #endregion
  402. #region 排序
  403. /// <summary>
  404. /// 排序
  405. /// </summary>
  406. /// <param name="Id"></param>
  407. public string Sort(int Id, int Sort)
  408. {
  409. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Sort("Col", Sort, Id);
  410. AddSysLog(Id.ToString(), "Col", "sort");
  411. return "success";
  412. }
  413. #endregion
  414. #region 导入数据
  415. /// <summary>
  416. /// 导入数据
  417. /// </summary>
  418. /// <param name="ExcelData"></param>
  419. public string Import(string ExcelData)
  420. {
  421. ExcelData = HttpUtility.UrlDecode(ExcelData);
  422. JsonData list = JsonMapper.ToObject(ExcelData);
  423. for (int i = 1; i < list.Count; i++)
  424. {
  425. JsonData dr = list[i];
  426. bsdb.Col.Add(new Col()
  427. {
  428. CreateDate = DateTime.Now,
  429. UpdateDate = DateTime.Now,
  430. });
  431. bsdb.SaveChanges();
  432. }
  433. AddSysLog("0", "Col", "Import");
  434. return "success";
  435. }
  436. #endregion
  437. #region 导出Excel
  438. /// <summary>
  439. /// 导出Excel
  440. /// </summary>
  441. /// <returns></returns>
  442. public JsonResult ExportExcel(Col data)
  443. {
  444. Dictionary<string, string> Fields = new Dictionary<string, string>();
  445. Fields.Add("ColName", "2"); //名称
  446. Fields.Add("ColEnName", "2"); //英文名
  447. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("Col", Fields, "Id desc", "False", 1, 20000, "", "", false);
  448. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  449. Dictionary<string, object> result = new Dictionary<string, object>();
  450. result.Add("Status", "1");
  451. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  452. result.Add("Obj", diclist);
  453. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  454. result.Add("Fields", ReturnFields);
  455. AddSysLog("0", "Col", "ExportExcel");
  456. return Json(result);
  457. }
  458. #endregion
  459. #region 设置缓存
  460. private void SetRedis(string ColId)
  461. {
  462. int len = ColId.Length;
  463. List<Col> query = bsdb.Col.Where(m => m.ColId.StartsWith(ColId) && m.ColId.Length > len && m.Status == 1).OrderByDescending(m => m.Sort).ThenBy(m => m.Id).ToList();
  464. RedisDbconn.Instance.Clear("Col:" + ColId);
  465. RedisDbconn.Instance.AddList("Col:" + ColId, query.ToArray());
  466. }
  467. #endregion
  468. }
  469. }