MerchantColController.cs 18 KB

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