using Nirvana.Common;
using Nirvana.Common.ApiBase;
using Nirvana.Data;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YBDevice.Entity;

namespace YBDevice.NApi.DBServices
{
    /// <summary>
    /// 科普资讯、轮播图、开屏广告管理
    /// </summary>
    public partial class InfoApp : BaseApp
    {
        /// <summary>
        /// 科普资讯列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ParamReturnData<SecInfoApiListModel>> GetInfoListAsync(ParamQuery param)
        {
            RefAsync<int> totalnum = 0;
            using (var dbClient = ReadDbContext.GetInstance())
            {
                var temquery = dbClient.Queryable<YB_SecInfo>().Where(x => x.Status == 1);
                if (!string.IsNullOrEmpty(param.keyword))
                {
                    int tagid = param.keyword.ToInt();
                    if (tagid > 0)
                    {
                        temquery = temquery.Where(x => x.TagId == tagid);
                    }
                }
                string sorts = string.Format("{0} {1}", param.sort, param.order);
                var query = await temquery.OrderBy(sorts)
                    .Select(x => new SecInfoApiListModel
                    {
                        Id = x.Id,
                        Title = x.Title,
                        HeadImg = x.HeadImg,
                        time = x.CreateTime
                    })
                    .Mapper((it, cache) =>
                    {
                        if (!string.IsNullOrEmpty(it.HeadImg))
                        {
                          it.HeadImg = $"{CDNURL}{it.HeadImg}";
                        }
                        it.CreateTime = it.time.ToYearDate();
                    })
                     .ToPageListAsync(param.page, param.pagesize, totalnum);
                return new ParamReturnData<SecInfoApiListModel>
                {
                    page = param.page,
                    items = query,
                    totalnum = totalnum,
                    pagesize = param.pagesize
                };
            }
        }
        /// <summary>
        /// 科普资讯详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultInfo> InfoDetailAsync(int id)
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                var data = await dbClient.Queryable<YB_SecInfo>().FirstAsync(x => x.Id == id);
                if (data == null)
                {
                    return new ResultInfo(ResultState.FAIL, "记录未找到");
                }
                //返回内容
                var contentdata = await dbClient.Queryable<YB_SecInfoContent>()
                    .FirstAsync(x => x.InfoId == data.Id);
                return new ResultInfo(ResultState.SUCCESS, "success", new AdDetailModel
                {
                    Content = contentdata?.Content,
                    Title = data.Title,
                    CreateTime = data.CreateTime.ToYearDate()
                });
            }
        }

        /// <summary>
        /// 类型列表
        /// </summary>
        /// <returns></returns>
        public async Task<ResultInfo> GetInfoTagListAsync()
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                var list = await dbClient.Queryable<YB_SecInfoType>().Where(x => x.Status == 1).OrderBy(x => x.SortCode, OrderByType.Asc).ToListAsync();
                return new ResultInfo(ResultState.SUCCESS, "success", list);
            }
        }

        /// <summary>
        /// 轮播图列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ParamReturnData<BannerApiListModel>> GetBannerListAsync(ParamQuery param)
        {
            RefAsync<int> totalnum = 0;
            using (var dbClient = ReadDbContext.GetInstance())
            {
                var temquery = dbClient.Queryable<YB_Banner>().Where(x => x.Status == 1);
                string sorts = string.Format("{0} {1}", param.sort, param.order);
                var query = await temquery.OrderBy(sorts)
                    .Select(x => new BannerApiListModel
                    {
                        Id = x.Id,
                        HeadImg = x.HeadImg,
                        Type = x.Type
                    })
                    .Mapper((it, cache) =>
                    {
                        it.HeadImg = $"{CDNURL}{it.HeadImg}";
                    })
                     .ToPageListAsync(param.page, param.pagesize, totalnum);
                return new ParamReturnData<BannerApiListModel>
                {
                    page = param.page,
                    items = query,
                    totalnum = totalnum,
                    pagesize = param.pagesize
                };
            }
        }
        /// <summary>
        /// 轮播图详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultInfo> BannerDetailAsync(int id)
        {
            using (var dbClient = ReadDbContext.GetInstance())
            {
                var data = await dbClient.Queryable<YB_Banner>().FirstAsync(x => x.Id == id);
                if (data == null)
                {
                    return new ResultInfo(ResultState.FAIL, "记录未找到");
                }
                //返回内容
                var contentdata = await dbClient.Queryable<YB_BannerContent>().FirstAsync(x => x.BannerId == data.Id);
                return new ResultInfo(ResultState.SUCCESS, "success", contentdata);
            }
        }
    }
}
