using JiepeiWMS.Common; using JiepeiWMS.Common.Helper; using JiepeiWMS.Common.HttpContextUser; using JiepeiWMS.IRepository.UnitOfWork; using JiepeiWMS.IServices; using JiepeiWMS.Model; using JiepeiWMS.Model.Models; using JiepeiWMS.Model.ViewModels; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using SqlSugar; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; namespace JiepeiWMS.Api.Controllers { [Route("api/[controller]/[action]")] [ApiController] [Authorize(Permissions.Name)] public class WMProductListController : ControllerBase { /// /// 商品 /// private readonly IWMProductListServices _wMProductListServices; private readonly IWMProductSpecServices _wMProductSpecServices; private readonly IWMProductUnitServices _wMProductUnitServices; private readonly IWMProductClassServices _wMProductClassServices; private readonly IWMProductInfoServices _wMProductInfoServices; private readonly IWMPurchaseQuoteDetailServices _wMPurchaseQuoteDetailServices; private readonly IWMSaleDetailServices _wMSaleDetailServices; private readonly ISysUserInfoServices _sysUserInfoServices; private readonly IWMWareHouseServices _wMWareHouseServices; private readonly ISysOrgServices _BllSysOrg; private readonly IUser _user; private readonly IUnitOfWork _unitOfWork; private readonly IUserRoleServices _userRole; public WMProductListController(IWMProductListServices WMProductListServices, IWMProductSpecServices WMProductSpecServices, IWMProductUnitServices WMProductUnitServices, IWMProductClassServices WMProductClassServices, IWMProductInfoServices WMProductInfoServices, IWMPurchaseQuoteDetailServices WMPurchaseQuoteDetailServices, IWMSaleDetailServices WMSaleDetailServices, ISysUserInfoServices SysUserInfoServices, IWMWareHouseServices WMWareHouseServices, ISysOrgServices BllSysOrg, IUser user, IUnitOfWork unitOfWork, IUserRoleServices userRole) { _wMProductListServices = WMProductListServices; _wMProductSpecServices = WMProductSpecServices; _wMProductUnitServices = WMProductUnitServices; _wMProductClassServices = WMProductClassServices; _wMProductInfoServices = WMProductInfoServices; _wMPurchaseQuoteDetailServices = WMPurchaseQuoteDetailServices; _wMSaleDetailServices = WMSaleDetailServices; _sysUserInfoServices = SysUserInfoServices; _wMWareHouseServices = WMWareHouseServices; _BllSysOrg = BllSysOrg; _user = user; _unitOfWork = unitOfWork; _userRole = userRole; } /// /// 查询列表 /// /// 关键字 /// 分类筛选 /// 开始时间筛选 /// 结束时间筛选 /// 仓库Id /// 页数 /// 条数 /// [HttpGet] public async Task>> Index(string key = "", string options = "", DateTime? startdate = null, DateTime? enddate = null, int wareHouseId = 0, int page = 1, int intPageSize = 20) { if (string.IsNullOrWhiteSpace(key)) key = ""; if (string.IsNullOrWhiteSpace(options)) options = ""; var PageList = await _wMProductListServices.GetWMProductListPage(page, intPageSize, key, options, startdate, enddate, wareHouseId); return new MessageModel>() { msg = "获取成功", success = PageList.dataCount >= 0, response = PageList }; } /// /// 获取列表 /// /// [AllowAnonymous] [HttpGet] public async Task> Get(int id = 0, int wareHouseId = 0) { decimal quantity = 0; var listList = await _wMProductListServices.Query(x => x.IsUse == true); //listList = listList.Where(x => x.SysOrgId == _user.SysOrgId).ToList(); if (wareHouseId > 0) { var ProductInfoModel = await _wMProductInfoServices.GetModel(x => x.ProductListId == id && x.WareHouseId == wareHouseId); if (ProductInfoModel == null) quantity = 0; else quantity = ProductInfoModel.Quantity ?? 0; } return new MessageModel() { msg = "获取成功", success = true, response = new { listList, quantity } }; } /// /// 添加 /// /// /// [HttpPost] public async Task> Add([FromBody] WMProductList request) { var data = new MessageModel(); var sysUserInfoModel = await _sysUserInfoServices.QueryById(_user.ID); if (sysUserInfoModel == null || sysUserInfoModel.tdIsDelete == true) { data.msg = "当前用户已被禁用无法进行导入"; data.response = 0.ObjToString(); data.status = 500; return data; } var userRoleList = await _userRole.Query(x => x.UserId == sysUserInfoModel.uID); if (userRoleList == null && userRoleList.Count() == 0) { data.msg = "当前用户还未配置角色无法进行导入"; data.response = 0.ObjToString(); data.status = 500; return data; } //var productListModel = await _wMProductListServices.GetModel(x => x.Code == request.Code); //if (productListModel != null) //{ // data.msg = "已有相同商品编码,请联系管理员"; // return data; //} if (request.Price < 0) { data.msg = "零售价价格不能小于0"; return data; } if (request.PurchasePrice < 0) { data.msg = "采购价价格不能小于0"; return data; } var ProductClassMdoel = await _wMProductClassServices.QueryById(request.ClassId); if (ProductClassMdoel != null) { var ProductClassMdoel1 = await _wMProductClassServices.GetModel(x => x.ParentId == ProductClassMdoel.Id); if (ProductClassMdoel1 != null) { data.msg = "商品分类不是末级分类"; return data; } } else { data.msg = "未获取到商品分类"; return data; } request.AdminId = _user.ID; request.CreateTime = DateTime.Now; request.SysOrgId = _user.SysOrgId; var result2 = new Tuple("", true, ""); //根据配置是否调用NC接口 var NCInterfaceSecret = Appsettings.app("AppSettings", "NCInterfaceSecret").ToString(); if (NCInterfaceSecret == "True") //调NC新增物料接口 result2 = await _wMProductListServices.BeginMaterialInNC(request); if (result2.Item2) { if (!string.IsNullOrWhiteSpace(result2.Item3)) request.Code = result2.Item3; var result = await _wMProductListServices.Add(request); if (result > 0) { var wareHouseModel = await _wMWareHouseServices.GetModel(a => a.SysOrgId == _user.SysOrgId); WMProductInfo model = new WMProductInfo() { ProductListId = result, ProductSpecId = request.SpecIds, PurchasePrice = request.PurchasePrice, Quantity = 0, AdminId = _user.ID, CreateTime = DateTime.Now, SysOrgId = _user.SysOrgId, WareHouseId = wareHouseModel == null ? 0 : wareHouseModel.Id }; var result1 = await _wMProductInfoServices.Add(model); data.success = result1 > 0; if (data.success) { data.msg = "添加成功"; data.response = result.ObjToString(); } } } else if (!result2.Item2 && !string.IsNullOrWhiteSpace(result2.Item1)) { data.msg = result2.Item1; data.response = 1.ObjToString(); } return data; } /// /// 更新 /// /// /// //[HttpPut] //public async Task> Update([FromBody] WMProductList request) //{ // var data = new MessageModel(); // if (request.Id > 0) // { // //var productListModel = await _wMProductListServices.GetModel(x => x.Code == request.Code && x.Id != request.Id); // //if (productListModel != null) // //{ // // data.msg = "已有相同商品编码,请联系管理员"; // // return data; // //} // if (request.Price < 0) // { // data.msg = "零售价价格不能小于0"; // return data; // } // var ProductClassMdoel = await _wMProductClassServices.QueryById(request.ClassId); // if (ProductClassMdoel != null) // { // var ProductClassMdoel1 = await _wMProductClassServices.GetModel(x => x.ParentId == ProductClassMdoel.Id); // if (ProductClassMdoel1 != null) // { // data.msg = "商品分类不是末级分类"; // return data; // } // } // else // { // data.msg = "未获取到商品分类"; // return data; // } // var result = new Tuple("", true, ""); // //根据配置是否调用NC接口 // var NCInterfaceSecret = Appsettings.app("AppSettings", "NCInterfaceSecret").ToString(); // if (NCInterfaceSecret == "True") // //调NC新增物料接口 // result = await _wMProductListServices.BeginMaterialInNC(request); // if (result.Item2) // { // if (!string.IsNullOrWhiteSpace(result.Item3)) // request.Code = result.Item3; // data.success = await _wMProductListServices.Update(request); // if (data.success) // { // data.msg = "更新成功"; // data.response = request?.Id.ObjToString(); // } // } // else if (!result.Item2 && !string.IsNullOrWhiteSpace(result.Item1)) // { // data.msg = result.Item1; // data.response = request?.Id.ObjToString(); // } // } // return data; //} /// /// 设置商品 /// /// /// [HttpPut] public async Task> SetProduct([FromBody] WMProductList request) { var data = new MessageModel(); if (request.Id > 0) { var wareHouseModel = await _wMWareHouseServices.GetModel(x => x.Name == request.WareHouseName || x.Id == request.WareHouseName.ObjToInt()); var ProductInfoModel = await _wMProductInfoServices.GetModel(x => x.ProductListId == request.Id && x.WareHouseId == wareHouseModel.Id); if (ProductInfoModel != null) { ProductInfoModel.PurchasePrice = request.PurchasePrice; ProductInfoModel.Quantity = request.Quantity; ProductInfoModel.UpdateTime = DateTime.Now; ProductInfoModel.WareHouseId = wareHouseModel.Id; await _wMProductInfoServices.Update(ProductInfoModel); } else { WMProductInfo model = new WMProductInfo() { ProductListId = request.Id, ProductSpecId = request.SpecIds, PurchasePrice = request.PurchasePrice, Quantity = request.Quantity, AdminId = _user.ID, CreateTime = DateTime.Now, SysOrgId = _user.SysOrgId, WareHouseId = wareHouseModel.Id }; await _wMProductInfoServices.Add(model); } if (string.IsNullOrWhiteSpace(request.Price.ToString()) && request.Price <= 0) { request.Price = request.PurchasePrice; } request.UpdateTime = DateTime.Now; data.success = await _wMProductListServices.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 productListModel = await _wMProductListServices.QueryById(id); // if (productListModel != null) // { // //var purchaseQuoteDetailModel = await _wMPurchaseQuoteDetailServices.GetModel(x => x.ProductId == productListModel.Id); // //if (purchaseQuoteDetailModel != null) // //{ // // data.msg = productListModel.Name + "无法删除,该商品在采购申请使用中"; // // return data; // //} // //var saleDetailModel = await _wMSaleDetailServices.GetModel(x => x.ProductId == productListModel.Id); // //if (saleDetailModel != null) // //{ // // data.msg = productListModel.Name + "无法删除,该商品在领料申请使用中"; // // return data; // //} // productListModel.IsUse = false; // var result = new Tuple("", true, ""); // //根据配置是否调用NC接口 // var NCInterfaceSecret = Appsettings.app("AppSettings", "NCInterfaceSecret").ToString(); // if (NCInterfaceSecret == "True") // //调NC新增物料接口 // result = await _wMProductListServices.BeginMaterialInNC(productListModel); // if (result.Item2) // { // if (!string.IsNullOrWhiteSpace(result.Item3)) // productListModel.Code = result.Item3; // data.success = await _wMProductListServices.Update(productListModel); // if (data.success) // { // data.msg = "删除成功"; // data.response = productListModel?.Id.ObjToString(); // } // } // else if (!result.Item2 && !string.IsNullOrWhiteSpace(result.Item1)) // { // data.msg = result.Item1; // data.response = productListModel?.Id.ObjToString(); // } // } // } // return data; //} /// /// 批量删除 /// /// /// //[HttpDelete] //public async Task> BatchDelete(string ids = "") //{ // //var err = ""; // var data = new MessageModel(); // var ProductListList = new List(); // if (!string.IsNullOrWhiteSpace(ids)) // { // foreach (var item in ids.Split(',')) // { // var productListModel = await _wMProductListServices.QueryById(item.ObjToInt()); // if (productListModel != null) // { // //var purchaseQuoteDetailModel = await _wMPurchaseQuoteDetailServices.GetModel(x => x.ProductId == productListModel.Id); // //var saleDetailModel = await _wMSaleDetailServices.GetModel(x => x.ProductId == productListModel.Id); // //if (purchaseQuoteDetailModel != null || saleDetailModel != null) // // err += productListModel.Name + ","; // //else // ProductListList.Add(productListModel); // } // } // //if (string.IsNullOrWhiteSpace(err)) // //{ // foreach (var item in ProductListList) // { // item.IsUse = false; // var result = new Tuple("", true, ""); // //根据配置是否调用NC接口 // var NCInterfaceSecret = Appsettings.app("AppSettings", "NCInterfaceSecret").ToString(); // if (NCInterfaceSecret == "True") // //调NC新增物料接口 // result = await _wMProductListServices.BeginMaterialInNC(item); // if (result.Item2) // { // if (!string.IsNullOrWhiteSpace(result.Item3)) // item.Code = result.Item3; // data.success = await _wMProductListServices.Update(item); // if (data.success) // { // data.msg = "删除成功"; // data.response = item?.Id.ObjToString(); // } // } // else if (!result.Item2 && !string.IsNullOrWhiteSpace(result.Item1)) // { // data.msg = result.Item1; // data.response = item?.Id.ObjToString(); // return data; // } // } // //} // //else // //{ // // data.msg = err.Trim(',') + "无法删除,有商品在领料申请使用中"; // //} // } // return data; //} /// /// 导出商品表格 /// [HttpGet] public async Task ExportProductList() { var sqlString = ""; var sysUserInfoModel = new sysUserInfo(); var role = string.Join(",", _user.GetClaimValueByType(ClaimTypes.Role)); if (role.Contains("仓库管理员")) { sysUserInfoModel = await _sysUserInfoServices.QueryById(_user.ID); sqlString += " and pf.SysOrgId=" + sysUserInfoModel.SysOrgId; } //获取角色信息,判断是否数据展示 if (role.Contains("超级管理员")) { } else { sqlString += " and pl.IsUse=1"; } var productList = await _wMProductListServices.QuerySql("select pl.Id,Name,(select Name from WMProductClass where id=ClassId)ClassName,(select Name from WMProductSpec where id = SpecIds)SpecName, (select Unit from WMProductUnit where id = pl.Unit)UnitName,Code,BarCode,Price,pf.PurchasePrice,pf.Quantity,(select Name from WMWareHouse where id = pf.WareHouseId)WareHouseName,OddDays,OddFloatNum,StoreBaseline,StoreLimit,Sort,case when isuse = 1 then '启用' else '停用' end IsUseName,case when IsChecked = 1 then '采购申请时需要审核' else '采购申请时不需要审核' end IsCheckedName from WMProductList pl left join WMProductInfo pf on pl.id = pf.ProductListId where 1=1" + sqlString); if (productList != null && productList.Any()) { var PList = productList.Select(c => new { c.Id, c.Name, c.ClassName, c.SpecName, c.UnitName, c.Code, c.BarCode, c.Price, c.PurchasePrice, c.Quantity, c.WareHouseName, c.OddDays, c.OddFloatNum, c.StoreBaseline, c.StoreLimit, c.Sort, c.IsUseName, c.IsCheckedName }).OrderBy(x => x.Id).ToList(); var heads = new List() { "编号", "商品名称", "商品分类", "商品单位", "商品规格", "物料编码", "商品条码", "零售价", "采购价", "库存", "所属仓库", "异常浮动天数", "异常浮动倍数", "库存预警下限", "库存预警上限", "排序", "状态", "是否需要审核" }; var stream = ExcelHelper.CreateExcelStreamFromList(PList, heads); return File(stream, "application/octet-stream", "商品.xlsx"); } return null; } /// /// 导入商品表格 /// [HttpPost] public async Task> ImportProductList([FromForm] IFormCollection files) { var data = new MessageModel(); var importProductListModel = new ImportProductListViewModels(); var importProductListList = new List(); try { var sysUserInfoModel = await _sysUserInfoServices.QueryById(_user.ID); if (sysUserInfoModel == null || sysUserInfoModel.tdIsDelete == true) { data.msg = "当前用户已被禁用无法进行导入"; data.response = 0.ObjToString(); data.status = 500; return data; } var userRoleList = await _userRole.Query(x => x.UserId == sysUserInfoModel.uID); if (userRoleList == null && userRoleList.Count() == 0) { data.msg = "当前用户还未配置角色无法进行导入"; data.response = 0.ObjToString(); data.status = 500; return data; } //创建事务 _unitOfWork.BeginTran(); var path = Directory.GetCurrentDirectory(); var fileFolder = Path.Combine(path, "ImportFile"); if (!Directory.Exists(fileFolder)) Directory.CreateDirectory(fileFolder); FormFileCollection fileCollection = (FormFileCollection)files.Files; foreach (var file in fileCollection) { var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(file.FileName); var filePath = Path.Combine(fileFolder, fileName); //using (var stream = new FileStream(filePath, FileMode.Create)) //{ // await file.CopyToAsync(stream); //} //var dtExcel = ExcelHelper.Read(filePath); using (var stream = new FileStream(filePath, FileMode.Create)) { file.CopyTo(stream); } var dtExcel = ExcelHelper.ImportExcel(filePath); foreach (DataRow myRow in dtExcel.Rows) { if (string.IsNullOrWhiteSpace(myRow[0].ToString())) continue; if (string.IsNullOrWhiteSpace(myRow[1].ToString())) continue; if (string.IsNullOrWhiteSpace(myRow[2].ToString())) continue; if (string.IsNullOrWhiteSpace(myRow[3].ToString())) continue; importProductListModel = new ImportProductListViewModels() { OrgName = myRow[0].ToString(), WareHouse = myRow[1].ToString(), ListCode = myRow[2].ToString(), ListName = myRow[3].ToString(), Class = myRow[4].ToString(), Spec = myRow[5].ToString(), Unit = myRow[6].ToString(), Quantity = myRow[7].ToString(), }; importProductListList.Add(importProductListModel); } foreach (var item in importProductListList) { var warehouseId = 0;//仓库ID var classId = 0;//分类ID var unitId = 0;//单元ID var specId = 0;//规格ID var sysOrgId = 0;//主体ID //var userId = 0;//用户ID #region 获取分类、规格、单元的参数 var code = item.ListCode.Substring(0, item.ListCode.Trim().Length - 5);//编码 var Level = ExcelHelper.GradeForSort("一级");//等级 var productClassList = await _wMProductClassServices.GetClassCodeList(code, Level); var sysOrgModel = await _BllSysOrg.GetShortName(item.OrgName.Trim()); if (sysOrgModel != null) { sysOrgId = sysOrgModel.Id; //userId = sysOrgModel.UserId.Value; } //获取仓库 if (!string.IsNullOrWhiteSpace(item.WareHouse)) { var wareHouseModel = await _wMWareHouseServices.GetModel(a => a.Name == item.WareHouse.Trim() && a.SysOrgId == sysOrgId); if (wareHouseModel == null) { //添加单位表 WMWareHouse modelWareHouse = new WMWareHouse() { Name = item.WareHouse.Trim(), IsUse = true, Type = 1, CreateTime = DateTime.Now, AdminId = _user.ID, Sort = 0, Remark = "", SysOrgId = sysOrgId }; warehouseId = await _wMWareHouseServices.Add(modelWareHouse); } else { warehouseId = wareHouseModel.Id; } } if (string.IsNullOrWhiteSpace(item.Class)) item.Class = "无"; if (string.IsNullOrWhiteSpace(code)) code = "0"; //获取分类 if (!string.IsNullOrWhiteSpace(code) && !string.IsNullOrWhiteSpace(item.Class)) { var productClassModel = await _wMProductClassServices.GetModel(a => a.ClassCode == code); if (productClassModel == null) { var productClassParentId = productClassList.Any() ? productClassList.OrderBy(x => x.Id).FirstOrDefault().Id : 0; var productClassSort = await _wMProductClassServices.Query(x => x.ParentId == productClassParentId); //添加分类表 WMProductClass modelClass = new WMProductClass() { ClassName = item.Class.Trim(), ClassCode = code, Sort = productClassSort.Count(), ParentId = productClassParentId, LevelId = Level, SysOrgId = sysOrgId }; classId = await _wMProductClassServices.Add(modelClass); } else { classId = productClassModel.Id; } } //获取单位 if (!string.IsNullOrWhiteSpace(item.Unit)) { var productUnitModel = await _wMProductUnitServices.GetModel(a => a.Unit == item.Unit.Trim()); if (productUnitModel == null) { //添加单位表 WMProductUnit modelUnit = new WMProductUnit() { Unit = item.Unit.Trim(), SysOrgId = sysOrgId }; unitId = await _wMProductUnitServices.Add(modelUnit); } else { unitId = productUnitModel.Id; } } //获取规格 if (!string.IsNullOrWhiteSpace(item.Spec)) { var productSpecModel = await _wMProductSpecServices.GetModel(a => a.Name == item.Spec.Trim()); if (productSpecModel == null) { //添加规格表 WMProductSpec modelSpec = new WMProductSpec() { Name = item.Spec.Trim(), AdminId = _user.ID, CreateTime = DateTime.Now, Remark = "", SysOrgId = sysOrgId }; specId = await _wMProductSpecServices.Add(modelSpec); } else { specId = productSpecModel.Id; } } #endregion //匹配商品表的分类编号 var productListModel = await _wMProductListServices.GetModel(x => x.Code == item.ListCode); if (productListModel != null) { //更新商品表 //productListModel.Name = item.ListName.Trim(); //productListModel.ClassId = classId; //productListModel.SpecIds = specId; //productListModel.Unit = unitId; //await _wMProductListServices.Update(productListModel); //匹配商品关联表的仓库 var productInfoModel = await _wMProductInfoServices.GetModel(x => x.ProductListId == productListModel.Id && x.WareHouseId == warehouseId); if (productInfoModel != null) { //更新商品关联表 //productInfoModel.ProductSpecId = specId; productInfoModel.Quantity = item.Quantity.Trim().ObjToDecimal(); await _wMProductInfoServices.Update(productInfoModel); } else { //添加商品关联表 WMProductInfo model = new WMProductInfo() { ProductListId = productListModel.Id, ProductSpecId = specId, PurchasePrice = 0, Quantity = item.Quantity.Trim().ObjToDecimal(), AdminId = _user.ID, CreateTime = DateTime.Now, SysOrgId = sysOrgId, WareHouseId = warehouseId }; await _wMProductInfoServices.Add(model); } } else { //添加商品表 var productList = new WMProductList { ClassId = classId, Name = item.ListName.Trim(), Code = item.ListCode, BarCode = "", Unit = unitId, StoreLimit = 100, StoreBaseline = 10, Price = 0, PurchasePrice = 0, IsUse = true, AdminId = _user.ID, CreateTime = DateTime.Now, SpecIds = specId, Sort = 0, OddDays = 15, OddFloatNum = 2, IsChecked = true, SimpleName = null, SysOrgId = sysOrgId }; var result = await _wMProductListServices.Add(productList); if (result > 0) { //添加商品关联表 WMProductInfo model = new WMProductInfo() { ProductListId = result, ProductSpecId = specId, PurchasePrice = 0, Quantity = item.Quantity.Trim().ObjToDecimal(), AdminId = _user.ID, CreateTime = DateTime.Now, SysOrgId = sysOrgId, WareHouseId = warehouseId }; await _wMProductInfoServices.Add(model); } } } } _unitOfWork.CommitTran(); data.response = 1.ObjToString(); data.msg = "导入商品成功"; data.success = true; return data; } catch (Exception ex) { _unitOfWork.RollbackTran(); data.msg = ex.Message; data.response = 0.ObjToString(); data.status = 500; return data; } } } }