ProjectsController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 ProjectsController : BaseController
  23. {
  24. public ProjectsController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  25. {
  26. }
  27. #region 小程序项目列表
  28. /// <summary>
  29. /// 根据条件查询小程序项目列表
  30. /// </summary>
  31. /// <returns></returns>
  32. public IActionResult Index(Projects data, string right)
  33. {
  34. ViewBag.RightInfo = RightInfo;
  35. ViewBag.right = right;
  36. return View();
  37. }
  38. #endregion
  39. #region 根据条件查询小程序项目列表
  40. /// <summary>
  41. /// 小程序项目列表
  42. /// </summary>
  43. /// <returns></returns>
  44. public JsonResult IndexData(Projects data, string UserIdRealName, string UserIdMobile, int page = 1, int limit = 30)
  45. {
  46. Dictionary<string, string> Fields = new Dictionary<string, string>();
  47. Fields.Add("ProjectName", "1"); //项目名称
  48. Fields.Add("CreateDate", "3"); //时间
  49. string condition = " and Status>-1";
  50. //所属用户真实姓名
  51. if (!string.IsNullOrEmpty(UserIdRealName))
  52. {
  53. condition += " and UserId in (select UserId from UsersForRealName where RealName='" + UserIdRealName + "')";
  54. }
  55. //所属用户手机号
  56. if (!string.IsNullOrEmpty(UserIdMobile))
  57. {
  58. condition += " and UserId in (select UserId from UsersForMobile where Mobile='" + UserIdMobile + "')";
  59. }
  60. Dictionary<string, object> obj = new AdminContent(_accessor.HttpContext, SystemPublicFuction.dbtables).IndexData("Projects", Fields, "Id desc", "0", 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. dic["StatusName"] = dic["Status"].ToString() == "1" ? "正常" : "关闭";
  65. //所属用户
  66. int UserId = int.Parse(function.CheckInt(dic["UserId"].ToString()));
  67. Users userid_Users = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  68. dic["UserIdRealName"] = userid_Users.RealName;
  69. dic["UserIdMobile"] = userid_Users.Mobile;
  70. dic.Remove("UserId");
  71. }
  72. return Json(obj);
  73. }
  74. #endregion
  75. #region 增加小程序项目
  76. /// <summary>
  77. /// 增加或修改小程序项目信息
  78. /// </summary>
  79. /// <returns></returns>
  80. public IActionResult Add(string right)
  81. {
  82. ViewBag.RightInfo = RightInfo;
  83. ViewBag.right = right;
  84. return View();
  85. }
  86. #endregion
  87. #region 增加小程序项目
  88. /// <summary>
  89. /// 增加或修改小程序项目信息
  90. /// </summary>
  91. /// <returns></returns>
  92. [HttpPost]
  93. public string Add(Projects data)
  94. {
  95. Dictionary<string, object> Fields = new Dictionary<string, object>();
  96. Fields.Add("ProjectName", data.ProjectName); //项目名称
  97. Fields.Add("ProjectDetail", data.ProjectDetail); //项目介绍
  98. Fields.Add("ProjectContent", data.ProjectContent); //项目详情
  99. Fields.Add("UserId", data.UserId); //所属用户
  100. Fields.Add("SeoTitle", data.SeoTitle);
  101. Fields.Add("SeoKeyword", data.SeoKeyword);
  102. Fields.Add("SeoDescription", data.SeoDescription);
  103. int Id = new AdminContent(_accessor.HttpContext, SystemPublicFuction.dbtables).Add("Projects", Fields, 0);
  104. AddSysLog(data.Id.ToString(), "Projects", "add");
  105. db.SaveChanges();
  106. return "success";
  107. }
  108. #endregion
  109. #region 修改小程序项目
  110. /// <summary>
  111. /// 增加或修改小程序项目信息
  112. /// </summary>
  113. /// <returns></returns>
  114. public IActionResult Edit(string right, int Id = 0)
  115. {
  116. ViewBag.RightInfo = RightInfo;
  117. ViewBag.right = right;
  118. Projects editData = db.Projects.FirstOrDefault(m => m.Id == Id) ?? new Projects();
  119. ViewBag.data = editData;
  120. return View();
  121. }
  122. #endregion
  123. #region 修改小程序项目
  124. /// <summary>
  125. /// 增加或修改小程序项目信息
  126. /// </summary>
  127. /// <returns></returns>
  128. [HttpPost]
  129. public string Edit(Projects data)
  130. {
  131. Dictionary<string, object> Fields = new Dictionary<string, object>();
  132. Fields.Add("ProjectName", data.ProjectName); //项目名称
  133. Fields.Add("ProjectDetail", data.ProjectDetail); //项目介绍
  134. Fields.Add("ProjectContent", data.ProjectContent); //项目详情
  135. Fields.Add("UserId", data.UserId); //所属用户
  136. Fields.Add("SeoTitle", data.SeoTitle);
  137. Fields.Add("SeoKeyword", data.SeoKeyword);
  138. Fields.Add("SeoDescription", data.SeoDescription);
  139. new AdminContent(_accessor.HttpContext, SystemPublicFuction.dbtables).Edit("Projects", Fields, data.Id);
  140. AddSysLog(data.Id.ToString(),"Projects","update");
  141. db.SaveChanges();
  142. return "success";
  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,"Projects","del");
  154. foreach (string subid in idlist)
  155. {
  156. int id = int.Parse(subid);
  157. Dictionary<string, object> Fields = new Dictionary<string, object>();
  158. Fields.Add("Status", -1);
  159. new AdminContent(_accessor.HttpContext, SystemPublicFuction.dbtables).Edit("Projects", Fields, id);
  160. }
  161. db.SaveChanges();
  162. return "success";
  163. }
  164. #endregion
  165. #region 开启
  166. /// <summary>
  167. /// 开启
  168. /// </summary>
  169. /// <returns></returns>
  170. public string Open(string Id)
  171. {
  172. string[] idlist = Id.Split(new char[] { ',' });
  173. AddSysLog(Id,"Projects","open");
  174. foreach (string subid in idlist)
  175. {
  176. int id = int.Parse(subid);
  177. Dictionary<string, object> Fields = new Dictionary<string, object>();
  178. Fields.Add("Status", 1);
  179. new AdminContent(_accessor.HttpContext, SystemPublicFuction.dbtables).Edit("Projects", Fields, id);
  180. }
  181. db.SaveChanges();
  182. return "success";
  183. }
  184. #endregion
  185. #region 关闭
  186. /// <summary>
  187. /// 关闭
  188. /// </summary>
  189. /// <returns></returns>
  190. public string Close(string Id)
  191. {
  192. string[] idlist = Id.Split(new char[] { ',' });
  193. AddSysLog(Id,"Projects","close");
  194. foreach (string subid in idlist)
  195. {
  196. int id = int.Parse(subid);
  197. Dictionary<string, object> Fields = new Dictionary<string, object>();
  198. Fields.Add("Status", 0);
  199. new AdminContent(_accessor.HttpContext, SystemPublicFuction.dbtables).Edit("Projects", Fields, id);
  200. }
  201. db.SaveChanges();
  202. return "success";
  203. }
  204. #endregion
  205. #region 排序
  206. /// <summary>
  207. /// 排序
  208. /// </summary>
  209. /// <param name="Id"></param>
  210. public string Sort(int Id, int Sort)
  211. {
  212. new AdminContent(_accessor.HttpContext, SystemPublicFuction.dbtables).Sort("Projects", Sort, Id);
  213. AddSysLog(Id.ToString(), "Projects", "sort");
  214. return "success";
  215. }
  216. #endregion
  217. #region 导入数据
  218. /// <summary>
  219. /// 导入数据
  220. /// </summary>
  221. /// <param name="ExcelData"></param>
  222. public string Import(string ExcelData)
  223. {
  224. ExcelData = HttpUtility.UrlDecode(ExcelData);
  225. JsonData list = JsonMapper.ToObject(ExcelData);
  226. for (int i = 1; i < list.Count;i++ )
  227. {
  228. JsonData dr = list[i];
  229. db.Projects.Add(new Projects()
  230. {
  231. CreateDate = DateTime.Now,
  232. UpdateDate = DateTime.Now,
  233. });
  234. db.SaveChanges();
  235. }
  236. AddSysLog("0", "Projects", "Import");
  237. return "success";
  238. }
  239. #endregion
  240. #region 导出Excel
  241. /// <summary>
  242. /// 导出Excel
  243. /// </summary>
  244. /// <returns></returns>
  245. public JsonResult ExportExcel(Projects data, string UserIdRealName, string UserIdMobile)
  246. {
  247. Dictionary<string, string> Fields = new Dictionary<string, string>();
  248. Fields.Add("ProjectName", "1"); //项目名称
  249. Fields.Add("CreateDate", "3"); //时间
  250. string condition = " and Status>-1";
  251. //所属用户真实姓名
  252. if (!string.IsNullOrEmpty(UserIdRealName))
  253. {
  254. condition += " and UserId in (select UserId from UsersForRealName where RealName='" + UserIdRealName + "')";
  255. }
  256. //所属用户手机号
  257. if (!string.IsNullOrEmpty(UserIdMobile))
  258. {
  259. condition += " and UserId in (select UserId from UsersForMobile where Mobile='" + UserIdMobile + "')";
  260. }
  261. Dictionary<string, object> obj = new AdminContent(_accessor.HttpContext, SystemPublicFuction.dbtables).IndexData("Projects", Fields, "Id desc", "0", 1, 20000, condition, "", false);
  262. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  263. foreach (Dictionary<string, object> dic in diclist)
  264. {
  265. dic["StatusName"] = dic["Status"].ToString() == "1" ? "正常" : "关闭";
  266. //所属用户
  267. int UserId = int.Parse(function.CheckInt(dic["UserId"].ToString()));
  268. Users userid_Users = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  269. dic["UserIdRealName"] = userid_Users.RealName;
  270. dic["UserIdMobile"] = userid_Users.Mobile;
  271. dic.Remove("UserId");
  272. }
  273. Dictionary<string, object> result = new Dictionary<string, object>();
  274. result.Add("Status", "1");
  275. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  276. result.Add("Obj", diclist);
  277. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  278. result.Add("Fields", ReturnFields);
  279. AddSysLog("0", "Projects", "ExportExcel");
  280. return Json(result);
  281. }
  282. #endregion
  283. }
  284. }