using BPA.SAAS.Manage.Application.Device.Dtos.Device;
using BPA.SAAS.Manage.Application.Device.Dtos.DeviceVesion;
using BPA.SAAS.Manage.Application.Device.Interface;
using BPA.SAAS.Manage.Comm.Const;
using BPA.SAAS.Manage.Comm.Enum;
using BPA.SAAS.Manage.Core.Base;
using BPA.SAAS.Manage.Core.Product;
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 ProductService : IProductService, ITransient
    {
        ISqlSugarClient _db;
        public ProductService(ISqlSugarClient db)
        {
            _db=db;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<PageUtil> GetProductPage(ProductQueryInputDto inputDto)
        {
            var total = new RefAsync<int>();
            var data = await _db.Queryable<BPA_Product>().Where((x) => x.IsDeleted == 0)
                .WhereIF(!string.IsNullOrWhiteSpace(inputDto.Name), x => x.Name.Contains(inputDto.Name))
                 .WhereIF(!string.IsNullOrWhiteSpace(inputDto.Key), x => x.Key.Contains(inputDto.Key))
                 .WhereIF(!string.IsNullOrWhiteSpace(inputDto.Code), x => x.Code.Contains(inputDto.Code))
                  .WhereIF(!string.IsNullOrWhiteSpace(inputDto.Remark), x => x.Remark.Contains(inputDto.Remark))
                .OrderBy(x => x.CreateAt, OrderByType.Desc)
                .ToPageListAsync(inputDto.Current, inputDto.PageSize, total);

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

            };
        }
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<BPA_Product>> GetProductList()
        {
            var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
            var data = await _db.Queryable<BPA_Product>().Where(x=>x.Status==0) .ToListAsync();
            return data;
        }
        public async Task<BPA_Product> GetProduct(string productId)
        {
            var data = await _db.Queryable<BPA_Product>().Where(x => x.Id == productId).FirstAsync();
            return data;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>

        public async Task<bool> AddProduct(ProductBaseDto inputDto)
        {
            var count=_db.Queryable<BPA_Product>().Count();
            var check=_db.Queryable<BPA_Product>().Where(x => x.Name == inputDto.Name || x.Code == inputDto.Code).Any();
            if (check) throw Oops.Oh("产品名称和标识不能重复");
            var res = await _db.Insertable(new BPA_Product
            {
                Name = inputDto.Name,
                Key = GetNumber2(count),
                Remark=inputDto.Remark,
                Code= inputDto.Code,
                Status = CommonStatus.ENABLE
            }).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
            return res > 0;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="inputList"></param>
        /// <returns></returns>
        public async Task<bool> DeProduct(List<string> inputList)
        {
            var datas = await _db.Queryable<BPA_Product>()
                .Where(x => inputList.Contains(x.Id))
                .ToListAsync();
            _db.Deleteable(datas).ExecuteCommand();
            return true;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public async Task<bool> UpdateProduct(ProductBaseDto inputDto)
        {
            var data = await _db.Queryable<BPA_Product>().Where(x => x.Id == inputDto.Id).FirstAsync();
            var check = _db.Queryable<BPA_Product>().Where(x => (x.Name == inputDto.Name || x.Code == inputDto.Code) && x.Id != inputDto.Id).Any();
            if (check) throw Oops.Oh("产品名称和标识不能重复");
            if (data != null)
            {
                var res = _db.Updateable<BPA_Product>()
                     .SetColumns(t => t.Name == inputDto.Name)
                       .SetColumns(t => t.Code == inputDto.Code)
                      .SetColumns(t => t.Remark == inputDto.Remark).Where(t => t.Id == inputDto.Id)
                     .ExecuteCommandHasChange();

                return res;
            }

            return false;
        }
        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> UpdateDeviceVesionSatatus(ProductSatatusDto inputDto)
        {
            var data = await _db.Queryable<BPA_Product>().Where(x => x.Id == inputDto.Id).FirstAsync();

            if (data != null)
            {
                var res = _db.Updateable<BPA_Product>()
                     .SetColumns(t => t.Status == (CommonStatus)inputDto.Status).Where(t => t.Id == inputDto.Id)
                     .ExecuteCommandHasChange();

                return res;
            }

            return false;
        }
        private string GetNumber2(int count)
        {
            var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
            var res=MD5Encryption.Encrypt(groupId + DateTime.Now.ToString("yyyyMMddhhmmsss"), false,true)+ count;
            return res;
        }
    }
}
