ColController.cs 18 KB

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