using JCSoft.WX.Framework.Api; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Common.Wechat; using Common; using System.Data; using Common.Model; using ZcPeng.PublicLibrary; using CoreEntity.Entity; using CoreEntity.DAL; using System.Collections.Concurrent; using Newtonsoft.Json.Converters; using Newtonsoft.Json; using System.Data.SqlClient; using Microsoft.Extensions.Primitives; using Microsoft.Extensions.Caching.Memory; using Jwt; using PublicLibrary.Model; using Newtonsoft.Json.Linq; using SupplierWeb.Codes.mvc; using SupplierWeb.Codes.Auth; using Common.Config; namespace SupplierWeb.Controllers { [Route("web/Mem_AreaMap")] public class mem_AreaMapController : BaseController { public mem_AreaMapController(IMemoryCache cache, IApiClient client) : base(cache, client) { } private ConcurrentDictionary Mem_AreaMapMap = new ConcurrentDictionary(); /// /// 获取组织机构列表 /// /// /// /// /// /// /// /// /// [AuthPermission] [HttpPost, Route("index")] public JsonResult Indexp(QueryFilter[] filters, Int32 pageIndex, Int32 pageSize, string sortField, Int32 sortDirection, string[] sumFields, [FromBody]dynamic data) { if (data != null) { filters = data.filters.ToObject(); pageIndex = data.pageIndex; pageSize = data.pageSize; sortField = data.sortField; sortDirection = data.sortDirection; sumFields = data.sumFields.ToObject(); } #region 获取机构列表 DataTable dt = new DataTable(); string result; IList permss = new List(0); List parameters = new List(); string filterstr = QueryFilter.getFilterSqlParam(filters, out parameters, new Mem_AreaMap(), "A."); string direct = " desc "; if (sortDirection != 1) { direct = " asc"; } int start = (pageIndex - 1) * pageSize; int end = (start + 1 + pageSize); string commandText0 = "select * from "; string commandText1 = "(" + "select A.*,row_number() over" + "( order by A.AreaCode " + direct + " ) as rownum from " + "Mem_AreaMap as A " + " where 1=1 " + filterstr + ")AAA "; string commandText2 = " where AAA.rownum>" + start + " and AAA.rownum<" + end + " ORDER BY AAA.AreaCode desc"; string commandText3 = commandText0 + commandText1 + commandText2; bool success = DataAccess.GetValues(commandText3, ref dt, parameters.ToArray(), out result); #endregion if (dt != null && dt.Rows.Count > 0) { // 把DataTable转换为IList permss = ModelConvertHelper.ConvertToModel(dt); } string result1; long totalcount = DataAccess.GetRowCountDefine("select count(AreaCode) from " + commandText1, parameters.ToArray(), out result1); IsoDateTimeConverter timejson = new IsoDateTimeConverter { DateTimeFormat = "yyyy'-'MM'-'dd' 'HH':'mm':'ss" }; //IList menus = Permission.Convert(permss); var jsonData = JsonConvert.SerializeObject(permss, timejson); return Json(new { items = JsonConvert.DeserializeObject(jsonData), sum = new { }, totalCount = totalcount }); } /// /// 添加 /// /// /// /// /// /// /// [AuthPermission] [HttpPost, Route("add")] public JsonResult Addp( string AreaCode, string AreaName, string ParentCode, Int32 Level, string RelateID, string MappingCode, [FromBody]dynamic data ) { if (data != null) { if (data.AreaCode != null) { AreaCode = data.AreaCode; } else { AreaCode = ""; } if (data.AreaName != null) { AreaName = data.AreaName; } else { AreaName = ""; } if (data.ParentCode != null) { ParentCode = data.ParentCode; } else { ParentCode = ""; } if (data.Level != null) { Level = data.Level; } else { Level = 0; } if (data.RelateID != null) { RelateID = data.RelateID; } else { RelateID = ""; } if (data.MappingCode != null) { MappingCode = data.MappingCode; } else { MappingCode = ""; } } #region 添加机构节点 //String uuid = System.Guid.NewGuid().ToString("N"); int newid = Convert.ToInt32(ReturnMaxID("AreaCode", "Mem_AreaMap")) + 1; string commandText = "INSERT INTO Mem_AreaMap (AreaCode," + "AreaName,ParentCode,Level,RelateID,MappingCode)" + " VALUES (" + newid + ",@AreaCode,@AreaName,@ParentCode,@Level,@RelateID,@MappingCode)"; string result; //string result1; //准备参数 List> parameters = new List>(); parameters.Add(new List() { "AreaCode", AreaCode }); parameters.Add(new List() { "AreaName", AreaName }); parameters.Add(new List() { "ParentCode", ParentCode }); parameters.Add(new List() { "Level", Level }); parameters.Add(new List() { "RelateID", RelateID }); parameters.Add(new List() { "MappingCode", MappingCode }); List parameters1 = DataAccess.ToParameters(parameters); int success = DataAccess.ExecuteCommand(commandText, parameters1, out result); #endregion return Json(new { success = success }); } /// /// 修改 /// /// /// /// /// /// /// [AuthPermission] [HttpPost, Route("edit")] public JsonResult editp( string AreaCode, string AreaName, string ParentCode, Int32 Level, string RelateID, string MappingCode, [FromBody]dynamic data ) { if (data != null) { if (data.AreaCode != null) { AreaCode = data.AreaCode; } else { AreaCode = ""; } if (data.AreaName != null) { AreaName = data.AreaName; } else { AreaName = ""; } if (data.ParentCode != null) { ParentCode = data.ParentCode; } else { ParentCode = ""; } if (data.Level != null) { Level = data.Level; } else { Level = 0; } if (data.RelateID != null) { RelateID = data.RelateID; } else { RelateID = ""; } if (data.MappingCode != null) { MappingCode = data.MappingCode; } else { MappingCode = ""; } } #region 编辑用户 string commandText = "UPDATE Mem_AreaMap " + " SET AreaCode=@AreaCode, AreaName = @AreaName,"; commandText += " ParentCode = @ParentCode" + ",Level = @Level" + ",RelateID = @RelateID" + ",MappingCode = @MappingCode" + " WHERE AreaCode= @AreaCode"; string result; //准备参数 List> parameters = new List>(); parameters.Add(new List() { "AreaCode", AreaCode }); parameters.Add(new List() { "AreaName", AreaName }); parameters.Add(new List() { "ParentCode", ParentCode }); parameters.Add(new List() { "Level", Level }); parameters.Add(new List() { "RelateID", RelateID }); parameters.Add(new List() { "MappingCode", MappingCode }); List parameters1 = DataAccess.ToParameters(parameters); int success = DataAccess.ExecuteCommand(commandText, parameters1, out result); #endregion return Json(new { success = success, result = result, }); } /// /// 组织机构删除 /// /// /// [AuthPermission] [HttpPost, Route("delete")] public ActionResult Delete(string id) { //SQL语句 string commandText = "DELETE FROM Mem_AreaMap WHERE AreaCode = @id"; //string commandText1 = "DELETE FROM Mem_AreaMapRole WHERE AreaCode = @id"; //准备参数 List> parameters = new List>(); parameters.Add(new List() { "id", id }); string result = ""; //string result1 = ""; //参数转换 List parameters1 = DataAccess.ToParameters(parameters); //执行并返回结果 int success = DataAccess.ExecuteCommand(commandText, parameters1, out result); //int success1 = DataAccess.ExecuteCommand(commandText1, parameters1, out result1); //如果Mem_AreaMap和Mem_AreaMapRole表记录都删除成功 int success2 = 0; if (success > 0) { success2 = success;//此处应该是success2 = success+success1,由于不清楚前端是怎么接收处理的,暂且这么处理。 } else { success2 = -1; } return Content("{success:" + success2 + "}"); } /// /// 获取最大的id /// /// /// /// public object ReturnMaxID(string field, string tablename) { string result; List parameters = new List(); bool boolen = DataAccess.GetOneValue("select max(" + field + ") from " + tablename, parameters.ToArray(), out object objValue, out result); if (boolen == true) { return objValue; } else { return result; } } } }