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
|
{
|
/// <summary>
|
/// 服务器接口,因为是模板生成,所以首字母是大写的,自己可以重构下
|
/// </summary>
|
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;
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="page"></param>
|
/// <param name="key"></param>
|
/// <param name="intPageSize"></param>
|
/// <returns></returns>
|
[HttpGet]
|
public async Task<MessageModel<PageModel<WMSupplierType>>> Get(int page = 1, string key = "", int intPageSize = 20)
|
{
|
if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
|
{
|
key = "";
|
}
|
|
Expression<Func<WMSupplierType, bool>> 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<PageModel<WMSupplierType>>()
|
{
|
msg = "获取成功",
|
success = true,
|
response = await _wMSupplierTypeServices.QueryPage(whereExpression, page, intPageSize)
|
};
|
|
}
|
|
/// <summary>
|
/// 查询树形节点
|
/// </summary>
|
/// <param name="parent">父节点</param>
|
/// <param name="key">关键字</param>
|
/// <returns></returns>
|
[HttpGet]
|
[AllowAnonymous]
|
public async Task<MessageModel<List<WMSupplierType>>> GetTreeTable(int parent = 0, string key = "")
|
{
|
List<WMSupplierType> SupplierType = new List<WMSupplierType>();
|
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<int> parentIdarr = new List<int> { };
|
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<List<WMSupplierType>>()
|
{
|
msg = "获取成功",
|
success = SupplierType.Count >= 0,
|
response=SupplierType
|
};
|
}
|
|
/// <summary>
|
/// 获取分类树下拉
|
/// </summary>
|
/// <param name="parentId"></param>
|
/// <param name="needbtn"></param>
|
/// <returns></returns>
|
[HttpGet]
|
public async Task<MessageModel<SupplierTypeTree>> GetSupplierTypeTree(int parentId = 0, bool needbtn = false)
|
{
|
var data = new MessageModel<SupplierTypeTree>();
|
|
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;
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="page"></param>
|
/// <param name="key"></param>
|
/// <param name="intPageSize"></param>
|
/// <returns></returns>
|
[HttpGet]
|
public async Task<MessageModel<PageModel<WMSupplierType>>> GetSelect(int page = 1, string key = "", int intPageSize = 500)
|
{
|
if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
|
{
|
key = "";
|
}
|
|
Expression<Func<WMSupplierType, bool>> whereExpression = a => a.Id > 0;
|
whereExpression = whereExpression.And(w => w.IsEnable == true);
|
return new MessageModel<PageModel<WMSupplierType>>()
|
{
|
msg = "获取成功",
|
success = true,
|
response = await _wMSupplierTypeServices.QueryPage(whereExpression, page, intPageSize)
|
};
|
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="id"></param>
|
/// <returns></returns>
|
[HttpGet("{id}")]
|
public async Task<MessageModel<WMSupplierType>> Get(int id = 0)
|
{
|
return new MessageModel<WMSupplierType>()
|
{
|
msg = "获取成功",
|
success = true,
|
response = await _wMSupplierTypeServices.QueryById(id)
|
};
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="request"></param>
|
/// <returns></returns>
|
[HttpPost]
|
public async Task<MessageModel<string>> Post([FromBody] WMSupplierType request)
|
{
|
var data = new MessageModel<string>();
|
//添加时判断供应商类型名称是否存在
|
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;
|
}
|
|
/// <summary>
|
/// 编辑
|
/// </summary>
|
/// <param name="request"></param>
|
/// <returns></returns>
|
[HttpPut]
|
public async Task<MessageModel<string>> Put([FromBody] WMSupplierType request)
|
{
|
var data = new MessageModel<string>();
|
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;
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="id"></param>
|
/// <returns></returns>
|
[HttpDelete]
|
public async Task<MessageModel<string>> Delete(int id = 0)
|
{
|
var data = new MessageModel<string>();
|
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;
|
}
|
}
|
}
|