ServiceCenterController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * 服务中心
  3. */
  4. using System;
  5. using System.Web;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.Extensions.Logging;
  13. using Microsoft.Extensions.Options;
  14. using MySystem.Models;
  15. using Library;
  16. using LitJson;
  17. using MySystemLib;
  18. namespace MySystem.Areas.Admin.Controllers
  19. {
  20. [Area("Admin")]
  21. [Route("Admin/[controller]/[action]")]
  22. public class ServiceCenterController : BaseController
  23. {
  24. public ServiceCenterController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  25. {
  26. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  27. }
  28. #region 服务中心列表
  29. /// <summary>
  30. /// 根据条件查询服务中心列表
  31. /// </summary>
  32. /// <returns></returns>
  33. public IActionResult Index(ServiceCenter data, string right)
  34. {
  35. ViewBag.RightInfo = RightInfo;
  36. ViewBag.right = right;
  37. return View();
  38. }
  39. #endregion
  40. #region 根据条件查询服务中心列表
  41. /// <summary>
  42. /// 服务中心列表
  43. /// </summary>
  44. /// <returns></returns>
  45. public JsonResult IndexData(ServiceCenter data, int page = 1, int limit = 30)
  46. {
  47. Dictionary<string, string> Fields = new Dictionary<string, string>();
  48. Fields.Add("Title", "2"); //标题
  49. Fields.Add("CreateDate", "3"); //时间
  50. Fields.Add("CategoryId", "0");
  51. string condition = "";
  52. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("ServiceCenter", Fields, "Id desc", "0", page, limit, condition);
  53. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  54. foreach (Dictionary<string, object> dic in diclist)
  55. {
  56. dic["CategoryId"] = RelationClass.GetColInfo(int.Parse(dic["CategoryId"].ToString()));
  57. dic["Status"] = dic["Status"].ToString() == "-1" ? "已删除" : "未删除";
  58. }
  59. return Json(obj);
  60. }
  61. #endregion
  62. #region 增加服务中心
  63. /// <summary>
  64. /// 增加或修改服务中心信息
  65. /// </summary>
  66. /// <returns></returns>
  67. public IActionResult Add(string right)
  68. {
  69. ViewBag.RightInfo = RightInfo;
  70. ViewBag.right = right;
  71. return View();
  72. }
  73. #endregion
  74. #region 增加服务中心
  75. /// <summary>
  76. /// 增加或修改服务中心信息
  77. /// </summary>
  78. /// <returns></returns>
  79. [HttpPost]
  80. public string Add(ServiceCenter data)
  81. {
  82. Dictionary<string, object> Fields = new Dictionary<string, object>();
  83. Fields.Add("Title", data.Title); //标题
  84. Fields.Add("SubTitle", data.SubTitle); //副标题
  85. Fields.Add("Content", data.Content); //内容
  86. Fields.Add("CategoryId", data.CategoryId); //问答类型
  87. Fields.Add("CreateMan", SysUserName);
  88. Fields.Add("SeoTitle", data.SeoTitle);
  89. Fields.Add("SeoKeyword", data.SeoKeyword);
  90. Fields.Add("SeoDescription", data.SeoDescription);
  91. int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Add("ServiceCenter", Fields, 0);
  92. AddSysLog(Id.ToString(), "ServiceCenter", "add");
  93. db.SaveChanges();
  94. SetRedis(Id, data.CategoryId);
  95. return "success";
  96. }
  97. #endregion
  98. #region 修改服务中心
  99. /// <summary>
  100. /// 增加或修改服务中心信息
  101. /// </summary>
  102. /// <returns></returns>
  103. public IActionResult Edit(string right, int Id = 0)
  104. {
  105. ViewBag.RightInfo = RightInfo;
  106. ViewBag.right = right;
  107. ServiceCenter editData = db.ServiceCenter.FirstOrDefault(m => m.Id == Id) ?? new ServiceCenter();
  108. ViewBag.data = editData;
  109. return View();
  110. }
  111. #endregion
  112. #region 修改服务中心
  113. /// <summary>
  114. /// 增加或修改服务中心信息
  115. /// </summary>
  116. /// <returns></returns>
  117. [HttpPost]
  118. public string Edit(ServiceCenter data)
  119. {
  120. Dictionary<string, object> Fields = new Dictionary<string, object>();
  121. Fields.Add("Title", data.Title); //标题
  122. Fields.Add("SubTitle", data.SubTitle); //副标题
  123. Fields.Add("Content", data.Content); //内容
  124. Fields.Add("CategoryId", data.CategoryId); //问答类型
  125. Fields.Add("SeoTitle", data.SeoTitle);
  126. Fields.Add("SeoKeyword", data.SeoKeyword);
  127. Fields.Add("SeoDescription", data.SeoDescription);
  128. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("ServiceCenter", Fields, data.Id);
  129. AddSysLog(data.Id.ToString(), "ServiceCenter", "update");
  130. db.SaveChanges();
  131. SetRedis(data.Id, data.CategoryId);
  132. return "success";
  133. }
  134. #endregion
  135. #region 详情
  136. public IActionResult Detail(string right, int Id = 0)
  137. {
  138. ViewBag.RightInfo = RightInfo;
  139. ViewBag.right = right;
  140. ServiceCenter editData = db.ServiceCenter.FirstOrDefault(m => m.Id == Id) ?? new ServiceCenter();
  141. ViewBag.data = editData;
  142. return View();
  143. }
  144. #endregion
  145. #region 删除服务中心信息
  146. /// <summary>
  147. /// 删除服务中心信息
  148. /// </summary>
  149. /// <returns></returns>
  150. public string Delete(string Id)
  151. {
  152. string[] idlist = Id.Split(new char[] { ',' });
  153. AddSysLog(Id, "ServiceCenter", "del");
  154. List<int> CategoryIds = new List<int>();
  155. foreach (string subid in idlist)
  156. {
  157. int id = int.Parse(subid);
  158. ServiceCenter item = db.ServiceCenter.FirstOrDefault(m => m.Id == id) ?? new ServiceCenter();
  159. if (!CategoryIds.Contains(item.CategoryId))
  160. {
  161. CategoryIds.Add(item.CategoryId);
  162. }
  163. Dictionary<string, object> Fields = new Dictionary<string, object>();
  164. Fields.Add("Status", -1);
  165. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("ServiceCenter", Fields, id);
  166. }
  167. db.SaveChanges();
  168. foreach (int CategoryId in CategoryIds)
  169. {
  170. SetRedis(0, CategoryId);
  171. }
  172. return "success";
  173. }
  174. #endregion
  175. #region 开启
  176. /// <summary>
  177. /// 开启
  178. /// </summary>
  179. /// <returns></returns>
  180. public string Open(string Id)
  181. {
  182. string[] idlist = Id.Split(new char[] { ',' });
  183. AddSysLog(Id, "ServiceCenter", "open");
  184. foreach (string subid in idlist)
  185. {
  186. int id = int.Parse(subid);
  187. Dictionary<string, object> Fields = new Dictionary<string, object>();
  188. Fields.Add("Status", 1);
  189. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("ServiceCenter", Fields, id);
  190. }
  191. db.SaveChanges();
  192. return "success";
  193. }
  194. #endregion
  195. #region 关闭
  196. /// <summary>
  197. /// 关闭
  198. /// </summary>
  199. /// <returns></returns>
  200. public string Close(string Id)
  201. {
  202. string[] idlist = Id.Split(new char[] { ',' });
  203. AddSysLog(Id, "ServiceCenter", "close");
  204. foreach (string subid in idlist)
  205. {
  206. int id = int.Parse(subid);
  207. Dictionary<string, object> Fields = new Dictionary<string, object>();
  208. Fields.Add("Status", 0);
  209. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("ServiceCenter", Fields, id);
  210. }
  211. db.SaveChanges();
  212. return "success";
  213. }
  214. #endregion
  215. #region 排序
  216. /// <summary>
  217. /// 排序
  218. /// </summary>
  219. /// <param name="Id"></param>
  220. public string Sort(int Id, int Sort)
  221. {
  222. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Sort("ServiceCenter", Sort, Id);
  223. AddSysLog(Id.ToString(), "ServiceCenter", "sort");
  224. ServiceCenter item = db.ServiceCenter.FirstOrDefault(m => m.Id == Id) ?? new ServiceCenter();
  225. SetRedis(0, item.CategoryId);
  226. return "success";
  227. }
  228. #endregion
  229. #region 导入数据
  230. /// <summary>
  231. /// 导入数据
  232. /// </summary>
  233. /// <param name="ExcelData"></param>
  234. public string Import(string ExcelData)
  235. {
  236. ExcelData = HttpUtility.UrlDecode(ExcelData);
  237. JsonData list = JsonMapper.ToObject(ExcelData);
  238. for (int i = 1; i < list.Count; i++)
  239. {
  240. JsonData dr = list[i];
  241. db.ServiceCenter.Add(new ServiceCenter()
  242. {
  243. CreateDate = DateTime.Now,
  244. UpdateDate = DateTime.Now,
  245. });
  246. db.SaveChanges();
  247. }
  248. AddSysLog("0", "ServiceCenter", "Import");
  249. return "success";
  250. }
  251. #endregion
  252. #region 导出Excel
  253. /// <summary>
  254. /// 导出Excel
  255. /// </summary>
  256. /// <returns></returns>
  257. public JsonResult ExportExcel(ServiceCenter data)
  258. {
  259. Dictionary<string, string> Fields = new Dictionary<string, string>();
  260. Fields.Add("Title", "2"); //标题
  261. Fields.Add("CreateDate", "3"); //时间
  262. Fields.Add("CategoryId", "0");
  263. string condition = " and Status>-1";
  264. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("ServiceCenter", Fields, "Id desc", "0", 1, 20000, condition, "", false);
  265. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  266. foreach (Dictionary<string, object> dic in diclist)
  267. {
  268. dic["CategoryId"] = RelationClass.GetColInfo(int.Parse(dic["CategoryId"].ToString()));
  269. }
  270. Dictionary<string, object> result = new Dictionary<string, object>();
  271. result.Add("Status", "1");
  272. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  273. result.Add("Obj", diclist);
  274. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  275. result.Add("Fields", ReturnFields);
  276. AddSysLog("0", "ServiceCenter", "ExportExcel");
  277. return Json(result);
  278. }
  279. #endregion
  280. #region 设置缓存
  281. private void SetRedis(int Id, int CategoryId)
  282. {
  283. if (CategoryId > 0)
  284. {
  285. RedisDbconn.Instance.Clear("ServiceCenterList");
  286. List<ServiceCenter> products = db.ServiceCenter.Where(m => m.Status > -1).OrderByDescending(m => m.Sort).ThenByDescending(m => m.Id).ToList();
  287. RedisDbconn.Instance.AddList("ServiceCenterList", products.ToArray());
  288. }
  289. if (Id > 0)
  290. {
  291. ServiceCenter edit = db.ServiceCenter.FirstOrDefault(m => m.Id == Id);
  292. if (edit != null)
  293. {
  294. RedisDbconn.Instance.Set("ServiceCenter:" + Id, edit);
  295. }
  296. }
  297. }
  298. #endregion
  299. }
  300. }