基础字典管理功能开发-API接口设计


项目中Controllers文件夹中 新建接口控制器

添加列表查询接口

/// <summary>
/// 数据字典查询
/// </summary>
/// <param name="dictType">字典类型</param>
/// <returns></returns>
[HttpGet]
public GZAPIResultArray<data_CommonDict> List(string dictType)
{
    var data = entitiesQuery.data_CommonDict
        .Where(w => w.DictType == dictType)
        .OrderBy(o => o.Sort).ToList();

    return GZAPISuccessArray(data);
}
GarsonZhang www.yesdotnet.com

该接口中使用 entitiesQuery ,会自动按照账套编号过滤数据(如果不同账套指向同一数据库的话)

新增接口

这里DictID使用流水单号生成,

流水单号生成规则按照不同类型自增4位流水号:

效果示例:

业务来源,字典类型标识为1,流水号规则是:10001, 10002, 10003, 10004……

客户类型,字典类型标识为2,流水号规则是:20001, 20002, 20003, 20004……

证件类型,字典类型标识为3,流水号规则是:30001, 30002, 30003, 30004……

实现:

项目中添加 YESWEB.DocNoProvider 引用

新建文件夹 DocNos 用于存放流水单号规则

添加基础字典流水单号规则类:SNCommonDict.cs

public class SNCommonDict : YESWEB.DocNoProvider.ModelDocNo
{
    public SNCommonDict()
    {
        this.DocCode = "CommonDict";
        this.DocName = "基础字典编号";
        this.DocHeader = "";
        this.Length = 4;
        this.DocType = YESWEB.DocNoProvider.GenerateDocSNRule.Custom;
    }
}
GarsonZhang www.yesdotnet.com

 

新建接口实现

[HttpPost]
public GZAPIResultEntity<data_CommonDict> Create(P_Create data)
{
    using (var trans = entities.Database.BeginTransaction())
    {
        try
        {
            Enum_DictType dictType = (Enum_DictType)Enum.Parse(typeof(Enum_DictType), data.DictType);

            string sn = YESWEB.DocNoProvider.DocNoHelper.Intance.getSN<DocNos.SNCommonDict>(trans.GetSqlTransaction(), obj =>
            {
                obj.CustomerSeed = obj.DocCode + "_" + dictType.GetHashCode().ToString();
                obj.DocHeader = dictType.GetHashCode().ToString();
            });

            data_CommonDict model = new data_CommonDict()
            {
                DictType = data.DictType,
                DictID = sn,
                DictDescription = data.DictDescription
            };
            #region 重新排序
            var list = entitiesQuery.data_CommonDict.OrderBy(o => o.Sort).ToList();
            if (data.Sort == 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].Sort != i + 1)
                    {
                        list[i].Sort = i + 1;
                    }
                }
                model.Sort = list.Count + 1;
            }
            else
            {
                // 是否已经插入,插入后,后面的元素Sort集体+1
                bool afterInsert = false;
                for (int i = 0; i < list.Count; i++)
                {
                    // 在这个位置插入
                    if (data.Sort == (i + 1))
                    {
                        model.Sort = (i + 1);
                        afterInsert = true;
                    }
                    else
                    {
                        int index = i + 1;
                        if (afterInsert)
                            index = index + 1;

                        if (list[i].Sort != index)
                        {
                            list[i].Sort = index;
                        }
                    }
                }
                if (afterInsert == false)
                    model.Sort = list.Count + 1;
            }
            #endregion 重新排序
            entities.data_CommonDict.Add(model);
            entities.SaveChanges();
            trans.Commit();
            return GZAPISuccessEntity(model);
        }
        catch (Exception ex)
        {
            trans.Rollback();
            return GZAPIExceptionEntity<data_CommonDict>(ex);
        }
    }
}
GarsonZhang www.yesdotnet.com

接口稍微复杂了一点,主要是加入了两个要点

1. 自定义生成流水号

2. 更新字典的排序字段

 

