wzp
2021-07-19 58ec6ffd2dc6a3e490e28026dd559352678a273d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using FineAdmin.IRepository;
using FineAdmin.IService;
using FineAdmin.Model;
using System.Collections.Generic;
 
namespace FineAdmin.Service
{
    public abstract class BaseService<T> where T : class, new()
    {
        public IBaseRepository<T> BaseRepository { get; set; }
        #region CRUD
        /// <summary>
        /// 根据主键返回实体
        /// </summary>
        public T GetById(int Id)
        {
            return BaseRepository.GetById(Id);
        }
        /// <summary>
        /// 新增
        /// </summary>
        public bool Insert(T model)
        {
            return BaseRepository.Insert(model) > 0 ? true : false;
        }
        /// <summary>
        /// 根据主键修改数据
        /// </summary>
        public bool UpdateById(T model)
        {
            return BaseRepository.UpdateById(model) > 0 ? true : false;
        }
        /// <summary>
        /// 根据主键修改数据 修改指定字段
        /// </summary>
        public bool UpdateById(T model, string updateFields)
        {
            return BaseRepository.UpdateById(model, updateFields) > 0 ? true : false;
        }
        /// <summary>
        /// 根据主键删除数据
        /// </summary>
        public bool DeleteById(int Id)
        {
            return BaseRepository.DeleteById(Id) > 0 ? true : false;
        }
        /// <summary>
        /// 根据主键批量删除数据
        /// </summary>
        public bool DeleteByIds(object Ids)
        {
            return BaseRepository.DeleteByIds(Ids) > 0 ? true : false;
        }
        /// <summary>
        /// 根据条件删除
        /// </summary>
        public bool DeleteByWhere(string where)
        {
            return BaseRepository.DeleteByWhere(where) > 0 ? true : false;
        }
        #endregion
        /// <summary>
        /// 获取分页数据
        /// </summary>
        public dynamic GetListByFilter(T filter, PageInfo pageInfo, string where = null)
        {
            string _orderBy = string.Empty;
            if (!string.IsNullOrEmpty(pageInfo.field))
            {
                _orderBy = string.Format(" ORDER BY {0} {1}", pageInfo.field, pageInfo.order);
            }
            else
            {
                _orderBy = " ORDER BY CreateTime desc";
            }
            int total = 0;
            var list = BaseRepository.GetByPage(new SearchFilter { pageIndex = pageInfo.page, pageSize = pageInfo.limit, returnFields = pageInfo.returnFields, param = filter, where = where, orderBy = _orderBy }, out total);
            return Pager.Paging(list, total);
        }
        /// <summary>
        /// 获取分页数据 联合查询
        /// </summary>
        public dynamic GetPageUnite(T filter, PageInfo pageInfo, string where = null)
        {
            string _orderBy = string.Empty;
            if (!string.IsNullOrEmpty(pageInfo.field))
            {
                _orderBy = string.Format(" ORDER BY {0} {1}", pageInfo.field, pageInfo.order);
            }
            else
            {
                _orderBy = " ORDER BY CreateTime desc";
            }
            int total = 0;
            var list = BaseRepository.GetByPageUnite(new SearchFilter { prefix = pageInfo.prefix, pageIndex = pageInfo.page, pageSize = pageInfo.limit, returnFields = pageInfo.returnFields, param = filter, where = where, orderBy = _orderBy }, out total);
            return Pager.Paging(list, total);
        }
        /// <summary>
        /// 返回整张表数据
        /// returnFields需要返回的列,用逗号隔开。默认null,返回所有列
        /// </summary>
        public IEnumerable<T> GetAll(string returnFields = null, string orderby = null)
        {
            return BaseRepository.GetAll(returnFields, orderby);
        }
        /// <summary>
        /// 创建时间范围条件
        /// </summary>
        protected string CreateTimeWhereStr(string StartEndDate, string _where, string prefix = null)
        {
            if (!string.IsNullOrEmpty(StartEndDate) && StartEndDate != " ~ ")
            {
                if (StartEndDate.Contains("~"))
                {
                    if (StartEndDate.Contains("+"))
                    {
                        StartEndDate = StartEndDate.Replace("+", "");
                    }
                    var dts = StartEndDate.Split('~');
                    var start = dts[0].Trim();
                    var end = dts[1].Trim();
                    if (!string.IsNullOrEmpty(start))
                    {
                        if (!string.IsNullOrEmpty(prefix))
                        {
                            _where += string.Format(" and {1}CreateTime>='{0} 00:00'", start, prefix);
                        }
                        else
                        {
                            _where += string.Format(" and CreateTime>='{0} 00:00'", start);
                        }
                    }
                    if (!string.IsNullOrEmpty(end))
                    {
                        if (!string.IsNullOrEmpty(prefix))
                        {
                            _where += string.Format(" and {1}CreateTime<='{0} 59:59'", end, prefix);
                        }
                        else
                        {
                            _where += string.Format(" and CreateTime<='{0} 59:59'", end);
                        }
                    }
                }
            }
            return _where;
        }
        /// <summary>
        /// 根据查询条件获取数据
        /// </summary>
        public IEnumerable<T> GetByWhere(string where = null, object param = null, string returnFields = null, string orderby = null)
        {
            return BaseRepository.GetByWhere(where, param, returnFields, orderby);
        }
    }
}