using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.UserModule;
using DS.WMS.Common.Enum;
using DS.WMS.Core.BaseInfo.Dtos;
using DS.WMS.Core.BaseInfo.Entity;
using DS.WMS.Core.System.Dtos;
using DS.WMS.Core.System.Entity;
using DS.WMS.Core.System.Interface;
using Mapster;
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 whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = db.Queryable()
.LeftJoin((a, b) => a.GoodsTypeGID == b.GID)
.LeftJoin((a, b,c) => a.CompanyId == c.GID.ToString())
.Select((a, b,c) => new CodeGoodsAuditViewModel()
{
AuditTime = a.AuditTime,
CompanyName = c.DESCRIPTION //手动指定一列在自动映射
},
true)
.MergeTable()
// .Select()
.Where(whereList).Where(a => a.AuditStatus != AuditStatus.NoAudit.ToEnumInt()).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.Adapt();
data.GID = gid.ToString();
data.ISSTOP = false;
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.Adapt(exitGoodAudit);
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 AuditGoods(CommonAuditInput model)
{
var gid = Guid.Parse(model.Id);
var exitGoods = db.Queryable().First(x=>x.GID == model.Id);
if (exitGoods.IsNull())
{
return DataResult.Failed("商品申报信息不存在!");
}
if (exitGoods.AuditStatus == AuditStatus.Approve.ToEnumInt())
{
return DataResult.Failed("已审批通过!");
}
if (model.Status == 1)
{
try
{
//开启事务
db.Ado.BeginTran();
exitGoods.AuditStatus = model.Status;
exitGoods.ISSTOP = false;
exitGoods.AuditNote = model.AuditNote;
db.Updateable(exitGoods).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
var goods = exitGoods.MapTo();;
goods.PinYinCode = PinYinUtil.GetFristLetter(exitGoods.GOODNAME);
goods.ISSTOP = false;
db.Insertable(goods).ExecuteCommand();
db.Ado.CommitTran();
return DataResult.Successed("审批成功!");
}
catch (Exception ex)
{
db.Ado.RollbackTran();
return DataResult.Failed("审批失败!" + ",请联系管理员!");
}
}
else
{
exitGoods.AuditStatus = model.Status;
exitGoods.AuditNote = model.AuditNote;
db.Updateable(exitGoods).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("驳回成功!");
}
}
}