Blame view

sys/Hh.Mes.Service/WebService/Configure/factoryService.cs 9.6 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.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.Data;
11
using System.Linq;
唐召明 authored
12
using System.Linq.Expressions;
赖素文 authored
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

namespace Hh.Mes.Service.Configure
{
    /// <summary>
    /// 工厂
    /// </summary>
    public class FactoryService : RepositorySqlSugar<base_factory>
    {
        /// <summary>
        /// 新增  code = 字典前缀+对应表标识最大值
        /// </summary>
        /// <returns></returns>
        public dynamic Ins(base_factory model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
30
                model.keys = Guid.NewGuid();
赖素文 authored
31
32
                model.createBy = base.sysWebUser?.Account;
                model.createTime = DateTime.Now;
33
34
35
                //var prefix = base.GetDictionaryDictValue("base_factory");
                //var maxValue = GetTableMaxValue<base_factory>("id");
                //model.factoryCode = prefix + maxValue;
唐召明 authored
36
                response.Status = Add(model);
赖素文 authored
37
38
39
40
41
42
43
44
                if (!response.Status) response.Message = "新增失败";
                return response;
            });
        }

        /// <summary>
        /// 根据主键数组 删除
        /// </summary>
HuXiYu authored
45
        public dynamic DelByIds(string[] ids)
赖素文 authored
46
47
48
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
唐召明 authored
49
                var Result = new Response { Message = "" };
赖素文 authored
50
51
52
                var index = 0;
                foreach (var id in ids)
                {
唐召明 authored
53
                    var isExist = Context.Queryable<base_equipment>().Any(u => u.factoryCode == id);//是否存在
赖素文 authored
54
55
56
57
58
                    if (isExist)
                    {
                        index++;
                        //提示
                        Result.Code = 500;
HuXiYu authored
59
                        Result.Message += $"keys为【{id}】的在设备表里面存在数据不能删除!!!";
赖素文 authored
60
61
62
                    }
                }
                if (index > 0) return Result;
唐召明 authored
63
                var result = Context.Deleteable<base_factory>().In(x => x.factoryCode, ids).ExecuteCommand() > 0;
赖素文 authored
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
                if (!result)
                {
                    Result.Code = 300;
                    Result.Message = SystemVariable.dataActionError;
                }
                return Result;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(base_factory model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                response.Status = Update(model);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }
唐召明 authored
87
        #region 旧代码
赖素文 authored
88
唐召明 authored
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
        //public Task<DataSet> Load(PageReq pageReq, base_factory mode)
        //{
        //    string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " id desc" : $"{pageReq.field} {pageReq.order} ";
        //    string sqlWhere = LoadSqlWhere(mode);
        //    var stringBuilder = new StringBuilder();
        //    //页码,页数
        //    //Exel ture 不分页
        //    if (!mode.Exel && pageReq != null)
        //    {
        //        stringBuilder.Append("declare @pageIndex int,@pageSize int,@offset int");
        //        stringBuilder.AppendLine($"  select @pageIndex={pageReq.page}, @pageSize={pageReq.limit}, @offset=(@pageIndex - 1) * @pageSize");
        //    }

        //    stringBuilder.AppendLine($@" select t1.*,t2.userAccount from base_factory t1 
        //                                 left join sys_user_project_rel t2 on t1.projectKeys=t2.clientKeys 
        //                                 where {sqlWhere}
        //                                 order by {orderBy} ");

        //    //Exel false 分页
        //    if (!mode.Exel)
        //    {
        //        stringBuilder.AppendLine("  offset @offset row fetch next @pageSize row only ");
        //        stringBuilder.Append($@" select rowTotal= count(*) from base_factory  t1  with(nolock)  
        //                                 left join sys_user_project_rel t2 on t1.projectKeys=t2.clientKeys  
        //                                 where {sqlWhere}");
        //    }
        //    return base.Context.Ado.GetDataSetAllAsync(stringBuilder.ToString(), new List<SugarParameter>(){
        //        new SugarParameter("@factoryCode", $"%{mode.factoryCode}%"),
        //        new SugarParameter("@factoryName", $"%{mode.factoryName}%"),
        //        new SugarParameter("@factorySelectWhere", $"{mode.factorySelectWhere}"),
        //    });
        //}

        //private string LoadSqlWhere(base_factory mode)
        //{
        //    var stringBuilder = new StringBuilder();
        //    stringBuilder.Append(" 1=1 ");
        //    if (!string.IsNullOrEmpty(mode.factoryCode))
        //    {
        //        stringBuilder.Append(" and  t1.factoryCode like  @factoryCode");
        //    }
        //    if (!string.IsNullOrEmpty(mode.factoryName))
        //    {
        //        stringBuilder.Append(" and  t1.factoryName like  @factoryName");
        //    }
        //    if (!string.IsNullOrEmpty(mode.projectKeysString))
        //    {
        //        var projectKeysList = mode.projectKeysString.Split(',', StringSplitOptions.RemoveEmptyEntries);
        //        var projectKeys = string.Join("','", projectKeysList.ToArray());
        //        stringBuilder.AppendFormat(" and t1.projectKeys in ('{0}')", projectKeys);

        //    }
        //    if (!string.IsNullOrEmpty(mode.factorySelectWhere))
        //    {
        //        stringBuilder.Append(" and  t1.projectKeys =  @factorySelectWhere");
        //    }
        //    //非管理员,查询登陆用户绑定 信息
        //    string user = sysWebUser.Account;
        //    if (user != SystemVariable.DefaultCreated)
        //    {
        //        stringBuilder.Append($" and  t2.userAccount ='{user}'");
        //    }
        //    return stringBuilder.ToString();
        //}

        #endregion

        public Response<List<base_factory>> Load(PageReq pageReq, base_factory mode)
