using AutoMapper;
using Model.ModelDto.SysDto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using WMS.Entity.SysEntity;
using WMS.IBLL.ISysServer;
using WMS.IDAL.ISysInterface;
namespace WMS.BLL.SysServer
{
public class DictionaryServer : IDictionaryServer
{
public IDictionaryRepository _dic { get; set; }
private readonly IMapper _mapper;
public DictionaryServer(IDictionaryRepository dic, IMapper mapper)
{
_dic = dic;
_mapper = mapper;
}
///
/// 获取字典表数据(根据父级字典号)
///
/// 父级字典号
///
public List GetDictionaryByParentNo(string parentNo)
{
try
{
var data = _dic.GetAllWhereAsync(m => m.ParentNo == parentNo && m.Level == "1").OrderBy(m => m.Ord).ToList();
return data;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 获取字典信息列表
///
/// 字典名称
/// 父级字典号
/// 层级
/// 允许编辑
/// 允许新增
///
public List GetDicList(string DictName,string DictNo, string Level, string IsEdit, string IsAdd)
{
List diclist = _dic.GetDicList(DictName,DictNo, Level, IsEdit, IsAdd);
return diclist;
}
///
/// 获取父级字典号(根据层级根目录)
///
///
public List GetDicParentListByLevel()
{
List diclist = _dic.GetDicParentListByLevel();
return diclist;
}
///
/// 根据id查询字典信息
///
/// 字典id
///
public SysDictionary GetDicById(int id)
{
SysDictionary dic = _dic.GetDicById(id);
return dic;
}
///
/// 根据编号查询字典信息
///
/// 字典编号
///
public int GetDicByNo(string DictNo)
{
List diclist = _dic.GetDicByNo(DictNo);
return diclist.Count;
}
///
/// 新增字典信息
///
/// 数据字典Dto
///
/// 捕获异常
public async Task AddDic(DictionaryDto dicdto)
{
//捕获异常
try
{
//映射模型
SysDictionary dic = _mapper.Map(dicdto);
//获取是否拥有相同字典编码
int count = GetDicByNo(dic.DictNo);
int i = 0;
if (count > 0)
{
i = 3;
}
else if (count == 0)
{
//如层级为子级时验证父级是否为空
if (dic.Level == "1")
{
if (string.IsNullOrEmpty(dic.ParentNo))
{
throw new Exception("当层级为子级时,父级目录不可为空!");
}
}
//新增
i = await _dic.AddDic(dic);
}
return i;
}
catch (Exception ex)
{
//抛出异常
throw new Exception("新增字典异常", ex);
}
}
///
/// 删除字典信息
///
/// 字典实体模型
///
/// 捕获异常
public async Task DelDic(SysDictionary dic)
{
//捕获异常
try
{
//删除
int i = await _dic.DelDic(dic);
return i;
}
catch (Exception ex)
{
//抛出异常
throw new Exception("删除字典异常", ex);
}
}
///
/// 编辑字典信息
///
/// 数据字典Dto
///
/// 捕获异常
public async Task ExitDic(DictionaryDto dicdto)
{
//捕获异常
try
{
//映射模型
SysDictionary dic = _mapper.Map(dicdto);
//编辑
int i = await _dic.ExitDic(dic);
return i;
}
catch (Exception ex)
{
//抛出异常
throw new Exception("编辑字典异常", ex);
}
}
}
}