BaseTeamService.cs
9.47 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
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.POJO.WebEntity;
using Hh.Mes.Service.Repository;
using NPOI.POIFS.FileSystem;
using NPOI.SS.Formula.Functions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Hh.Mes.Service.Configure
{
/// <summary>
/// 工厂
/// </summary>
public class BaseTeamService : RepositorySqlSugar<base_team>
{
/// <summary>
/// 新增 code = 字典前缀+对应表标识最大值
/// </summary>
/// <returns></returns>
public dynamic Ins(base_team model)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
Context.Insertable(model).ExecuteCommand();
return response;
});
}
/// <summary>
/// 根据主键数组 删除
/// </summary>
public dynamic DelByIds(int[] ids)
{
//var response = new Response();
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var Result = new Response { Message = "" };
var index = 0;
//根据id获取班组编码
var teamCode = Context.Queryable<base_team>().In(x => x.id, ids).Select(x => x.teamCode).ToList();
//查看班组与用户关系表是否存在,存在报错
var Exit = Context.Queryable<base_team_user_rel>().In(x => x.teamCode, teamCode).ToList();
if (Exit != null)
{
if(Exit.Count > 0)
{
Result.Code = 500;
Result.Message += $"teamCode为【{teamCode[0]}】的班组已经分配了用户,不能删除.";
return Result;
}
}
//查看班组与工作中心关系表是否存在,存在报错
var Exit1 = Context.Queryable<base_work_center_team_rel>().In(x => x.teamCode, teamCode).ToList();
if (Exit1 != null)
{
if (Exit1.Count > 0)
{
Result.Code = 500;
Result.Message += $"teamCode为【{teamCode[0]}】的班组已经被分配到了工作中心,不能删除.";
return Result;
}
}
var result = Context.Deleteable<base_team>().Where(x => ids.Contains(x.id)).ExecuteCommand();
if (result <= 0)
{
Result.Code = 300;
return Result.ResponseError(SystemVariable.dataActionError);
}
return Result;
});
}
/// <summary>
/// 更新
/// </summary>
public dynamic Upd(base_team model)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
Context.Updateable(model).ExecuteCommand();
return response;
});
}
/// <summary>
/// 界面数据加载
/// </summary>
public dynamic Load(PageReq pageReq, base_team mode)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var result = new Response<List<base_team>>();
var expression = LinqWhere(mode);
//先组合查询表达式
var query = Context.Queryable<base_team>().Where(expression);
//Exel为ture就不分页,因为导出的话是全部导出
if (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");
}
/// <summary>
/// 数据过滤条件
/// </summary>
public Expression<Func<base_team, bool>> LinqWhere(base_team model)
{
try
{
var exp = Expressionable.Create<base_team>();
if (!string.IsNullOrWhiteSpace(model.teamCode))
{
exp.And(x => x.teamCode.Contains(model.teamCode));
}
if (!string.IsNullOrWhiteSpace(model.otherCode))
{
exp.And(x => x.otherCode.Contains(model.otherCode));
}
if (!string.IsNullOrWhiteSpace(model.teamName))
{
exp.And(x => x.teamName.Contains(model.teamName));
}
return exp.ToExpression();//拼接表达式
}
catch (Exception ex)
{
throw new Exception($"{ex.Message}");
}
}
/// <summary>
/// 查找班组符合条件的用户
/// </summary>
public dynamic GetTeamCodeUserById(string teamCode)
{
var response = new Response();
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var result = base.Context.Queryable<base_team_user_rel>()
.Where(x => x.teamCode == teamCode)
.Select(it => new
{
it.userId
}).ToList();
response.Result = result;
return response;
});
}
/// <summary>
/// 班组分配人员 删除后新增。 1个班组对应多个用户
/// </summary>
/// <param name="teamCode"></param>
/// <param name="userId"></param>
/// <param name="flag">(check选择,uncheck 取消)</param>
public dynamic Assign(string teamCode, string userId, string flag)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
var BaseTeamCode = teamCode.Split(",")[0];
var userIdS = userId.Split(",");
var rels = new List<base_team_user_rel>();
foreach (var t in userIdS)
{
var model = new base_team_user_rel
{
teamCode = BaseTeamCode,
userId = int.Parse(t),
createBy = sysWebUser?.Account,
createTime = DateTime.Now
};
rels.Add(model);
Context.Deleteable<base_team_user_rel>().Where(x => x.teamCode == teamCode && x.userId == model.userId).AddQueue();
}
if (flag.Equals("check")) Context.Insertable(rels).AddQueue();
response.Status = Context.SaveQueues() > 0;
if (!response.Status)
{
response.Message = SystemVariable.dataActionError;
}
return response;
});
}
#region 明细
/// <summary>
/// 查询班组明细
/// </summary>
public Response LoadDesc(PageReq pageReq, base_team_user_rel model)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var test = new Response();
//获取班组已添加的用户ID
var temp = base.Context.Queryable<base_team_user_rel>()
.Where(x => x.teamCode == model.teamCode).Select(it => new {it.userId}).ToList();
List<int> list = new List<int>();
temp.ForEach(x => list.Add(x.userId));
//根据获取到的用户ID查询用户信息
var result = base.Context.Queryable<sys_user>().In(x => x.id, list).ToList();
List<base_team_user> userList = new List<base_team_user>();
result.ForEach(x => {
base_team_user user = new base_team_user();
user.teamCode = model.teamCode;
user.userId = x.id;
user.userName = x.name;
userList.Add(user);
});
test.Result = userList;
return test;
}, catchRetrunValue: "list");
}
#endregion
/// <summary>
/// 数据导出
/// </summary>
public dynamic ExportData(base_team entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
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;
}, catchRetrunValue: "list");
}
}
}