ColController.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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, "ColId asc", "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. string code = "";
  121. int len = PColId.Length + 3;
  122. Col model = bsdb.Col.Where(m => m.ColId.StartsWith(PColId) && m.ColId.Length == len).OrderByDescending(m => m.ColId).FirstOrDefault();
  123. if (model != null)
  124. {
  125. string CurCode = model.ColId;
  126. int num = int.Parse(CurCode.Substring(CurCode.Length - 3)) + 1;
  127. code = num.ToString();
  128. for (int i = 0; i < 3 - num.ToString().Length; i++)
  129. {
  130. code = "0" + code;
  131. }
  132. code = PColId + code;
  133. }
  134. else
  135. {
  136. code = PColId + "001";
  137. }
  138. //获取父级数据
  139. Col parentcol = bsdb.Col.FirstOrDefault(m => m.ColId == PColId) ?? new Col();
  140. //获取父级ID
  141. int ParentId = 0;
  142. if (!string.IsNullOrEmpty(PColId))
  143. {
  144. ParentId = parentcol.Id;
  145. }
  146. //获取英文名,为空则自动翻译
  147. string EnName = col.ColEnName;
  148. if (string.IsNullOrEmpty(EnName))
  149. {
  150. EnName = new PublicFunction().TranslateZHToEn(col.ColName);
  151. EnName = EnName.Replace(" ", "").Replace("'", "").ToLower();
  152. }
  153. //判断英文名是否存在
  154. string CheckCol = code.Substring(0, 3);
  155. Col check = bsdb.Col.FirstOrDefault(m => m.ColId.StartsWith(CheckCol) && m.ColEnName == EnName);
  156. if (check != null)
  157. {
  158. return "英文名已存在";
  159. }
  160. Col add = bsdb.Col.Add(new Col()
  161. {
  162. ColId = code,
  163. Sort = col.Sort,
  164. ColName = col.ColName,
  165. ColEnName = EnName,
  166. ColPicPath = col.ColPicPath,
  167. ColDetail = col.ColDetail, //简介
  168. Recommend = col.Recommend,
  169. Url = col.Url,
  170. Contents = col.Contents,
  171. CreateDate = DateTime.Now,
  172. SeoTitle = col.SeoTitle,
  173. SeoKeyword = col.SeoKeyword,
  174. SeoDescription = col.SeoDescription,
  175. ParentId = ParentId,
  176. Status = 1,
  177. }).Entity;
  178. bsdb.SaveChanges();
  179. AddSysLog(add.Id.ToString(), "Col", "add");
  180. SetRedis(PColId);
  181. return "success";
  182. }
  183. #endregion
  184. #region 修改分类设置
  185. /// <summary>
  186. /// 增加或修改分类设置信息
  187. /// </summary>
  188. /// <returns></returns>
  189. public IActionResult Edit(string right, string ColId, string PColId, string CurColId, int Id = 0)
  190. {
  191. ViewBag.SysUserName = SysUserName;
  192. ViewBag.right = right;
  193. ViewBag.CurColId = CurColId;
  194. if (string.IsNullOrEmpty(PColId))
  195. {
  196. PColId = "001";
  197. }
  198. ViewBag.PColId = PColId;
  199. Col editData = bsdb.Col.FirstOrDefault(m => m.ColId == ColId) ?? new Col();
  200. ViewBag.data = editData;
  201. return View();
  202. }
  203. #endregion
  204. #region 修改分类设置
  205. /// <summary>
  206. /// 增加或修改分类设置信息
  207. /// </summary>
  208. /// <returns></returns>
  209. [HttpPost]
  210. public string Edit(Col col, string OldEnName, string PColId = "", string CurColId = "")
  211. {
  212. //构造ColId
  213. string code = "";
  214. int len = PColId.Length + 3;
  215. Col model = bsdb.Col.Where(m => m.ColId.StartsWith(PColId) && m.ColId.Length == len).OrderByDescending(m => m.ColId).FirstOrDefault();
  216. if (model != null)
  217. {
  218. string CurCode = model.ColId;
  219. int num = int.Parse(CurCode.Substring(CurCode.Length - 3)) + 1;
  220. code = num.ToString();
  221. for (int i = 0; i < 3 - num.ToString().Length; i++)
  222. {
  223. code = "0" + code;
  224. }
  225. code = PColId + code;
  226. }
  227. else
  228. {
  229. code = PColId + "001";
  230. }
  231. //获取英文名,为空则自动翻译
  232. string EnName = col.ColEnName;
  233. if (string.IsNullOrEmpty(EnName))
  234. {
  235. EnName = new PublicFunction().TranslateZHToEn(col.ColName);
  236. EnName = EnName.Replace(" ", "").Replace("'", "").ToLower();
  237. }
  238. //判断英文名是否存在
  239. string CheckCol = PColId.Substring(0, 3);
  240. Col check = bsdb.Col.FirstOrDefault(m => m.ColId.StartsWith(CheckCol) && m.ColEnName == EnName && m.ColEnName != OldEnName);
  241. if (check != null)
  242. {
  243. return "英文名已存在";
  244. }
  245. Col editData = bsdb.Col.FirstOrDefault(c => c.ColId == col.ColId);
  246. if (editData != null)
  247. {
  248. //修改时,上级有变更,则同步更新同步更新数据表相关的ColId
  249. if (PColId != col.ColId.Substring(0, PColId.Length))
  250. {
  251. if (col.ColId.Substring(0, 3) == "001")
  252. {
  253. dbconn.op("update Articles set ColId='" + code + "' where ColId='" + col.ColId + "'");
  254. }
  255. else if (col.ColId.Substring(0, 3) == "003")
  256. {
  257. dbconn.op("update Advertisment set ColId='" + code + "' where ColId='" + col.ColId + "'");
  258. }
  259. editData.ColId = code;
  260. if (!string.IsNullOrEmpty(PColId))
  261. {
  262. Col parentcol = bsdb.Col.FirstOrDefault(m => m.ColId == PColId) ?? new Col();
  263. editData.ParentId = parentcol.Id;
  264. }
  265. }
  266. editData.ColName = col.ColName;
  267. editData.Sort = col.Sort;
  268. editData.ColEnName = EnName;
  269. editData.ColPicPath = col.ColPicPath;
  270. editData.ColDetail = col.ColDetail; //简介
  271. editData.Recommend = col.Recommend;
  272. editData.Url = col.Url;
  273. editData.Contents = col.Contents;
  274. editData.UpdateDate = DateTime.Now;
  275. editData.SeoTitle = col.SeoTitle;
  276. editData.SeoKeyword = col.SeoKeyword;
  277. editData.SeoDescription = col.SeoDescription;
  278. bsdb.SaveChanges();
  279. }
  280. else
  281. {
  282. int ParentId = 0;
  283. if (!string.IsNullOrEmpty(PColId))
  284. {
  285. Col parentcol = bsdb.Col.FirstOrDefault(m => m.ColId == PColId) ?? new Col();
  286. ParentId = parentcol.Id;
  287. }
  288. Col add = bsdb.Col.Add(new Col()
  289. {
  290. ColId = col.ColId,
  291. ColName = col.ColName,
  292. ColEnName = EnName,
  293. ColPicPath = col.ColPicPath,
  294. ColDetail = col.ColDetail, //简介
  295. Recommend = col.Recommend,
  296. Url = col.Url,
  297. Contents = col.Contents,
  298. CreateDate = DateTime.Now,
  299. SeoTitle = col.SeoTitle,
  300. SeoKeyword = col.SeoKeyword,
  301. SeoDescription = col.SeoDescription,
  302. ParentId = ParentId,
  303. Status = 1,
  304. }).Entity;
  305. bsdb.SaveChanges();
  306. }
  307. AddSysLog(col.Id.ToString(), "Col", "update");
  308. SetRedis(PColId);
  309. return "success";
  310. }
  311. #endregion
  312. #region 删除分类设置信息
  313. /// <summary>
  314. /// 删除分类设置信息
  315. /// </summary>
  316. /// <returns></returns>
  317. public string Delete(string Id)
  318. {
  319. string[] idlist = Id.Split(new char[] { ',' });
  320. List<string> ColIds = new List<string>();
  321. AddSysLog(Id, "Col", "del");
  322. foreach (string subid in idlist)
  323. {
  324. int id = int.Parse(subid);
  325. Col item = bsdb.Col.FirstOrDefault(m => m.Id == id) ?? new Col();
  326. if (!ColIds.Contains(item.ColId))
  327. {
  328. ColIds.Add(item.ColId);
  329. }
  330. var check = bsdb.Col.Any(m => m.ParentId == item.Id);
  331. if (check)
  332. {
  333. var cols = bsdb.Col.Where(m => m.ParentId == item.Id).ToList();
  334. foreach (var col in cols)
  335. {
  336. var dcol = cols.FirstOrDefault(m => m.Id == col.Id);
  337. int colId = int.Parse(dcol.Id.ToString());
  338. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Delete("Col", colId);
  339. }
  340. }
  341. var studys = db.SchoolMakerStudy.Where(m => m.SeoKeyword.StartsWith(item.ColId)).ToList();
  342. foreach (var items in studys)
  343. {
  344. var study = studys.FirstOrDefault(m => m.Id == items.Id);
  345. study.SeoKeyword = null;
  346. }
  347. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Delete("Col", id);
  348. }
  349. db.SaveChanges();
  350. bsdb.SaveChanges();
  351. foreach (string CurColId in ColIds)
  352. {
  353. SetRedis(CurColId);
  354. }
  355. return "success";
  356. }
  357. #endregion
  358. #region 开启
  359. /// <summary>
  360. /// 开启
  361. /// </summary>
  362. /// <returns></returns>
  363. public string Open(string Id)
  364. {
  365. string[] idlist = Id.Split(new char[] { ',' });
  366. AddSysLog(Id, "Col", "open");
  367. foreach (string subid in idlist)
  368. {
  369. int id = int.Parse(subid);
  370. Dictionary<string, object> Fields = new Dictionary<string, object>();
  371. Fields.Add("Status", 1);
  372. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("Col", Fields, id);
  373. }
  374. bsdb.SaveChanges();
  375. return "success";
  376. }
  377. #endregion
  378. #region 关闭
  379. /// <summary>
  380. /// 关闭
  381. /// </summary>
  382. /// <returns></returns>
  383. public string Close(string Id)
  384. {
  385. string[] idlist = Id.Split(new char[] { ',' });
  386. AddSysLog(Id, "Col", "close");
  387. foreach (string subid in idlist)
  388. {
  389. int id = int.Parse(subid);
  390. Dictionary<string, object> Fields = new Dictionary<string, object>();
  391. Fields.Add("Status", 0);
  392. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("Col", Fields, id);
  393. }
  394. bsdb.SaveChanges();
  395. return "success";
  396. }
  397. #endregion
  398. #region 排序
  399. /// <summary>
  400. /// 排序
  401. /// </summary>
  402. /// <param name="Id"></param>
  403. public string Sort(int Id, int Sort)
  404. {
  405. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Sort("Col", Sort, Id);
  406. AddSysLog(Id.ToString(), "Col", "sort");
  407. return "success";
  408. }
  409. #endregion
  410. #region 导入数据
  411. /// <summary>
  412. /// 导入数据
  413. /// </summary>
  414. /// <param name="ExcelData"></param>
  415. public string Import(string ExcelData)
  416. {
  417. ExcelData = HttpUtility.UrlDecode(ExcelData);
  418. JsonData list = JsonMapper.ToObject(ExcelData);
  419. for (int i = 1; i < list.Count; i++)
  420. {
  421. JsonData dr = list[i];
  422. bsdb.Col.Add(new Col()
  423. {
  424. CreateDate = DateTime.Now,
  425. UpdateDate = DateTime.Now,
  426. });
  427. bsdb.SaveChanges();
  428. }
  429. AddSysLog("0", "Col", "Import");
  430. return "success";
  431. }
  432. #endregion
  433. #region 导出Excel
  434. /// <summary>
  435. /// 导出Excel
  436. /// </summary>
  437. /// <returns></returns>
  438. public JsonResult ExportExcel(Col data)
  439. {
  440. Dictionary<string, string> Fields = new Dictionary<string, string>();
  441. Fields.Add("ColName", "2"); //名称
  442. Fields.Add("ColEnName", "2"); //英文名
  443. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("Col", Fields, "Id desc", "False", 1, 20000, "", "", false);
  444. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  445. Dictionary<string, object> result = new Dictionary<string, object>();
  446. result.Add("Status", "1");
  447. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  448. result.Add("Obj", diclist);
  449. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  450. result.Add("Fields", ReturnFields);
  451. AddSysLog("0", "Col", "ExportExcel");
  452. return Json(result);
  453. }
  454. #endregion
  455. #region 设置缓存
  456. private void SetRedis(string ColId)
  457. {
  458. int len = ColId.Length;
  459. 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();
  460. RedisDbconn.Instance.Clear("Col:" + ColId);
  461. RedisDbconn.Instance.AddList("Col:" + ColId, query.ToArray());
  462. }
  463. #endregion
  464. }
  465. }