更新数据接口

/// <summary>
/// 更新数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
[HttpPost]
public GZAPIResultEntity<data_CommonDict> Update(P_Update data)
{
    using (var trans = entities.Database.BeginTransaction())
    {
        try
        {
            data_CommonDict model = entitiesQuery.data_CommonDict.Where(w => w.DictType == data.DictType && w.DictID == data.DictID).FirstOrDefault();
            if (model == null)
            {
                return GZAPIBadRequestEntity<data_CommonDict>("数据不存在或已经被删除!");
            }
            if (String.IsNullOrEmpty(data.DictDescription))
            {
                return GZAPIBadRequestEntity<data_CommonDict>("字典描述不能为空!");
            }
            model.DictDescription = data.DictDescription;
            model.Sort = data.Sort == 0 ? 1 : data.Sort;
            entities.SaveChanges();

            #region 重新排序
            var list = entitiesQuery.data_CommonDict
                .Where(w => w.Sort >= data.Sort && w.isid != model.isid)
                .OrderBy(o => o.Sort).ToList();

            for (int i = 0; i < list.Count; i++)
            {
                int index = model.Sort + i + 1;
                if (list[i].Sort != index)
                {
                    list[i].Sort = index;
                }
            }
            entities.SaveChanges();
            #endregion 重新排序
            trans.Commit();
            return GZAPISuccessEntity(model);
        }
        catch (Exception ex)
        {
            trans.Rollback();
            return GZAPIExceptionEntity<data_CommonDict>(ex);
        }
    }
}
GarsonZhang www.yesdotnet.com

 删除数据

/// <summary>
/// 删除字典数据
/// </summary>
/// <param name="dictType"></param>
/// <param name="dictID"></param>
/// <returns></returns>
public GZAPIResult Delete(string dictType, string dictID)
{
    using (var trans = entities.Database.BeginTransaction())
    {
        try
        {
            data_CommonDict model = entitiesQuery.data_CommonDict.Where(w => w.DictType == dictType && w.DictID == dictID).FirstOrDefault();
            if (model == null)
            {
                return GZAPIBadRequest("数据不存在或已经被删除!");
            }
            else
            {
                entities.Entry(model).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
                entities.SaveChanges();

                #region 重新排序
                var list = entitiesQuery.data_CommonDict
                    .Where(w => w.Sort >= model.Sort)
                    .OrderBy(o => o.Sort).ToList();

                for (int i = 0; i < list.Count; i++)
                {
                    int index = model.Sort + i;
                    if (list[i].Sort != index)
                    {
                        list[i].Sort = index;
                    }
                }
                entities.SaveChanges();
                #endregion 重新排序

                trans.Commit();
                return GZAPISuccess();
            }
        }
        catch (Exception ex)
        {
            trans.Rollback();
            return GZAPIException(ex);
        }
    }
}
GarsonZhang www.yesdotnet.com

 

全部代码:

public class CommonDictController : _APIBase
{
    public class R_GetCommonData
    {
        public string DictID { get; set; }
        public string DictDescription { get; set; }
    }
    /// <summary>
    /// 根据类型获得字典数据,给Select绑定数据源使用
    /// </summary>
    /// <param name="dictType"></param>
    /// <returns></returns>
    [YESWEB.ServerLibs.Attributes.APIAuthorize(ServerLibs.Attributes.APIAuthorizeAttribute.RequestType.login)]
    [HttpGet]
    public GZAPIResultArray<R_GetCommonData> GetCommonData(string dictType)
    {
        var data = entitiesQuery.data_CommonDict.Where(w => w.DictType == dictType)
            .OrderBy(o => o.Sort).Select(s => new R_GetCommonData()
            {
                DictID = s.DictID,
                DictDescription = s.DictDescription
            }).ToList();
        return GZAPISuccessArray(data);
    }


