Blame view

sys/Hh.Mes.Service/WebService/Configure/DaqClientStatusService.cs 10 KB
赖素文 authored
1
2
3
4
5
6
7
8
9
10
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Linq;
using System.Linq.Expressions;
using Hh.Mes.Pojo.System;
唐召明 authored
11
using Hh.Mes.Service.SystemAuth;
12
using System.Collections.Generic;
13
using Hh.Mes.POJO.WebEntity.configure;
赖素文 authored
14
15
16
17
18
19

namespace Hh.Mes.Service.Configure
{
    /// <summary>
    /// 客户端状态
    /// </summary>
20
    public class DaqClientStatusService : RepositorySqlSugar<daq_client_config>
赖素文 authored
21
    {
唐召明 authored
22
23
        public DaqClientStatusService(IAuth auth) : base(auth) { }
24
        public dynamic Load(PageReq pageReq, daq_client_config entity)
赖素文 authored
25
26
27
28
29
30
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var expression = LinqWhere(entity);
                //先组合查询表达式(多表查询查看IOT 设备列表案例)
31
                var query = Context.Queryable<daq_client_config, base_project>((c, p) => new JoinQueryInfos(JoinType.Left, c.projectCode == p.projectCode)).Where(expression).OrderBy((c, p) => c.projectCode);
赖素文 authored
32
33
                //Exel false 分页
                if (!entity.Exel)
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
                {
                    int total = 0;
                    result.Result = query.Select((c, p) => new
                    {
                        c.id,
                        c.name,
                        c.type,
                        c.projectCode,
                        p.projectName,
                        c.lastSeenDate,
                        c.factoryCode,
                        factoryName = SqlFunc.Subqueryable<base_factory>().Where(s => s.projectKeys == p.keys && s.factoryCode == c.factoryCode).Select(s => s.factoryName),
                        c.remark,
                    }).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 LoadStatus(PageReq pageReq, daq_client_config entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
唐召明 authored
64
                var expression = StatusLinqWhere(entity);
65
66
67
68
                //先组合查询表达式(多表查询查看IOT 设备列表案例)
                var query = Context.Queryable<daq_client_config, base_project>((c, p) => new JoinQueryInfos(JoinType.Left, c.projectCode == p.projectCode)).Where(expression).OrderBy((c, p) => c.lastSeenDate);
                //Exel false 分页
                if (!entity.Exel)
赖素文 authored
69
70
                {
                    int total = 0;
71
72
73
                    result.Result = query.Select((c, p) => new
                    {
                        c.id,
74
75
                        c.name,
                        c.type,
76
77
78
                        c.projectCode,
                        p.projectName,
                        c.lastSeenDate,
79
80
                        c.factoryCode,
                        factoryName = SqlFunc.Subqueryable<base_factory>().Where(s => s.projectKeys == p.keys && s.factoryCode == c.factoryCode).Select(s => s.factoryName),
81
82
                        c.remark,
                    }).ToOffsetPage(pageReq.page, pageReq.limit, ref total);
赖素文 authored
83
84
85
86
                    result.Count = total;
                }
                else
                {
87
                    result.Result = query.ToList();
赖素文 authored
88
89
90
91
92
                    result.Count = result.Result.Count;
                }
                return result;
            }, catchRetrunValue: "list");
        }
93
94
        public dynamic Ins(daq_client_config entity)
赖素文 authored
95
96
97
98
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
99
                if (string.IsNullOrWhiteSpace(entity.name))
100
101
102
103
104
105
106
107
                {
                    return response.ResponseError($"客户端名称不能为空!");
                }

                if (string.IsNullOrWhiteSpace(entity.projectCode))
                {
                    return response.ResponseError($"项目信息不能为空!");
                }
108
109
                entity.created = DateTime.Now;
                entity.createdBy = sysWebUser?.Account;
110
111
                Context.Insertable(entity).ExecuteCommand();
                return response.ResponseSuccess();
赖素文 authored
112
113
            });
        }
114
115
        public dynamic Upd(daq_client_config entity)
赖素文 authored
116
117
118
119
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
120
                Context.Updateable<daq_client_config>().SetColumns(x => new daq_client_config
121
                {
122
123
                    name = entity.name,
                    type = entity.type,
124
                    projectCode = entity.projectCode,
125
                    factoryCode = entity.factoryCode,
126
127
                    remark = entity.remark
                }).Where(x => x.id == entity.id).ExecuteCommand();
128
                return response.ResponseSuccess();
赖素文 authored
129
130
            });
        }
131
132
        public dynamic DelByIds(Guid[] ids)
赖素文 authored
133
134
135
136
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
137
                Context.Deleteable<daq_client_config>(t => ids.Contains(t.id)).ExecuteCommand();
赖素文 authored
138
139
140
                return response;
            });
        }
