using BPA.KitChen.GroupMeal.SqlSugar;
using BPA.SAAS.Manage.Application.AExternalPlatform.Service.WeighingService.Dtos;
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.AExternalPlatform.Service.WeighingService.Services
{
    public class WeighingService : IWeighingService, ITransient
    {

        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <returns></returns>
        [HttpPost("/api/ExternalPlatform/WeighingService/GetGoodsInfo")]
        public async Task<List<GoodsTypeDto>> GetGoodsInfo()
        {
            //获取商品类型
            var goodsType = await SqlSugarDb.Db.Queryable<BPA_GoodsType>()
                .OrderBy(x => x.Sort)
                .Where(x=>x.IsDeleted==0)
                .Select(x => new GoodsTypeDto()
                {
                    Id = x.Id,
                    Name = x.Name,
                    Sort = x.Sort,
                }).ToListAsync();

            //获取商品
            var goodsList = await SqlSugarDb.Db.Queryable<BPA_GoodsInfo>().OrderBy(x => x.Sort)
                .Where(x => x.IsDeleted == 0)
                .Select(x => new GoodsDto()
                {
                    Id = x.Id,
                    Name = x.Name,
                    Sort = x.Sort,
                    Cover = x.ImgUrl,
                    GoodsTypeId = x.GoodsTypeId,
                    Price = x.Price,
                    VipPrice=x.Price
                }).ToListAsync();

            ////获取商品属性价格
            //var goodsPriceList = await SqlSugarDb.Db.Queryable<BPA_GoodsAttributePrice>().ToListAsync();
            //foreach (var goods in goodsList)
            //{
            //    var data = goodsPriceList.Where(x=>x.GoodsId==goods.Id).ToList();
            //    if (data!=null)
            //    {
            //        goods.Price = data.FirstOrDefault()!.Price;
            //        goods.VipPrice = goods.Price;
            //    }
            //}

            foreach (var item in goodsType)
            {
                var data = goodsList.Where(x => x.GoodsTypeId == item.Id).ToList();
                item.Foods = data;
            }

            return goodsType;
        }
    }
}
