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