SysDictDataService.cs 4.6 KB
using Hh.Mes.Common.Infrastructure;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Hh.Mes.Service.WebService.Base
{
    /// <summary>
    /// 字典集合
    /// </summary>
    public class SysDictDataService : RepositorySqlSugar<SysDictData>
    {
        public dynamic Load(PageReq pageReq, SysDictData entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response<List<SysDictData>>();
                var expression = LinqWhere(entity);
                var linqOrder = LinqOrder(pageReq, entity);
                var orderByType = pageReq.order == "desc" ? OrderByType.Desc : OrderByType.Asc;
                //先组合查询表达式
                var query = Context.Queryable<SysDictData>().Where(expression).OrderBy(linqOrder, orderByType);
                //Exel为ture就不分页,因为导出的话是全部导出
                if (!entity.Exel && pageReq != null)
                {
                    int total = 0;
                    result.Result = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
                    result.Count = total;
                }
                else
                {
                    result.Result = query.ToList();
                    result.Count = result.Result.Count();
                }
                return result;
            }, catchRetrunValue: "list");
        }

        public dynamic GetDictDataList(Expression<Func<SysDictData, bool>> linqWhere,
                                                             Expression<Func<SysDictData, object>> linqOrder,
                                                             OrderByType orderByType = OrderByType.Asc,
                                                             int takeCount = 0)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response<List<SysDictData>>();
                var query = Context.Queryable<SysDictData>().Where(linqWhere).OrderBy(linqOrder, orderByType);
                if (takeCount > 0) query.Take(takeCount);
                result.Result = query.ToList();
                result.Count = result.Result.Count();
                return result;
            }, catchRetrunValue: "list");
        }

        public dynamic Ins(SysDictData entity)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = Add(entity);
                if (!result) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic Upd(SysDictData entity)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var resultCount = Context.Updateable(entity)
                                         .IgnoreColumns(it => new { it.CreateBy, it.CreateTime })
                                         .ExecuteCommand();
                response.Status = resultCount > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic DelByIds(int[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var resultCount = Context.Deleteable<SysDictData>().Where(t => ids.Contains(t.Id)).ExecuteCommand();
                response.Status = resultCount > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public Expression<Func<SysDictData, bool>> LinqWhere(SysDictData model)
        {
            try
            {
                var exp = Expressionable.Create<SysDictData>();
                //if (!string.IsNullOrWhiteSpace(model.DictName)) exp.And(x => x.DictName.Contains(model.DictName));
                if (!string.IsNullOrWhiteSpace(model.DictType)) exp.And(x => x.DictType.Contains(model.DictType));
                if (!string.IsNullOrWhiteSpace(model.Remark)) exp.And(x => x.Remark.Contains(model.Remark));
                return exp.ToExpression();//拼接表达式
            }
            catch (Exception ex)
            {
                throw new Exception($"{ex.Message}");
            }
        }
    }
}