    /// <summary>
    /// 获得数据字典类型列表
    /// </summary>
    [HttpGet]
    public GZAPIResultArray<EnumItemObject> GetDictTypeData()
    {
        List<EnumItemObject> data = EnumHelper.ConvertToDict<Enum_DictType>();
        data.Sort((a, b) => a.Value.CompareTo(b.Value));
        return GZAPISuccessArray(data);
    }

    /// <summary>
    /// 数据字典查询
    /// </summary>
    /// <param name="dictType">字典类型</param>
    /// <returns></returns>
    [HttpGet]
    public GZAPIResultArray<data_CommonDict> List(string dictType)
    {
        var data = entitiesQuery.data_CommonDict
            .Where(w => w.DictType == dictType)
            .OrderBy(o => o.Sort).ToList();

        return GZAPISuccessArray(data);
    }

    public class P_Create
    {
        public string DictType { get; set; }
        public string DictDescription { get; set; }

        public int Sort { get; set; }
    }




    [HttpPost]
    public GZAPIResultEntity<data_CommonDict> Create(P_Create data)
    {
        if (String.IsNullOrEmpty(data.DictType))
        {
            return GZAPIBadRequestEntity<data_CommonDict>("字典类型不能为空!");
        }
        if (String.IsNullOrEmpty(data.DictDescription))
        {
            return GZAPIBadRequestEntity<data_CommonDict>("字典描述不能为空!");
        }
        Enum_DictType? _dictType = EnumHelper.ConvertFromName<Enum_DictType>(data.DictType);
        if (_dictType == null)
        {
            return GZAPIBadRequestEntity<data_CommonDict>("字段类型未登记!");
        }

        Enum_DictType dictType = _dictType.Value;


        using (var trans = entities.Database.BeginTransaction())
        {
            try
            {

                string sn = YESWEB.DocNoProvider.DocNoHelper.Intance.getSN<DocNos.SNCommonDict>(trans.GetSqlTransaction(), obj =>
                {
                    obj.CustomerSeed = obj.DocCode + "_" + dictType.GetHashCode().ToString();
                    obj.DocHeader = dictType.GetHashCode().ToString();
                });

                data_CommonDict model = new data_CommonDict()
                {
                    DictType = data.DictType,
                    DictID = sn,
                    DictDescription = data.DictDescription
                };
                #region 重新排序
                var list = entitiesQuery.data_CommonDict.OrderBy(o => o.Sort).ToList();
                if (data.Sort == 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list[i].Sort != i + 1)
                        {
                            list[i].Sort = i + 1;
                        }
                    }
                    model.Sort = list.Count + 1;
                }
                else
                {
                    // 是否已经插入,插入后,后面的元素Sort集体+1
                    bool afterInsert = false;
                    for (int i = 0; i < list.Count; i++)
                    {
                        // 在这个位置插入
                        if (data.Sort == (i + 1))
                        {
                            model.Sort = (i + 1);
                            afterInsert = true;
                        }
                        else
                        {
                            int index = i + 1;
                            if (afterInsert)
                                index = index + 1;

                            if (list[i].Sort != index)
                            {
                                list[i].Sort = index;
                            }
                        }
                    }
                    if (afterInsert == false)
                        model.Sort = list.Count + 1;
                }
                #endregion 重新排序
                entities.data_CommonDict.Add(model);
                entities.SaveChanges();
                trans.Commit();
                return GZAPISuccessEntity(model);
            }
            catch (Exception ex)
            {
                trans.Rollback();
                return GZAPIExceptionEntity<data_CommonDict>(ex);
            }
        }
    }

    public enum Enum_DictType
    {
        业务来源 = 1,
        客户类型 = 2,
        申请人类型 = 3,
        国籍 = 4,
        证件类型 = 5

    }

    public class P_Update : P_Create
    {
        public string DictID { get; set; }
    }

    /// <summary>
    /// 更新字典数据
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost]
    public GZAPIResultEntity<data_CommonDict> Update(P_Update data)
    {
        using (var trans = entities.Database.BeginTransaction())
        {
            try
            {
                data_CommonDict model = entitiesQuery.data_CommonDict.Where(w => w.DictType == data.DictType && w.DictID == data.DictID).FirstOrDefault();
                if (model == null)
                {
                    return GZAPIBadRequestEntity<data_CommonDict>("数据不存在或已经被删除!");
                }
                if (String.IsNullOrEmpty(data.DictDescription))
                {
                    return GZAPIBadRequestEntity<data_CommonDict>("字典描述不能为空!");
                }
                model.DictDescription = data.DictDescription;
                model.Sort = data.Sort == 0 ? 1 : data.Sort;
                entities.SaveChanges();

                #region 重新排序
                var list = entitiesQuery.data_CommonDict
                    .Where(w => w.Sort >= data.Sort && w.isid != model.isid)
                    .OrderBy(o => o.Sort).ToList();

                for (int i = 0; i < list.Count; i++)
                {
                    int index = model.Sort + i + 1;
                    if (list[i].Sort != index)
                    {
                        list[i].Sort = index;
                    }
                }
                entities.SaveChanges();
                #endregion 重新排序
                trans.Commit();
                return GZAPISuccessEntity(model);
            }
            catch (Exception ex)
            {
                trans.Rollback();
                return GZAPIExceptionEntity<data_CommonDict>(ex);
            }
        }
    }

    /// <summary>
    /// 删除字典数据
    /// </summary>
    /// <param name="dictType"></param>
    /// <param name="dictID"></param>
    /// <returns></returns>
    public GZAPIResult Delete(string dictType, string dictID)
    {
        using (var trans = entities.Database.BeginTransaction())
        {
            try
            {
                data_CommonDict model = entitiesQuery.data_CommonDict.Where(w => w.DictType == dictType && w.DictID == dictID).FirstOrDefault();
                if (model == null)
                {
                    return GZAPIBadRequest("数据不存在或已经被删除!");
                }
                else
                {
                    entities.Entry(model).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
                    entities.SaveChanges();

                    #region 重新排序
                    var list = entitiesQuery.data_CommonDict
                        .Where(w => w.Sort >= model.Sort)
                        .OrderBy(o => o.Sort).ToList();

                    for (int i = 0; i < list.Count; i++)
                    {
                        int index = model.Sort + i;
                        if (list[i].Sort != index)
                        {
                            list[i].Sort = index;
                        }
                    }
                    entities.SaveChanges();
                    #endregion 重新排序

                    trans.Commit();
                    return GZAPISuccess();
                }
            }
            catch (Exception ex)
            {
                trans.Rollback();
                return GZAPIException(ex);
            }
        }
    }
}
GarsonZhang www.yesdotnet.com

 

 

 

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
YES开发框架
评论列表

发表评论

评论内容
昵称:
关联文章

基础字典管理功能开发-API接口设计
基础字典管理功能开发-表结构设计,模型更新和数据库迁移脚本生成
API接口安全设计方案
ABP VNext框架基础知识介绍(1)--框架基础类继承关系
系统账套管理功能调整
公共字典扩展
.Net Core 5.x Api开发笔记 -- 基础日志(Log4Net)(八)
使用.NET 6开发TodoList应用(11)——使用FluentValidation和MediatR实现接口请求验证
.Net Minimal API 介绍
使用.NET 6开发TodoList应用(27)——实现API的Swagger文档化
YESWin winform开发框架 模块功能注册配置
Linq to SQL语句基础讲解
使用Hot Chocolate和.NET 6构建GraphQL应用(3) —— 实现Query基础功能
.NET SAAS 架构与设计 -SqlSugar ORM
第三方导入用户API接口
使用.NET 6开发TodoList应用(29)——实现静态字符串本地化功能
使用.NET 6开发TodoList应用(21)——实现API版本控制
YES-WIN winform开发框架报表打印功能开发
添加公共字典
公共数据字典选择组件SelectCommonDict