WcsDeviceInfoService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using WMS.Core.APPBLL;
  8. using WMS.Info;
  9. using WMS.Util;
  10. namespace WMS.Core.ServiceCore
  11. {
  12. public class GenerateDevice
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. public string Code { get; set; }
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. public string DBCode { get; set; }
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. public int Position { get; set; }
  26. /// <summary>
  27. ///
  28. /// </summary>
  29. public string Prefix { get; set; }
  30. }
  31. public class WcsDeviceInfoService
  32. {
  33. /// <summary>
  34. /// 获取分页数据
  35. /// <summary>
  36. /// <returns></returns>
  37. public IEnumerable<dynamic> GetPageList(Pagination pagination, string queryJson)
  38. {
  39. SqlSugarClient client = null;
  40. try
  41. {
  42. client = WCSDbCore.GetDbCtx();
  43. int count = 0;
  44. var queryParam = queryJson.ToJObject();
  45. //var db = client.Queryable<WcsDeviceEntity>();
  46. //if (!queryParam["keyword"].IsEmpty())
  47. //{
  48. // string kw = queryParam["keyword"].ToString();
  49. // db.Where(ord => ord.Code.Contains(kw));
  50. //}
  51. //if (!queryParam["NAME"].IsEmpty())
  52. //{
  53. // string name = queryParam["NAME"].ToString();
  54. // db.Where(o => o.Name.Contains(name));
  55. //}
  56. if (pagination.sord.ToUpper() != "ASC")
  57. {
  58. pagination.sidx = pagination.sidx + " DESC";
  59. }
  60. if (pagination.sidx.IsEmpty())
  61. {
  62. pagination.sidx = "Code DESC";
  63. }
  64. //var list = db.OrderBy(pagination.sidx).Select<dynamic>(@"*").ToPageList(pagination.page, pagination.rows, ref count);
  65. //pagination.records = count;
  66. var list = client.Queryable<WcsDeviceEntity
  67. , WcsDeviceprotocolEntity
  68. >((a
  69. , a1
  70. ) => new JoinQueryInfos(
  71. JoinType.Left, a1.Devicecode == a.Code
  72. ))
  73. .WhereIF(!queryParam["keyword"].IsEmpty(), a => a.Code.Contains(queryParam["keyword"].ToString()))
  74. .WhereIF(!queryParam["NAME"].IsEmpty(), a => a.Name.Contains(queryParam["NAME"].ToString()))
  75. .WhereIF(!queryParam["DBCode"].IsEmpty(), (a, a1) => a1.DBCODE.Equals(queryParam["DBCode"].ToString()))
  76. //.WhereIF(!string.IsNullOrEmpty(input.a_CODE), a => a.Code.Contains(input.a_CODE))
  77. //.WhereIF(!string.IsNullOrEmpty(input.a1_DATABLOCKCODE), (a, a1) => a1.Datablockcode.Equals(input.a1_DATABLOCKCODE))
  78. .Select((a
  79. , a1
  80. ) => new
  81. {
  82. CODE = a.Code,
  83. NAME = a.Name,
  84. Enabled = SqlFunc.IIF(a.Enabled == true, "开", "关"),
  85. DBCODE = a1.DBCODE,
  86. POSITION = a1.Position,
  87. }).OrderBy(pagination.sidx).ToPageList(pagination.page, pagination.rows, ref count);
  88. pagination.records = count;
  89. return list;
  90. }
  91. catch (Exception ex)
  92. {
  93. throw ex;
  94. }
  95. finally
  96. {
  97. client.Dispose();
  98. }
  99. }
  100. public IEnumerable<dynamic> GetSelectDeviceNameList()
  101. {
  102. var client = WCSDbCore.GetDbCtx();
  103. var db = client.Queryable<WcsDeviceEntity>().Where(o => o.Enabled).Select<dynamic>("CODE as id, NAME as text");
  104. return db.ToList();
  105. }
  106. /// <summary>
  107. /// 获取实体数据
  108. /// <param name="keyValue">主键</param>
  109. /// <summary>
  110. /// <returns></returns>
  111. public WcsDeviceEntity GetEntity(string keyValue)
  112. {
  113. try
  114. {
  115. return WCSDbCore.GetDbCtx().Queryable<WcsDeviceEntity>().Where(it => it.Code == keyValue).First();
  116. }
  117. catch (Exception ex)
  118. {
  119. throw ex;
  120. }
  121. }
  122. /// <summary>
  123. /// 删除实体数据
  124. /// <param name="keyValue">主键</param>
  125. /// <summary>
  126. /// <returns></returns>
  127. public void DeleteEntity(string keyValue)
  128. {
  129. try
  130. {
  131. //SysDbCore.GetDbCtx().Deleteable<BASE_WAREHOUSE>().Where(it => it.F_NO == keyValue).ExecuteCommand();
  132. WCSDbCore.GetDbCtx().Deleteable<WcsDeviceEntity>().Where(it => it.Code == keyValue).ExecuteCommand();
  133. }
  134. catch (Exception ex)
  135. {
  136. throw ex;
  137. }
  138. }
  139. /// <summary>
  140. ///
  141. /// </summary>
  142. /// <param name="loginUserInfo"></param>
  143. /// <param name="keyValue"></param>
  144. /// <param name="entity"></param>
  145. /// <param name="wcsDeviceprotocolEntityList"></param>
  146. public void SaveEntity(LoginUserInfo loginUserInfo, string keyValue, WcsDeviceEntity entity, List<WcsDeviceprotocolEntity> wcsDeviceprotocolEntityList)
  147. {
  148. if (entity == null)
  149. {
  150. throw SysExCore.ThrowFailException("输入数据为空。");
  151. }
  152. if (string.IsNullOrWhiteSpace(entity.Code))
  153. {
  154. throw SysExCore.ThrowFailException("编码为空。");
  155. }
  156. if (string.IsNullOrWhiteSpace(entity.Name))
  157. {
  158. throw SysExCore.ThrowFailException("名称为空。");
  159. }
  160. if (!wcsDeviceprotocolEntityList.Any()) throw SysExCore.ThrowFailException($"明细不能为空!");
  161. entity.Updatetime = DateTime.Now;
  162. entity.Updateuser = loginUserInfo.UserNo;
  163. if (string.IsNullOrEmpty(keyValue))
  164. {
  165. using (var ctx = WCSDbCore.GetDbCtx())
  166. {
  167. try
  168. {
  169. var DeviceQueryable = ctx.Queryable<WcsDeviceEntity>();
  170. var DeviceprotocolQueryable = ctx.Queryable<WcsDeviceprotocolEntity>();
  171. if (DeviceQueryable.Any(x => (x.Code == entity.Code || x.Name == entity.Name) && x.Enabled))
  172. {
  173. throw SysExCore.ThrowFailException("已存在同名同编码数据!");
  174. }
  175. if (wcsDeviceprotocolEntityList != null && wcsDeviceprotocolEntityList.Any())
  176. {
  177. foreach (WcsDeviceprotocolEntity item in wcsDeviceprotocolEntityList)
  178. {
  179. if (DeviceprotocolQueryable.Any(x => x.Devicecode == entity.Code && x.DBCODE == item.DBCODE && x.Enabled))
  180. throw SysExCore.ThrowFailException("设备协议信息DB编号已存在");
  181. }
  182. }
  183. ctx.Ado.BeginTran();
  184. ctx.Insertable(entity).ExecuteCommand();
  185. foreach (WcsDeviceprotocolEntity item in wcsDeviceprotocolEntityList)
  186. {
  187. item.Devicecode = entity.Code;
  188. item.Updatetime = DateTime.Now;
  189. item.Updateuser = loginUserInfo.UserNo;
  190. }
  191. ctx.Insertable(wcsDeviceprotocolEntityList).ExecuteCommand();
  192. ctx.Ado.CommitTran();
  193. }
  194. catch (Exception ex)
  195. {
  196. ctx.Ado.RollbackTran();
  197. throw ex;
  198. }
  199. }
  200. }
  201. else
  202. {
  203. using (var ctx = WCSDbCore.GetDbCtx())
  204. {
  205. try
  206. {
  207. var DeviceQueryable = ctx.Queryable<WcsDeviceEntity>();
  208. var DeviceprotocolQueryable = ctx.Queryable<WcsDeviceprotocolEntity>();
  209. if (DeviceQueryable.Any(x => x.Code != keyValue && (x.Code == entity.Code || x.Name == entity.Name) && x.Enabled))
  210. {
  211. throw SysExCore.ThrowFailException("已存在同名同编码数据!");
  212. }
  213. if (wcsDeviceprotocolEntityList != null && wcsDeviceprotocolEntityList.Any())
  214. {
  215. foreach (WcsDeviceprotocolEntity item in wcsDeviceprotocolEntityList)
  216. {
  217. if (DeviceprotocolQueryable.Any(x => x.Id != item.Id && x.Devicecode == entity.Code && x.DBCODE == item.DBCODE && x.Enabled))
  218. throw SysExCore.ThrowFailException("设备协议信息DB编号已存在");
  219. }
  220. }
  221. ctx.Ado.BeginTran();
  222. ctx.Updateable(entity).ExecuteCommand();
  223. ctx.Deleteable<WcsDeviceprotocolEntity>().Where(it => it.Devicecode == keyValue).ExecuteCommand();
  224. foreach (WcsDeviceprotocolEntity item in wcsDeviceprotocolEntityList)
  225. {
  226. item.Devicecode = entity.Code;
  227. item.Updatetime = DateTime.Now;
  228. item.Updateuser = loginUserInfo.UserNo;
  229. }
  230. ctx.Insertable(wcsDeviceprotocolEntityList).ExecuteCommand();
  231. ctx.Ado.CommitTran();
  232. }
  233. catch (Exception ex)
  234. {
  235. ctx.Ado.RollbackTran();
  236. throw ex;
  237. }
  238. }
  239. }
  240. }
  241. public void SaveGenerateDevice(LoginUserInfo loginUserInfo, GenerateDevice entity)
  242. {
  243. if (entity == null)
  244. {
  245. throw SysExCore.ThrowFailException("输入数据为空。");
  246. }
  247. if (string.IsNullOrWhiteSpace(entity.Code))
  248. {
  249. throw SysExCore.ThrowFailException("请选择PLC。");
  250. }
  251. if (string.IsNullOrWhiteSpace(entity.DBCode))
  252. {
  253. throw SysExCore.ThrowFailException("请选择PLC DB块!");
  254. }
  255. if (entity.Position < 0)
  256. {
  257. throw SysExCore.ThrowFailException("开始值应该大于0的正整数!");
  258. }
  259. entity.Prefix = string.IsNullOrWhiteSpace(entity.Prefix) ? "" : entity.Prefix?.Trim();
  260. var dbcodes = entity.DBCode.Split(',').OrderBy(o => o.ToString()).ToArray();
  261. using (var ctx = WCSDbCore.GetDbCtx())
  262. {
  263. try
  264. {
  265. var DeviceQueryable = ctx.Queryable<WcsDeviceEntity>();
  266. var datablockList = ctx.Queryable<WcsDatablockEntity>().WhereIF(!string.IsNullOrEmpty(entity.Code), a => a.Enabled && a.Plccode.Equals(entity.Code)).Select<WcsDatablockEntity>(@" [CODE],[NAME],[PLCCODE],[NO],[LENGTH] ,[DATALENGTH]").ToList();
  267. List<WcsDeviceprotocolEntity> deviceprotocolList = new List<WcsDeviceprotocolEntity>();
  268. List<string> deviceDic = new List<string>();
  269. var now = DateTime.Now;
  270. for (int i = 0; i < dbcodes.Length; i++)
  271. {
  272. var datablock = datablockList.FirstOrDefault(o => o.Code == dbcodes[i]);
  273. if (datablock != null)
  274. {
  275. var len = datablock.Datalength != 0 ? datablock.Length / datablock.Datalength : 0;
  276. if (len <= 0) continue;
  277. for (int j = 0; j < len; j++)
  278. {
  279. var devicecode = string.IsNullOrEmpty(entity.Prefix) ? Convert.ToString(entity.Position + j)
  280. : entity.Prefix + Convert.ToString(entity.Position + j);
  281. deviceprotocolList.Add(new WcsDeviceprotocolEntity()
  282. {
  283. DBCODE = dbcodes[i],
  284. Devicecode = devicecode,
  285. Position = datablock.Datalength * j,
  286. Enabled = true,
  287. Updateuser = "WCS",
  288. Updatetime = now
  289. });
  290. if (!deviceDic.Contains(devicecode))
  291. {
  292. deviceDic.Add(devicecode);
  293. }
  294. }
  295. }
  296. }
  297. var wcsDeviceList = DeviceQueryable.Select<WcsDeviceEntity>(@" [CODE]").ToList();
  298. var exceptList = deviceDic.Except(wcsDeviceList.Select(o => o.Code).ToList()).ToList();
  299. List<WcsDeviceEntity> deviceslists = new List<WcsDeviceEntity>();
  300. for (int i = 0; i < exceptList.Count(); i++)
  301. {
  302. deviceslists.Add(new WcsDeviceEntity
  303. {
  304. Code = exceptList[i],
  305. Name = exceptList[i],
  306. Enabled = true,
  307. Updateuser = "WCS",
  308. Updatetime = now
  309. });
  310. }
  311. try
  312. {
  313. //开启事务
  314. ctx.Ado.BeginTran();
  315. if (deviceslists.Any())
  316. {
  317. ctx.Insertable(deviceslists).ExecuteCommand();
  318. }
  319. //删除WcsDeviceprotocolEntity
  320. ctx.Deleteable<WcsDeviceprotocolEntity>().Where(it => deviceDic.Contains(it.Devicecode)).ExecuteCommand();
  321. ctx.Insertable(deviceprotocolList).ExecuteCommand();
  322. //关闭事务
  323. ctx.Ado.CommitTran();
  324. }
  325. catch (Exception ex)
  326. {
  327. //回滚事务
  328. ctx.Ado.RollbackTran();
  329. if (ex.Message.Contains("Duplicate"))
  330. {
  331. throw SysExCore.ThrowFailException("已存在同名或同变码数据!");
  332. }
  333. else
  334. {
  335. throw SysExCore.ThrowFailException("新增数据失败!");
  336. }
  337. }
  338. }
  339. catch (Exception ex)
  340. {
  341. throw ex;
  342. }
  343. }
  344. }
  345. }
  346. }