InventoryService.cs
9.32 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
using Hh.Mes.Common.config;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hh.Mes.Service.Warehous
{
public class InventoryService : RepositorySqlSugar<base_inventory>
{
public string SelectRequestUrl()
{
var urlDic = "UrlGetInventoriesWithPage" + ConfigRead.GetInstance.GetAppsetConnection().AppCustomExtend1;
var url = GetDictionaryDictValue(urlDic, "GetUrl");
return url;
}
/// <summary>
/// 查询方法
/// </summary>
/// <param name="pageReq">页码</param>
/// <param name="model">传入的参数</param>
/// <returns></returns>
public Response Load(PageReq pageReq, base_inventory model)
{
var result = new Response();
string orderBy = (pageReq == null || string.IsNullOrEmpty(pageReq.field)) ? " id desc" : $"{pageReq.field} {pageReq.order} ";
string sqlWhere = SqlWhere(model);
var stringBuilder = new StringBuilder();
//页码,页数
//Exel ture 不分页
if (!model.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 t5.materialName,t5.diameter,t5.thickness, t1.* from base_inventory t1
left join base_material t5 on t1.materialCode=t5.materialCode
where {sqlWhere} order by {orderBy} ");
//Exel ture 不分页
if (!model.Exel)
{
stringBuilder.AppendLine(" offset @offset row fetch next @pageSize row only ");
stringBuilder.Append($@" select rowTotal= count(*) from base_inventory t1 with(nolock) where {sqlWhere}");
}
var ds = base.Context.Ado.GetDataSetAll(stringBuilder.ToString());
result.Result = ds.Tables[0];
result.Count = model.Exel ? (int)result.Result.Rows.Count : (int)ds.Tables[1].Rows[0]["rowTotal"];
return result;
}
public string SqlWhere(base_inventory model)
{
var stringBuilder = new StringBuilder();
stringBuilder.Append(" 1=1 ");
//stringBuilder.Append($" and t1.useState={(int)InventoryUseState.未套料} and t1.oddments=1 and t1.qty>0 and t5.MaterialCategory='PIPE'");
if (model.id != 0) stringBuilder.Append($" and t1.id=${model.id}");
if (model.lineCode != null) stringBuilder.Append($" and t1.lineCode like '%{model.lineCode}%' ");
if (model.locationCode != null) stringBuilder.Append($" and t1.locationCode like '%{model.locationCode}%' ");
if (model.materialCode != null) stringBuilder.Append($" and t1.materialCode like '%{model.materialCode}%' ");
//if (model.oddments != null) stringBuilder.Append($" and t1.oddments = 1 ");
if (!string.IsNullOrEmpty(model.pipeSN)) stringBuilder.Append($" and t1.pipeSN like '%{model.pipeSN}%'");
if (!string.IsNullOrEmpty(model.zoneCode)) stringBuilder.Append($" and t1.zoneCode ='{model.zoneCode}'");
return stringBuilder.ToString();
}
/// <summary>
/// 库存盘点
/// </summary>
/// <returns></returns>
public dynamic InventoryCheck(List<base_inventory> model)
{
var response = new Response();
return ExceptionsHelp.Instance.ExecuteT(() =>
{
model.ForEach(x =>
{
x.updateBy = sysWebUser?.Account;
x.updateTime = DateTime.Now;
});
#region 提交的数据管材SN是否有重复数据,提交的管材sn是否存在数据库
var fields = model.Select(x => x.pipeSN).Distinct();
var pipeSNArr = fields as string[] ?? fields.ToArray();
var strPipeSn = string.Join(",", pipeSNArr);
if (pipeSNArr.Length != model.Count) return response.ResponseError("库存盘点:提交的数据管材SN存在重复的数据:" + strPipeSn);
//var pipeSnList = base.Context.Queryable<inventory>().Where(it => pipeSNArr.Contains(it.pipeSN)).Select(x => x.pipeSN).ToList();
//if (pipeSnList.Count > 0)
//{
// return response.ResponseError("库存盘点:管材SN数据库已存在重复的SN:" + strPipeSn);
//}
#endregion
var result = base.Context.Updateable(model).UpdateColumns(x => new
{
x.qty,
x.pipeLength,
x.locationCode,
//x.furnaceNo,
//x.lotNo,
x.batchNo
}).ExecuteCommand();
if (result <= 0) response.Message = SystemVariable.dataActionError;
return response;
});
}
/// <summary>
/// 导出
/// </summary>
public dynamic ExportData(base_inventory entity)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var result = new Response();
var ds = Load(null, entity);
if (ds == null || ds.Count == 0)
{
result.Result = "[]";
result.Count = 0;
}
else
{
result.Result = ds.Result;
result.Count = ds.Count;
}
return result;
}, catchRetrunValue: "list");
}
/// <summary>
/// 添加
/// </summary>
/// <param name="model">参数</param>
/// <returns></returns>
public dynamic Ins(base_inventory model)
{
var response = new Response();
return ExceptionsHelp.Instance.ExecuteT(() =>
{
if (model.pipeLength < 0)
{
response.Code = 500;
response.Message = "长度不能小于0.";
return response;
}
var isExt = base.Context.Queryable<base_inventory>().Any(x => x.pipeSN == model.pipeSN);
if (isExt) return response.ResponseError($"库存中已存在相同的管材SN【{model.pipeSN}】不能重复新增!");
if (!string.IsNullOrEmpty(model.oldSN))
{
var isExtOldSn = base.Context.Queryable<base_inventory>().Any(x => x.oldSN == model.oldSN);
if (isExtOldSn) return response.ResponseError($"库存中已存在相同的管材旧SN【{model.oldSN}】不能重复新增!");
}
model.pipeSN = GetNumPwd();
model.createBy = sysWebUser?.Account;
model.createTime = DateTime.Now;
model.useState = (int)InventoryUseState.未套料;
model.status = InventoryStatus.good.ToString();
response.Status = Add(model);
if (!response.Status)
{
response.Message = SystemVariable.dataActionError;
}
return response;
});
}
private static string GetNumPwd()//生成数字随机数
{
string a = "0123456789";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 8; i++)
{
sb.Append(a[new Random(Guid.NewGuid().GetHashCode()).Next(0, a.Length - 1)]);
}
return sb.ToString();
}
/// <summary>
/// 修改
/// </summary>
/// <param name="model">参数</param>
/// <returns></returns>
public dynamic Upd(base_inventory model)
{
var response = new Response();
return ExceptionsHelp.Instance.ExecuteT(() =>
{
if (model.pipeLength<0)
{
response.Code = 500;
response.Message = "长度不能小于0.";
return response;
}
if (model.qty <= 0)
{
response.Code = 500;
response.Message = "数量不能小于0.";
return response;
}
var upModel = base.Context.Queryable<base_inventory>().Where(x => x.id == model.id).First();
upModel.pipeLength = model.pipeLength;
upModel.qty = model.qty;
upModel.updateBy = sysWebUser?.Account;
upModel.updateTime = DateTime.Now;
response.Status = base.Context.Updateable(upModel).ExecuteCommand() > 0;
if (!response.Status)
{
response.Message = SystemVariable.dataActionError;
}
return response;
});
}
}
}