diff --git a/Sinvo.EplanHpD.Plugin.WPFUI/Service/MotorLectotypeService.cs b/Sinvo.EplanHpD.Plugin.WPFUI/Service/MotorLectotypeService.cs new file mode 100644 index 0000000..515c78b --- /dev/null +++ b/Sinvo.EplanHpD.Plugin.WPFUI/Service/MotorLectotypeService.cs @@ -0,0 +1,242 @@ +using Sinvo.EplanHpD.Plugin.Service.Model; +using Sinvo.EplanHpD.Plugin.WPFUI.Enum; +using Sinvo.EplanHpD.Plugin.WPFUI.Models; +using Sinvo.EplanHpD.Plugin.WPFUI.ViewModel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sinvo.EplanHpD.Plugin.Service +{ + public class MotorLectotypeService + { + public void AddMotor(Motor motor) + { + DBHelper.DB.Insertable(motor).ExecuteCommand(); + } + + public Motor GetMotorById(string occPartId) + { + var data = DBHelper.DB.Queryable("mt") + .Where(mt => mt.OccPartId == occPartId) + .First(); + return data; + } + + //public string GetMotorLectotypeData(string motorOccId) + //{ + // var data = DBHelper.DB.Queryable("mt") + // .Where(mt => mt.ID == motorOccId) + // .First(); + // if (data != null) + // { + // return data.Data; + // } + // else + // { + // return ""; + // } + //} + + public CableLectotypeViewModel GetMotorLectotypeData(string motorOccId) + { + // 查询 Motor 数据 + var motor = DBHelper.DB.Queryable() + .Where(m => m.OccPartId == motorOccId) + .First(); + + if (motor == null) + { + return null; + } + + // 查询关联的 CableLectotype 数据 + var cableLectotype = DBHelper.DB.Queryable() + .Where(cl => cl.MotorId == motor.OccPartId) + .First(); + + if (cableLectotype == null) + { + return null; + } + + // 查询关联的 LectotypeLine 数据 + var selectedLines = DBHelper.DB.Queryable() + .Where(ll => ll.CableLectotypeId == cableLectotype.CableLectotypeId) + //.OrderBy(ll => ll.SeqNo) + .ToList(); + + // 构建 CableLectotypeViewModel + var viewModel = new CableLectotypeViewModel + { + Motor = new MotorModel + { + MotorPower = motor.MotorPower, + Brand = motor.Brand, + MotorSerie = motor.MotorSerie, + MotorModelStr = motor.MotorModelStr, + AxisNo = motor.AxisNo, + OccPartId = motor.OccPartId + }, + CableConnectionType = (ConnectionType)Enum.Parse(typeof(ConnectionType), cableLectotype.CableConnectionType.ToString()), + AxisNo = cableLectotype.AxisNo, + CableType = cableLectotype.CableType, + EncoderLineParagraph = cableLectotype.EncoderLineParagraph, + PowerLineParagraph = cableLectotype.PowerLineParagraph, + CableModelStr = cableLectotype.CableModelStr, + IsEnableParagraph = cableLectotype.IsEnableParagraph, + }; + viewModel.SelectedLines = selectedLines.Select(line => new LectotypeLineModel + { + CableLectotypeId = line.CableLectotypeId, + SeqNo = line.SeqNo, + AxisNo = line.AxisNo, + CableConnectionClass = line.CableConnectionClass, + CableType = line.CableType, + IsFlexibility = line.IsFlexibility, + Motor = viewModel.Motor, + PowerLineLength = line.PowerLineLength, + EncoderLineLength = line.EncoderLineLength, + DrawingNo = line.DrawingNo, + CableModelNo = line.CableModelNo, + LineCount = line.LineCount, + CurrentLine = line.CurrentLine, + IsLectotype = line.IsLectotype, + CableModel = line.CableModel, + IsChecked = line.IsChecked, + SubLines = GetSubLines(line.LectotypeLineId) + }).OrderBy(it => it.CurrentLine).ToList(); + return viewModel; + } + + // 获取子线数据 + private List GetSubLines(string parentLineId) + { + var subLines = DBHelper.DB.Queryable() + .Where(sl => sl.ParentLectotypeLineId == parentLineId) + .ToList(); + + return subLines.Select(subLine => new LectotypeLineModel + { + SeqNo = subLine.SeqNo, + CableModel = subLine.CableModel, + CableType = subLine.CableType, + IsChecked = subLine.IsChecked + }).ToList(); + } + + public bool SaveMotorLectotypeData(string motorOccId, CableLectotypeViewModel data) + { + var db = DBHelper.DB; + + db.BeginTran(); + try + { + var motor = GetMotorById(data.Motor.OccPartId);//.FirstOrDefault(m => m.OccPartId == Motor.OccPartId); + if (motor == null) + { + motor = new Motor + { + //MotorId = Guid.NewGuid().ToString(), + //motorOccId + MotorPower = data.Motor.MotorPower, + Brand = data.Motor.Brand, + MotorSerie = data.Motor.MotorSerie, + MotorModelStr = data.Motor.MotorModelStr, + AxisNo = data.Motor.AxisNo, + OccPartId = data.Motor.OccPartId, + CableLectotypeLines = [] + }; + AddMotor(motor); + } + else + { + motor.CableLectotypeLines = []; + + } + // 创建 CableLectotype + var cableLectotype = new CableLectotype + { + //Motor = motor, + CableLectotypeId = Guid.NewGuid().ToString(), + MotorId = motor.OccPartId, + CableConnectionType = (int)data.CableConnectionType, + AxisNo = data.AxisNo, + CableType = data.CableType, + EncoderLineParagraph = data.EncoderLineParagraph, + PowerLineParagraph = data.PowerLineParagraph, + CableModelStr = data.CableModelStr, + IsEnableParagraph = data.IsEnableParagraph, + SelectedLines = [] + }; + // 添加 SelectedLines + foreach (var line in data.SelectedLines) + { + var lectotypeLine = new LectotypeLine + { + CableLectotypeId = cableLectotype.CableLectotypeId, + MotorId = motor.OccPartId, + LectotypeLineId = Guid.NewGuid().ToString(), + SeqNo = line.SeqNo, + AxisNo = line.AxisNo, + CableConnectionClass = line.CableConnectionClass, + CableType = line.CableType, + IsFlexibility = line.IsFlexibility, + PowerLineLength = line.PowerLineLength, + EncoderLineLength = line.EncoderLineLength, + DrawingNo = line.DrawingNo, + CableModelNo = line.CableModelNo, + LineCount = line.LineCount, + CurrentLine = line.CurrentLine, + IsLectotype = line.IsLectotype, + CableModel = line.CableModel, + IsChecked = line.IsChecked, + SubLines = [] + }; + + // 添加 SubLines + if (line.SubLines != null) + { + foreach (var subLine in line.SubLines) + { + var subLectotypeLine = new LectotypeLine + { + LectotypeLineId = Guid.NewGuid().ToString(), + MotorId = motor.OccPartId, + SeqNo = subLine.SeqNo, + ParentLectotypeLineId = lectotypeLine.LectotypeLineId, + CableModel = subLine.CableModel, + CableType = subLine.CableType, + IsChecked = subLine.IsChecked + }; + lectotypeLine.SubLines.Add(subLectotypeLine); + } + } + motor.CableLectotypeLines.Add(lectotypeLine); + } + db.Storageable(motor).ExecuteCommand(); + // 清空后再保存 + db.Deleteable().Where(it => it.MotorId == motor.OccPartId).ExecuteCommand(); + db.Storageable(cableLectotype).ExecuteCommand(); + + db.Deleteable().Where(it => it.MotorId == motor.OccPartId).ExecuteCommand(); + db.Storageable(motor.CableLectotypeLines.ToList()).ExecuteCommand(); + db.Storageable(motor.CableLectotypeLines.SelectMany(it => it.SubLines).ToList()).ExecuteCommand(); + + db.CommitTran(); + return true; + + } + catch (Exception) + { + + db.RollbackTran(); + return false; + } + } + + + } +}