using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.User;
using DS.WMS.Common.Enum;
using DS.WMS.Core.System.Dtos;
using DS.WMS.Core.System.Entity;
using DS.WMS.Core.System.Interface;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.System.Method;
///
///
///
public class CodeGoodsAuditService : ICodeGoodsAuditService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
///
///
///
///
public CodeGoodsAuditService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
}
///
/// 商品列表
///
///
///
public DataResult> GetListByPage(PageRequest request)
{
var companyId = user.GetCompanyId();
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable()
.LeftJoin((a, b) => a.GoodsTypeGID == b.GID)
.Select()
.Where(whereList).Where(a => a.CompanyId == companyId).ToQueryPage(request.PageCondition);
return data;
}
///
/// 获取商品信息
///
///
///
public DataResult GetGoodsAuditInfo(string id)
{
var goodFiles = db.Queryable().Where(x => x.PID == Guid.Parse(id) && x.FILETYPE == "商品资料")
.Select(it => new fileinfo
{
FileName = it.FILENAME,
FilePath = it.IMGPATH
}).ToArray();
var data = db.Queryable()
.LeftJoin((a, b) => a.GoodsTypeGID == b.GID)
.Where(a => a.GID == id)
.Select()
.Mapper(it =>
{
//只能写在Select后面
it.File = goodFiles;
})
.First();
return DataResult.Success(data);
}
///
/// 维护商品申报信息
///
///
///
public DataResult EditGoodsAuditInfo(GoodsAuditInput model)
{
if (model.GID.IsNullOrEmpty())
{
var isExist = db.Queryable().Where(x => x.GOODNAME == model.GOODNAME.Trim()).First();
if (isExist != null)
{
return DataResult.Failed("商品申报该名称已存在!");
}
var isGoodsExist = db.Queryable().Where(x => x.GOODNAME == model.GOODNAME.Trim()).First();
if (isGoodsExist != null)
{
return DataResult.Failed("商品信息库中该名称已存在!");
}
try
{
//开启事务
db.Ado.BeginTran();
var gid = Guid.NewGuid();
var data = model.MapTo();
data.GID = gid.ToString();
data.CompanyId = user.GetCompanyId();
data.AuditStatus = AuditStatus.NoAudit.ToEnumInt();
db.Insertable(data).ExecuteCommand();
var infoList = new List();
if (model.File.IsNotNull())
{
foreach (var item in model.File)
{
var infoFile = new INFO_FILES
{
GID = Guid.NewGuid(),
PID = gid,
FILENAME = item.FileName,
IMGPATH = item.FilePath,
FILETYPE = "商品资料",
CORPID = user.CompanyId.ToString(),
CreateDate = DateTime.Now,
ModifyDate = DateTime.Now,
UPLOADEMPLY = user.UserId
};
infoList.Add(infoFile);
}
if (infoList.Count > 0)
{
db.Insertable(infoList).ExecuteCommand();
}
}
db.Ado.CommitTran();
return DataResult.Successed("添加成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("添加失败!" + ex);
}
}
else
{
var exitGoodAudit = db.Queryable().Where(x => x.GID == model.GID).First();
if (exitGoodAudit.AuditStatus == (short)NomalStatus.Approve)
{
return DataResult.Failed("已审核通过,不能修改!");
}
try
{
//开启事务
db.Ado.BeginTran();
var data = model.MapTo();
db.Updateable(data).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommand();
var gid =Guid.Parse( model.GID);
var infoList = new List();
if (model.File.IsNotNull())
{
#region 处理商品资料
//先删除 商品资料
var existGoods = db.Queryable().Where(x => x.PID == gid && x.FILETYPE == "商品资料").Select(n=>n.IMGPATH).ToArray();
var goodFiles = model.File.Select(x => x.FilePath).ToArray();
IEnumerable delGoods = existGoods.AsQueryable().Except(goodFiles);
if (delGoods.Count()>0)
{
foreach (var item in delGoods)
{
var existInfo = db.Queryable()
.Where(x => x.PID == gid && x.IMGPATH == item).First();
db.Deleteable(existInfo).ExecuteCommand();
}
}
//2.添加新的商品资料
IEnumerable addGoods = goodFiles.AsQueryable().Except(existGoods);
if (addGoods.Count()>0)
{
foreach (var item in addGoods)
{
var infoFile = new INFO_FILES
{
GID = Guid.NewGuid(),
PID = gid,
FILENAME = model.File.Where(x=>x.FilePath ==item).Select(x=>x.FileName).First(),
IMGPATH = item,
FILETYPE = "商品资料",
CORPID = gid.ToString(),
CreateDate = DateTime.Now,
ModifyDate = DateTime.Now,
UPLOADEMPLY = ""
};
db.Insertable(infoFile).ExecuteCommand();
}
}
#endregion
}
db.Ado.CommitTran();
return DataResult.Successed("更新成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("更新失败!" + ex);
}
}
}
///
/// 提交审核
///
///
///
public DataResult SubmitGoodsAudit(string id)
{
var exitGoods = db.Queryable().First(x=>x.GID == id);
if (exitGoods.AuditStatus == AuditStatus.Approve.ToEnumInt() || exitGoods.AuditStatus == AuditStatus.Auditing.ToEnumInt())
{
return DataResult.Failed("审核通过或待审批状态不能提交!");
}
var files = db.Queryable().Where(x => x.PID == Guid.Parse(id) && x.FILETYPE == "商品资料").ToList();
if (files.Count() == 0)
{
return DataResult.Failed("商品资料未上传不能提交!");
}
//更新状态为待审批
exitGoods.AuditStatus = AuditStatus.Auditing.ToEnumInt();
db.Updateable(exitGoods).ExecuteCommand();
return DataResult.Successed("提交成功");
}
}