|
- using BPA.SAAS.Manage.Application.DataBase.Dtos.Bom;
- using BPA.SAAS.Manage.Application.DataBase.Interface;
- using BPA.SAAS.Manage.Comm.Enum;
- using BPA.SAAS.Manage.Core.DataBase;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPA.SAAS.Manage.Application.DataBase.Services
- {
- public class BomService: IBomService,ITransient
- {
- private readonly ISqlSugarClient _db;
- public BomService(ISqlSugarClient db)
- {
- _db=db;
- }
- /// <summary>
- /// 配方分类列表
- /// </summary>
- /// <returns></returns>
- public async Task<List<BomSelectList>> GetBomList()
- {
- var res = await _db.Queryable<BPA_Bom>().Select(x => new BomSelectList
-
- {
- Id = x.Id,
- Name = x.Name
- }).ToListAsync();
- return res;
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<bool> AddBom(BomInputDto dto)
- {
- BPA_Bom newbPA_BOM = new BPA_Bom
- {
- Name = dto.Name,
- Code = GetNumber2(),
- IsMain = dto.IsMain,
- Sort = dto.Sort,
- };
- var bom =await _db.Insertable(newbPA_BOM).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync();
- //添加配方分类
- var list = dto.BomTypeIds.Select(item => new BPA_BomTypeInfo() { BomId = bom.Id, BomTypeId = item }).ToList();
- var res =await _db.Insertable(list).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
- return res > 0;
- }
- /// <summary>
- /// 更新
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<bool> UpdateBom(BomInputDto dto)
- {
- // 查询数据库中是否存在未删除的商品类型
- var resEntity = _db.Queryable<BPA_Bom>().Where(it => it.IsDeleted == 0).First(it => it.Id == dto.Id);
- if (null == resEntity)
- {
- return false;
- }
-
- resEntity.Name = dto.Name;
- resEntity.IsMain = dto.IsMain;
- resEntity.Sort = dto.Sort;
- var res =await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true) .ExecuteCommandAsync();
- return res > 0;
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<bool> DeleteBom(string id)
- {
- try
- {
- _db.Ado.BeginTran();
- var resEntity = _db.Queryable<BPA_Bom>().Where(it => it.IsDeleted == 0).First(it => it.Id == id);
- if (null == resEntity)
- {
- throw Oops.Oh("配方不存在");
- }
- resEntity.IsDeleted = 1;
- var res = await _db.Updateable(resEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
- var BomTypeInfo = _db.Queryable<BPA_BomTypeInfo>().Where(it => it.BomId == id).Select(x => x.Id).ToList();
- await _db.Deleteable<BPA_BomTypeInfo>(BomTypeInfo).ExecuteCommandAsync();
- _db.Ado.CommitTran();
- return res > 0;
- }
- catch (Exception)
- {
- _db.Ado.RollbackTran();
- throw Oops.Oh("删除失败"); ;
- }
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public async Task<bool> AddBomType(AddBomTypeInputDto dto)
- {
- BPA_BomType newbPA_BOM = new BPA_BomType
- {
- Name = dto.Name
- };
- var res = await _db.Insertable(newbPA_BOM).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
- return res > 0;
- }
- /// <summary>
- /// 配方分类列表
- /// </summary>
- /// <returns></returns>
- public async Task<List<dynamic>> GetBomTypeList()
- {
- var res = await _db.Queryable<BPA_BomType>().Select<dynamic>(x => new
-
- {
- id = x.Id,
- name = x.Name
- }).ToListAsync();
- return res;
- }
-
- private string GetNumber2(int Length = 10)
- {
- byte[] buffer = Guid.NewGuid().ToByteArray();
- var ram = BitConverter.ToInt64(buffer, 0);
- var str = string.Format("{0}", ram.ToString().Substring(0, Length));
- return str;
- }
- }
- }
|