123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- 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
- {
- /// <summary>
- ///
- /// </summary>
- public string Code { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string DBCode { get; set; }
- /// <summary>
- ///
- /// </summary>
- public int Position { get; set; }
- /// <summary>
- ///
- /// </summary>
- public string Prefix { get; set; }
- }
- public class WcsDeviceInfoService
- {
- /// <summary>
- /// 获取分页数据
- /// <summary>
- /// <returns></returns>
- public IEnumerable<dynamic> GetPageList(Pagination pagination, string queryJson)
- {
- SqlSugarClient client = null;
- try
- {
- client = WCSDbCore.GetDbCtx();
- int count = 0;
- var queryParam = queryJson.ToJObject();
- //var db = client.Queryable<WcsDeviceEntity>();
- //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<dynamic>(@"*").ToPageList(pagination.page, pagination.rows, ref count);
- //pagination.records = count;
- var list = client.Queryable<WcsDeviceEntity
- , WcsDeviceprotocolEntity
- >((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<dynamic> GetSelectDeviceNameList()
- {
- var client = WCSDbCore.GetDbCtx();
- var db = client.Queryable<WcsDeviceEntity>().Where(o => o.Enabled).Select<dynamic>("CODE as id, NAME as text");
- return db.ToList();
- }
- /// <summary>
- /// 获取实体数据
- /// <param name="keyValue">主键</param>
- /// <summary>
- /// <returns></returns>
- public WcsDeviceEntity GetEntity(string keyValue)
- {
-
- try
- {
- return WCSDbCore.GetDbCtx().Queryable<WcsDeviceEntity>().Where(it => it.Code == keyValue).First();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- /// 删除实体数据
- /// <param name="keyValue">主键</param>
- /// <summary>
- /// <returns></returns>
- public void DeleteEntity(string keyValue)
- {
- try
- {
- //SysDbCore.GetDbCtx().Deleteable<BASE_WAREHOUSE>().Where(it => it.F_NO == keyValue).ExecuteCommand();
- WCSDbCore.GetDbCtx().Deleteable<WcsDeviceEntity>().Where(it => it.Code == keyValue).ExecuteCommand();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="loginUserInfo"></param>
- /// <param name="keyValue"></param>
- /// <param name="entity"></param>
- /// <param name="wcsDeviceprotocolEntityList"></param>
- public void SaveEntity(LoginUserInfo loginUserInfo, string keyValue, WcsDeviceEntity entity, List<WcsDeviceprotocolEntity> 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<WcsDeviceEntity>();
- var DeviceprotocolQueryable = ctx.Queryable<WcsDeviceprotocolEntity>();
- 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<WcsDeviceEntity>();
- var DeviceprotocolQueryable = ctx.Queryable<WcsDeviceprotocolEntity>();
- 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<WcsDeviceprotocolEntity>().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<WcsDeviceEntity>();
- 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();
- List<WcsDeviceprotocolEntity> deviceprotocolList = new List<WcsDeviceprotocolEntity>();
- List<string> deviceDic = new List<string>();
- 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<WcsDeviceEntity>(@" [CODE]").ToList();
- var exceptList = deviceDic.Except(wcsDeviceList.Select(o => o.Code).ToList()).ToList();
- List<WcsDeviceEntity> deviceslists = new List<WcsDeviceEntity>();
- 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<WcsDeviceprotocolEntity>().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;
- }
- }
- }
- }
- }
|