using AngleSharp.Dom;
using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.Module.SqlSugar;
using DS.Module.UserModule;
using DS.WMS.Core.Check.Entity;
using DS.WMS.Core.Code.Dtos;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Code.Interface;
using DS.WMS.Core.Fee.Entity;
using DS.WMS.Core.Sys.Entity;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Code.Method
{
///
///
///
public class CodeCheckBillTemplateService : ICodeCheckBillTemplateService
{
private readonly IServiceProvider _serviceProvider;
private readonly ISqlSugarClient db;
private readonly IUser user;
private readonly ISaasDbService saasService;
///
///
///
///
public CodeCheckBillTemplateService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
db = _serviceProvider.GetRequiredService();
user = _serviceProvider.GetRequiredService();
saasService = _serviceProvider.GetRequiredService();
}
public DataResult EditCheckBillTemplate(CheckBillTemplateReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Id == 0)
{
if (tenantDb.Queryable().Where(x => x.Name == req.Name.Trim()).Any())
return DataResult.Failed("对账模板信息已存在!", MultiLanguageConst.CheckBillTemplateExist);
var data = req.Adapt();
var entity = tenantDb.Insertable(data).ExecuteReturnEntity();
//添加费用明细
if (req.FeeList.Count > 0)
{
var feeList = new List();
foreach (var item in req.FeeList)
{
var fee = item.Adapt();
fee.TemplateId = entity.Id;
feeList.Add(fee);
}
tenantDb.Insertable(feeList).ExecuteCommand();
}
//添加排序明细
if (req.SortList.Count > 0)
{
var sortList = new List();
foreach (var item in req.FeeList)
{
var sort = item.Adapt();
sort.TemplateId = entity.Id;
sortList.Add(sort);
}
tenantDb.Insertable(sortList).ExecuteCommand();
}
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
}
else
{
var info = tenantDb.Queryable().Where(x => x.Id == req.Id).First();
info = req.Adapt(info);
//更新费用明细
if (req.FeeList.Count > 0)
{
var feeList = tenantDb.Queryable().Where(x => x.TemplateId == req.Id).ToList();
var addFeeList = new List();
var updateFeeList = new List();
foreach (var item in req.FeeList)
{
if (item.Id == 0)
{
var fee = item.Adapt();
fee.TemplateId = info.Id;
addFeeList.Add(fee);
}
else
{
var fee = feeList.Where(x => x.Id == item.Id).First();
fee = item.Adapt(fee);
updateFeeList.Add(fee);
}
}
if (addFeeList.Count > 0)
tenantDb.Insertable(addFeeList).ExecuteCommand();
if (updateFeeList.Count > 0)
tenantDb.Updateable(updateFeeList).ExecuteCommand();
}
//更新排序明细
if (req.SortList.Count > 0)
{
var sortList = tenantDb.Queryable().Where(x => x.TemplateId == req.Id).ToList();
var addSortList = new List();
var updateSortList = new List();
foreach (var item in req.SortList)
{
if (item.Id == 0)
{
var sort = item.Adapt();
sort.TemplateId = info.Id;
addSortList.Add(sort);
}
else
{
var sort = sortList.Where(x => x.Id == item.Id).First();
sort = item.Adapt(sort);
updateSortList.Add(sort);
}
}
if (addSortList.Count > 0)
tenantDb.Insertable(addSortList).ExecuteCommand();
if (updateSortList.Count > 0)
tenantDb.Updateable(updateSortList).ExecuteCommand();
}
tenantDb.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
}
}
public DataResult GetCheckBillTemplateInfo(string id)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var users = db.Queryable().Select(x => new { x.Id, x.UserName }).ToList();
var data = tenantDb.Queryable()
.Where(x => x.Id == long.Parse(id))
.Select()
.Mapper(it =>
{
it.CreateByName = users.Find(x => x.Id == it.CreateBy).UserName;
it.FeeList = tenantDb.Queryable()
.Where(x => x.TemplateId == it.Id)
.Select().ToList();
it.SortList = tenantDb.Queryable()
.Where(x => x.TemplateId == it.Id)
.Select().OrderBy(x=>x.OrderNo).ToList();
}).First();
return DataResult.Success(data, MultiLanguageConst.DataQuerySuccess);
}
public DataResult> GetListByPage(PageRequest request)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var users = db.Queryable().Select(x => new { x.Id, x.UserName }).ToList();
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = tenantDb.Queryable()
.Where(whereList)
.Select()
.Mapper(it =>
{
it.CreateByName = users.Find(x => x.Id == it.CreateBy).UserName;
})
.ToQueryPage(request.PageCondition);
return data;
}
///
/// 对账模板费用批量删除
///
/// 主表Id及费用明细Ids
///
public DataResult BatchDelCheckBillTemplateFee(IdModel req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var tmpId = long.Parse(req.Id);
if (req.Id.IsNull())
return DataResult.Failed("主表Id不能为空");
var info = tenantDb.Queryable().First(x => x.Id == tmpId);
if (info.IsNull())
return DataResult.Failed("对账模板信息不存在");
if (req.Ids.Length == 0)
return DataResult.Failed("费用明细Ids不能为空");
var feeList = tenantDb.Queryable().Where(x => x.TemplateId == tmpId && req.Ids.Contains(x.Id)).ToList();
if (feeList.Count > 0)
tenantDb.Deleteable(feeList).ExecuteCommand();
return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess);
}
///
/// 对账模板排序批量删除
///
/// 主表Id及费用明细Ids
///
public DataResult BatchDelCheckBillTemplateSort(IdModel req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var tmpId = long.Parse(req.Id);
if (req.Id.IsNull())
return DataResult.Failed("主表Id不能为空");
var info = tenantDb.Queryable().First(x => x.Id == tmpId);
if (info.IsNull())
return DataResult.Failed("对账模板信息不存在");
if (req.Ids.Length == 0)
return DataResult.Failed("排序明细Ids不能为空");
var sortList = tenantDb.Queryable().Where(x => x.TemplateId == tmpId && req.Ids.Contains(x.Id)).ToList();
if (sortList.Count > 0)
tenantDb.Deleteable(sortList).ExecuteCommand();
return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess);
}
///
/// 对账模板批量删除
///
/// 主表Ids
///
public DataResult BatchDelCheckBillTemplate(IdModel req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (req.Ids.Length == 0)
return DataResult.Failed("对账模板Ids不能为空");
var tmpList = tenantDb.Queryable().Where(x => req.Ids.Contains(x.Id)).ToList();
if (tmpList.Count > 0)
tenantDb.Deleteable(tmpList).ExecuteCommand();
var feeList = tenantDb.Queryable().Where(x => req.Ids.Contains(x.TemplateId)).ToList();
if (feeList.Count > 0)
tenantDb.Deleteable(feeList).ExecuteCommand();
var sortList = tenantDb.Queryable().Where(x => req.Ids.Contains(x.TemplateId)).ToList();
if (sortList.Count > 0)
tenantDb.Deleteable(sortList).ExecuteCommand();
return DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess);
}
}
}