ColController.cs 18 KB

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