LeadersController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 LeadersController : BaseController
  23. {
  24. public LeadersController(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(Leaders 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(Leaders data, string UserIdMakerCode, string LeaderLevelSelect, string CreateDateData, int page = 1, int limit = 30)
  46. {
  47. Dictionary<string, string> Fields = new Dictionary<string, string>();
  48. string condition = " and Status>-1";
  49. //创客编号
  50. if (!string.IsNullOrEmpty(UserIdMakerCode))
  51. {
  52. condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + UserIdMakerCode + "')";
  53. }
  54. //盟主等级
  55. if (!string.IsNullOrEmpty(LeaderLevelSelect))
  56. {
  57. condition += " and LeaderLevel=" + LeaderLevelSelect;
  58. }
  59. //创建时间
  60. if (!string.IsNullOrEmpty(CreateDateData))
  61. {
  62. string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
  63. string start = datelist[0];
  64. string end = datelist[1];
  65. condition += " and CreateDate>='" + start + " 00:00:00' and CreateDate<='" + end + " 23:59:59'";
  66. }
  67. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("Leaders", Fields, "Id desc", "0", page, limit, condition);
  68. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  69. foreach (Dictionary<string, object> dic in diclist)
  70. {
  71. //创客
  72. int UserId = int.Parse(function.CheckInt(dic["UserId"].ToString()));
  73. Users userid_Users = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  74. dic["UserIdMakerCode"] = userid_Users.MakerCode;
  75. dic["UserIdRealName"] = userid_Users.RealName;
  76. dic.Remove("UserId");
  77. //盟主等级
  78. int LeaderLevel = int.Parse(dic["LeaderLevel"].ToString());
  79. if (LeaderLevel == 1) dic["LeaderLevel"] = "小盟主";
  80. if (LeaderLevel == 2) dic["LeaderLevel"] = "大盟主";
  81. if (LeaderLevel == 0) dic["LeaderLevel"] = "";
  82. }
  83. return Json(obj);
  84. }
  85. #endregion
  86. #region 增加盟主管理
  87. /// <summary>
  88. /// 增加或修改盟主管理信息
  89. /// </summary>
  90. /// <returns></returns>
  91. public IActionResult Add(string right)
  92. {
  93. ViewBag.RightInfo = RightInfo;
  94. ViewBag.right = right;
  95. return View();
  96. }
  97. #endregion
  98. #region 增加盟主管理
  99. /// <summary>
  100. /// 增加或修改盟主管理信息
  101. /// </summary>
  102. /// <returns></returns>
  103. [HttpPost]
  104. public string Add(Leaders data, string MakerCode)
  105. {
  106. Dictionary<string, object> Fields = new Dictionary<string, object>();
  107. var userForMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode) ?? new UserForMakerCode();
  108. if (userForMakerCode.UserId > 0)
  109. {
  110. var user = db.Users.FirstOrDefault(m => m.Id == userForMakerCode.UserId) ?? new Users();
  111. if (user.Id > 0)
  112. {
  113. user.LeaderLevel = data.LeaderLevel;
  114. Fields.Add("UserId", userForMakerCode.UserId); //创客
  115. Fields.Add("LeaderLevel", data.LeaderLevel); //盟主等级
  116. Fields.Add("SeoKeyword", data.SeoKeyword);//附件
  117. int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Add("Leaders", Fields, 0);
  118. AddSysLog(data.Id.ToString(), "Leaders", "add");
  119. db.SaveChanges();
  120. }
  121. }
  122. return "success";
  123. }
  124. #endregion
  125. #region 修改盟主管理
  126. /// <summary>
  127. /// 增加或修改盟主管理信息
  128. /// </summary>
  129. /// <returns></returns>
  130. public IActionResult Edit(string right, int Id = 0)
  131. {
  132. ViewBag.RightInfo = RightInfo;
  133. ViewBag.right = right;
  134. Leaders editData = db.Leaders.FirstOrDefault(m => m.Id == Id) ?? new Leaders();
  135. Users editDatas = db.Users.FirstOrDefault(m => m.Id == editData.UserId) ?? new Users();
  136. ViewBag.data = editData;
  137. ViewBag.datas = editDatas;
  138. return View();
  139. }
  140. #endregion
  141. #region 修改盟主管理
  142. /// <summary>
  143. /// 增加或修改盟主管理信息
  144. /// </summary>
  145. /// <returns></returns>
  146. [HttpPost]
  147. public string Edit(Leaders data, string MakerCode)
  148. {
  149. Dictionary<string, object> Fields = new Dictionary<string, object>();
  150. var userForMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode) ?? new UserForMakerCode();
  151. if (userForMakerCode.UserId > 0)
  152. {
  153. var user = db.Users.FirstOrDefault(m => m.Id == userForMakerCode.UserId) ?? new Users();
  154. if (user.Id > 0)
  155. {
  156. user.LeaderLevel = data.LeaderLevel;
  157. Fields.Add("UserId", userForMakerCode.UserId); //创客
  158. Fields.Add("LeaderLevel", data.LeaderLevel); //盟主等级
  159. Fields.Add("SeoKeyword", data.SeoKeyword);
  160. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("Leaders", Fields, data.Id);
  161. var leader = db.Leaders.FirstOrDefault(m => m.Id == data.Id) ?? new Leaders();
  162. if (leader.Id > 0)
  163. {
  164. leader.CreateDate = leader.CreateDate;
  165. }
  166. AddSysLog(data.Id.ToString(), "Leaders", "update");
  167. db.SaveChanges();
  168. }
  169. }
  170. return "success";
  171. }
  172. #endregion
  173. #region 删除盟主管理信息
  174. /// <summary>
  175. /// 删除盟主管理信息
  176. /// </summary>
  177. /// <returns></returns>
  178. public string Delete(string Id)
  179. {
  180. string[] idlist = Id.Split(new char[] { ',' });
  181. AddSysLog(Id, "Leaders", "del");
  182. foreach (string subid in idlist)
  183. {
  184. int id = int.Parse(subid);
  185. Dictionary<string, object> Fields = new Dictionary<string, object>();
  186. Fields.Add("Status", -1);
  187. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("Leaders", Fields, id);
  188. }
  189. db.SaveChanges();
  190. return "success";
  191. }
  192. #endregion
  193. #region 开启
  194. /// <summary>
  195. /// 开启
  196. /// </summary>
  197. /// <returns></returns>
  198. public string Open(string Id)
  199. {
  200. string[] idlist = Id.Split(new char[] { ',' });
  201. AddSysLog(Id, "Leaders", "open");
  202. foreach (string subid in idlist)
  203. {
  204. int id = int.Parse(subid);
  205. Dictionary<string, object> Fields = new Dictionary<string, object>();
  206. Fields.Add("Status", 1);
  207. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("Leaders", Fields, id);
  208. }
  209. db.SaveChanges();
  210. return "success";
  211. }
  212. #endregion
  213. #region 关闭
  214. /// <summary>
  215. /// 关闭
  216. /// </summary>
  217. /// <returns></returns>
  218. public string Close(string Id)
  219. {
  220. string[] idlist = Id.Split(new char[] { ',' });
  221. AddSysLog(Id, "Leaders", "close");
  222. foreach (string subid in idlist)
  223. {
  224. int id = int.Parse(subid);
  225. Dictionary<string, object> Fields = new Dictionary<string, object>();
  226. Fields.Add("Status", 0);
  227. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("Leaders", Fields, id);
  228. }
  229. db.SaveChanges();
  230. return "success";
  231. }
  232. #endregion
  233. #region 排序
  234. /// <summary>
  235. /// 排序
  236. /// </summary>
  237. /// <param name="Id"></param>
  238. public string Sort(int Id, int Sort)
  239. {
  240. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Sort("Leaders", Sort, Id);
  241. AddSysLog(Id.ToString(), "Leaders", "sort");
  242. return "success";
  243. }
  244. #endregion
  245. #region 导入数据
  246. /// <summary>
  247. /// 导入数据
  248. /// </summary>
  249. /// <param name="ExcelData"></param>
  250. public string Import(string ExcelData)
  251. {
  252. ExcelData = HttpUtility.UrlDecode(ExcelData);
  253. JsonData list = JsonMapper.ToObject(ExcelData);
  254. for (int i = 1; i < list.Count; i++)
  255. {
  256. JsonData dr = list[i];
  257. db.Leaders.Add(new Leaders()
  258. {
  259. CreateDate = DateTime.Now,
  260. UpdateDate = DateTime.Now,
  261. });
  262. db.SaveChanges();
  263. }
  264. AddSysLog("0", "Leaders", "Import");
  265. return "success";
  266. }
  267. #endregion
  268. #region 导出Excel
  269. /// <summary>
  270. /// 导出Excel
  271. /// </summary>
  272. /// <returns></returns>
  273. public JsonResult ExportExcel(Leaders data, string UserIdMakerCode, string CreateDateData, string LeaderLevelSelect)
  274. {
  275. Dictionary<string, string> Fields = new Dictionary<string, string>();
  276. string condition = " and Status>-1";
  277. //创客创客编号
  278. if (!string.IsNullOrEmpty(UserIdMakerCode))
  279. {
  280. condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + UserIdMakerCode + "')";
  281. }
  282. //盟主等级
  283. if (!string.IsNullOrEmpty(LeaderLevelSelect))
  284. {
  285. condition += " and LeaderLevel=" + LeaderLevelSelect;
  286. }
  287. //创建时间
  288. if (!string.IsNullOrEmpty(CreateDateData))
  289. {
  290. string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
  291. string start = datelist[0];
  292. string end = datelist[1];
  293. condition += " and CreateDate>='" + start + " 00:00:00' and CreateDate<='" + end + " 23:59:59'";
  294. }
  295. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("Leaders", Fields, "Id desc", "0", 1, 20000, condition, "UserId,LeaderLevel,CreateDate", false);
  296. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  297. foreach (Dictionary<string, object> dic in diclist)
  298. {
  299. //创客
  300. int UserId = int.Parse(function.CheckInt(dic["UserId"].ToString()));
  301. Users userid_Users = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  302. dic["UserIdMakerCode"] = userid_Users.MakerCode;
  303. dic["UserIdRealName"] = userid_Users.RealName;
  304. dic.Remove("UserId");
  305. //盟主等级
  306. int LeaderLevel = int.Parse(dic["LeaderLevel"].ToString());
  307. if (LeaderLevel == 1) dic["LeaderLevel"] = "小盟主";
  308. if (LeaderLevel == 2) dic["LeaderLevel"] = "大盟主";
  309. if (LeaderLevel == 0) dic["LeaderLevel"] = "";
  310. }
  311. Dictionary<string, object> result = new Dictionary<string, object>();
  312. result.Add("Status", "1");
  313. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  314. result.Add("Obj", diclist);
  315. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  316. ReturnFields.Add("UserIdMakerCode", "创客创客编号");
  317. ReturnFields.Add("UserIdRealName", "创客真实姓名");
  318. ReturnFields.Add("LeaderLevel", "盟主等级");
  319. ReturnFields.Add("CreateDate", "创建时间");
  320. result.Add("Fields", ReturnFields);
  321. AddSysLog("0", "Leaders", "ExportExcel");
  322. return Json(result);
  323. }
  324. #endregion
  325. }
  326. }