using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WMS.Core.APPBLL; using WMS.Info; using WMS.Util; namespace WMS.Core.ServiceCore { public class GenerateDevice { /// /// /// public string Code { get; set; } /// /// /// public string DBCode { get; set; } /// /// /// public int Position { get; set; } /// /// /// public string Prefix { get; set; } } public class WcsDeviceInfoService { /// /// 获取分页数据 /// /// public IEnumerable GetPageList(Pagination pagination, string queryJson) { SqlSugarClient client = null; try { client = WCSDbCore.GetDbCtx(); int count = 0; var queryParam = queryJson.ToJObject(); //var db = client.Queryable(); //if (!queryParam["keyword"].IsEmpty()) //{ // string kw = queryParam["keyword"].ToString(); // db.Where(ord => ord.Code.Contains(kw)); //} //if (!queryParam["NAME"].IsEmpty()) //{ // string name = queryParam["NAME"].ToString(); // db.Where(o => o.Name.Contains(name)); //} if (pagination.sord.ToUpper() != "ASC") { pagination.sidx = pagination.sidx + " DESC"; } if (pagination.sidx.IsEmpty()) { pagination.sidx = "Code DESC"; } //var list = db.OrderBy(pagination.sidx).Select(@"*").ToPageList(pagination.page, pagination.rows, ref count); //pagination.records = count; var list = client.Queryable((a , a1 ) => new JoinQueryInfos( JoinType.Left, a1.Devicecode == a.Code )) .WhereIF(!queryParam["keyword"].IsEmpty(), a => a.Code.Contains(queryParam["keyword"].ToString())) .WhereIF(!queryParam["NAME"].IsEmpty(), a => a.Name.Contains(queryParam["NAME"].ToString())) .WhereIF(!queryParam["DBCode"].IsEmpty(), (a, a1) => a1.DBCODE.Equals(queryParam["DBCode"].ToString())) //.WhereIF(!string.IsNullOrEmpty(input.a_CODE), a => a.Code.Contains(input.a_CODE)) //.WhereIF(!string.IsNullOrEmpty(input.a1_DATABLOCKCODE), (a, a1) => a1.Datablockcode.Equals(input.a1_DATABLOCKCODE)) .Select((a , a1 ) => new { CODE = a.Code, NAME = a.Name, Enabled = SqlFunc.IIF(a.Enabled == true, "开", "关"), DBCODE = a1.DBCODE, POSITION = a1.Position, }).OrderBy(pagination.sidx).ToPageList(pagination.page, pagination.rows, ref count); pagination.records = count; return list; } catch (Exception ex) { throw ex; } finally { client.Dispose(); } } public IEnumerable GetSelectDeviceNameList() { var client = WCSDbCore.GetDbCtx(); var db = client.Queryable().Where(o => o.Enabled).Select("CODE as id, NAME as text"); return db.ToList(); } /// /// 获取实体数据 /// 主键 /// /// public WcsDeviceEntity GetEntity(string keyValue) { try { return WCSDbCore.GetDbCtx().Queryable().Where(it => it.Code == keyValue).First(); } catch (Exception ex) { throw ex; } } /// /// 删除实体数据 /// 主键 /// /// public void DeleteEntity(string keyValue) { try { //SysDbCore.GetDbCtx().Deleteable().Where(it => it.F_NO == keyValue).ExecuteCommand(); WCSDbCore.GetDbCtx().Deleteable().Where(it => it.Code == keyValue).ExecuteCommand(); } catch (Exception ex) { throw ex; } } /// /// /// /// /// /// /// public void SaveEntity(LoginUserInfo loginUserInfo, string keyValue, WcsDeviceEntity entity, List wcsDeviceprotocolEntityList) { if (entity == null) { throw SysExCore.ThrowFailException("输入数据为空。"); } if (string.IsNullOrWhiteSpace(entity.Code)) { throw SysExCore.ThrowFailException("编码为空。"); } if (string.IsNullOrWhiteSpace(entity.Name)) { throw SysExCore.ThrowFailException("名称为空。"); } if (!wcsDeviceprotocolEntityList.Any()) throw SysExCore.ThrowFailException($"明细不能为空!"); entity.Updatetime = DateTime.Now; entity.Updateuser = loginUserInfo.UserNo; if (string.IsNullOrEmpty(keyValue)) { using (var ctx = WCSDbCore.GetDbCtx()) { try { var DeviceQueryable = ctx.Queryable(); var DeviceprotocolQueryable = ctx.Queryable(); if (DeviceQueryable.Any(x => (x.Code == entity.Code || x.Name == entity.Name) && x.Enabled)) { throw SysExCore.ThrowFailException("已存在同名同编码数据!"); } if (wcsDeviceprotocolEntityList != null && wcsDeviceprotocolEntityList.Any()) { foreach (WcsDeviceprotocolEntity item in wcsDeviceprotocolEntityList) { if (DeviceprotocolQueryable.Any(x => x.Devicecode == entity.Code && x.DBCODE == item.DBCODE && x.Enabled)) throw SysExCore.ThrowFailException("设备协议信息DB编号已存在"); } } ctx.Ado.BeginTran(); ctx.Insertable(entity).ExecuteCommand(); foreach (WcsDeviceprotocolEntity item in wcsDeviceprotocolEntityList) { item.Devicecode = entity.Code; item.Updatetime = DateTime.Now; item.Updateuser = loginUserInfo.UserNo; } ctx.Insertable(wcsDeviceprotocolEntityList).ExecuteCommand(); ctx.Ado.CommitTran(); } catch (Exception ex) { ctx.Ado.RollbackTran(); throw ex; } } } else { using (var ctx = WCSDbCore.GetDbCtx()) { try { var DeviceQueryable = ctx.Queryable(); var DeviceprotocolQueryable = ctx.Queryable(); if (DeviceQueryable.Any(x => x.Code != keyValue && (x.Code == entity.Code || x.Name == entity.Name) && x.Enabled)) { throw SysExCore.ThrowFailException("已存在同名同编码数据!"); } if (wcsDeviceprotocolEntityList != null && wcsDeviceprotocolEntityList.Any()) { foreach (WcsDeviceprotocolEntity item in wcsDeviceprotocolEntityList) { if (DeviceprotocolQueryable.Any(x => x.Id != item.Id && x.Devicecode == entity.Code && x.DBCODE == item.DBCODE && x.Enabled)) throw SysExCore.ThrowFailException("设备协议信息DB编号已存在"); } } ctx.Ado.BeginTran(); ctx.Updateable(entity).ExecuteCommand(); ctx.Deleteable().Where(it => it.Devicecode == keyValue).ExecuteCommand(); foreach (WcsDeviceprotocolEntity item in wcsDeviceprotocolEntityList) { item.Devicecode = entity.Code; item.Updatetime = DateTime.Now; item.Updateuser = loginUserInfo.UserNo; } ctx.Insertable(wcsDeviceprotocolEntityList).ExecuteCommand(); ctx.Ado.CommitTran(); } catch (Exception ex) { ctx.Ado.RollbackTran(); throw ex; } } } } public void SaveGenerateDevice(LoginUserInfo loginUserInfo, GenerateDevice entity) { if (entity == null) { throw SysExCore.ThrowFailException("输入数据为空。"); } if (string.IsNullOrWhiteSpace(entity.Code)) { throw SysExCore.ThrowFailException("请选择PLC。"); } if (string.IsNullOrWhiteSpace(entity.DBCode)) { throw SysExCore.ThrowFailException("请选择PLC DB块!"); } if (entity.Position < 0) { throw SysExCore.ThrowFailException("开始值应该大于0的正整数!"); } entity.Prefix = string.IsNullOrWhiteSpace(entity.Prefix) ? "" : entity.Prefix?.Trim(); var dbcodes = entity.DBCode.Split(',').OrderBy(o => o.ToString()).ToArray(); using (var ctx = WCSDbCore.GetDbCtx()) { try { var DeviceQueryable = ctx.Queryable(); var datablockList = ctx.Queryable().WhereIF(!string.IsNullOrEmpty(entity.Code), a => a.Enabled && a.Plccode.Equals(entity.Code)).Select(@" [CODE],[NAME],[PLCCODE],[NO],[LENGTH] ,[DATALENGTH]").ToList(); List deviceprotocolList = new List(); List deviceDic = new List(); var now = DateTime.Now; for (int i = 0; i < dbcodes.Length; i++) { var datablock = datablockList.FirstOrDefault(o => o.Code == dbcodes[i]); if (datablock != null) { var len = datablock.Datalength != 0 ? datablock.Length / datablock.Datalength : 0; if (len <= 0) continue; for (int j = 0; j < len; j++) { var devicecode = string.IsNullOrEmpty(entity.Prefix) ? Convert.ToString(entity.Position + j) : entity.Prefix + Convert.ToString(entity.Position + j); deviceprotocolList.Add(new WcsDeviceprotocolEntity() { DBCODE = dbcodes[i], Devicecode = devicecode, Position = datablock.Datalength * j, Enabled = true, Updateuser = "WCS", Updatetime = now }); if (!deviceDic.Contains(devicecode)) { deviceDic.Add(devicecode); } } } } var wcsDeviceList = DeviceQueryable.Select(@" [CODE]").ToList(); var exceptList = deviceDic.Except(wcsDeviceList.Select(o => o.Code).ToList()).ToList(); List deviceslists = new List(); for (int i = 0; i < exceptList.Count(); i++) { deviceslists.Add(new WcsDeviceEntity { Code = exceptList[i], Name = exceptList[i], Enabled = true, Updateuser = "WCS", Updatetime = now }); } try { //开启事务 ctx.Ado.BeginTran(); if (deviceslists.Any()) { ctx.Insertable(deviceslists).ExecuteCommand(); } //删除WcsDeviceprotocolEntity ctx.Deleteable().Where(it => deviceDic.Contains(it.Devicecode)).ExecuteCommand(); ctx.Insertable(deviceprotocolList).ExecuteCommand(); //关闭事务 ctx.Ado.CommitTran(); } catch (Exception ex) { //回滚事务 ctx.Ado.RollbackTran(); if (ex.Message.Contains("Duplicate")) { throw SysExCore.ThrowFailException("已存在同名或同变码数据!"); } else { throw SysExCore.ThrowFailException("新增数据失败!"); } } } catch (Exception ex) { throw ex; } } } } }