using BPA.SAAS.Manage.Application.Device.Dtos.Device;
using BPA.SAAS.Manage.Application.Device.Dtos.ProductTopics;
using BPA.SAAS.Manage.Application.Device.Interface;
using BPA.SAAS.Manage.Comm.Enum;
using BPA.SAAS.Manage.Core.Base;
using BPA.SAAS.Manage.Core.Device;
using BPA.SAAS.Manage.Core.Product;
using StackExchange.Profiling.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPA.SAAS.Manage.Application.Device.Services
{
    public class ProductTopicsService : IProductTopicsService, ITransient
    {
        private readonly ISqlSugarClient _db;
        public ProductTopicsService(ISqlSugarClient db)
        {
            _db= db;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<PageUtil> GetProductTopicsPage(ProductTopicsQueryInputDto inputDto)
        {
            var total = new RefAsync<int>();
            var data = await _db.Queryable<BPA_ProductTopics>().Where(x=>x.ProductId== inputDto.ProductId && x.ProductVesionId== inputDto.ProductVesionId && x.IsDefault== inputDto.IsDefault)
                .WhereIF(inputDto.TopicsType!=null, x => x.TopicsType== (TopicsTypeEnum)(Enum.Parse(typeof(TopicsTypeEnum), inputDto.TopicsType.ToString())))
                .WhereIF(!string.IsNullOrWhiteSpace(inputDto.Topics), x => x.Topics == inputDto.Topics)
                .OrderBy(x => x.CreateAt, OrderByType.Desc).OrderBy(x => x.Name, OrderByType.Desc)
                .ToPageListAsync(inputDto.Current, inputDto.PageSize, total);

            return new PageUtil()
            {
                Data = data,
                Total = total

            };
        }
        public async Task<List<BPA_ProductTopics>> GetProductTopicList(ProductTopicsQueryDto inputDto)
        {
            var total = new RefAsync<int>();
            var data = await _db.Queryable<BPA_ProductTopics>().Where(x => x.ProductId == inputDto.ProductId && x.ProductVesionId == inputDto.ProductVesionId && x.IsDefault == inputDto.IsDefault)
        
                .OrderBy(x => x.CreateAt, OrderByType.Desc)
                .ToListAsync();

            return data;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> AddProductTopics(ProductTopicsBaseDto inputDto)
        {
            BPA_ProductTopics BPA_ProductTopics = inputDto.Adapt<BPA_ProductTopics>();
            var res = await _db.Insertable(BPA_ProductTopics).CallEntityMethod(m => m.Create()).ExecuteReturnEntityAsync();
            return res != null;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateProductTopics(ProductTopicsBaseDto inputDto)
        {
            var data = await _db.Queryable<BPA_ProductTopics>().Where(x => x.Id == inputDto.Id).FirstAsync();

            if (data != null)
            {
                var product = _db.Queryable<BPA_ProductTopics>().Where(x => x.Id == inputDto.ProductId).First();
                var inputData = inputDto.Adapt<BPA_ProductTopics>();
                var res = await _db.Updateable(inputData)
                    .UpdateColumns(x => new
                    {
                        x.Topics,
                        x.TopicsType,
                        x.Description,
                    })
                    .Where(x => x.Id == inputDto.Id).ExecuteCommandAsync();

                return res > 0;
            }
            return false;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="inputList"></param>
        /// <returns></returns>
        public async Task<bool> DelProductTopics(List<string> inputList)
        {
            var data = await _db.Queryable<BPA_ProductTopics>().Where(x => inputList.Contains(x.Id)).ToListAsync();
          
            var res = await _db.Deleteable(data).ExecuteCommandAsync();
            return res > 0;
        }
    }
}
