ColController.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. // if (SysUserName != "admin" && SysUserName != "root")
  61. // {
  62. // condition += " and ColId in ('" + RightInfo.Trim(',').Replace(",", "','") + "')";
  63. // }
  64. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("Col", Fields, "ColId asc", "False", page, limit, condition);
  65. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  66. List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
  67. foreach (Dictionary<string, object> dic in diclist)
  68. {
  69. string ColId = dic["ColId"].ToString();
  70. // dic["ColName"] = dic["ColName"].ToString();
  71. // dic["Status"] = dic["Status"].ToString() == "1" ? "正常" : "关闭";
  72. string UrlString = dic["Url"].ToString();
  73. if (string.IsNullOrEmpty(UrlString))
  74. {
  75. UrlString = "";
  76. for (int i = ColId.Length; i >= 6; i -= 3)
  77. {
  78. string subcolid = ColId.Substring(0, i);
  79. Col query = bsdb.Col.FirstOrDefault(m => m.ColId == subcolid);
  80. if (query != null)
  81. {
  82. UrlString = "/" + query.ColEnName + UrlString;
  83. }
  84. }
  85. }
  86. Dictionary<string, object> item = new Dictionary<string, object>();
  87. item.Add("id", int.Parse(dic["Id"].ToString()));
  88. item.Add("pid", int.Parse(dic["ParentId"].ToString()));
  89. item.Add("title", dic["ColName"].ToString());
  90. item.Add("url", UrlString);
  91. item.Add("ColId", ColId);
  92. item.Add("Status", dic["Status"].ToString() == "1" ? "正常" : "关闭");
  93. result.Add(item);
  94. }
  95. return Json(result);
  96. }
  97. #endregion
  98. #region 增加分类设置
  99. /// <summary>
  100. /// 增加或修改分类设置信息
  101. /// </summary>
  102. /// <returns></returns>
  103. public IActionResult Add(string right, string PColId, string CurColId)
  104. {
  105. ViewBag.SysUserName = SysUserName;
  106. ViewBag.right = right;
  107. ViewBag.CurColId = CurColId;
  108. if (string.IsNullOrEmpty(PColId))
  109. {
  110. PColId = "001";
  111. }
  112. ViewBag.PColId = PColId;
  113. return View();
  114. }
  115. #endregion
  116. #region 增加分类设置
  117. /// <summary>
  118. /// 增加或修改分类设置信息
  119. /// </summary>
  120. /// <returns></returns>
  121. [HttpPost]
  122. public string Add(Col col, string PColId = "", string CurColId = "")
  123. {
  124. //构造ColId
  125. string code = "";
  126. int len = PColId.Length + 3;
  127. Col model = bsdb.Col.Where(m => m.ColId.StartsWith(PColId) && m.ColId.Length == len).OrderByDescending(m => m.ColId).FirstOrDefault();
  128. if (model != null)
  129. {
  130. string CurCode = model.ColId;
  131. int num = int.Parse(CurCode.Substring(CurCode.Length - 3)) + 1;
  132. code = num.ToString();
  133. for (int i = 0; i < 3 - num.ToString().Length; i++)
  134. {
  135. code = "0" + code;
  136. }
  137. code = PColId + code;
  138. }
  139. else
  140. {
  141. code = PColId + "001";
  142. }
  143. //获取父级数据
  144. Col parentcol = bsdb.Col.FirstOrDefault(m => m.ColId == PColId) ?? new Col();
  145. //获取父级ID
  146. int ParentId = 0;
  147. if (!string.IsNullOrEmpty(PColId))
  148. {
  149. ParentId = parentcol.Id;
  150. }
  151. //获取英文名,为空则自动翻译
  152. string EnName = col.ColEnName;
  153. if (string.IsNullOrEmpty(EnName))
  154. {
  155. EnName = new PublicFunction().TranslateZHToEn(col.ColName);
  156. EnName = EnName.Replace(" ", "").Replace("'", "").ToLower();
  157. }
  158. //判断英文名是否存在
  159. string CheckCol = code.Substring(0, 3);
  160. Col check = bsdb.Col.FirstOrDefault(m => m.ColId.StartsWith(CheckCol) && m.ColEnName == EnName);
  161. if (check != null)
  162. {
  163. return "英文名已存在";
  164. }
  165. Col add = bsdb.Col.Add(new Col()
  166. {
  167. ColId = code,
  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.ColEnName = EnName;
  272. editData.ColPicPath = col.ColPicPath;
  273. editData.ColDetail = col.ColDetail; //简介
  274. editData.Recommend = col.Recommend;
  275. editData.Url = col.Url;
  276. editData.Contents = col.Contents;
  277. editData.UpdateDate = DateTime.Now;
  278. editData.SeoTitle = col.SeoTitle;
  279. editData.SeoKeyword = col.SeoKeyword;
  280. editData.SeoDescription = col.SeoDescription;
  281. bsdb.SaveChanges();
  282. }
  283. else
  284. {
  285. int ParentId = 0;
  286. if (!string.IsNullOrEmpty(PColId))
  287. {
  288. Col parentcol = bsdb.Col.FirstOrDefault(m => m.ColId == PColId) ?? new Col();
  289. ParentId = parentcol.Id;
  290. }
  291. Col add = bsdb.Col.Add(new Col()
  292. {
  293. ColId = col.ColId,
  294. ColName = col.ColName,
  295. ColEnName = EnName,
  296. ColPicPath = col.ColPicPath,
  297. ColDetail = col.ColDetail, //简介
  298. Recommend = col.Recommend,
  299. Url = col.Url,
  300. Contents = col.Contents,
  301. CreateDate = DateTime.Now,
  302. SeoTitle = col.SeoTitle,
  303. SeoKeyword = col.SeoKeyword,
  304. SeoDescription = col.SeoDescription,
  305. ParentId = ParentId,
  306. Status = 1,
  307. }).Entity;
  308. bsdb.SaveChanges();
  309. }
  310. AddSysLog(col.Id.ToString(), "Col", "update");
  311. SetRedis(PColId);
  312. return "success";
  313. }
  314. #endregion
  315. #region 删除分类设置信息
  316. /// <summary>
  317. /// 删除分类设置信息
  318. /// </summary>
  319. /// <returns></returns>
  320. public string Delete(string Id)
  321. {
  322. string[] idlist = Id.Split(new char[] { ',' });
  323. List<string> ColIds = new List<string>();
  324. AddSysLog(Id, "Col", "del");
  325. foreach (string subid in idlist)
  326. {
  327. int id = int.Parse(subid);
  328. Col item = bsdb.Col.FirstOrDefault(m => m.Id == id) ?? new Col();
  329. if (!ColIds.Contains(item.ColId))
  330. {
  331. ColIds.Add(item.ColId);
  332. }
  333. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Delete("Col", id);
  334. }
  335. bsdb.SaveChanges();
  336. foreach (string CurColId in ColIds)
  337. {
  338. SetRedis(CurColId);
  339. }
  340. return "success";
  341. }
  342. #endregion
  343. #region 开启
  344. /// <summary>
  345. /// 开启
  346. /// </summary>
  347. /// <returns></returns>
  348. public string Open(string Id)
  349. {
  350. string[] idlist = Id.Split(new char[] { ',' });
  351. AddSysLog(Id, "Col", "open");
  352. foreach (string subid in idlist)
  353. {
  354. int id = int.Parse(subid);
  355. Dictionary<string, object> Fields = new Dictionary<string, object>();
  356. Fields.Add("Status", 1);
  357. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("Col", Fields, id);
  358. }
  359. bsdb.SaveChanges();
  360. return "success";
  361. }
  362. #endregion
  363. #region 关闭
  364. /// <summary>
  365. /// 关闭
  366. /// </summary>
  367. /// <returns></returns>
  368. public string Close(string Id)
  369. {
  370. string[] idlist = Id.Split(new char[] { ',' });
  371. AddSysLog(Id, "Col", "close");
  372. foreach (string subid in idlist)
  373. {
  374. int id = int.Parse(subid);
  375. Dictionary<string, object> Fields = new Dictionary<string, object>();
  376. Fields.Add("Status", 0);
  377. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Edit("Col", Fields, id);
  378. }
  379. bsdb.SaveChanges();
  380. return "success";
  381. }
  382. #endregion
  383. #region 排序
  384. /// <summary>
  385. /// 排序
  386. /// </summary>
  387. /// <param name="Id"></param>
  388. public string Sort(int Id, int Sort)
  389. {
  390. new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).Sort("Col", Sort, Id);
  391. AddSysLog(Id.ToString(), "Col", "sort");
  392. return "success";
  393. }
  394. #endregion
  395. #region 导入数据
  396. /// <summary>
  397. /// 导入数据
  398. /// </summary>
  399. /// <param name="ExcelData"></param>
  400. public string Import(string ExcelData)
  401. {
  402. ExcelData = HttpUtility.UrlDecode(ExcelData);
  403. JsonData list = JsonMapper.ToObject(ExcelData);
  404. for (int i = 1; i < list.Count; i++)
  405. {
  406. JsonData dr = list[i];
  407. bsdb.Col.Add(new Col()
  408. {
  409. CreateDate = DateTime.Now,
  410. UpdateDate = DateTime.Now,
  411. });
  412. bsdb.SaveChanges();
  413. }
  414. AddSysLog("0", "Col", "Import");
  415. return "success";
  416. }
  417. #endregion
  418. #region 导出Excel
  419. /// <summary>
  420. /// 导出Excel
  421. /// </summary>
  422. /// <returns></returns>
  423. public JsonResult ExportExcel(Col data)
  424. {
  425. Dictionary<string, string> Fields = new Dictionary<string, string>();
  426. Fields.Add("ColName", "2"); //名称
  427. Fields.Add("ColEnName", "2"); //英文名
  428. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.BsTables).IndexData("Col", Fields, "Id desc", "False", 1, 20000, "", "", false);
  429. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  430. Dictionary<string, object> result = new Dictionary<string, object>();
  431. result.Add("Status", "1");
  432. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  433. result.Add("Obj", diclist);
  434. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  435. result.Add("Fields", ReturnFields);
  436. AddSysLog("0", "Col", "ExportExcel");
  437. return Json(result);
  438. }
  439. #endregion
  440. #region 设置缓存
  441. private void SetRedis(string ColId)
  442. {
  443. int len = ColId.Length;
  444. 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();
  445. RedisDbconn.Instance.Clear("Col:" + ColId);
  446. RedisDbconn.Instance.AddList("Col:" + ColId, query.ToArray());
  447. }
  448. #endregion
  449. }
  450. }