using JiepeiWMS.IServices; using JiepeiWMS.Model; using JiepeiWMS.Model.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.Linq.Expressions; using System.Threading.Tasks; using System.Collections.Generic; using System.Linq; using System.Collections.Generic; using JiepeiWMS.Extends; using JiepeiWMS.Common.HttpContextUser; using JiepeiWMS.Common.Helper; namespace JiepeiWMS.Api.Controllers { [Route("api/[controller]/[action]")] [ApiController] [Authorize(Permissions.Name)] public class WMSupplierTypeController : ControllerBase { /// /// 服务器接口,因为是模板生成,所以首字母是大写的,自己可以重构下 /// private readonly IWMSupplierTypeServices _wMSupplierTypeServices; private readonly IUser _user; private readonly ISysUserInfoServices _sysUserInfoServices; public WMSupplierTypeController(IWMSupplierTypeServices WMSupplierTypeServices, IUser user, ISysUserInfoServices SysUserInfoServices) { _wMSupplierTypeServices = WMSupplierTypeServices; _user = user; _sysUserInfoServices = SysUserInfoServices; } /// /// /// /// /// /// /// [HttpGet] public async Task>> Get(int page = 1, string key = "", int intPageSize = 20) { if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) { key = ""; } Expression> whereExpression = a => a.Id > 0; whereExpression = whereExpression.And(w => w.IsEnable == true); if (!string.IsNullOrEmpty(key)) { whereExpression = whereExpression.And(w => w.SupTypeName.Contains(key.Trim())); } return new MessageModel>() { msg = "获取成功", success = true, response = await _wMSupplierTypeServices.QueryPage(whereExpression, page, intPageSize) }; } /// /// 查询树形节点 /// /// 父节点 /// 关键字 /// [HttpGet] [AllowAnonymous] public async Task>> GetTreeTable(int parent = 0, string key = "") { List SupplierType = new List(); var WMSupplierList = await _wMSupplierTypeServices.Query(); if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) { key = ""; } //筛选启用的类型 WMSupplierList = WMSupplierList.Where(a => a.IsEnable == true).ToList(); if (key != "") { SupplierType = WMSupplierList.Where(a => a.SupTypeName.Contains(key)).OrderBy(a => a.Id).ToList(); } else { SupplierType = WMSupplierList.Where(a => a.ParentId == parent).OrderBy(a => a.Id).ToList(); } foreach (var item in SupplierType) { List parentIdarr = new List { }; var SupplierTypeModel = WMSupplierList.FirstOrDefault(d => d.Id == item.ParentId); while (SupplierTypeModel != null) { parentIdarr.Add(SupplierTypeModel.Id); SupplierTypeModel = WMSupplierList.FirstOrDefault(d => d.Id == SupplierTypeModel.ParentId); } parentIdarr.Reverse(); parentIdarr.Insert(0, 0); item.ParentIdArr = parentIdarr; item.hasChildren = key != "" ? false : WMSupplierList.Where(d => d.ParentId == item.Id).Any(); } return new MessageModel>() { msg = "获取成功", success = SupplierType.Count >= 0, response=SupplierType }; } /// /// 获取分类树下拉 /// /// /// /// [HttpGet] public async Task> GetSupplierTypeTree(int parentId = 0, bool needbtn = false) { var data = new MessageModel(); var supplierType = await _wMSupplierTypeServices.Query(); var supplierTypeTrees = (from child in supplierType orderby child.Id select new SupplierTypeTree { value = child.Id, label = child.SupTypeName, parentId = child.ParentId, order = child.Id, }).ToList(); SupplierTypeTree rootRoot = new SupplierTypeTree { value = 0, parentId = 0, label = "根节点" }; supplierTypeTrees = supplierTypeTrees.OrderBy(d => d.order).ToList(); RecursionHelper.LoopToAppendChildrenSupplierType(supplierTypeTrees, rootRoot, parentId, needbtn); data.success = true; if (data.success) { data.response = rootRoot; data.msg = "获取成功"; } return data; } /// /// /// /// /// /// /// [HttpGet] public async Task>> GetSelect(int page = 1, string key = "", int intPageSize = 500) { if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key)) { key = ""; } Expression> whereExpression = a => a.Id > 0; whereExpression = whereExpression.And(w => w.IsEnable == true); return new MessageModel>() { msg = "获取成功", success = true, response = await _wMSupplierTypeServices.QueryPage(whereExpression, page, intPageSize) }; } /// /// /// /// /// [HttpGet("{id}")] public async Task> Get(int id = 0) { return new MessageModel() { msg = "获取成功", success = true, response = await _wMSupplierTypeServices.QueryById(id) }; } /// /// /// /// /// [HttpPost] public async Task> Post([FromBody] WMSupplierType request) { var data = new MessageModel(); //添加时判断供应商类型名称是否存在 var WMSupplier = await _wMSupplierTypeServices.GetModel(w => w.SupTypeName == request.SupTypeName); if (WMSupplier!=null) { data.msg = "供应商类型名称已存在!"; return data; } var SupplierTypeList = await _wMSupplierTypeServices.Query(x => x.Id == request.ParentId); if (SupplierTypeList != null && SupplierTypeList.Any()) { request.LevelId = SupplierTypeList.FirstOrDefault().LevelId + 1; } else { request.LevelId = 1; } request.SysOrgId = _user.SysOrgId; var id = await _wMSupplierTypeServices.Add(request); data.success = id > 0; if (data.success) { data.response = id.ObjToString(); data.msg = "添加成功"; } return data; } /// /// 编辑 /// /// /// [HttpPut] public async Task> Put([FromBody] WMSupplierType request) { var data = new MessageModel(); if (request.Id > 0) { var SupplierTypeList = await _wMSupplierTypeServices.Query(x=>x.ParentId==request.ParentId); if (SupplierTypeList != null && SupplierTypeList.Any()) { request.LevelId = SupplierTypeList.FirstOrDefault().LevelId; } else { request.LevelId = 1; } data.success = await _wMSupplierTypeServices.Update(request); if (data.success) { data.msg = "更新成功"; data.response = request?.Id.ObjToString(); } } return data; } /// /// /// /// /// [HttpDelete] public async Task> Delete(int id = 0) { var data = new MessageModel(); if (id > 0) { var detail = await _wMSupplierTypeServices.QueryById(id); detail.IsEnable = false; if (detail != null) { data.success = await _wMSupplierTypeServices.Update(detail); if (data.success) { data.msg = "删除成功"; data.response = detail?.Id.ObjToString(); } } } return data; } } }