using BPA.KitChen.GroupMeal.Application.Service.OneCard.DiningPlate.Dtos;
using BPA.KitChen.GroupMeal.Core.Entity;
using BPA.KitChen.GroupMeal.Core.Enum;
using BPA.KitChen.GroupMeal.SqlSugar;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using MQTTnet.Client;
using NPOI.POIFS.Crypt.Dsig;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BPA.KitChen.GroupMeal.Application.BaseDto;
using BPA.KitChen.GroupMeal.Application.Service.OneCard.Member.MemberInfo.Dtos.MemberTag;
using Microsoft.AspNetCore.Components.Forms;
using MathNet.Numerics.Distributions;

namespace BPA.KitChen.GroupMeal.Application.Service.OneCard.DiningPlate.Service
{
    public class DiningPlateService:ITransient, IDiningPlateService
    {
        private readonly SqlSugarScope db;

        public DiningPlateService()
        {
            db = SqlSugarDb.Db;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<PageUtil> Page(DiningPlatePageIndexDto inputDto)
        {
            int total = new RefAsync<int>();
            var res = db.Queryable<BPA_DiningPlate>()
                .Where(a => a.IsDeleted == 0)
                .WhereIF(!string.IsNullOrEmpty(inputDto.QRCode),x=>x.QRCode==inputDto.QRCode)
                .WhereIF(!string.IsNullOrEmpty(inputDto.ChipCode), x => x.ChipCode == inputDto.ChipCode)
               
                .OrderBy(x => x.CreateAt, OrderByType.Desc)
                .Select(t => new DiningPlateDto
                {
                    Id = t.Id.SelectAll()

                }).ToPageList(inputDto.Current, inputDto.PageSize, ref total);
            return new PageUtil()
            {
                Data = res,
                Total = total
            };
        }

        /// <summary>
        /// 新增餐盘 
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> Add(DiningPlateCreateOrUpdataDto inputDto)
        {
            //
            var data = await db.Queryable<BPA_DiningPlate>()
                .FirstAsync(x => inputDto.QRCode == x.QRCode || inputDto.ChipCode == x.ChipCode);
            if (data!=null)
            {
                throw Oops.Oh($"二维码或芯片码已存在");
            }

            data = new BPA_DiningPlate()
            {
                ChipCode = inputDto.ChipCode,
                QRCode = inputDto.QRCode,
                Status = CommonStatus.ENABLE,
            };
            var res=await db.Insertable(data).ExecuteCommandAsync();

            return res > 0;
        }


        /// <summary>
        /// 修改餐盘
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> Update(DiningPlateCreateOrUpdataDto inputDto)
        {
            var data1 = await db.Queryable<BPA_DiningPlate>()
                .FirstAsync(x => inputDto.QRCode == x.QRCode || inputDto.ChipCode == x.ChipCode);

            var data2 = await db.Queryable<BPA_DiningPlate>()
                .FirstAsync(x =>x.Id==inputDto.Id);
            if (data2==null)
            {
                throw Oops.Oh($"操作数据不存在");
            }
            else
            {
                if (data1!=null&& data1.Id!=data2.Id)
                {
                    throw Oops.Oh($"二维码或芯片码已存在");
                }
            }

            data2.ChipCode = inputDto.ChipCode;
            data2.QRCode = inputDto.QRCode;
            var res = await db.Updateable(data2).ExecuteCommandAsync();
            return res > 0;
        }


        /// <summary>
        /// 启用警用
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> EnableDisable(DiningPlateCreateOrUpdataDto inputDto)
        {
            

            var data = await db.Queryable<BPA_DiningPlate>()
                .FirstAsync(x => x.Id == inputDto.Id);
            if (data == null)
            {
                throw Oops.Oh($"操作数据不存在");
            }

            data.Status = data.Status== CommonStatus.DISABLE? CommonStatus.ENABLE: CommonStatus.DISABLE;
            var res = await db.Updateable(data).ExecuteCommandAsync();
            return res > 0;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> Del(string id)
        {
            var data = await db.Queryable<BPA_DiningPlate>()
                .FirstAsync(x => x.Id == id);
            if (data == null)
            {
                throw Oops.Oh($"操作数据不存在");
            }
            var res =await db.Deleteable<BPA_DiningPlate>(data).ExecuteCommandAsync();

            return res > 0;
        }
    }
}
