Blame view

sys/Hh.Mes.Service/WmsService/WmsSnPartService.cs 7.9 KB
1
2
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
3
using Hh.Mes.POJO.Entity;
4
5
6
7
8
9
10
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.WMSEntity;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
11
12
using System.Text;
using System.Linq;
唐召明 authored
13
using NPOI.SS.Formula.Functions;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

namespace Hh.Mes.Service.WmsService
{
    public class WmsSnPartService : RepositorySqlSugar<Sn>
    {
        public Response<List<SnPartDetailHistory>> GetSnPartDetailHistory(string snCode)
        {
            var result = new Response<List<SnPartDetailHistory>>();
            try
            {
                result.Status = true;
                result.Result = DimsContextWms.Queryable<SnPartDetailHistory>().Where(x => x.snCode == snCode).OrderBy(x => x.created).ToList();
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Status = false;
                result.Message = ex.Message;
            }
            return result;
        }
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
        public dynamic GetTreeList()
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var stringBuilder = new StringBuilder();
                var nodes = new List<dynamic>();
                var rootNode = new
                {
                    id = Guid.NewGuid(),
                    name = "根节点",
                    keys = "r-1",
                    parentId = "0",
                    isok = false,
                    projectKeys = Guid.Empty,
                };

                var codes = DimsContextWms.Queryable<Sn>().Where(LinqWhere(new Sn())).OrderBy(x => x.correlatedCode).Select(x => x.correlatedCode).Distinct().ToList();
唐召明 authored
54
                var projectCodes = Context.Queryable<base_project>().Where(x => codes.Contains(x.projectCode)).Select(x => x.projectCode).Distinct().ToList();
唐召明 authored
55
56
57
58
59
60
61
62
63
64
65
66

                var tempNode1 = new
                {
                    id = Guid.NewGuid().ToString("N"),
                    name = "IOT已使用",
                    keys = "r-2",
                    parentId = rootNode.keys,
                    isok = false,
                };

                //已使用
                var temp1 = codes.Where(code => projectCodes.Contains(code)).Select(code => new
67
68
                {
                    id = Guid.NewGuid().ToString("N"),
唐召明 authored
69
                    name = code,
70
                    keys = code,
唐召明 authored
71
72
73
74
75
76
77
78
79
                    parentId = tempNode1.keys,
                    isok = true,
                });

                var tempNode2 = new
                {
                    id = Guid.NewGuid().ToString("N"),
                    name = "IOT未使用",
                    keys = "r-3",
80
                    parentId = rootNode.keys,
唐召明 authored
81
82
83
84
85
86
87
88
89
                    isok = false,
                };
                //未使用
                var temp2 = codes.Where(code => !projectCodes.Contains(code)).Select(code => new
                {
                    id = Guid.NewGuid().ToString("N"),
                    name = code,
                    keys = code,
                    parentId = tempNode2.keys,
90
91
92
93
94
                    isok = true,
                    //projectKeys = x.keys
                }).ToList();

                nodes.Add(rootNode);
唐召明 authored
95
96
97
98
                nodes.Add(tempNode1);
                nodes.Add(tempNode2);
                nodes.AddRange(temp1);
                nodes.AddRange(temp2);
99
100
101
102
                return nodes;
            });
        }
103
104
105
106
107
108
109
        public dynamic Load(PageReq pageReq, Sn entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var expression = LinqWhere(entity);
                //先组合查询表达式(多表查询查看IOT 设备列表案例)
110
111
112
113
114
                var query = DimsContextWms.Queryable<Sn>().Where(expression).Select(x => new
                {
                    x.id,
                    x.code,
                    x.correlatedCode,
唐召明 authored
115
116
                    operation = SqlFunc.Subqueryable<SnPartDetailHistory>().Where(s => s.snCode == x.code).OrderByDesc(s => s.created).Select(s => s.operation),
                    progress = SqlFunc.Subqueryable<SnPartDetailHistory>().Where(s => s.snCode == x.code).DistinctCount(s => s.operation) * 100 / 8,
117
118
119
                    x.created,
                    x.createdBy,
                });
120
121

                int total = 0;
唐召明 authored
122
123
124
                var data = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
                var equipmentCodes = Context.Queryable<base_equipment>().Select(x => x.equipmentCode).Distinct().ToList();
唐召明 authored
125
126
127
128
129
130
131
132
                var createds = data.Select(x => x.createdBy).Distinct().ToList();
                var users = Context.Queryable<sys_user>().Where(x => createds.Contains(x.account)).Select(x => new sys_user
                {
                    id = x.id,
                    account = x.account,
                    name = x.name
                }).ToList();
唐召明 authored
133
134
135
136
137
138
                result.Result = data.Select(x => new
                {
                    x.id,
                    x.code,
                    x.correlatedCode,
                    snStatus = equipmentCodes.Contains(x.code),//SN使用状态
唐召明 authored
139
                    x.operation,
唐召明 authored
140
141
142
                    x.progress,
                    x.created,
                    x.createdBy,
唐召明 authored
143
                    userName = users.Where(s => s.account == x.createdBy).Select(s => s.name).FirstOrDefault(),
唐召明 authored
144
                }).ToList();
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
                result.Count = total;
                return result;
            }, catchRetrunValue: "list");
        }

        public dynamic LoadDesc(PageReq pageReq, SnPartDetail entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var expression = LinqWhereDetail(entity);
                //先组合查询表达式(多表查询查看IOT 设备列表案例)
                var query = DimsContextWms.Queryable<SnPartDetail>().Where(expression);
                int total = 0;
                result.Result = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
                result.Count = total;
                return result;
            }, catchRetrunValue: "list");
        }

        private Expression<Func<Sn, bool>> LinqWhere(Sn entity)
        {
            var exp = Expressionable.Create<Sn>();
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
            exp.And(x => x.created >= new DateTime(2024, 11, 1));
            exp.And(x => !string.IsNullOrEmpty(x.correlatedCode));
            var filterCodes = new List<string>
            {
                "/",
                "测试",
                "test",
                "ceshi",
            };
            //过滤掉以数字开头的数据
            for (int i = 0; i < 10; i++)
            {
                filterCodes.Add($"{i}");
            }

            foreach (var item in filterCodes)
            {
                exp.And(x => !x.correlatedCode.StartsWith(item));
            }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
            if (!string.IsNullOrWhiteSpace(entity.code))
            {
                exp.And(x => x.code.Contains(entity.code));
            }
            if (!string.IsNullOrWhiteSpace(entity.correlatedCode))
            {
                exp.And(x => x.correlatedCode.Contains(entity.correlatedCode));
            }
            if (entity.syncIot >= 0)
            {
                exp.And(x => x.syncIot == entity.syncIot);
            }

            return exp.ToExpression();//拼接表达式
        }

        private Expression<Func<SnPartDetail, bool>> LinqWhereDetail(SnPartDetail entity)
        {
            var exp = Expressionable.Create<SnPartDetail>();
            if (entity.snId != default)
            {
                exp.And(x => x.snId == entity.snId);
            }
            if (!string.IsNullOrWhiteSpace(entity.snCode))
            {
                exp.And(x => x.snCode.Contains(entity.snCode));
            }
            return exp.ToExpression();//拼接表达式
        }
    }
}