|
|
@@ -1,5 +1,4 @@ |
|
|
|
using BPA.Franchisee.Application.FranchiseeCenter.GoodsServices; |
|
|
|
using BPA.SAAS.Manage.Application.DataBase.Dtos; |
|
|
|
using BPA.SAAS.Manage.Application.DataBase.Dtos; |
|
|
|
using BPA.SAAS.Manage.Application.DataBase.Dtos.Batching; |
|
|
|
using BPA.SAAS.Manage.Application.DataBase.Dtos.Bom; |
|
|
|
using BPA.SAAS.Manage.Application.DataBase.Dtos.Goods; |
|
|
@@ -34,7 +33,6 @@ using System.Linq; |
|
|
|
using System.Reflection; |
|
|
|
using System.Text; |
|
|
|
using System.Text.RegularExpressions; |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
namespace BPA.SAAS.Manage.Application.DataBase.Services |
|
|
|
{ |
|
|
@@ -44,14 +42,12 @@ namespace BPA.SAAS.Manage.Application.DataBase.Services |
|
|
|
IGoodsAttributeService _goodsAttributeService; |
|
|
|
ISystemConfigService _SystemConfigService; |
|
|
|
IAliyunOssService _aliyunOssService; |
|
|
|
IWebHostEnvironment _hostingEnvironment; |
|
|
|
public GoodsService(ISqlSugarClient db, IGoodsAttributeService goodsAttributeService, ISystemConfigService SystemConfigService, IAliyunOssService aliyunOssService, IWebHostEnvironment webHostEnvironment) |
|
|
|
{ |
|
|
|
_db=db; |
|
|
|
_goodsAttributeService=goodsAttributeService; |
|
|
|
_SystemConfigService = SystemConfigService; |
|
|
|
_aliyunOssService = aliyunOssService; |
|
|
|
_hostingEnvironment = webHostEnvironment; |
|
|
|
} |
|
|
|
/// <summary> |
|
|
|
/// 分页查询 |
|
|
@@ -383,7 +379,7 @@ namespace BPA.SAAS.Manage.Application.DataBase.Services |
|
|
|
var productCode = _db.Queryable<BPA_GoodsUint>().Where(x =>x.Name == dto.Name).ToList(); |
|
|
|
if (productCode.Count() > 0) |
|
|
|
{ |
|
|
|
throw Oops.Oh("商品单位已存在"); |
|
|
|
throw Oops.Oh($"{dto.Name}已存在,添加失败!"); |
|
|
|
} |
|
|
|
try |
|
|
|
{ |
|
|
@@ -392,9 +388,9 @@ namespace BPA.SAAS.Manage.Application.DataBase.Services |
|
|
|
var res = await _db.Insertable(bPA_Product).CallEntityMethod(m => m.Create()).ExecuteCommandAsync(); |
|
|
|
return res > 0; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
throw Oops.Oh("添加失败"); |
|
|
|
throw Oops.Oh(e.Message); |
|
|
|
} |
|
|
|
} |
|
|
|
/// <summary> |
|
|
@@ -1257,15 +1253,170 @@ namespace BPA.SAAS.Manage.Application.DataBase.Services |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 商品导入 |
|
|
|
/// </summary> |
|
|
|
/// <param name="importDto"></param> |
|
|
|
/// <returns></returns> |
|
|
|
public async Task<bool> GoodsImport(GoodsInfoImportDto importDto) |
|
|
|
{ |
|
|
|
var file = importDto.File; |
|
|
|
var aliyunHost = "https://bpa.oss-cn-chengdu.aliyuncs.com/"; |
|
|
|
var userId = App.User?.FindFirst(ClaimConst.CLAINM_USERID)?.Value; |
|
|
|
if (string.IsNullOrEmpty(userId)) |
|
|
|
{ |
|
|
|
userId = ""; |
|
|
|
} |
|
|
|
var goodsInfo = await _db.Queryable<BPA_GoodsInfo>().ToListAsync(); |
|
|
|
var goodsType = await _db.Queryable<BPA_GoodsType>().ToListAsync(); |
|
|
|
var goodsUint = await _db.Queryable<BPA_GoodsUint>().ToListAsync(); |
|
|
|
try |
|
|
|
{ |
|
|
|
_db.Ado.BeginTran(); |
|
|
|
var source = file.OpenReadStream(); |
|
|
|
var mapper = new Mapper(source); |
|
|
|
var excelDto = new ReadExcel<GoodsImportDto>(); |
|
|
|
var dataList = excelDto.ExcelToList(source, mapper); |
|
|
|
dataList.RemoveAll(t => t.Name == null); |
|
|
|
UpdateGoodsImportDto(dataList); |
|
|
|
var imageList = await GetImagesWithExcel(file.OpenReadStream(), Path.GetExtension(file.FileName)); |
|
|
|
var newUnitList = new List<BPA_GoodsUint>(); |
|
|
|
var i = 0; |
|
|
|
foreach (var data in dataList) |
|
|
|
{ |
|
|
|
if (data.Type == null) |
|
|
|
throw Oops.Oh($"商品分类不能为空!"); |
|
|
|
if (data.Name == null) |
|
|
|
throw Oops.Oh($"商品名称不能为空!"); |
|
|
|
if (data.Uint == null) |
|
|
|
throw Oops.Oh($"商品单位不能为空!"); |
|
|
|
var type = goodsType.FirstOrDefault(t => t.Name == data.Type); |
|
|
|
if (type == null) |
|
|
|
throw Oops.Oh($"{data.Name} 不存在分类:{data.Type}!"); |
|
|
|
var old = goodsInfo.FirstOrDefault(t => t.Name == data.Name && t.GoodsTypeId == type.Id); |
|
|
|
if (old != null) |
|
|
|
throw Oops.Oh($"{data.Name} 已存在该商品!"); |
|
|
|
var uintId = ""; |
|
|
|
var uintData = goodsUint.FirstOrDefault(t => t.Name == data.Uint); |
|
|
|
var newUnitData = newUnitList.FirstOrDefault(t => t.Name == data.Uint); |
|
|
|
if (uintData != null || newUnitData != null) |
|
|
|
uintId = uintData != null ? uintData.Id : newUnitData.Id; |
|
|
|
else |
|
|
|
{ |
|
|
|
uintId = Guid.NewGuid().ToString(); |
|
|
|
var newUint = new BPA_GoodsUint |
|
|
|
{ |
|
|
|
Id = uintId, |
|
|
|
Name = data.Uint, |
|
|
|
CreateBy = userId |
|
|
|
}; |
|
|
|
newUnitList.Add(newUint); |
|
|
|
await _db.Insertable(newUint).ExecuteCommandAsync(); |
|
|
|
} |
|
|
|
var imgUrl = data.ImgUrl != null ? data.ImgUrl.ToString() : ""; |
|
|
|
if (!imgUrl.Contains("=DISPIMG")) |
|
|
|
imgUrl = null; |
|
|
|
else |
|
|
|
{ |
|
|
|
imgUrl = aliyunHost + imageList[i]; |
|
|
|
i++; |
|
|
|
} |
|
|
|
var newValue = new BPA_GoodsInfo |
|
|
|
{ |
|
|
|
Id = Guid.NewGuid().ToString(), |
|
|
|
Name = data.Name, |
|
|
|
GoodsTypeId = type.Id, |
|
|
|
Descritption = data.Descritption, |
|
|
|
ImgUrl = imgUrl, |
|
|
|
Code = GetNumber2(8), |
|
|
|
Price = data.Price, |
|
|
|
CreateBy = userId, |
|
|
|
GoodsUintId = uintId, |
|
|
|
IsWeigh = data.IsWeigh == "是", |
|
|
|
IsAttrubute = true |
|
|
|
}; |
|
|
|
await _db.Insertable(newValue).ExecuteCommandAsync(); |
|
|
|
} |
|
|
|
_db.Ado.CommitTran(); |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
_db.Ado.RollbackTran(); |
|
|
|
throw Oops.Oh($"Excel导入错误," + e.Message); |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
public List<GoodsImportDto> UpdateGoodsImportDto(List<GoodsImportDto> list) |
|
|
|
{ |
|
|
|
foreach (var item in list) |
|
|
|
{ |
|
|
|
item.Name = item.Name?.Trim(); |
|
|
|
item.Type = item.Type?.Trim(); |
|
|
|
item.Uint = item.Uint?.Trim(); |
|
|
|
item.IsWeigh = item.IsWeigh?.Trim(); |
|
|
|
item.Descritption = item.Descritption?.Trim(); |
|
|
|
} |
|
|
|
return list; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<List<string>> GetImagesWithExcel(Stream stream, string fileType) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
var result = new List<string>(); |
|
|
|
IWorkbook workbook; |
|
|
|
if (fileType == ".xlsx") |
|
|
|
{ |
|
|
|
workbook = new XSSFWorkbook(stream); |
|
|
|
var pictures = workbook.GetAllPictures(); |
|
|
|
foreach (XSSFPictureData pic in pictures) |
|
|
|
{ |
|
|
|
using (var ms = new MemoryStream(pic.Data)) |
|
|
|
{ |
|
|
|
var suffix = pic.SuggestFileExtension(); |
|
|
|
var fileName = Guid.NewGuid().ToString(); |
|
|
|
IFormFile formFile = new FormFile(ms, 0, ms.Length, "name", $"{fileName}.{suffix}"); |
|
|
|
result.Add(await _aliyunOssService.UpFileAsync(formFile)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (fileType == ".xls") |
|
|
|
{ |
|
|
|
workbook = new HSSFWorkbook(stream); |
|
|
|
var pictures = workbook.GetAllPictures(); |
|
|
|
foreach (HSSFPictureData pic in pictures) |
|
|
|
{ |
|
|
|
using (var ms = new MemoryStream(pic.Data)) |
|
|
|
{ |
|
|
|
var suffix = pic.SuggestFileExtension(); |
|
|
|
var fileName = Guid.NewGuid().ToString(); |
|
|
|
IFormFile formFile = new FormFile(ms, 0, ms.Length, "name", $"{fileName}.{suffix}"); |
|
|
|
result.Add(await _aliyunOssService.UpFileAsync(formFile)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
throw new Exception("传入的不是Excel文件!"); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
throw new Exception(ex.Message); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 商品工艺导入 |
|
|
|
/// </summary> |
|
|
|
/// <param name="importDto"></param> |
|
|
|
/// <returns></returns> |
|
|
|
public async Task<bool> GoodsTechnologyImport(GoodsTechnologyImportDto importDto) |
|
|
|
{ |
|
|
|
{ |
|
|
|
string pattern = @"(?<part1>[^,(]+),(?<part2>\([^)]+\)),(?<part3>[^,]+)"; |
|
|
|
var file = importDto.file; |
|
|
|
var file = importDto.File; |
|
|
|
var userId = App.User?.FindFirst(ClaimConst.CLAINM_USERID)?.Value; |
|
|
|
if (string.IsNullOrEmpty(userId)) |
|
|
|
{ |
|
|
@@ -1480,7 +1631,7 @@ namespace BPA.SAAS.Manage.Application.DataBase.Services |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 商品工艺导出 |
|
|
|
/// 商品工艺模版导出 |
|
|
|
/// </summary> |
|
|
|
/// <param name="exportDto"></param> |
|
|
|
/// <returns></returns> |
|
|
@@ -1576,7 +1727,7 @@ namespace BPA.SAAS.Manage.Application.DataBase.Services |
|
|
|
sheet.AddMergedRegion(new CellRangeAddress(1, 2, 4, 4)); |
|
|
|
#endregion |
|
|
|
} |
|
|
|
var tempFolder = Path.Combine(_hostingEnvironment.ContentRootPath, "TechnologyTemplate"); |
|
|
|
var tempFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TechnologyTemplate"); |
|
|
|
if (!Directory.Exists(tempFolder)) |
|
|
|
Directory.CreateDirectory(tempFolder); |
|
|
|
var time = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒"); |
|
|
@@ -1589,6 +1740,70 @@ namespace BPA.SAAS.Manage.Application.DataBase.Services |
|
|
|
return fileBase64; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// 商品工艺导出 |
|
|
|
/// </summary> |
|
|
|
/// <param name="goodName"></param> |
|
|
|
/// <returns></returns> |
|
|
|
public async Task<string> GoodsSimpleExport(string goodName) |
|
|
|
{ |
|
|
|
var goodsAttributeList = await _db.Queryable<BPA_GoodsAttribute>().ToListAsync(); |
|
|
|
var goodsAttributeValueList = await _db.Queryable<BPA_GoodsAttributeValue>().Where(t => goodsAttributeList.Select(a => a.Id).Contains(t.GoodsAttributeId)).OrderBy(a => a.Sort).ToListAsync(); |
|
|
|
var goodsType = await _db.Queryable<BPA_GoodsType>().ToListAsync(); |
|
|
|
var goodsInfo = await _db.Queryable<BPA_GoodsInfo>().FirstAsync(t => t.Name == goodName); |
|
|
|
if (goodsInfo == null) |
|
|
|
throw Oops.Oh("商品不存在!"); |
|
|
|
var actionList = await _db.Queryable<BPA_GoodsTechnologyAction>().Where(t => t.GoodsId == goodsInfo.Id).OrderBy(a => a.GoodsAttributeId).OrderBy(a => a.Order).ToListAsync(); |
|
|
|
IWorkbook workbook = new XSSFWorkbook(); |
|
|
|
string[] charsToReplace = new string[] { "\\", "/", "?", "*", "[", "]" }; |
|
|
|
foreach (string c in charsToReplace) |
|
|
|
{ |
|
|
|
goodName = goodName.Replace(c, " "); |
|
|
|
} |
|
|
|
ISheet sheet = workbook.CreateSheet(goodName); |
|
|
|
IRow headerRow = sheet.CreateRow(0); |
|
|
|
headerRow.CreateCell(0).SetCellValue("商品名称"); |
|
|
|
headerRow.CreateCell(1).SetCellValue("属性组合"); |
|
|
|
headerRow.CreateCell(2).SetCellValue("工序"); |
|
|
|
headerRow.CreateCell(3).SetCellValue("工序参数"); |
|
|
|
headerRow.CreateCell(4).SetCellValue("参数值"); |
|
|
|
var k = 1; |
|
|
|
for (int i = 0; i < actionList.Count; i++) |
|
|
|
{ |
|
|
|
var stepName = actionList[i].StepName; |
|
|
|
var attributeNames = ""; |
|
|
|
var attributeNameList = new List<string>(); |
|
|
|
var goodsAttributeIdList = actionList[i].GoodsAttributeId.Split(','); |
|
|
|
foreach (var goodsAttributeId in goodsAttributeIdList) |
|
|
|
{ |
|
|
|
attributeNameList.Add(goodsAttributeValueList.FirstOrDefault(t => t.Id == goodsAttributeId)?.AttributeValue); |
|
|
|
} |
|
|
|
attributeNames = string.Join("-", attributeNameList); |
|
|
|
var actionJsonList = JsonConvert.DeserializeObject<List<ActionJsonModel>>(actionList[i].ActionJson); |
|
|
|
for (int j = 0; j < actionJsonList.Count; j++) |
|
|
|
{ |
|
|
|
IRow dataRow = sheet.CreateRow(k); |
|
|
|
dataRow.CreateCell(0).SetCellValue(goodName); |
|
|
|
dataRow.CreateCell(1).SetCellValue(attributeNames); |
|
|
|
dataRow.CreateCell(2).SetCellValue(stepName); |
|
|
|
dataRow.CreateCell(3).SetCellValue(actionJsonList[j].actionName); |
|
|
|
dataRow.CreateCell(4).SetCellValue(actionJsonList[j].actionValue); |
|
|
|
k++; |
|
|
|
} |
|
|
|
} |
|
|
|
var tempFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GoodsTechnologyData"); |
|
|
|
if (!Directory.Exists(tempFolder)) |
|
|
|
Directory.CreateDirectory(tempFolder); |
|
|
|
var time = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒"); |
|
|
|
var filePath = Path.Combine(tempFolder, $"{goodName}导出-{time}.xlsx"); |
|
|
|
using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) |
|
|
|
{ |
|
|
|
workbook.Write(stream); |
|
|
|
} |
|
|
|
var fileBase64 = Convert.ToBase64String(File.ReadAllBytes(filePath)); |
|
|
|
return fileBase64; |
|
|
|
} |
|
|
|
|
|
|
|
public static List<string> GenerateCombinations(Dictionary<string, List<string>> values, bool isId, BPA_GoodsInfo goodInfo, BPA_DeviceInfo deviceInfo) |
|
|
|
{ |
|
|
|
var attributes = values.Keys.ToList(); |
|
|
@@ -1858,5 +2073,6 @@ namespace BPA.SAAS.Manage.Application.DataBase.Services |
|
|
|
var str = string.Format("{0}", ram.ToString().Substring(0, Length)); |
|
|
|
return str; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |