using Dm;
using Model.ModelDto.SysDto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IDAL.ISysInterface;

namespace WMS.DAL.SysInfrastructure
{
    public class DictionaryRepository : BaseRepository<SysDictionary>, IDictionaryRepository
    {
        private static readonly SqlSugarScope Db = DataContext.Db;
        public DictionaryRepository() : base(Db)
        {
        }

        /// <summary>
        /// 获取字典信息列表
        /// </summary>
        /// <param name="DictName">字典名称</param>
        /// <param name="DictNo">父级字典号</param>
        /// <param name="Level">层级</param>
        /// <param name="IsEdit">允许编辑</param>
        /// <param name="IsAdd">允许新增</param>
        /// <returns></returns>
        public List<DictionaryDto> GetDicList(string DictName, string DictNo, string Level, string IsEdit, string IsAdd)
        {
            string str = "select dic.*,user1.RealName CreateName from SysDictionary dic left join SysUserInfor user1 on dic.CreateUser = user1.Id where dic.IsDel = @isdel";
            //判断层级
            if (Level == "1")
            {
                str += " and dic.Level = @level";
            }
            //判断允许编辑
            if (!string.IsNullOrEmpty(IsEdit))
            {
                str += " and dic.IsEdit = @isedit";
            }
            //判断允许新增
            if (!string.IsNullOrEmpty(IsAdd))
            {
                str += " and dic.IsAdd = @isadd";
            }
            //判断字典名称是否为空
            if (!string.IsNullOrEmpty(DictName))
            {
                if (Level == "1")
                {
                    str += " and dic.ParentNo in (select DictNo from SysDictionary where DictName like @dictname)";
                }
                if (Level == "0")
                {
                    str += " and dic.DictName like @dictname";
                }
                if (string.IsNullOrEmpty(Level))
                {
                    str += " and dic.DictName like @dictname or dic.ParentNo in (select DictNo from SysDictionary where DictName like @dictname)";
                }
            }
            //判断父级字典号是否为空
            if (!string.IsNullOrEmpty(DictNo))
            {
                if (Level == "1")
                {
                    str += " and dic.ParentNo = @dictno";
                }
                if (Level == "0")
                {
                    str += " and dic.DictNo = @dictno";
                }
                if (string.IsNullOrEmpty(Level))
                {
                    str += " and dic.DictNo = @dictno or dic.ParentNo = @dictno";
                }
            }
            //判断层级
            if (Level == "0")
            {
                str += " and dic.Level = @level";
            }
            //排序(根据ord显示顺序)
            str += " and dic.IsPublic ='0'";
            List<DictionaryDto> diclist = Db.Ado.SqlQuery<DictionaryDto>(str, new
            {
                isdel = "0", //是否删除
                dictname = "%" + DictName + "%", //字典名称
                dictno = DictNo, //字典号
                level = Level, //层级
                isedit = IsEdit, //允许编辑
                isadd = IsAdd //允许新增
            });
            diclist = diclist.OrderBy(a => int.Parse(a.Ord)).ToList();
            return diclist;
        }

        /// <summary>
        /// 根据id获取字典信息
        /// </summary>
        /// <param name="id">å­—å…¸id</param>
        /// <returns></returns>
        public SysDictionary GetDicById(int id)
        {
            string str = "select dic.*,user1.UserName CreateName from SysDictionary dic left join SysUserInfor user1 on dic.CreateUser = user1.Id where dic.IsDel = @isdel and dic.Id = @id";
            SysDictionary dic = Db.Ado.SqlQuery<SysDictionary>(str, new
            {
                isdel = "0", //是否删除
                id //id
            }).First();
            return dic;
        }

        /// <summary>
        /// 获取父级字典号(根据层级根目录)
        /// </summary>
        /// <returns></returns>
        public List<SysDictionary> GetDicParentListByLevel()
        {
            string str = "select Id,DictNo,DictName,ParentNo,Level from SysDictionary where Level = @level and IsDel = @isdel and IsAdd = @isadd";
            List<SysDictionary> diclist = Db.Ado.SqlQuery<SysDictionary>(str, new
            {
                level = "0", //层级
                isdel = "0", //是否删除
                isadd = "0" //允许添加
            });
            return diclist;
        }

        /// <summary>
        /// 根据编号查询字典信息
        /// </summary>
        /// <param name="DictNo">字典编号</param>
        /// <returns></returns>
        public List<SysDictionary> GetDicByNo(string DictNo)
        {
            string str = $"select * from SysDictionary where DictNo = @dictno";
            List<SysDictionary> diclist = Db.Ado.SqlQuery<SysDictionary>(str, new
            {
                dictno = DictNo //字典编号
            });
            return diclist;
        }

        /// <summary>
        /// 新增字典信息
        /// </summary>
        /// <param name="dic">数据字典实体模型</param>
        /// <returns></returns>
        public async Task<int> AddDic(SysDictionary dic)
        {
            //获取是否拥有相同字典名称
            if (!string.IsNullOrEmpty(dic.DictName))
            {
                var dicno = Db.Queryable<SysDictionary>().First(a => a.DictName == dic.DictName);
                if (dicno != null)
                {
                    throw new Exception("获取是否拥有相同字典名称");
                }
            }
            else
            {
                throw new Exception("字典名称为空,请核实!");
            }
            string str = "insert into SysDictionary values(@dictno, @dictname, @parentno, @ord, @level, @ispublic, @isedit, @isadd, @isdel, @createtime, @createuser, null, null)";
            //int i = await CudAsync(str, new
            int i = await Db.Ado.ExecuteCommandAsync(str, new
            {
                dictno = dic.DictNo, //字典编号
                dictname = dic.DictName, //字典名称
                parentno = dic.ParentNo, //父级字典号
                ord = dic.Ord, //显示顺序
                level = dic.Level, //层级
                ispublic = dic.IsPublic, //是否公开
                isedit = dic.IsEdit, //允许编辑
                isadd = dic.IsAdd, //允许添加
                isdel = "0", //是否删除
                createtime = Db.GetDate(), //创建时间
                createuser = dic.CreateUser //创建人
            });
            return i;
        }

        /// <summary>
        /// 删除字典信息
        /// </summary>
        /// <param name="dic">数据字典实体模型</param>
        /// <returns></returns>
        public async Task<int> DelDic(SysDictionary dic)
        {
            string str = $"update SysDictionary set IsDel = @isdel, UpdateTime = @updatetime, UpdateUser = @updateuser where id = @id";
            int i = await Db.Ado.ExecuteCommandAsync(str, new
            {
                isdel = "1", //是否删除
                updatetime = Db.GetDate(), //更改时间
                updateuser = dic.UpdateUser, //更改人
                id = dic.Id //id
            });
            return i;
        }

        /// <summary>
        /// 编辑字典信息
        /// </summary>
        /// <param name="dic">数据字典实体模型</param>
        /// <returns></returns>
        public async Task<int> ExitDic(SysDictionary dic)
        {
            //获取是否拥有相同字典名称
            if (!string.IsNullOrEmpty(dic.DictName))
            {
                var dicno = Db.Queryable<SysDictionary>().First(a => a.DictName == dic.DictName);
                if (dicno != null)
                {
                    throw new Exception("获取是否拥有相同字典名称");
                }
            }
            else
            {
                throw new Exception("字典名称为空,请核实!");
            }
            string str = "update SysDictionary set DictNo = @dictno, DictName = @dictname, ParentNo = @parentno, Ord = @ord, IsPublic = @ispublic, IsEdit = @isedit, IsAdd = @isadd, UpdateTime = @updatetime, UpdateUser = @updateuser where id = @id";
            int i = await Db.Ado.ExecuteCommandAsync(str, new
            {
                dictno = dic.DictNo, //字典编号
                dictname = dic.DictName, //字典名称
                parentno = dic.ParentNo, //父级字典号
                ord = dic.Ord, //显示顺序
                //level = dic.Level, //层级
                ispublic = dic.IsPublic, //是否公开
                isedit = dic.IsEdit, //允许编辑
                isadd = dic.IsAdd, //允许添加
                updatetime = Db.GetDate(), //更改时间
                updateuser = dic.UpdateUser, //更改人
                id = dic.Id //id
            });
            return i;
        }
    }

    internal class NewClass
    {
        public NewClass()
        {
        }

        public override bool Equals(object obj)
        {
            return obj is NewClass other;
        }

        public override int GetHashCode()
        {
            return 0;
        }
    }
}