141
142
        public Response ExportData(daq_client_config entity)
赖素文 authored
143
144
145
        {
            return Load(null, entity);
        }
146
147
        public Expression<Func<daq_client_config, base_project, bool>> LinqWhere(daq_client_config client)
赖素文 authored
148
        {
149
            var exp = Expressionable.Create<daq_client_config, base_project>();
150
151
            //数据过滤条件
            //if (!string.IsNullOrWhiteSpace(model.XXX)) exp.And(x => x.XXX.Contains(model.XXX));
152
            if (!string.IsNullOrWhiteSpace(client.name))
赖素文 authored
153
            {
154
                exp.And((c, p) => c.name.Contains(client.name));
155
156
157
158
            }
            if (!string.IsNullOrWhiteSpace(client.remark))
            {
                exp.And((c, p) => c.remark.Contains(client.remark));
赖素文 authored
159
            }
160
            if (!string.IsNullOrWhiteSpace(client.projectCode))
赖素文 authored
161
            {
162
                exp.And((c, p) => c.projectCode == client.projectCode);
赖素文 authored
163
            }
164
165
166
167
168
169
170
171

            string user = sysWebUser.Account;
            if (user != SystemVariable.DefaultCreated)
            {
                var projectRoleKeys = GetProjectRoleKeys(user);
                exp.And((c, p) => SqlFunc.Subqueryable<sys_role_projects_rel>().Where(x => projectRoleKeys.Contains(x.project_roles_key) && x.project_key == p.keys).Any());
            }
172
            return exp.ToExpression();//拼接表达式
唐召明 authored
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
        }

        public Expression<Func<daq_client_config, base_project, bool>> StatusLinqWhere(daq_client_config client)
        {
            var exp = Expressionable.Create<daq_client_config, base_project>();
            //数据过滤条件
            //if (!string.IsNullOrWhiteSpace(model.XXX)) exp.And(x => x.XXX.Contains(model.XXX));
            if (!string.IsNullOrWhiteSpace(client.name))
            {
                exp.And((c, p) => c.name.Contains(client.name));
            }
            if (!string.IsNullOrWhiteSpace(client.remark))
            {
                exp.And((c, p) => c.remark.Contains(client.remark));
            }
            if (!string.IsNullOrWhiteSpace(client.projectCode))
            {
                exp.And((c, p) => c.projectCode == client.projectCode);
            }
192
            exp.And((c, p) => SystemVariable.SoftTypes.Contains(c.type));
唐召明 authored
193
194
195
196
197
198
199
200
201

            string user = sysWebUser.Account;
            if (user != SystemVariable.DefaultCreated)
            {
                var projectRoleKeys = GetProjectRoleKeys(user);
                exp.And((c, p) => SqlFunc.Subqueryable<sys_role_projects_rel>().Where(x => projectRoleKeys.Contains(x.project_roles_key) && x.project_key == p.keys).Any());
            }

            return exp.ToExpression();//拼接表达式
赖素文 authored
202
        }
203
204
        public Dictionary<string, string> GetProjectInfo()
205
206
207
208
209
210
211
212
        {
            try
            {
                var projects = Context.Queryable<base_project>().Select(x => new base_project
                {
                    projectCode = x.projectCode,
                    projectName = x.projectName
                }).Distinct().ToList();
213
214
215
216
217
218
219
220
221
222
223
224
225
                return projects.ToDictionary(x => x.projectCode, y => y.projectName);
            }
            catch (Exception)
            {
                return new Dictionary<string, string>();
            }
        }

        public Dictionary<string, string> GetSoftType()
        {
            try
            {
                var dictTypeId = Context.Queryable<sys_dict_type>().Where(x => x.dictType == "softType").Select(x => x.id).First();
唐召明 authored
226
                var softTypes = Context.Queryable<sys_dict_data>().Where(x => x.headerId == dictTypeId).OrderBy(x => x.dictSort).ToList();
227
                return softTypes.ToDictionary(x => x.dictValue, y => y.dictLabel);
228
229
230
            }
            catch (Exception)
            {
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
                return new Dictionary<string, string>();
            }
        }

        public Response<List<base_factory>> GetFactorys(string projectCode)
        {
            var response = new Response<List<base_factory>>();
            try
            {
                var projectKey = Context.Queryable<base_project>().Where(x => x.projectCode == projectCode).Select(x => x.keys).First();
                var factorys = Context.Queryable<base_factory>().Where(x => x.projectKeys == projectKey).Select(x => new base_factory
                {
                    factoryCode = x.factoryCode,
                    factoryName = x.factoryName
                }).ToList();
                response.Result = factorys;
                response.Status = true;
            }
            catch (Exception ex)
            {
                response.ResponseErr(ex.Message);
252
            }
253
            return response;
254
        }
赖素文 authored
255
256
    }
}