using JNPF.Common.Configuration;
using JNPF.Common.Const;
using JNPF.Common.Core.Manager;
using JNPF.Common.Core.Manager.Files;
using JNPF.Common.Extension;
using JNPF.Common.Manager;
using JNPF.Common.Models;
using JNPF.Common.Models.WorkFlow;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.WorkFlow.Entitys;
using JNPF.WorkFlow.Entitys.Dto.WorkFlowForm.SalesOrder;
using JNPF.WorkFlow.Entitys.Model.Item;
using JNPF.WorkFlow.Interfaces.Service;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
namespace JNPF.WorkFlow.WorkFlowForm;
///
/// 销售订单
/// 版 本:V3.2
/// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com)
/// 日 期:2021-06-01.
///
[ApiDescriptionSettings(Tag = "WorkflowForm", Name = "SalesOrder", Order = 532)]
[Route("api/workflow/Form/[controller]")]
public class SalesOrderService : ISalesOrderService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _sqlSugarRepository;
private readonly ICacheManager _cacheManager;
private readonly IFileManager _fileManager;
private readonly IUserManager _userManager;
public SalesOrderService(
ISqlSugarRepository sqlSugarRepository,
ICacheManager cacheManager,
IFileManager fileManager,
IUserManager userManager)
{
_sqlSugarRepository = sqlSugarRepository;
_cacheManager = cacheManager;
_fileManager = fileManager;
_userManager = userManager;
}
#region GET
///
/// 信息.
///
/// 主键值
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
if ("0".Equals(id)) return null;
var data = (await _sqlSugarRepository.GetFirstAsync(x => x.Id == id)).Adapt();
data.entryList = (await _sqlSugarRepository.AsSugarClient().Queryable().Where(x => x.SalesOrderId == id).ToListAsync()).Adapt>();
return data;
}
#endregion
#region POST
///
/// 保存.
///
///
///
///
[HttpPost("{id}")]
public async Task Save(string id, [FromBody] SalesOrderInput input)
{
input.id = id;
var entity = input.Adapt();
var entityList = input.entryList.Adapt>();
entity.Id = id;
if (_sqlSugarRepository.IsAny(x => x.Id == id))
{
if (entityList.IsNotEmptyOrNull())
{
foreach (var item in entityList)
{
item.Id = SnowflakeIdHelper.NextId();
item.SalesOrderId = entity.Id;
item.SortCode = entityList.IndexOf(item);
}
}
await _sqlSugarRepository.AsSugarClient().Deleteable(x => x.SalesOrderId == id).ExecuteCommandAsync();
await _sqlSugarRepository.AsSugarClient().Insertable(entityList).ExecuteCommandAsync();
await _sqlSugarRepository.UpdateAsync(entity);
if (entity.FileJson.IsNotEmptyOrNull() && entity.FileJson.IsNotEmptyOrNull())
{
foreach (var item in entity.FileJson.ToList())
{
if (item.IsNotEmptyOrNull() && item.FileType == "delete")
{
await _fileManager.DeleteFile(Path.Combine(FileVariable.SystemFilePath, item.FileName));
}
}
}
}
else
{
if (entityList.IsNotEmptyOrNull())
{
foreach (var item in entityList)
{
item.Id = SnowflakeIdHelper.NextId();
item.SalesOrderId = entity.Id;
item.SortCode = entityList.IndexOf(item);
}
}
await _sqlSugarRepository.AsSugarClient().Insertable(entityList).ExecuteCommandAsync();
await _sqlSugarRepository.InsertAsync(entity);
_cacheManager.Del(string.Format("{0}{1}_{2}", CommonConst.CACHEKEYBILLRULE, _userManager.TenantId, _userManager.UserId + "WF_LeaveApplyNo"));
}
}
#endregion
}