using BPA.KitChen.GroupMeal.Application.Service.Shop.Dtos;
using BPA.KitChen.GroupMeal.Core.Common.Const;
using BPA.KitChen.GroupMeal.Core.Entity;
using BPA.KitChen.GroupMeal.SqlSugar;
using Furion;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace BPA.KitChen.GroupMeal.Application.Service.Shop
{
    [ApiDescriptionSettings("店铺管理", Tag = "店铺管理", SplitCamelCase = false)]
    public class ShopService : IDynamicApiController, ITransient, IShopService
    {

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutDto> GetPage(ShopPageDto inputDto)
        {
            var total = new RefAsync<int>();
            var data =await SqlSugarDb.Db.Queryable<ShopEntity>()
                .WhereIF(!string.IsNullOrEmpty(inputDto.Name),x=>x.Name.Contains(inputDto.Name))
                .WhereIF(!string.IsNullOrEmpty(inputDto.Phone), x => x.Phone.Contains(inputDto.Phone))
                .WhereIF(!string.IsNullOrEmpty(inputDto.OrgId), x => x.OrgId==inputDto.OrgId)
                .OrderBy(a => a.CreateAt, OrderByType.Desc)
                .ToPageListAsync(inputDto.Current, inputDto.PageSize, total);

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

            };
        }

        public async Task<List<ShopEntity>> GetAllList()
        {
            var data = await SqlSugarDb.Db.Queryable<ShopEntity>().Where(x=>x.IsDeleted==0).ToListAsync();
            return data;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> Add(ShopCreateOrUpdateDto inputDto)
        {
            inputDto.Id=Guid.NewGuid().ToString();
            var data=await SqlSugarDb.Db.Queryable< ShopEntity >().FirstAsync(x=>x.Name==inputDto.Name);
            if (data != null)
            {
                throw Oops.Oh($"名称重复");
            }

            var res =await SqlSugarDb.Db.Insertable(inputDto.Adapt<ShopEntity>()).ExecuteCommandAsync();

            return res > 0;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="inputDto"></param>
        /// <returns></returns>
        public async Task<bool> Update(ShopCreateOrUpdateDto inputDto)
        {
            var data = await SqlSugarDb.Db.Queryable<ShopEntity>().FirstAsync(x => x.Id == inputDto.Id);
            if (data == null)
            {
                throw Oops.Oh($"数据不存在");
            }
           var data2= await SqlSugarDb.Db.Queryable<ShopEntity>().FirstAsync(x => x.Id != inputDto.Id&&x.Name==inputDto.Name);
            if (data2 != null)
            {
                throw Oops.Oh($"名称存在");
            }
            data.Name = inputDto.Name;
            data.OrgId = inputDto.OrgId;
            data.Phone = inputDto.Phone;

            var res=await SqlSugarDb.Db.Updateable(data).ExecuteCommandAsync();
            return res > 0;
        }

        public async Task<bool> Del(string id)
        {
            var data = await SqlSugarDb.Db.Queryable<ShopEntity>().FirstAsync(x => x.Id == id);

            var res =await SqlSugarDb.Db.Updateable<ShopEntity>()
                .SetColumns(it => new ShopEntity {IsDeleted=1 })//类只能在表达示里面不能提取
                .Where(it => it.Id==id)
                .ExecuteCommandAsync();

            return res > 0;
        }
    }
}
