using BPA.KitChen.GroupMeal.SqlSugar;
using BPA.SAAS.Manage.Application.AExternalPlatform.BaseDto;
using BPA.SAAS.Manage.Application.AExternalPlatform.Enum;
using BPA.SAAS.Manage.Application.AExternalPlatform.Service.Goods.Dtos;
using BPA.SAAS.Manage.Application.AExternalPlatform.Service.Material.Dtos;
using BPA.SAAS.Manage.Application.AExternalPlatform.Service.ThirdpartyPush.Dtos;
using BPA.SAAS.Manage.Application.AExternalPlatform.Service.ThirdpartyPush.Services;
using BPA.SAAS.Manage.Application.DataBase.Dtos.Goods;
using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsEnergyConfig;
using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsTechnology;
using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsType;
using BPA.SAAS.Manage.Core.Base;
using BPA.SAAS.Manage.Core.DataBase;
using BPA.SAAS.Manage.Core.Device;
using Microsoft.AspNetCore.Components.Forms;
using Newtonsoft.Json;
using SqlSugar;
using System.Linq;
using GoodsDto = BPA.SAAS.Manage.Application.AExternalPlatform.Service.Goods.Dtos.GoodsDto;

namespace BPA.SAAS.Manage.Application.AExternalPlatform.Service.Goods.Services
{
    public class GoodsService : IGoodsService, ITransient
    {
        private readonly IThirdpartyPushService _thirdpartyPushService;
        public GoodsService(IThirdpartyPushService thirdpartyPushService)
        {
            _thirdpartyPushService = thirdpartyPushService;
        }
        /// <summary>
        /// 分页查询商品
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<PageUtil<List<GoodsDto>>> GetGoodsPageList(GoodsPageInputDto inputDto)
        {
            int total = new RefAsync<int>();
            var data = SqlSugarDb.Db.Queryable<BPA_GoodsInfo, BPA_GoodsType, BPA_GoodsUint>((a, b, c) =>
                new JoinQueryInfos(JoinType.Left, a.GoodsTypeId == b.Id,
                    JoinType.Left, a.GoodsUintId == c.Id))
                .Where((a, b, c) => a.IsDeleted == 0)
                .WhereIF(!string.IsNullOrEmpty(inputDto.GoodsTypeName), (a, b, c) => b.Name.Contains(inputDto.GoodsTypeName))
                .Select((a, b, c) => new GoodsDto()
                {
                    Id = a.Id.SelectAll(),
                    GoodsTypeName = b.Name,
                    GoodsUnitName = c.Name

                })
                .Mapper(x =>
                {
                    var GoodsAttributePriceList = SqlSugarDb.Db.Queryable<BPA_GoodsAttributePrice>().Where(y => y.GoodsId == x.Id).ToList();
                    var list = GoodsAttributePriceList.Adapt<List<GoodsAttributePrice>>();
                    x.GoodsAttributePrice = list;
                })
                .ToPageList(inputDto.Current, inputDto.PageSize, ref total);

            return new PageUtil<List<GoodsDto>>()
            {
                Total = total,
                Data = data
            };
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> AddGoods(BaseRequestDto<GoodsInsertDto> dto)
        {
            try
            {
                SqlSugarDb.Db.BeginTran();
                List<BPA_GoodsInfo> list = new();
                List<BPA_GoodsType> GoodsTypelist = new();
                List<BPA_GoodsUint> GoodsUintlist = new();
                for (int i = 0; i < dto.DataInfo.Count; i++)
                {
                    var resEntity = new BPA_GoodsInfo();
                    resEntity.Id = Guid.NewGuid().ToString();
                    resEntity.Name = dto.DataInfo[i].Name;
                    resEntity.Descritption = dto.DataInfo[i].Descritption;
                    resEntity.ImgUrl = dto.DataInfo[i].ImgUrl;
                    resEntity.Price = dto.DataInfo[i].Price;
                    resEntity.IsWeigh = dto.DataInfo[i].IsWeigh;
                    resEntity.IsAttrubute = true;
                    resEntity.Code = GetNumber2(8);
                    if (!string.IsNullOrEmpty(dto.DataInfo[i].GoodsTypeName))
                    {
                        var check = SqlSugarDb.Db.Queryable<BPA_GoodsType>().Where(x => x.Name == dto.DataInfo[i].GoodsTypeName).First();
                        if (check == null)
                        {
                            var GoodsType = new BPA_GoodsType() { Id = Guid.NewGuid().ToString(), Pid = "", Name = dto.DataInfo[i].GoodsTypeName };
                            GoodsTypelist.Add(GoodsType);
                            resEntity.GoodsTypeId = GoodsType.Id;
                        }
                        else
                        {
                            resEntity.GoodsTypeId = check.Id;
                        }
                    }
                    if (!string.IsNullOrEmpty(dto.DataInfo[i].GoodsUintName))
                    {
                        var check1 = SqlSugarDb.Db.Queryable<BPA_GoodsUint>().Where(x => x.Name == dto.DataInfo[i].GoodsUintName).First();
                        if (check1 == null)
                        {
                            var GoodsUint = new BPA_GoodsUint() { Id = Guid.NewGuid().ToString(), Name = dto.DataInfo[i].GoodsUintName };
                            GoodsUintlist.Add(GoodsUint);
                            resEntity.GoodsUintId = GoodsUint.Id;
                        }
                        else
                        {
                            resEntity.GoodsUintId = check1.Id;
                        }
                    }
                    list.Add(resEntity);
                }
                await SqlSugarDb.Db.Insertable(GoodsTypelist).ExecuteCommandAsync();
                await SqlSugarDb.Db.Insertable(GoodsUintlist).ExecuteCommandAsync();
                var res = await SqlSugarDb.Db.Insertable(list).ExecuteReturnEntityAsync();
                SqlSugarDb.Db.CommitTran();
                #region 下发数据到设备

                if (dto.IsPush)
                {
                    var data = SqlSugarDb.Db.Queryable<BPA_GoodsInfo, BPA_GoodsType, BPA_GoodsUint>((a, b, c) =>
                new JoinQueryInfos(JoinType.Left, a.GoodsTypeId == b.Id,
                    JoinType.Left, a.GoodsUintId == c.Id))
                .Where((a, b, c) => a.Id == res.Id).Select((a, b, c) => new GoodsInfoPushDto()
                {
                    Id = a.Id,
                    Name = a.Name,
                    Descritption = a.Descritption,
                    ImgUrl = a.ImgUrl,
                    Sort = a.Sort,
                    Price = a.Price,
                    GoodsUintId = a.GoodsUintId,
                    GoodsUintName = c.Name,
                    IsWeigh = a.IsWeigh,
                    Design = a.Design,
                    DefaultMate = a.DefaultMate,
                    IsAttrubute = a.IsAttrubute,
                    GoodsTypeId = a.GoodsTypeId,
                    GoodsTypeName = b.Name,
                }).First();
                    await _thirdpartyPushService.AddPushRecordAndPushDevice(dto, 1, res.Id,
                        JsonConvert.SerializeObject(data));
                }
                #endregion
                return res != null;
            }
            catch (Exception)
            {
                SqlSugarDb.Db.RollbackTran();
                throw Oops.Oh(ErrorCodeEnum.Code1007);
            }

        }
        /// <summary>
        /// 更新商品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateGoods(BaseRequestDto<GoodsUpdateDto> dto)
        {
            List<BPA_GoodsInfo> list = new();
            if (dto.DataInfo.Count > 1) throw Oops.Oh("不支持多条商品修改");
            if (dto.DataInfo == null  || dto.DataInfo.Count==0) throw Oops.Oh("修改数据不能为空");
            var model = dto.DataInfo.First();
            var checktype=SqlSugarDb.Db.Queryable<BPA_GoodsType>().Where(x => x.Id == model.GoodsTypeId).Any();
            if (!checktype) throw Oops.Oh("商品分类不存在");
            var checktunit = SqlSugarDb.Db.Queryable<BPA_GoodsUint>().Where(x => x.Id == model.GoodsUintId).Any();
            if (!checktunit) throw Oops.Oh("商品单位不存在");
            var resEntity = SqlSugarDb.Db.Queryable<BPA_GoodsInfo>().Where(it => it.IsDeleted == 0).First(it => it.Id == model.Id);
                if (resEntity != null)
                {
                    resEntity.Id= model.Id;
                    resEntity.GoodsTypeId = model.GoodsTypeId;
                    resEntity.Name = model.Name;
                    resEntity.Descritption = model.Descritption;
                    resEntity.ImgUrl = model.ImgUrl;
                    resEntity.Price = model.Price;
                    resEntity.IsWeigh = model.IsWeigh;
                    resEntity.GoodsUintId = model.GoodsUintId;
                    list.Add(resEntity);
                    var res = await SqlSugarDb.Db.Updateable(list).ExecuteCommandAsync();
                    #region 下发数据到设备
                    
                    if (dto.IsPush)
                    {
                        var data = SqlSugarDb.Db.Queryable<BPA_GoodsInfo, BPA_GoodsType, BPA_GoodsUint>((a, b, c) =>
                     new JoinQueryInfos(JoinType.Left, a.GoodsTypeId == b.Id,
                         JoinType.Left, a.GoodsUintId == c.Id))
                     .Where((a, b, c) => a.Id == resEntity.Id).Select((a, b, c) => new GoodsInfoPushDto()
                     {
                         Id = a.Id,
                         Name = a.Name,
                         Descritption = a.Descritption,
                         ImgUrl = a.ImgUrl,
                         Sort = a.Sort,
                         Price = a.Price,
                         GoodsUintId = a.GoodsUintId,
                         GoodsUintName = c.Name,
                         IsWeigh = a.IsWeigh,
                         Design = a.Design,
                         DefaultMate = a.DefaultMate,
                         IsAttrubute = a.IsAttrubute,
                         GoodsTypeId = a.GoodsTypeId,
                         GoodsTypeName = b.Name,
                     }).First();
                        await _thirdpartyPushService.AddPushRecordAndPushDevice(dto, 1, resEntity.Id,
                            JsonConvert.SerializeObject(data));
                    }
                    #endregion
                
            }
                // 查询数据库中是否存在未删除的商品类型
        
            return true;
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<bool> DeleteGoods(DeleteGoodsDto dto)
        {
            try
            {
                SqlSugarDb.Db.BeginTran();
                var goods = SqlSugarDb.Db.Queryable<BPA_GoodsInfo>().Where(x => dto.Ids.Contains(x.Id)).ToList();
                if (goods == null) throw Oops.Oh(ErrorCodeEnum.Code1008);
                var goodsbom = SqlSugarDb.Db.Queryable<BPA_GoodsBom>().Where(x => dto.Ids.Contains( x.Goods_Id)).ToList();
                var goodsTechnology = SqlSugarDb.Db.Queryable<BPA_GoodsTechnologyAction>().Where(x => dto.Ids.Contains(x.GoodsId)).ToList();
                await SqlSugarDb.Db.Deleteable(goodsbom).ExecuteCommandAsync();
                await SqlSugarDb.Db.Deleteable(goodsTechnology).ExecuteCommandAsync();
                var res = await SqlSugarDb.Db.Deleteable(goods).ExecuteCommandAsync();
                SqlSugarDb.Db.CommitTran();
                return res > 0;
            }
            catch (Exception)
            {
                SqlSugarDb.Db.RollbackTran();
                throw Oops.Oh("删除失败");
            }

        }
       /// <summary>
       ///添加商品属性
       /// </summary>
       /// <param name="dto"></param>
       /// <returns></returns>
        public async Task<bool> AddGoodsAttribute(GoodsAttributeinsertDto dto)
        {
            try
            {
                SqlSugarDb.Db.BeginTran();
                BPA_GoodsAttribute _GoodsAttribute = new();
                _GoodsAttribute.Id = Guid.NewGuid().ToString();
                _GoodsAttribute.AttributeName = dto.AttributeName;
                var GoodsType = SqlSugarDb.Db.Queryable<BPA_GoodsType>().Where(x => x.Name == dto.GoodsTypeName).First();
                if (GoodsType == null)
                {
                    var resGoodsType = await SqlSugarDb.Db.Insertable(new BPA_GoodsType() { Pid = "0", Name = dto.GoodsTypeName }).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync();
                    _GoodsAttribute.GoodsTypeId = resGoodsType.Id;
                }
                else
                {
                    _GoodsAttribute.GoodsTypeId = GoodsType.Id;
                }
                var res = await SqlSugarDb.Db.Insertable(_GoodsAttribute).ExecuteCommandAsync();
                List<BPA_GoodsAttributeValue> GoodsAttributeValueList = new();
                if (dto.GoodsAttributeValue.Count > 0)
                {
                    SqlSugarDb.Db.Deleteable<BPA_GoodsAttributeValue>().Where(x => x.GoodsAttributeId == _GoodsAttribute.Id).ExecuteCommand();
                    for (int i = 0; i < dto.GoodsAttributeValue.Count; i++)
                    {
                        BPA_GoodsAttributeValue _GoodsAttributeValue = new();
                        _GoodsAttributeValue.Id= Guid.NewGuid().ToString();
                        _GoodsAttributeValue.GoodsAttributeId = _GoodsAttribute.Id;
                        _GoodsAttributeValue.AttributeValue = dto.GoodsAttributeValue[i].AttributeValue;
                        _GoodsAttributeValue.Sort = dto.GoodsAttributeValue[i].Sort;
                        GoodsAttributeValueList.Add(_GoodsAttributeValue);
                    }
                    SqlSugarDb.Db.Insertable(GoodsAttributeValueList).ExecuteCommand();
                }
                SqlSugarDb.Db.CommitTran();
                return res > 0;
            }
            catch (Exception)
            {
                SqlSugarDb.Db.RollbackTran();
                throw Oops.Oh(ErrorCodeEnum.Code1007);
                throw;
            }
           
        }
        public async Task<bool> AddGoodsAttributeValue(List<GoodsAttributeValue> dto)
        {
            try
            {
                List<BPA_GoodsAttributeValue> GoodsAttributeValueList = new();
                List<BPA_GoodsAttributeValue> GoodsAttributeValueListup = new();
                if (dto.Count > 0)
                {
                    for (int i = 0; i < dto.Count; i++)
                    {
                        if (string.IsNullOrWhiteSpace(dto[i].Id))
                        {
                            BPA_GoodsAttributeValue _GoodsAttributeValue = new();
                            _GoodsAttributeValue.Id = Guid.NewGuid().ToString();
                            _GoodsAttributeValue.GoodsAttributeId = dto[i].GoodsAttributeId;
                            _GoodsAttributeValue.AttributeValue = dto[i].AttributeValue;
                            _GoodsAttributeValue.Sort = dto[i].Sort;
                            GoodsAttributeValueList.Add(_GoodsAttributeValue);
                        }
                        else
                        {
                            var _GoodsAttributeValue=SqlSugarDb.Db.Queryable<BPA_GoodsAttributeValue>().Where(x => x.Id == dto[i].Id).First();
                            if (_GoodsAttributeValue != null)
                            {
                              //  _GoodsAttributeValue.Id = Guid.NewGuid().ToString();
                                _GoodsAttributeValue.GoodsAttributeId = dto[i].GoodsAttributeId;
                                _GoodsAttributeValue.AttributeValue = dto[i].AttributeValue;
                                _GoodsAttributeValue.Sort = dto[i].Sort;
                                GoodsAttributeValueListup.Add(_GoodsAttributeValue);
                            }
                        }
                    }
                }
                var res = 0;
                if (GoodsAttributeValueListup.Count > 0)
                {
                     res= SqlSugarDb.Db.Updateable(GoodsAttributeValueListup).ExecuteCommand();
                }
                if (GoodsAttributeValueList.Count > 0)
                {
                     res = SqlSugarDb.Db.Insertable(GoodsAttributeValueList).ExecuteCommand();
                }
                return res > 0;
            }
            catch (Exception)
            {
                SqlSugarDb.Db.RollbackTran();
                throw Oops.Oh(ErrorCodeEnum.Code1007);
                throw;
            }

        }
        /// <summary>
        /// 查询商品属性
        /// </summary>
        /// <returns></returns>
        public async Task<List<GoodsAttributeVewDto>> GetGoodsAttributeList(GoodsIdDto dto)
        {
            var goodstypeid = "";
            var goods=SqlSugarDb.Db.Queryable<BPA_GoodsInfo>().Where(x => x.Id == dto.GoodsId).First();
            if (goods != null)
            {
                goodstypeid = goods.GoodsTypeId;
            }
            if (!string.IsNullOrWhiteSpace(dto.GoodsTypeId)) goodstypeid = dto.GoodsTypeId;
            var res = await SqlSugarDb.Db.Queryable<BPA_GoodsAttribute>()
                .WhereIF(!string.IsNullOrEmpty(goodstypeid),x=>x.GoodsTypeId== goodstypeid)
                .OrderBy(a => a.CreateAt, OrderByType.Desc)
                .Select(a => new GoodsAttributeVewDto
                {
                    Id = a.Id,
                    AttributeName = a.AttributeName,
                    GoodsTypeId = a.GoodsTypeId,
                    Sort = a.Sort,
                })
                .Mapper(x =>
                {
                    var list = SqlSugarDb.Db.Queryable<BPA_GoodsAttributeValue>().Where(p => p.GoodsAttributeId == x.Id).Select(x=>new GoodsAttributeValue() { AttributeValue=x.AttributeValue,Id=x.Id, GoodsAttributeId =x.GoodsAttributeId}).ToList();
                    x.AttributeValueList = list;
                })
                .ToListAsync();
            return res;
        }
        /// <summary>
        /// 查询商品工艺
        /// </summary>
        /// <param name="goodsId"></param>
        /// <returns></returns>
        public async Task<List<GoodsTechnologyActionListViewDto>> GetGoodsTechnologyAction(GoodsIdDto dto)
        {
            List<GoodsTechnologyActionListViewDto> goodsTechnologyActionListViews = new List<GoodsTechnologyActionListViewDto>();
            var list = SqlSugarDb.Db.Queryable<BPA_GoodsTechnologyAction>().Where(x => x.GoodsId == dto.GoodsId)
                .Select(x => new GoodsTechnologyActionViewDto()
                {
                    Id = x.Id.SelectAll(),
                }).OrderBy(x => x.Sort, OrderByType.Asc).ToList();
            var Devicelist = await SqlSugarDb.Db.Queryable<BPA_DeviceInfo>().ToListAsync();
            var GoodsAttributeValue = await SqlSugarDb.Db.Queryable<BPA_GoodsAttributeValue>().ToListAsync();
            var sf = list.GroupBy(x => x.GoodsAttributeId).ToList();
            var sf1 = list.GroupBy(x => x.WarehousrTemplateId).ToList();
            for (int i = 0; i < sf.Count; i++)
            {
                var sd = GoodsAttributeValue?.Where(x => sf[i].Key.Contains(x.Id)).Select(x => x.AttributeValue).ToArray();
                var sdw = GoodsAttributeValue?.Where(x => sf[i].Key.Contains(x.Id)).Select(x => x.Id).ToArray();
                var nane = string.Join('-', sd);
                var s = sf[i].AsQueryable().ToList().GroupBy(x => x.DeviceId).ToList();
                for (int t = 0; t < s.Count; t++)
                {
                    GoodsTechnologyActionListViewDto item = new()
                    {
                        DeviceId = s[t].Key,
                        WarehousrTemplateId = sf1[t].Key,
                        GoodsAttributeId = string.Join(',', sdw),
                        TechnologyName = Devicelist?.FirstOrDefault(x => x.Id == s[t].Key).DeviceName + "【" + nane + "】",
                        Data = s[t].AsQueryable().ToList(),
                    };
                    goodsTechnologyActionListViews.Add(item);
                }
            }

            return goodsTechnologyActionListViews;
        }
        public async Task<PageUtil> GetGoodsTypePage(GoodstypeQueryDto dto)
        {
            int total = new RefAsync<int>();
            var data = SqlSugarDb.Db.Queryable<BPA_GoodsType>()
                .Select(a=> new GoodstypeViewDto()
                {
                    Id = a.Id.SelectAll()
                })
                .ToPageList(dto.Current, dto.PageSize, ref total);

            return new PageUtil()
            {
                Total = total,
                Data = data
            };
        }
        public async Task<bool> AddGoodsType(List<GoodstypeInsertDto> dto)
        {
            try
            {
                List<BPA_GoodsType> GoodsTypelist = new();
                for (int i = 0; i < dto.Count; i++)
                {
                    if (dto[i].Pid == null) dto[i].Pid = "0";
                    var resEntity = new BPA_GoodsType();
                    resEntity.Id = Guid.NewGuid().ToString();
                    resEntity.Name = dto[i].Name;
                    resEntity.Pid =string.IsNullOrEmpty(dto[i].Pid)?"0": dto[i].Pid;
                    resEntity.Sort = dto[i].Sort;
                    GoodsTypelist.Add(resEntity);
                }
               var res= await SqlSugarDb.Db.Insertable(GoodsTypelist).ExecuteCommandAsync();
                return res>0;
            }
            catch (Exception)
            {
                throw Oops.Oh(ErrorCodeEnum.Code1007);
            }

        }
        public async Task<bool> UpdateGoodsType(GoodstypeUpdateDto dto)
        {
            try
            {
                var model=SqlSugarDb.Db.Queryable<BPA_GoodsType>().Where(x => x.Id == dto.Id).First();
                if(model==null) throw Oops.Oh("商品分类不存在");
                if (dto.Pid == null) dto.Pid = "0";
                model.Name = dto.Name;
                model.Pid =string.IsNullOrEmpty(dto.Pid)?"0": dto.Pid;
                model.Sort = dto.Sort;
                var res = await SqlSugarDb.Db.Updateable(model).ExecuteCommandAsync();
                return res > 0;
            }
            catch (Exception)
            {
                throw Oops.Oh(ErrorCodeEnum.Code1007);
            }

        }
        public async Task<bool> DelGoodsType(GoodstypeDelDto dto)
        {
            try
            {
                var model = SqlSugarDb.Db.Queryable<BPA_GoodsType>().Where(x => dto.Ids.Contains(x.Id)).ToList();
                if (model == null) throw Oops.Oh("商品分类不存在");
                var res = await SqlSugarDb.Db.Deleteable(model).ExecuteCommandAsync();
                return res > 0;
            }
            catch (Exception)
            {
                throw Oops.Oh(ErrorCodeEnum.Code1007);
            }

        }
        public async Task<PageUtil> GetGoodsUintPage(GoodsUintQueryDto dto)
        {
            int total = new RefAsync<int>();
            var data = SqlSugarDb.Db.Queryable<BPA_GoodsUint>()
                .Select(a => new GoodsUintViewDto()
                {
                    Id = a.Id.SelectAll()
                })
                .ToPageList(dto.Current, dto.PageSize, ref total);

            return new PageUtil()
            {
                Total = total,
                Data = data
            };
        }
        public async Task<bool> AddGoodsUint(List<GoodsUintInsertDto> dto)
        {
            try
            {
                List<BPA_GoodsUint> GoodsUintlist = new();
                for (int i = 0; i < dto.Count; i++)
                {
                    var resEntity = new BPA_GoodsUint();
                    resEntity.Id = Guid.NewGuid().ToString();
                    resEntity.Name = dto[i].Name;
                    resEntity.Remark = dto[i].Remark;
                    GoodsUintlist.Add(resEntity);
                }
                var res = await SqlSugarDb.Db.Insertable(GoodsUintlist).ExecuteCommandAsync();
                return res > 0;
            }
            catch (Exception)
            {
                throw Oops.Oh(ErrorCodeEnum.Code1007);
            }

        }
        public async Task<bool> UpdateGoodsUint(GoodsUintUpdateDto dto)
        {
            try
            {
                var model = SqlSugarDb.Db.Queryable<BPA_GoodsUint>().Where(x => x.Id == dto.Id).First();
                if (model == null) throw Oops.Oh("商品单位不存在");
                model.Name = dto.Name;
                model.Remark = dto.Remark;
                var res = await SqlSugarDb.Db.Updateable(model).ExecuteCommandAsync();
                return res > 0;
            }
            catch (Exception)
            {
                throw Oops.Oh(ErrorCodeEnum.Code1007);
            }

        }
        public async Task<bool> DelGoodsUint(GoodsUintDelDto dto)
        {
            try
            {
                var model = SqlSugarDb.Db.Queryable<BPA_GoodsUint>().Where(x => dto.Ids.Contains(x.Id)).ToList();
                if (model == null) throw Oops.Oh("商品单位不存在");
                var res = await SqlSugarDb.Db.Deleteable(model).ExecuteCommandAsync();
                return res > 0;
            }
            catch (Exception)
            {
                throw Oops.Oh(ErrorCodeEnum.Code1007);
            }

        }
        /// <summary>
        /// 查询商品能量配置
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<List<GoodsEnergyConfigVuewDto>> GetGoodsEnergyConfigPage(GoodsEnergyConfigQuDto dto)
        {
            List<IConditionalModel> conModels = new List<IConditionalModel>();
            //string groupId = App.User.FindFirst(ClaimConst.GroupId)?.Value;
            if (!string.IsNullOrEmpty(dto.Name))
            {
                conModels.Add(new ConditionalModel() { FieldName = "Name", ConditionalType = ConditionalType.Like, FieldValue = dto.Name });
            }
            if (!string.IsNullOrEmpty(dto.GoodsId))
            {
                conModels.Add(new ConditionalModel() { FieldName = "GoodsId", ConditionalType = ConditionalType.Like, FieldValue = dto.GoodsId });
            }
            int total = new RefAsync<int>();
            var res = await SqlSugarDb.Db.Queryable<BPA_GoodsEnergyConfig>()
                .Where(conModels)
                .OrderBy(a => a.CreateAt, OrderByType.Desc)
                .Select(x=>new GoodsEnergyConfigVuewDto() { Id=x.Id,Name=x.Name, GoodsId =x.GoodsId})
                .Mapper(x =>
                {
                    var data=SqlSugarDb.Db.Queryable<BPA_GoodsEnergyConfigDetails>().Where(it => it.GoodsenergyconfigId == x.Id).Select(it=>new GoodsEnergyConfigDetailsView() { Id=it.Id.SelectAll()}).ToList();
                    x.Details = data;
                })
                .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;
        }
    }
}
