| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794 |
- using DBEntity;
- using IvfTl.Control.Entity.DBEntitys;
- using IvfTl.Control.Entity.DTO.ControllerResults;
- using IvfTl.Control.Entity.GlobalEntitys;
- using IvfTl.Control.Entity.GlobalEnums;
- using IvfTl.Control.Entity.InitEntitys;
- using IvfTl.Control.Services;
- using ivf_tl_UtilHelper;
- using Microsoft.Data.Sqlite;
- using MQTTnet.Formatter.V5;
- using Org.BouncyCastle.Asn1.Ocsp;
- using Org.BouncyCastle.Asn1.X509;
- using Org.BouncyCastle.Crypto;
- using Org.BouncyCastle.Utilities.Collections;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.DirectoryServices.ActiveDirectory;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- namespace ivf_tl_ServicesImpl.DBServices
- {
- public class DBServiceImplNo
- {
- public event Action<string, LogEnum> ErrorLogEvent;
- public event Action<Exception, string, string, LogEnum> ExceptionLogEvent;
- public string DbPath = "";
- private SqlSugarScope Db;
- private DBServiceImplNo()
- {
- }
- public bool StartDbService(string dbPath)
- {
- try
- {
- DbPath = dbPath;
- string connStr = new SqliteConnectionStringBuilder()
- {
- DataSource = dbPath,
- Mode = SqliteOpenMode.ReadWrite,
- }.ToString();
- ConnectionConfig connectionConfig = new ConnectionConfig()
- {
- ConnectionString = connStr,
- DbType = DbType.Sqlite,//数据库类型
- IsAutoCloseConnection = true //不设成true要手动close
- };
- Db = new SqlSugarScope(connectionConfig);
- return true;
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.StartDbService", null, LogEnum.DbException);
- return false;
- }
- }
- public void BuildEntity()
- {
- Db.DbFirst.IsCreateAttribute().CreateClassFile(@"C:\Users\jxb\Desktop\work\10 时差\8 开发文档\6 服务器架构\本地数据库", "IvfTl.Control.Entity.DBEntitys");
- }
- public void DBUpdateTLInfo(TLInitControllerResult tLInitControllerResult)
- {
- try
- {
- TLSettingDB tLSettingDB = ConvertHelper.ConvertToTLSettingDB(tLInitControllerResult.TLSetting);
- if (tLSettingDB == null)
- {
- return;
- }
- var oldSetting = Db.Queryable<HouseDB>().First(x => x.tlSn == tLSettingDB.tlSn);
- if (oldSetting != null)
- {
- tLSettingDB.cid = oldSetting.cid;
- Db.Updateable(tLSettingDB).ExecuteCommand();
- }
- else
- {
- Db.Insertable(tLSettingDB).ExecuteCommand();
- }
- UpdateHoustInfo(tLInitControllerResult.HouseList, tLSettingDB.tlSn);
- UpdateHouseWellSetting(tLInitControllerResult.HouseWellList, tLSettingDB.tlSn);
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.DBUpdateTLInfo", null, LogEnum.DbException);
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="tLInitData"></param>
- /// <returns></returns>
- public (TLSettingDB, List<HouseDB>, List<HouseWellSettingDB>) DBGetTLInfo(TLInitData tLInitData)
- {
- try
- {
- Db.Updateable<TLSettingDB>()
- .SetColumns(it => it.softwareVersion == tLInitData.softwareVersion)
- .SetColumns(x => x.verticalMotorPulseMax == tLInitData.verticalMotorPulseMax)
- .Where(it => it.tlSn == tLInitData.tlSn).ExecuteCommand();
- var houseSnList = tLInitData.houseLinkDataList.Select(x => x.houseSn).ToList();
- UpdateHouseInfo(tLInitData.houseLinkDataList, tLInitData.tlSn);
- UpdateHouseWellSetting(tLInitData.houseEEPROInitDTOList, tLInitData.tlSn);
- TLSettingDB tLSettingDB = Db.Queryable<TLSettingDB>().First(it => it.tlSn == tLInitData.tlSn);
- List<HouseDB> houseDBList = Db.Queryable<HouseDB>().Where(x => x.tlSn == tLInitData.tlSn && houseSnList.Contains(x.houseSn)).ToList();
- List<HouseWellSettingDB> HouseWellSettingDBList = Db.Queryable<HouseWellSettingDB>().Where(x => x.tlSn == tLInitData.tlSn && houseSnList.Contains(x.houseSn)).ToList();
- return (tLSettingDB, houseDBList, HouseWellSettingDBList);
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdateHouseInfo", null, LogEnum.DbException);
- return (new TLSettingDB(), new List<HouseDB>(), new List<HouseWellSettingDB>());
- }
- }
- #region DishDB
- public List<Dish> GetDBDishs(string tlsn)
- {
- try
- {
- var dishList = Db.Queryable<DishDB>().Where(it => it.tlSn == tlsn && !it.endTime.HasValue).ToList();
- if (!dishList.Any()) return new List<Dish>();
- List<Dish> result = new List<Dish>();
- foreach (var item in dishList)
- {
- Dish dish = ConvertHelper.ConvertToDish(item);
- result.Add(dish);
- var embryoList = Db.Queryable<EmbryoDB>().Where(it => it.dishId == item.id).ToList();
- if (!embryoList.Any()) continue;
- dish.Embryo = new List<Embryo>();
- foreach (var itemEmbryo in embryoList)
- {
- dish.Embryo.Add(ConvertHelper.ConvertToEmbryo(itemEmbryo));
- }
- }
- return result;
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.GetDBDishs", null, LogEnum.DbException);
- return new List<Dish>();
- }
- }
- public void AddDishs(List<DishDB> dishDbList, string tlsn)
- {
- try
- {
- var alldishList = Db.Queryable<DishDB>().Where(it => it.tlSn == tlsn).ToList();
- var oldDishList = alldishList.Where(it => !it.endTime.HasValue).ToList();
- List<DishDB> endDishs = new List<DishDB>();
- foreach (var item in oldDishList)
- {
- var aa = dishDbList.FirstOrDefault(x => x.id == item.id);
- if (aa == null)
- {
- item.endTime = DateTime.Now;
- endDishs.Add(item);
- }
- }
- if (endDishs.Any())
- {
- Db.Updateable(endDishs).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
- }
- List<DishDB> insertDishList = new List<DishDB>();
- List<DishDB> updateDishList = new List<DishDB>();
- foreach (var item in dishDbList)
- {
- var aa = alldishList.FirstOrDefault(x => x.id == item.id);
- if (aa == null)
- {
- insertDishList.Add(item);
- }
- else
- {
- if (aa.endTime.HasValue)
- {
- aa.endTime = null;
- updateDishList.Add(aa);
- }
- }
- }
- if (insertDishList.Any())
- {
- Db.Insertable(insertDishList).ExecuteCommand();
- }
- if (updateDishList.Any())
- {
- Db.Updateable(updateDishList).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
- }
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddDishs", null, LogEnum.DbException);
- }
- }
- public void AddDish(Dish dish, string tlsn)
- {
- try
- {
- var oldDish = Db.Queryable<DishDB>().First(x => x.id == dish.id && x.tlSn == tlsn);
- if (oldDish != null)
- {
- Db.Deleteable<DishDB>().Where(x => x.id == dish.id && x.tlSn == tlsn).ExecuteCommand();
- Db.Deleteable<EmbryoDB>().Where(x => x.dishId == dish.id && x.tlSn == tlsn).ExecuteCommand();
- }
- DishDB dishDB = ConvertHelper.ConvertToDishDB(dish);
- Db.Insertable(dishDB).ExecuteCommand();
- List<EmbryoDB> embryoDbList = new List<EmbryoDB>();
- foreach (var item in dish.Embryo)
- {
- embryoDbList.Add(ConvertHelper.ConvertToEmbryoDB(item));
- }
- Db.Insertable(embryoDbList).ExecuteCommand();
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddDish", null, LogEnum.DbException);
- }
- }
- public void EndDish(long id, DateTime endTime, string tlsn)
- {
- try
- {
- Db.Updateable<DishDB>().SetColumns(it => it.endTime == endTime).Where(it => it.id == id && it.tlSn == tlsn).ExecuteCommand();
- Db.Updateable<EmbryoDB>().SetColumns(it => new EmbryoDB()
- {
- state = (int)EmbryoState.End,
- stateTime = endTime,
- }).Where(it => it.dishId == id && it.tlSn == tlsn && it.state == (int)EmbryoState.None).ExecuteCommand();
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.EndDish", null, LogEnum.DbException);
- }
- }
- #endregion
- #region EmbryoDB
- public void AddEmbryos(List<EmbryoDB> embryoDbList, string tlsn)
- {
- try
- {
- var allEmbryoList = Db.Queryable<EmbryoDB>().Where(it => it.tlSn == tlsn).ToList();
- var ingEmbryoList = allEmbryoList.Where(x => x.state == (int)EmbryoState.None).ToList();
- var updateEmbryoList = new List<EmbryoDB>();
- foreach (var item in ingEmbryoList)
- {
- var a = embryoDbList.FirstOrDefault(x => x.id == item.id);
- if (a == null)
- {
- item.state = (int)EmbryoState.End;
- item.stateTime = DateTime.Now;
- updateEmbryoList.Add(item);
- }
- if (a.state != item.state)
- {
- item.state = a.state;
- item.stateTime = a.stateTime;
- updateEmbryoList.Add(item);
- }
- }
- if (updateEmbryoList.Any())
- {
- Db.Updateable(updateEmbryoList).UpdateColumns(it => new { it.state, it.stateTime }).ExecuteCommand();
- }
- updateEmbryoList.Clear();
- var insertEmbryoList = new List<EmbryoDB>();
- foreach (var item in embryoDbList)
- {
- var aa = allEmbryoList.FirstOrDefault(x => x.id == item.id);
- if (aa == null)
- {
- insertEmbryoList.Add(item);
- }
- else
- {
- if (aa.state != item.state)
- {
- aa.state = item.state;
- aa.stateTime = item.stateTime;
- updateEmbryoList.Add(aa);
- }
- }
- }
- if (insertEmbryoList.Any())
- {
- Db.Insertable(insertEmbryoList).ExecuteCommand();
- }
- if (updateEmbryoList.Any())
- {
- Db.Updateable(updateEmbryoList).UpdateColumns(it => new { it.state, it.stateTime }).ExecuteCommand();
- }
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddEmbryos", null, LogEnum.DbException);
- }
- }
- #endregion
- #region BalanceDB
- public List<Balance> DbGetBalances(string tlsn)
- {
- try
- {
- var a = Db.Queryable<BalanceDB>().Where(it => it.tlSn == tlsn && !it.endTime.HasValue).ToList();
- if (!a.Any()) return new List<Balance>();
- List<Balance> result = new List<Balance>();
- foreach (var item in a)
- {
- result.Add(ConvertHelper.ConvertToBalance(item));
- }
- return result;
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.DbGetBalances", null, LogEnum.DbException);
- return new List<Balance>();
- }
- }
- public void AddBalances(List<BalanceDB> balanceDbList, string tlsn)
- {
- try
- {
- var allBalanceList = Db.Queryable<BalanceDB>().Where(it => it.tlSn == tlsn).ToList();
- var ingBalacneList = allBalanceList.Where(x => !x.endTime.HasValue).ToList();
- var updateList = new List<BalanceDB>();
- foreach (var item in ingBalacneList)
- {
- var a = balanceDbList.FirstOrDefault(x => x.id == item.id);
- if (a == null)
- {
- item.endTime = DateTime.Now;
- updateList.Add(item);
- }
- }
- if (updateList.Any())
- {
- Db.Updateable(updateList).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
- }
- updateList.Clear();
- var insertList = new List<BalanceDB>();
- foreach (var item in balanceDbList)
- {
- var old = allBalanceList.FirstOrDefault(x => x.id == item.id);
- if (old == null)
- {
- insertList.Add(item);
- }
- else
- {
- if (old.endTime != item.endTime)
- {
- old.endTime = item.endTime;
- updateList.Add(old);
- }
- }
- }
- if (updateList.Any())
- {
- Db.Updateable(updateList).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
- }
- if (insertList.Any())
- {
- Db.Insertable(insertList).ExecuteCommand();
- }
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddBalances", null, LogEnum.DbException);
- }
- }
- public void AddBalance(Balance balance)
- {
- try
- {
- var oldBalance = Db.Queryable<BalanceDB>().First(x => x.id == balance.id);
- if (oldBalance == null)
- {
- BalanceDB balanceDB = ConvertHelper.ConvertToBalanceDB(balance);
- Db.Insertable(balanceDB).ExecuteCommand();
- }
- else
- {
- if (oldBalance.endTime.HasValue)
- {
- oldBalance.endTime = null;
- Db.Updateable(oldBalance).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
- }
- }
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddBalance", null, LogEnum.DbException);
- }
- }
- public void EndBalance(long id, DateTime? endTime)
- {
- try
- {
- var oldBalance = Db.Queryable<BalanceDB>().First(x => x.id == id);
- if (oldBalance == null) return;
- oldBalance.endTime = endTime;
- Db.Updateable(oldBalance).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.EndBalance", null, LogEnum.DbException);
- }
- }
- #endregion
- #region houseDB
- private void UpdateHoustInfo(List<House> houseList, string tlsn)
- {
- try
- {
- List<HouseDB> houseDBList = new List<HouseDB>();
- foreach (var item in houseList)
- {
- houseDBList.Add(ConvertHelper.ConvertToHouseDB(item));
- }
- if (!houseDBList.Any()) return;
- Db.Deleteable<HouseDB>().Where(x => x.tlSn == tlsn).ExecuteCommand();
- Db.Insertable(houseDBList).ExecuteCommand();
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdateHoustInfo", null, LogEnum.DbException);
- }
- }
- private void UpdateHouseInfo(List<HouseInitData> houseLinkDataList, string tlsn)
- {
- try
- {
- if (!houseLinkDataList.Any()) return;
- var houseSnList = houseLinkDataList.Select(x => x.houseSn).ToList();
- var oldHouseList = Db.Queryable<HouseDB>().Where(x => x.tlSn == tlsn && houseSnList.Contains(x.houseSn)).ToList();
- if (!oldHouseList.Any()) return;
- foreach (var item in oldHouseList)
- {
- var newItem = houseLinkDataList.FirstOrDefault(x => x.houseSn == item.houseSn);
- if (newItem == null) continue;
- item.housePort = newItem.housePort;
- item.ccdId = newItem.ccdId;
- item.ccdSn = newItem.ccdSn;
- item.inletValveOpeningTime = newItem.inletValveOpeningTime;
- item.temperatureLowerHeatingPlate = newItem.temperatureLowerHeatingPlate;
- item.verticalMotorSpacePulse = newItem.verticalMotorSpacePulse;
- }
- var updateHouseRsult = Db.Updateable(oldHouseList)
- .UpdateColumns(it => new
- {
- it.housePort,
- it.ccdId,
- it.ccdSn,
- it.inletValveOpeningTime,
- it.temperatureLowerHeatingPlate,
- it.verticalMotorSpacePulse
- }).ExecuteCommand();
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdateHouseInfo", null, LogEnum.DbException);
- }
- }
- #endregion
- #region HouseWellSettingDB
- private void UpdateHouseWellSetting(List<HouseWellSetting> houseWellSettingList, string tlSn)
- {
- try
- {
- List<HouseWellSettingDB> houseWellSettingDBList = new List<HouseWellSettingDB>();
- foreach (var item in houseWellSettingList)
- {
- houseWellSettingDBList.Add(ConvertHelper.ConvertToHouseWellSettingDB(item));
- }
- Db.Deleteable<HouseWellSettingDB>().Where(x => x.tlSn == tlSn).ExecuteCommand();
- Db.Insertable(houseWellSettingDBList).ExecuteCommand();
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdateHouseWellSetting", null, LogEnum.DbException);
- }
- }
- private void UpdateHouseWellSetting(List<HouseEEPROInfo> houseEEPROInfos, string tlsn)
- {
- try
- {
- if (!houseEEPROInfos.Any()) return;
- var houseSnList = houseEEPROInfos.Select(x => x.houseSn).ToList();
- var oldHouseList = Db.Queryable<HouseWellSettingDB>().Where(x => x.tlSn == tlsn && houseSnList.Contains(x.houseSn)).ToList();
- if (!oldHouseList.Any()) return;
- int updateHouseNum = 0;
- foreach (var item in oldHouseList)
- {
- var newItem = houseEEPROInfos.FirstOrDefault(x => x.houseSn == item.houseSn);
- if (newItem == null) continue;
- item.eepromClearPosition = newItem.eepromClearPosition;
- switch (item.wellSn)
- {
- case 1:
- item.horizontalMotorPosition = newItem.hwell1;
- break;
- case 2:
- item.horizontalMotorPosition = newItem.hwell2;
- break;
- case 3:
- item.horizontalMotorPosition = newItem.hwell3;
- break;
- case 4:
- item.horizontalMotorPosition = newItem.hwell4;
- break;
- case 5:
- item.horizontalMotorPosition = newItem.hwell5;
- break;
- case 6:
- item.horizontalMotorPosition = newItem.hwell6;
- break;
- case 7:
- item.horizontalMotorPosition = newItem.hwell7;
- break;
- case 8:
- item.horizontalMotorPosition = newItem.hwell8;
- break;
- case 9:
- item.horizontalMotorPosition = newItem.hwell9;
- break;
- case 10:
- item.horizontalMotorPosition = newItem.hwell10;
- break;
- case 11:
- item.horizontalMotorPosition = newItem.hwell11;
- break;
- case 12:
- item.horizontalMotorPosition = newItem.hwell12;
- break;
- case 13:
- item.horizontalMotorPosition = newItem.hwell13;
- break;
- case 14:
- item.horizontalMotorPosition = newItem.hwell14;
- break;
- case 15:
- item.horizontalMotorPosition = newItem.hwell15;
- break;
- case 16:
- item.horizontalMotorPosition = newItem.hwell16;
- break;
- }
- }
- var updateHouseRsult = Db.Updateable(oldHouseList)
- .UpdateColumns(it => new
- {
- it.horizontalMotorPosition,
- it.eepromClearPosition,
- }).ExecuteCommand();
- if (updateHouseRsult != updateHouseNum)
- {
- ErrorLogEvent?.Invoke("仓室水平电机位置更新数据不匹配", LogEnum.RunError);
- }
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdateHouseWellSetting", null, LogEnum.DbException);
- }
- }
- #endregion
- #region pictureDB
- public void AddPictrue(PictureDB pictureDB)
- {
- try
- {
- var oldPic = Db.Queryable<PictureDB>().First(x => x.TlSn == pictureDB.TlSn && x.SourceImageName == pictureDB.SourceImageName);
- if(oldPic == null)
- {
- Db.Insertable(pictureDB).ExecuteCommand();
- }
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddPictrue", null, LogEnum.DbException);
- }
- }
- public void DeletePictrue(string fileName, string tlsn)
- {
- try
- {
- Db.Deleteable<PictureDB>().Where(x => x.TlSn == tlsn && x.SourceImageName == fileName).ExecuteCommand();
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.DeletePictrue", null, LogEnum.DbException);
- }
- }
- public PictureDB SearchPicture(string fileName, string tlsn)
- {
- try
- {
- var pic = Db.Queryable<PictureDB>().First(x => x.TlSn == tlsn && x.SourceImageName == fileName);
- return pic;
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.SearchPicture", null, LogEnum.DbException);
- return null;
- }
- }
- #endregion
- #region HouseWellPhotoDB
- /// <summary>
- /// 更新拍照位置
- /// </summary>
- /// <param name="houses"></param>
- /// <param name="housesn"></param>
- /// <param name="tlsn"></param>
- /// <param name="positionType">0表示自动对焦</param>
- public void UpdatePosition(List<HouseWellPhotoDB> houses,int housesn,string tlsn,int positionType)
- {
- try
- {
- var wellList = houses.Select(x => x.wellSn).ToList();
- if(positionType == 0)
- {
- Db.Deleteable<HouseWellPhotoDB>().Where(x => x.tlSn == tlsn && x.houseSn == housesn && wellList.Contains(x.wellSn) && x.positionType == 0).ExecuteCommand();
- }
- else
- {
- Db.Deleteable<HouseWellPhotoDB>().Where(x => x.tlSn == tlsn && x.houseSn == housesn && wellList.Contains(x.wellSn) && x.positionType != 0).ExecuteCommand();
- }
- Db.Insertable(houses).ExecuteCommand();
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdatePosition", null, LogEnum.DbException);
- }
- }
- /// <summary>
- /// 获取电机位置
- /// </summary>
- /// <param name="wellSn"></param>
- /// <param name="housesn"></param>
- /// <param name="tlsn"></param>
- /// <param name="positionType">0表示自动对焦</param>
- /// <returns></returns>
- public List<HouseWellPhotoDB> GetPosition(List<int> wellSn, int housesn, string tlsn, int positionType)
- {
- try
- {
- if(positionType == 0)
- {
- return Db.Queryable<HouseWellPhotoDB>().Where(x => x.tlSn == tlsn && x.houseSn == housesn && wellSn.Contains(x.wellSn) && x.positionType == 0).ToList();
- }
- else
- {
- return Db.Queryable<HouseWellPhotoDB>().Where(x => x.tlSn == tlsn && x.houseSn == housesn && wellSn.Contains(x.wellSn) && x.positionType != 0).ToList();
- }
-
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.GetPosition", null, LogEnum.DbException);
- return new List<HouseWellPhotoDB>();
- }
- }
- #endregion
- #region HouseAutofocusCalibrationDB(M2-04 标定结果库内镜像)
- /// <summary>
- /// 标定结果镜像入 house_autofocus_calibration(真相源仍为本地 calibration.json,见 12 §2.7)。
- /// scene=0 出厂基准:按 (tlSn,houseSn,wellSn,scene=0,deleted==null) upsert(每 well 唯一);
- /// scene=1 日常对焦:直接 Insertable append(留历史,清理任务只删 scene=1)。
- /// 写库异常被本方法吞掉(记日志),绝不向上抛,保证不崩对焦/采集线程。
- /// </summary>
- public void SaveAutofocusCalibration(string tlSn, int houseSn, int wellSn, int scene,
- int focusZ, int exposure, int horizontalPulse, decimal peakRatio, bool circleFound,
- decimal centerOffsetPct, string note = "", string source = "LOCAL_JSON")
- {
- try
- {
- DateTime now = DateTime.Now;
- if (scene == 0)
- {
- var old = Db.Queryable<HouseAutofocusCalibrationDB>()
- .First(x => x.tlSn == tlSn && x.houseSn == houseSn && x.wellSn == wellSn
- && x.scene == 0 && x.deleted == null);
- if (old != null)
- {
- old.focusZ = focusZ;
- old.exposure = exposure;
- old.horizontalPulse = horizontalPulse;
- old.peakRatio = peakRatio;
- old.circleFound = circleFound ? 1 : 0;
- old.centerOffsetPct = centerOffsetPct;
- old.calibTime = now;
- old.source = source;
- old.note = note;
- old.updateTime = now;
- Db.Updateable(old).ExecuteCommand();
- return;
- }
- }
- var entity = new HouseAutofocusCalibrationDB
- {
- tlSn = tlSn,
- houseSn = houseSn,
- wellSn = wellSn,
- scene = scene,
- focusZ = focusZ,
- exposure = exposure,
- horizontalPulse = horizontalPulse,
- peakRatio = peakRatio,
- circleFound = circleFound ? 1 : 0,
- centerOffsetPct = centerOffsetPct,
- calibTime = now,
- source = source,
- note = note,
- createTime = now,
- };
- Db.Insertable(entity).ExecuteCommand();
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.SaveAutofocusCalibration", null, LogEnum.DbException);
- }
- }
- /// <summary>
- /// M2-06 安全门降级用:读 scene=0 出厂基准的最清晰层 Z 脉冲(FocusZ)。
- /// 查 house_autofocus_calibration 中 (tlSn,houseSn,wellSn,scene=0,deleted==null) 的基准行,
- /// 取最新一条(按 calibTime 倒序)的 focusZ;无基准/异常返回 null。
- /// 用于安全门关闭(local_autofocus_enabled=0)时按基准位置拍照(降级),不做实际对焦。
- /// 读库异常被本方法吞掉(记日志),绝不向上抛,保证不崩对焦/采集线程。
- /// </summary>
- public int? GetBaselineFocusZ(string tlSn, int houseSn, int wellSn)
- {
- try
- {
- var baseline = Db.Queryable<HouseAutofocusCalibrationDB>()
- .Where(x => x.tlSn == tlSn && x.houseSn == houseSn && x.wellSn == wellSn
- && x.scene == 0 && x.deleted == null)
- .OrderBy(x => x.calibTime, OrderByType.Desc)
- .First();
- return baseline?.focusZ;
- }
- catch (Exception ex)
- {
- ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.GetBaselineFocusZ", null, LogEnum.DbException);
- return null;
- }
- }
- #endregion
- }
- }
|