Mem_AreaRegionController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using JCSoft.WX.Framework.Api;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Microsoft.AspNetCore.Http;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Common.Wechat;
  9. using Common;
  10. using System.Data;
  11. using Common.Model;
  12. using ZcPeng.PublicLibrary;
  13. using CoreEntity.Entity;
  14. using CoreEntity.DAL;
  15. using System.Collections.Concurrent;
  16. using Newtonsoft.Json.Converters;
  17. using Newtonsoft.Json;
  18. using System.Data.SqlClient;
  19. using Microsoft.Extensions.Primitives;
  20. using Microsoft.Extensions.Caching.Memory;
  21. using Jwt;
  22. using PublicLibrary.Model;
  23. using Newtonsoft.Json.Linq;
  24. using SupplierWeb.Codes.mvc;
  25. using SupplierWeb.Codes.Auth;
  26. using Common.Config;
  27. namespace SupplierWeb.Controllers
  28. {
  29. [Route("web/Mem_AreaRegion")]
  30. public class Mem_AreaRegionController : BaseController
  31. {
  32. public Mem_AreaRegionController(IMemoryCache cache, IApiClient client) : base(cache, client)
  33. {
  34. }
  35. private ConcurrentDictionary<int, Mem_AreaRegion> Mem_AreaRegionMap = new ConcurrentDictionary<int, Mem_AreaRegion>();
  36. /// <summary>
  37. /// 获取组织机构列表
  38. /// </summary>
  39. /// <param name="filters"></param>
  40. /// <param name="pageIndex"></param>
  41. /// <param name="pageSize"></param>
  42. /// <param name="sortField"></param>
  43. /// <param name="sortDirection"></param>
  44. /// <param name="sumFields"></param>
  45. /// <param name="data"></param>
  46. /// <returns></returns>
  47. [AuthPermission]
  48. [HttpPost, Route("index")]
  49. public JsonResult Indexp(QueryFilter[] filters, Int32 pageIndex, Int32 pageSize,
  50. string sortField, Int32 sortDirection, string[] sumFields, [FromBody]dynamic data)
  51. {
  52. if (data != null)
  53. {
  54. filters = data.filters.ToObject<QueryFilter[]>();
  55. pageIndex = data.pageIndex;
  56. pageSize = data.pageSize;
  57. sortField = data.sortField;
  58. sortDirection = data.sortDirection;
  59. sumFields = data.sumFields.ToObject<string[]>();
  60. }
  61. #region 获取机构列表
  62. DataTable dt = new DataTable();
  63. string result;
  64. IList<Mem_AreaRegion> permss = new List<Mem_AreaRegion>(0);
  65. List<SqlParameter> parameters = new List<SqlParameter>();
  66. string filterstr = QueryFilter.getFilterSqlParam(filters, out parameters, new Mem_AreaRegion(), "A.");
  67. string direct = " desc ";
  68. if (sortDirection != 1)
  69. {
  70. direct = " asc";
  71. }
  72. int start = (pageIndex - 1) * pageSize;
  73. int end = (start + 1 + pageSize);
  74. //拼接SQL
  75. string commandText0 = "select * from ";
  76. string commandText1 = "(" +
  77. "select A.*,(select AreaName from Mem_AreaMap b where a.ParentCode=b.MappingCode) as ParentName" +
  78. ",row_number() over" +
  79. "( order by A.AreaCode " + direct + " ) as rownum from " +
  80. "Mem_AreaRegion as A " +
  81. " where 1=1 " +
  82. filterstr +
  83. ")AAA ";
  84. string commandText2 = " where AAA.rownum>" + start + " and AAA.rownum<" + end
  85. + " ORDER BY AAA.AreaCode desc";
  86. string commandText3 = commandText0 + commandText1 + commandText2;
  87. //执行SQL并返回结果
  88. bool success = DataAccess.GetValues(commandText3, ref dt, parameters.ToArray(), out result);
  89. #endregion
  90. if (dt != null && dt.Rows.Count > 0)
  91. {
  92. // 把DataTable转换为IList<T>
  93. permss = ModelConvertHelper<Mem_AreaRegion>.ConvertToModel(dt);
  94. }
  95. string result1;
  96. //总数
  97. long totalcount = DataAccess.GetRowCountDefine("select count(AreaCode) from " + commandText1, parameters.ToArray(), out result1);
  98. IsoDateTimeConverter timejson = new IsoDateTimeConverter
  99. {
  100. DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
  101. };
  102. //IList<Menu> menus = Permission.Convert(permss);
  103. var jsonData = JsonConvert.SerializeObject(permss, timejson);
  104. return Json(new
  105. {
  106. items = JsonConvert.DeserializeObject(jsonData),
  107. sum = new { },
  108. totalCount = totalcount
  109. });
  110. }
  111. /// <summary>
  112. /// 添加
  113. /// </summary>
  114. /// <param name="LogisticsEntKey"></param>
  115. /// <param name="LogisticsEntCode"></param>
  116. /// <param name="LogisticsEntName"></param>
  117. /// <param name="LogisticsEntCodeExt"></param>
  118. /// <param name="data"></param>
  119. /// <returns></returns>
  120. [AuthPermission]
  121. [HttpPost, Route("add")]
  122. public JsonResult Addp(
  123. string AreaCode, string AreaName, string ParentCode, [FromBody]dynamic data
  124. )
  125. {
  126. if (data != null)
  127. {
  128. if (data.AreaCode != null) { AreaCode = data.AreaCode; } else { AreaCode = ""; }
  129. if (data.AreaName != null) { AreaName = data.AreaName; } else { AreaName = ""; }
  130. if (data.ParentCode != null) { ParentCode = data.ParentCode; } else { ParentCode = ""; }
  131. }
  132. #region 添加机构节点
  133. //String uuid = System.Guid.NewGuid().ToString("N");
  134. int newid = Convert.ToInt32(ReturnMaxID("AreaCode", "Mem_AreaRegion")) + 1;
  135. string commandText = "INSERT INTO Mem_AreaRegion (AreaCode," +
  136. "AreaName,ParentCode)" +
  137. " VALUES (" + newid + ",@AreaName,@ParentCode)";
  138. string result;
  139. //string result1;
  140. //准备参数
  141. List<List<Object>> parameters = new List<List<Object>>();
  142. parameters.Add(new List<Object>() { "AreaCode", AreaCode });
  143. parameters.Add(new List<Object>() { "AreaName", AreaName });
  144. parameters.Add(new List<Object>() { "ParentCode", ParentCode });
  145. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  146. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  147. #endregion
  148. return Json(new
  149. {
  150. success = success
  151. });
  152. }
  153. /// <summary>
  154. /// 修改
  155. /// </summary>
  156. /// <param name="LogisticsEntKey"></param>
  157. /// <param name="LogisticsEntCode"></param>
  158. /// <param name="LogisticsEntName"></param>
  159. /// <param name="LogisticsEntCodeExt"></param>
  160. /// <param name="data"></param>
  161. /// <returns></returns>
  162. [AuthPermission]
  163. [HttpPost, Route("edit")]
  164. public JsonResult editp(
  165. string AreaCode, string AreaName, string ParentCode, [FromBody]dynamic data
  166. )
  167. {
  168. if (data != null)
  169. {
  170. if (data.AreaCode != null) { AreaCode = data.AreaCode; } else { AreaCode = ""; }
  171. if (data.AreaName != null) { AreaName = data.AreaName; } else { AreaName = ""; }
  172. if (data.ParentCode != null) { ParentCode = data.ParentCode; } else { ParentCode = ""; }
  173. }
  174. #region 编辑用户
  175. string commandText = "UPDATE Mem_AreaRegion " +
  176. " SET AreaCode=@AreaCode, AreaName = @AreaName,";
  177. commandText += " ParentCode = @ParentCode" +
  178. " WHERE AreaCode= @AreaCode";
  179. string result;
  180. //准备参数
  181. List<List<Object>> parameters = new List<List<Object>>();
  182. parameters.Add(new List<Object>() { "AreaCode", AreaCode });
  183. parameters.Add(new List<Object>() { "AreaName", AreaName });
  184. parameters.Add(new List<Object>() { "ParentCode", ParentCode });
  185. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  186. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  187. #endregion
  188. return Json(new
  189. {
  190. success = success,
  191. result = result,
  192. });
  193. }
  194. /// <summary>
  195. /// 组织机构删除
  196. /// </summary>
  197. /// <param name="id"></param>
  198. /// <returns></returns>
  199. [AuthPermission]
  200. [HttpPost, Route("delete")]
  201. public ActionResult Delete(string id)
  202. {
  203. //SQL语句
  204. string commandText = "DELETE FROM Mem_AreaRegion WHERE AreaCode = @id";
  205. //string commandText1 = "DELETE FROM Mem_AreaRegionRole WHERE AreaCode = @id";
  206. //准备参数
  207. List<List<Object>> parameters = new List<List<Object>>();
  208. parameters.Add(new List<Object>() { "id", id });
  209. string result = "";
  210. //string result1 = "";
  211. //参数转换
  212. List<SqlParameter> parameters1 = DataAccess.ToParameters(parameters);
  213. //执行并返回结果
  214. int success = DataAccess.ExecuteCommand(commandText, parameters1, out result);
  215. //int success1 = DataAccess.ExecuteCommand(commandText1, parameters1, out result1);
  216. //如果Mem_AreaRegion和Mem_AreaRegionRole表记录都删除成功
  217. int success2 = 0;
  218. if (success > 0)
  219. {
  220. success2 = success;//此处应该是success2 = success+success1,由于不清楚前端是怎么接收处理的,暂且这么处理。
  221. }
  222. else
  223. {
  224. success2 = -1;
  225. }
  226. return Content("{success:" + success2 + "}");
  227. }
  228. /// <summary>
  229. /// 获取最大的id
  230. /// </summary>
  231. /// <param name="field"></param>
  232. /// <param name="tablename"></param>
  233. /// <returns></returns>
  234. public object ReturnMaxID(string field, string tablename)
  235. {
  236. string result;
  237. List<SqlParameter> parameters = new List<SqlParameter>();
  238. bool boolen = DataAccess.GetOneValue("select max(" + field + ") from " + tablename, parameters.ToArray(), out object objValue, out result);
  239. if (boolen == true)
  240. {
  241. return objValue;
  242. }
  243. else
  244. {
  245. return result;
  246. }
  247. }
  248. }
  249. }