using BPA.SAAS.Manage.Application.DataBase.Dtos.Batching;
using BPA.SAAS.Manage.Application.DataBase.Interface;
using BPA.SAAS.Manage.Comm.Const;
using BPA.SAAS.Manage.Comm.Enum;
using BPA.SAAS.Manage.Core.Base;
using BPA.SAAS.Manage.Core.DataBase;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace BPA.SAAS.Manage.Application.DataBase.Services
{
public class BatchingService: IBatchingService ,ITransient
{
private readonly ISqlSugarClient _db;
public BatchingService(ISqlSugarClient db)
{
_db=db;
}
#region 物料
///
/// 分页
///
///
///
public async Task GetBatchingList(BatchingListQuery dto)
{
#region 查询条件
List conModels = new List();
if (!string.IsNullOrEmpty(dto.Name))//物料名称
{
conModels.Add(new ConditionalModel() { FieldName = "a.Batching_Name", ConditionalType = ConditionalType.Like, FieldValue = dto.Name });
}
if (!string.IsNullOrEmpty(dto.Code))//物料编码
{
conModels.Add(new ConditionalModel() { FieldName = "a.Code", ConditionalType = ConditionalType.Like, FieldValue = dto.Code });
}
if (!string.IsNullOrEmpty(dto.StockUint))//物料单位
{
conModels.Add(new ConditionalModel() { FieldName = "c.Id", ConditionalType = ConditionalType.Equal, FieldValue = dto.StockUint });
}
if (!string.IsNullOrEmpty(dto.Specs))//物料规格
{
conModels.Add(new ConditionalModel() { FieldName = "a.Specs", ConditionalType = ConditionalType.Equal, FieldValue = dto.Specs });
}
if (!string.IsNullOrEmpty(dto.Aittribute))//物料属性
{
conModels.Add(new ConditionalModel() { FieldName = "a.Aittribute", ConditionalType = ConditionalType.Equal, FieldValue = dto.Aittribute.ToString() });
}
if (!string.IsNullOrEmpty(dto.TypeID))//物料类别
{
conModels.Add(new ConditionalModel() { FieldName = "b.Id", ConditionalType = ConditionalType.Equal, FieldValue = dto.TypeID });
}
if (!string.IsNullOrEmpty(dto.Status))
{
conModels.Add(new ConditionalModel() { FieldName = "a.Status", ConditionalType = ConditionalType.Equal, FieldValue = dto.Status });
}
if (dto.Price != null)
{
conModels.Add(new ConditionalModel() { FieldName = "a.Price", ConditionalType = ConditionalType.Equal, FieldValue = dto.Price.ToString() });
}
if (dto.netrecovery != null)
{
conModels.Add(new ConditionalModel() { FieldName = "a.netrecovery", ConditionalType = ConditionalType.Equal, FieldValue = dto.netrecovery.ToString() });
}
#endregion
RefAsync total = 0;
var res =await _db.Queryable((a, b, c) =>
new JoinQueryInfos(JoinType.Inner, a.TypeID == b.Id, JoinType.Inner, a.StockUint == c.Id))
//.Where((a, b, c) => a.IsDeleted == 0 && b.IsDeleted == 0 && c.IsDeleted == 0)
.Where(conModels)
.WhereIF(dto.CreateAt.HasValue, a => SqlFunc.DateIsSame(a.CreateAt, Convert.ToDateTime(dto.CreateAt), DateType.Day))
.OrderBy((a, b, c) => a.CreateAt, OrderByType.Desc)
.Select((a, b, c) => new BatchingListQuery()
{
Id = a.Id,
Name = a.Batching_Name,
Code = a.Code,
StockUint = a.StockUint,
StockUintName = c.Name,
Specs = a.Specs,
Aittribute = a.Aittribute.ToString(),
TypeID = b.Id,
TypeName = b.Name,
CreateAt = a.CreateAt,
Status = a.Status.ToString(),
batchingType = a.Batching_Type,
Price = a.Price,
netrecovery = a.netrecovery,
outstockUint = a.outstockUint,
proportion = a.proportion,
ForeignKeyRe = a.ForeignKeyRe
})
.ToPageListAsync(dto.Current, dto.PageSize, total);
res.ForEach(list =>
{
int total1 = 0;
//var res1 = _db.Queryable()
// .Where(a => a.IsDeleted == 0 && a.ProductID == list.Id)
// .OrderBy(a => a.CreateAt, OrderByType.Asc)
// .Select(a => new ProductDetailedDto
// {
// Id = a.Id,
// Code = a.Code,
// PackUnit = a.PackUnit,
// Proportion = a.Proportion
// })
// .ToPageList(dto.Current, dto.PageSize, ref total1);
list.MembersList = null;
});
PageUtil util = new PageUtil()
{
Total = total,
Data = res
};
return util;
}
///
/// 添加
///
///
///
public async Task AddBatching(BatchingInfoDto dto)
{
var productCode = _db.Queryable().Where(x => x.IsDeleted == 0 && x.Code == dto.code).ToList();
if (productCode.Count() > 0)
{
throw Oops.Oh("编码已存在");
}
try
{
BPA_Batching bPA_Product = new BPA_Batching();
bPA_Product.StockUint = dto.StockUint;
bPA_Product.TypeID = dto.TypeID;
bPA_Product.Code = dto.code;
bPA_Product.Batching_Name = dto.Name;
bPA_Product.Specs = dto.specs;
bPA_Product.Status = CommonStatus.ENABLE;
bPA_Product.Aittribute = Convert.ToInt32(dto.Aittribute);
bPA_Product.Batching_Type = dto.batchingType;
bPA_Product.Price = dto.Price;
bPA_Product.netrecovery = dto.netrecovery;
bPA_Product.outstockUint = dto.outstockUint;
bPA_Product.proportion = dto.proportion;
bPA_Product.ForeignKeyRe = dto.ForeignKeyRe;
var res=await _db.Insertable(bPA_Product).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
return res>0;
}
catch (Exception ex)
{
throw Oops.Oh("添加失败");
}
}
///
/// 修改物料信息
///
///
///
public async Task UpdateBatching(BatchingInfoDto dto)
{
var Code = _db.Queryable().Where(x => x.Id == dto.Id).First();
if (Code.Code != dto.code)
{
throw Oops.Oh("编码已存在");
}
try
{
BPA_Batching bPA_Product = new BPA_Batching();
bPA_Product.StockUint = dto.StockUint;
bPA_Product.TypeID = dto.TypeID;
bPA_Product.Code = dto.code;
bPA_Product.Batching_Name = dto.Name;
bPA_Product.Specs = dto.specs;
bPA_Product.Id = dto.Id;
bPA_Product.Aittribute = Convert.ToInt32(dto.Aittribute);
bPA_Product.Batching_Type = dto.batchingType;
bPA_Product.Price = dto.Price;
bPA_Product.netrecovery = dto.netrecovery;
bPA_Product.outstockUint = dto.outstockUint;
bPA_Product.proportion = dto.proportion;
bPA_Product.ForeignKeyRe = dto.ForeignKeyRe;
if (!string.IsNullOrEmpty(dto.Status))
{
bPA_Product.Status = (CommonStatus)Enum.ToObject(typeof(CommonStatus), int.Parse(dto.Status));
}
var res=await _db.Updateable(bPA_Product).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
return res>0;
}
catch (Exception)
{
throw Oops.Oh("更新失败");
}
}
///
/// 删除物料信息
///
///
///
public async Task BatchDelBatching(List Ids)
{
try
{
// 查询数据库中是否存在未删除的活动信息
var resEntitites = _db.Queryable().In(Ids).ToList();
var res =await _db.Deleteable(resEntitites).ExecuteCommandAsync();
return res>0;
}
catch (Exception)
{
throw Oops.Oh("删除失败");
}
}
///
/// 查询所有物料信息
///
///
public async Task> GetBatchingList()
{
var res = await _db.Queryable()
.ToDictionaryAsync(x => x.Id, x => x.Batching_Name);
return res;
}
///
/// 查询所有物料信息
///
///
public async Task> GetBatchingSelectList()
{
var res = await _db.Queryable().Select(x=>new BatchingList() { Id=x.Id,Name=x.Batching_Name})
.ToListAsync();
return res;
}
///
/// 更新物料状态
///
///
///
public async Task UpdateBatchingStatus(BatchingStatusDto dto)
{
try
{
// 查询数据库中是否存在未删除的活动信息
var resEntitites = _db.Queryable().Where(x => x.Id == dto.Id).First();
if (resEntitites != null)
{
resEntitites.Status = (CommonStatus)Enum.ToObject(typeof(CommonStatus), dto.Status);
}
var res =await _db.Updateable(resEntitites).UpdateColumns(x => new { x.Status }).ExecuteCommandAsync();
return res>0;
}
catch (Exception)
{
return false;
}
}
///
/// 查询物料信息
///
///
public async Task> GetByTypeBatching()
{
var item = await _db.Queryable((a, b) => new JoinQueryInfos(
JoinType.Left, a.TypeID == b.Id
)).Select((a, b) => new BatchingView() { Id = a.Id, Name = a.Batching_Name, TypeName = b.Name }).ToListAsync();
return item;
}
#endregion
#region 物料类型
public async Task> GetBatchingTypeList()
{
var res = await _db.Queryable().Select(x => new
{
id = x.Id,
name=x.Name
}) .ToListAsync();
return res;
}
public async Task> GetBatchingTypeList_alm()
{
var groupId = App.HttpContext.Request.Headers["groupId"].ToString();
if (string.IsNullOrWhiteSpace(groupId)) throw Oops.Oh("加盟商id不能为空");
var res = await _db.Queryable().Where(x=>x.GroupId== groupId).Select(x => new
{
id = x.Id,
name = x.Name
}).ToListAsync();
return res;
}
public async Task AddBatchingType(BatchingTypeDto dto)
{
var productCode = _db.Queryable().Where(x => x.Name== dto.Name).ToList();
if (productCode.Count() > 0)
{
throw Oops.Oh("物料类型已存在");
}
try
{
BPA_BatchingType bPA_Product = new BPA_BatchingType();
bPA_Product.Name = dto.Name;
var res = await _db.Insertable(bPA_Product).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
return res > 0;
}
catch (Exception ex)
{
throw Oops.Oh("添加失败");
}
}
public async Task BatchTypeDel(string id)
{
try
{
// 查询数据库中是否存在未删除的活动信息
var resEntitites =await _db.Queryable().Where(x=>x.Id== id).FirstAsync();
resEntitites.IsDeleted= 1;
var res = await _db.Updateable(resEntitites).ExecuteCommandAsync();
return res > 0;
}
catch (Exception)
{
throw Oops.Oh("删除失败");
}
}
#endregion
#region 物料单位
public async Task> GetBatchingUintList()
{
var res = await _db.Queryable().Select(x => new
{
id = x.Id,
name = x.Name
}).ToListAsync();
return res;
}
public async Task> GetBatchingUintList_alm()
{
var groupId = App.HttpContext.Request.Headers["groupId"].ToString();
if(string.IsNullOrWhiteSpace(groupId)) throw Oops.Oh("加盟商id不能为空");
var res = await _db.Queryable().Where(x=>x.GroupId== groupId).Select(x => new
{
id = x.Id,
name = x.Name
}).ToListAsync();
return res;
}
public async Task AddBatchingUint(BatchingUintDto dto)
{
var productCode = _db.Queryable().Where(x => x.Name == dto.Name).ToList();
if (productCode.Count() > 0)
{
throw Oops.Oh("物料单位已存在");
}
try
{
BPA_BatchingUint bPA_Product = new BPA_BatchingUint();
bPA_Product.Name = dto.Name;
var res = await _db.Insertable(bPA_Product).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
return res > 0;
}
catch (Exception ex)
{
throw Oops.Oh("添加失败");
}
}
public async Task BatchingUintDel(string id)
{
try
{
// 查询数据库中是否存在未删除的活动信息
var resEntitites = await _db.Queryable().Where(x => x.Id == id).FirstAsync();
resEntitites.IsDeleted = 1;
var res = await _db.Updateable(resEntitites).ExecuteCommandAsync();
return res > 0;
}
catch (Exception)
{
throw Oops.Oh("删除失败");
}
}
#endregion
}
}