using Infrastructure;
using Infrastructure.Extensions;
using Microsoft.Extensions.Configuration;
using OpenAuth.App.Base;
using OpenAuth.App.CodeTable;
using OpenAuth.App.Request;
using OpenAuth.App.FormScheme.FormHelpers;
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 static Dm.net.buffer.ByteArrayBuffer;

namespace OpenAuth.App.DbTable
{
    public class DbTableApp : SqlSugarBaseApp<Repository.Domain.DbCodeTable, SugarDbContext>
    {
        private IConfiguration _configuration;
        public DbTableApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<Repository.Domain.DbCodeTable> repository, IConfiguration configuration) : base(unitWork, repository, null)
        {
            _configuration = configuration;
        }

        /// <summary>
        /// 创建表信息
        /// </summary>
        /// <param name="dbcode">数据库编码</param>
        /// <param name="dbTableReq">数据表信息</param>
        /// <returns></returns>
        public Response<bool> AddTable(string dbcode, DbTableReq dbTableReq)
        {
            if (dbTableReq.DbColumnInfoList == null || !dbTableReq.DbColumnInfoList.Any())
                throw new Exception("请添加数据列");
            using (var db = this.CodeClient(dbcode, _configuration))
            {
                var typeBuilder = db.DynamicBuilder().CreateClass(dbTableReq.TableName, new SugarTable() { TableName = dbTableReq.TableName, TableDescription = dbTableReq.Description });
                dbTableReq.DbColumnInfoList.ForEach(u =>
                {
                    typeBuilder.CreateProperty(u.DbColumnName, typeof(string), new SugarColumn()
                    {
                        IsPrimaryKey = u.IsPrimarykey == 1,
                        IsIdentity = u.IsIdentity == 1,
                        ColumnDataType = u.DataType,
                        Length = u.Length,
                        IsNullable = u.IsNullable == 1,
                        DecimalDigits = u.DecimalDigits,
                        ColumnDescription = u.ColumnDescription,
                    });
                });
                db.CodeFirst.InitTables(typeBuilder.BuilderType());
                return new Response<bool>()
                {
                    Code = 200,
                    Result = true,
                    Message = "创建成功"
                };
            }
        }
        /// <summary>
        /// 创建字段
        /// </summary>
        /// <param name="dbcode">数据库编码</param>
        /// <param name="dbColumnInput">表字段</param>
        /// <returns></returns>
        public Response<bool> AddColumn(string dbcode, DbColumnInput dbColumnInput)
        { 
            using (var db = this.CodeClient(dbcode, _configuration))
            {
                var column = new DbColumnInfo
                {
                    ColumnDescription = dbColumnInput.ColumnDescription,
                    DbColumnName = dbColumnInput.DbColumnName,
                    IsIdentity = dbColumnInput.IsIdentity == 1,
                    IsNullable = dbColumnInput.IsNullable == 1,
                    IsPrimarykey = dbColumnInput.IsPrimarykey == 1,
                    Length = dbColumnInput.Length,
                    DecimalDigits = dbColumnInput.DecimalDigits,
                    DataType = dbColumnInput.DataType
                };
                
                db.DbMaintenance.AddColumn(dbColumnInput.TableName, column);
                db.DbMaintenance.AddColumnRemark(dbColumnInput.DbColumnName, dbColumnInput.TableName, dbColumnInput.ColumnDescription);
                if (column.IsPrimarykey)
                    db.DbMaintenance.AddPrimaryKey(dbColumnInput.TableName, dbColumnInput.DbColumnName);
                return new Response<bool>()
                {
                    Code = 200,
                    Result = true,
                    Message = "创建成功"
                };
            }
        }
    }
}
