using Furion.DependencyInjection;
using Nirvana.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Waste.Domain;

namespace Waste.Application
{
    /// <summary>
    /// 垃圾信息管理
    /// </summary>
    public class WasteService : IWasteService, ITransient
    {
        private readonly ISqlSugarRepository<W_WasteType> repository;
        private readonly SqlSugarClient dbClient;

        public WasteService(ISqlSugarRepository<W_WasteType> sqlSugarRepository)
        {
            repository = sqlSugarRepository;
            dbClient = repository.Context;
        }
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public async Task<ResultInfo> DeleteTypeFormAsync(Guid keyValue)
        {
            if (!await dbClient.Queryable<W_WasteType>().AnyAsync(x => x.Id == keyValue))
            {
                return new ResultInfo(ResultState.FAIL, "记录未找到");
            }
            await dbClient.Deleteable<W_WasteType>().Where(x => x.Id == keyValue).ExecuteCommandAsync();
            return new ResultInfo(ResultState.SUCCESS, "删除成功");
        }
        /// <summary>
        /// 分类详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<W_WasteType> DetailTypeAsync(Guid id)
        {
            return await dbClient.Queryable<W_WasteType>().FirstAsync(x => x.Id == id);
        }
        /// <summary>
        /// 获取所有分类
        /// </summary>
        /// <returns></returns>
        public async Task<List<W_WasteType>> GetAllTypeList()
        {
            return await dbClient.Queryable<W_WasteType>().ToListAsync();
        }
        /// <summary>
        /// 垃圾分类列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<PageParms<W_WasteType>> GetTypeListAsync(QueryParams param)
        {
            RefAsync<int> totalnum = 0;
            var temquery = dbClient.Queryable<W_WasteType>();
            if (param.queryParam != null && param.queryParam.Count > 0)
            {
                List<IConditionalModel> conModels = new List<IConditionalModel>();
                param.queryParam.ForEach(x =>
                {
                    if (!string.IsNullOrEmpty(x.Value))
                    {
                        conModels.Add(new ConditionalModel()
                        {
                            FieldName = x.Name,
                            ConditionalType = (ConditionalType)x.Type,
                            FieldValue = x.Value.Trim()
                        });
                    }
                });
                if (conModels.Count > 0)
                {
                    temquery = temquery.Where(conModels);
                }
            }
            string sorts = string.Format("{0} {1}", param.sort, param.order);
            var query = await temquery.OrderBy(sorts)
                 .ToPageListAsync(param.offset, param.limit, totalnum);
            return new PageParms<W_WasteType>
            {
                page = param.offset,
                Items = query,
                totalnum = totalnum,
                limit = param.limit
            };
        }
        /// <summary>
        /// 垃圾分类信息提交
        /// </summary>
        /// <param name="w_WasteType"></param>
        /// <returns></returns>
        public async Task<ResultInfo> SubmitTypeFormAsync(W_WasteType w_WasteType)
        {
            if (w_WasteType.Id != Guid.Empty)
            {
                await dbClient.Updateable<W_WasteType>().SetColumns(x => new W_WasteType
                {
                    Code = w_WasteType.Code,
                    Name = w_WasteType.Name
                }).Where(x => x.Id == w_WasteType.Id).ExecuteCommandAsync();
                return new ResultInfo(ResultState.SUCCESS, "修改成功");
            }
            else
            {
                w_WasteType.CreateTime = DateTime.Now;
                w_WasteType.Status = (int)StatusType.Enabled;
                await dbClient.Insertable<W_WasteType>(w_WasteType).ExecuteCommandAsync();
                return new ResultInfo(ResultState.SUCCESS, "添加成功");
            }
        }
    }
}
