using Infrastructure;
using Infrastructure.Extensions;
using OpenAuth.App.Base;
using OpenAuth.App.Interface;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenAuth.App.BaseApp.Base;
using System.Web;

namespace OpenAuth.App
{
    public class SysDataItemDetailApp : SqlSugarBaseApp<SysDataItemDetail, SugarDbContext>
    {
        ISqlSugarClient client;
        public SysDataItemDetailApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<SysDataItemDetail> repository, IAuth auth) : base(unitWork, repository, auth)
        {
            client = base.Repository.AsSugarClient();
        }

        public async Task<List<SysDataItemDetail>> Load(string code, string keyWord)
        {
            var list = await client.Queryable<SysDataItemDetail>()
                   .LeftJoin<SysDataItem>((a, b) => a.ItemId == b.ItemId)
                   .Where((a, b) => b.ItemCode == code)
                   .WhereIF(!string.IsNullOrEmpty(keyWord), (a => a.ItemName.Contains(keyWord) || a.ItemCode.Contains(keyWord)))
                   .OrderBy(a => a.SortCode)
                   .Select(a => a)
                   .ToListAsync();

            return list;
        }

        /// <summary>
        /// 获取字段数据（有父级编号）
        /// </summary>
        /// <param name="code">编码</param>
        /// <param name="keyWord">筛选条件</param>
        /// <param name="pid">父级编号</param>
        /// <returns></returns>
        public async Task<List<SysDataItemDetail>> LoadWithPid(string code, string keyWord,string pid)
        {
            var list = await client.Queryable<SysDataItemDetail>()
                   .LeftJoin<SysDataItem>((a, b) => a.ItemId == b.ItemId)
                   .Where((a, b) => b.ItemCode == code)
                   .WhereIF(!string.IsNullOrEmpty(keyWord), (a => a.ItemName.Contains(keyWord) || a.ItemCode.Contains(keyWord)))
                   .WhereIF(!string.IsNullOrEmpty(pid),a=>a.ParentId==pid)
                   .OrderBy(a => a.SortCode)
                   .Select(a => a)
                   .ToListAsync();

            return list;
        }

        public async Task<Response<bool>> SaveDetailEntity(SysDataItemDetail entity)
        {
            var flag = false;

            var dataItem = await client.Queryable<SysDataItem>()
                .Where(a => a.DeleteMark == 0 && a.ItemId == entity.ItemId).FirstAsync();
            ;
            if (dataItem.IsTree != 1 || string.IsNullOrEmpty(dataItem.ParentId))
            {
                entity.ParentId = "0";
            }

            entity.QuickQuery = StringExtension.ConvertPinYin(entity.ItemName).ToUpper();
            entity.SimpleSpelling = StringExtension.PinYin(entity.ItemName);
            if (string.IsNullOrEmpty(entity.ItemDetailId))
            {
                entity.ItemDetailId = Guid.NewGuid().ToString();
                entity.CreateDate = DateTime.Now;
                if (string.IsNullOrEmpty(entity.ParentId))
                {
                    entity.ParentId = "0";
                }
                entity.DeleteMark = 0;
                flag = await base.Repository.InsertAsync(entity);
            }
            else
            {
                entity.ItemDetailId = entity.ItemDetailId;
                entity.ModifyDate = DateTime.Now;
                flag = await base.Repository.UpdateAsync(entity);
            }

            return new Response<bool>
            {
                Result = flag,
                Message = flag == true ? "success" : "error"
            };
        }

        public async Task<Response<bool>> Delete(string id)
        {
            var flag = await base.Repository.DeleteByIdAsync(id);
            return new Response<bool>
            {
                Result = flag,
                Message = flag == true ? "success" : "error"
            };
        }
    }
}