赖素文 authored
157
        {
唐召明 authored
158
159
            var response = new Response<List<base_factory>>();
            try
赖素文 authored
160
            {
-  
唐召明 authored
161
                var query = Context.Queryable<base_factory>().Where(LinqWhere(mode));
唐召明 authored
162
163
164
165
166
167
168
169
170
171
172
173
174
                var count = query.Count();
                var data = new List<base_factory>();
                if (!mode.Exel)
                {
                    data = query.ToList();
                }
                else
                {
                    data = query.ToPageList(pageReq.page, pageReq.limit).ToList();
                }
                response.Count = count;
                response.Result = data;
                return response;
赖素文 authored
175
唐召明 authored
176
177
            }
            catch (Exception ex)
赖素文 authored
178
            {
唐召明 authored
179
180
181
                response.Status = false;
                response.Message = ex.Message;
                return response;
赖素文 authored
182
183
184
            }
        }
-  
唐召明 authored
185
        private Expression<Func<base_factory, bool>> LinqWhere(base_factory model)
赖素文 authored
186
        {
唐召明 authored
187
188
            var exp = Expressionable.Create<base_factory>();
            if (!string.IsNullOrWhiteSpace(model.factoryCode))
赖素文 authored
189
            {
唐召明 authored
190
                exp.And(x => x.factoryCode.Contains(model.factoryCode));
赖素文 authored
191
            }
唐召明 authored
192
            if (!string.IsNullOrWhiteSpace(model.factoryName))
赖素文 authored
193
            {
唐召明 authored
194
                exp.And(x => x.factoryName.Contains(model.factoryName));
赖素文 authored
195
            }
唐召明 authored
196
197

            if (!string.IsNullOrWhiteSpace(model.projectKeysString))
198
            {
唐召明 authored
199
200
201
202
203
204
205
                var projectKeysList = model.projectKeysString.Split(',', StringSplitOptions.RemoveEmptyEntries);
                var projectKeysGuidList = projectKeysList.Select(x =>
                {
                    Guid.TryParse(x, out var guid);
                    return guid;
                }).Where(x => x != Guid.Empty).Distinct().ToList();
                exp.And(x => projectKeysGuidList.Contains(x.projectKeys));
206
            }
唐召明 authored
207
208

            if (!string.IsNullOrEmpty(model.factorySelectWhere))
209
            {
唐召明 authored
210
211
                Guid.TryParse(model.factorySelectWhere, out var guid);
                exp.And(x => x.projectKeys == guid);
212
            }
唐召明 authored
213
214
            //非管理员,查询登陆用户绑定 信息
-  
唐召明 authored
215
            string user = sysWebUser.Account;
216
217
            if (user != SystemVariable.DefaultCreated)
            {
-  
唐召明 authored
218
                var clientKeys = Context.Queryable<sys_user_project_rel>().Where(x => x.userAccount == user).Select(x => x.clientKeys).Distinct().ToList();
唐召明 authored
219
                exp.And(x => clientKeys.Contains(x.projectKeys));
220
            }
赖素文 authored
221
唐召明 authored
222
223
            return exp.ToExpression();//拼接表达式
        }
赖素文 authored
224
225
226
227
228
229
230
231

        /// <summary>
        /// 导出 
        /// </summary>
        public dynamic ExportData(base_factory entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
唐召明 authored
232
233
234
235
236
237
238
239
240
241
242
243
244
245
                //var result = new Response();
                //var ds = Load(null, entity);
                //if (ds == null || ds.Result.Tables[0].Rows.Count == 0)
                //{
                //    result.Result = "[]";
                //    result.Count = 0;
                //}
                //else
                //{
                //    result.Result = ds.Result.Tables[0];
                //    result.Count = result.Result.Rows.Count;
                //}
                //return result;
                var result = Load(null, entity);
赖素文 authored
246
247
248
249
250
                return result;
            }, catchRetrunValue: "list");
        }
    }
}