using BPA.SAAS.Manage.Application.AExternalPlatform.Service.Goods.Dtos;
using BPA.SAAS.Manage.Application.DataBase.Dtos.Bom;
using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsAttributePrice;
using BPA.SAAS.Manage.Application.DataBase.Dtos.GoodsType;
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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPA.SAAS.Manage.Application.DataBase.Services
{
    public class GoodsAttributePriceService : IGoodsAttributePriceService, ITransient
    {
        private readonly ISqlSugarClient _db;
        public GoodsAttributePriceService(ISqlSugarClient db)
        {
            _db = db;   // 推荐操作
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<PageUtil> GetGoodsAttributePricePage(GoodsAttributePriceQueryDto dto)
        {
            RefAsync<int> total =0;
            var res = await _db.Queryable<BPA_GoodsAttributePrice>()
                .WhereIF(!string.IsNullOrWhiteSpace(dto.GoodsId),x=>x.GoodsId== dto.GoodsId)
                .OrderBy(a => a.CreateAt, OrderByType.Desc)
                .Select(it => new GoodsAttributePriceView() { Id = it.Id, GoodsId = it.GoodsId, GoodsattributeValueId = it.GoodsattributeValueId,Price=it.Price,GoodsattributeValue=it.GoodsattributeValue })
                
                .ToPageListAsync(dto.Current, dto.PageSize, total);
            //.ToPageList(dto.Current, dto.PageSize, ref total);
      
            PageUtil util = new PageUtil()
            {
                Total = total,
                Data = res

            };
            return util;
        }
        public async Task<List<GoodsAttributePriceView>> GetGoodsAttributePriceList(string goodsId)
        {
            var res = await _db.Queryable<BPA_GoodsAttributePrice>()
                .WhereIF(!string.IsNullOrWhiteSpace(goodsId), x => x.GoodsId == goodsId)
                .OrderBy(a => a.CreateAt, OrderByType.Desc)
                .Select(it => new GoodsAttributePriceView() { Id = it.Id, GoodsId = it.GoodsId, GoodsattributeValueId = it.GoodsattributeValueId, Price = it.Price, GoodsattributeValue = it.GoodsattributeValue })
                .ToListAsync();
            return res;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> AddGoodsAttributePrice(GoodsAttributePriceDto dto)
        {
            var check=_db.Queryable<BPA_GoodsAttributePrice>().Where(x=>x.GoodsattributeValueId== dto.GoodsattributeValueId).Any();
            if (check) throw Oops.Oh("当前属性的价格已存在");
            var model = new BPA_GoodsAttributePrice
            {

                GoodsId = dto.GoodsId,
                GoodsattributeValueId = dto.GoodsattributeValueId,
                GoodsattributeValue= dto.GoodsattributeValue,
                Price = dto.Price
            };
          
            var res = await _db.Insertable(model).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
            return res > 0;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateGoodsAttributePrice(GoodsAttributePriceDto dto)
        {
            // 查询数据库中是否存在未删除的商品类型
            var resEntity = _db.Queryable<BPA_GoodsAttributePrice>().First(it => it.Id == dto.Id);
            if (null == resEntity)
            {
                return false;
            }

            resEntity.GoodsId = dto.GoodsId;
            resEntity.GoodsattributeValueId = dto.GoodsattributeValueId;
            resEntity.GoodsattributeValue = dto.GoodsattributeValue;
            resEntity.Price = dto.Price;
            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> DelGoodsAttributePrice(string Id)
        {
            var resEntity = _db.Queryable<BPA_GoodsAttributePrice>().First(it => it.Id == Id);
            if (resEntity == null) throw Oops.Oh("找不到相关记录");
            var res = await _db.Deleteable(resEntity).ExecuteCommandAsync();
            return res > 0;
        }
    }
}
