DBService.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. using IvfTl.Control.Entity.DBEntitys;
  2. using IvfTl.Control.Entity.DTO.ControllerResults;
  3. using IvfTl.Control.Entity.GlobalEntitys;
  4. using IvfTl.Control.Entity.GlobalEnums;
  5. using IvfTl.Control.Entity.InitEntitys;
  6. using ivf_tl_UtilHelper;
  7. using Microsoft.Data.Sqlite;
  8. using Newtonsoft.Json;
  9. using SqlSugar;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace IvfTl.Control.Services
  16. {
  17. public class DBService
  18. {
  19. public event Action<string, LogEnum> ErrorLogEvent;
  20. public event Action<Exception, string, string, LogEnum> ExceptionLogEvent;
  21. public string DbPath = "";
  22. private SqlSugarScope Db;
  23. public DBService()
  24. {
  25. }
  26. public bool StartDbService(string dbPath)
  27. {
  28. try
  29. {
  30. DbPath = dbPath;
  31. string connStr = new SqliteConnectionStringBuilder()
  32. {
  33. DataSource = dbPath,
  34. Mode = SqliteOpenMode.ReadWrite,
  35. }.ToString();
  36. ConnectionConfig connectionConfig = new ConnectionConfig()
  37. {
  38. ConnectionString = connStr,
  39. DbType = DbType.Sqlite,//数据库类型
  40. IsAutoCloseConnection = true //不设成true要手动close
  41. };
  42. Db = new SqlSugarScope(connectionConfig);
  43. // V-051:本地 SQLite CodeFirst 自动建 house_autofocus_calibration 镜像表
  44. // (M2-04 的标定镜像写入此表;本地库原无该表 → 写入失败无数据)。
  45. // InitTables 发 CREATE TABLE IF NOT EXISTS,已建则不动;单独 try 包裹,
  46. // 建表失败不阻断 DB 服务启动(与既有防御式风格一致)。
  47. try
  48. {
  49. Db.CodeFirst.InitTables(typeof(HouseAutofocusCalibrationDB));
  50. }
  51. catch (Exception exTable)
  52. {
  53. ExceptionLogEvent?.Invoke(exTable, "DBServiceImpl.StartDbService.InitTables", null, LogEnum.DbException);
  54. }
  55. return true;
  56. }
  57. catch (Exception ex)
  58. {
  59. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.StartDbService", null, LogEnum.DbException);
  60. return false;
  61. }
  62. }
  63. /// <summary>
  64. /// 生成实体
  65. /// </summary>
  66. public void BuildEntity(string path, string nameSpace)
  67. {
  68. Db.DbFirst.IsCreateAttribute().CreateClassFile(path, nameSpace);
  69. }
  70. /// <summary>
  71. /// 数据库更新
  72. /// </summary>
  73. /// <param name="tLInitControllerResult"></param>
  74. public void DBUpdateTLInfo(TLInitControllerResult tLInitControllerResult)
  75. {
  76. try
  77. {
  78. TLSettingDB tLSettingDB = ConvertHelper.ConvertToTLSettingDB(tLInitControllerResult.TLSetting);
  79. if (tLSettingDB == null)
  80. {
  81. return;
  82. }
  83. var oldSetting = Db.Queryable<TLSettingDB>().First(x => x.tlSn == tLSettingDB.tlSn);
  84. if (oldSetting != null)
  85. {
  86. tLSettingDB.cid = oldSetting.cid;
  87. Db.Updateable(tLSettingDB).ExecuteCommand();
  88. }
  89. else
  90. {
  91. Db.Insertable(tLSettingDB).ExecuteCommand();
  92. }
  93. UpdateHoustInfo(tLInitControllerResult.HouseList, tLSettingDB.tlSn);
  94. UpdateHouseWellSetting(tLInitControllerResult.HouseWellList, tLSettingDB.tlSn);
  95. }
  96. catch (Exception ex)
  97. {
  98. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.DBUpdateTLInfo", null, LogEnum.DbException);
  99. }
  100. }
  101. /// <summary>
  102. /// 数据库更新并获取数据
  103. /// </summary>
  104. /// <param name="tLInitData"></param>
  105. /// <returns></returns>
  106. public (TLSettingDB, List<HouseDB>, List<HouseWellSettingDB>) DBGetTLInfo(TLInitData tLInitData)
  107. {
  108. try
  109. {
  110. Db.Updateable<TLSettingDB>()
  111. .SetColumns(it => it.softwareVersion == tLInitData.softwareVersion)
  112. .SetColumns(x => x.verticalMotorPulseMax == tLInitData.verticalMotorPulseMax)
  113. .Where(it => it.tlSn == tLInitData.tlSn).ExecuteCommand();
  114. var houseSnList = tLInitData.houseLinkDataList.Select(x => x.houseSn).ToList();
  115. UpdateHouseInfo(tLInitData.houseLinkDataList, tLInitData.tlSn);
  116. UpdateHouseWellSetting(tLInitData.houseEEPROInitDTOList, tLInitData.tlSn);
  117. TLSettingDB tLSettingDB = Db.Queryable<TLSettingDB>().First(it => it.tlSn == tLInitData.tlSn);
  118. List<HouseDB> houseDBList = Db.Queryable<HouseDB>().Where(x => x.tlSn == tLInitData.tlSn && houseSnList.Contains(x.houseSn)).ToList();
  119. List<HouseWellSettingDB> HouseWellSettingDBList = Db.Queryable<HouseWellSettingDB>().Where(x => x.tlSn == tLInitData.tlSn && houseSnList.Contains(x.houseSn)).ToList();
  120. return (tLSettingDB, houseDBList, HouseWellSettingDBList);
  121. }
  122. catch (Exception ex)
  123. {
  124. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdateHouseInfo", null, LogEnum.DbException);
  125. return (new TLSettingDB(), new List<HouseDB>(), new List<HouseWellSettingDB>());
  126. }
  127. }
  128. #region DishDB
  129. public List<Dish> GetDBDishs(string tlsn)
  130. {
  131. try
  132. {
  133. var dishList = Db.Queryable<DishDB>().Where(it => it.tlSn == tlsn && it.endTime == null).ToList();
  134. if (!dishList.Any()) return new List<Dish>();
  135. List<Dish> result = new List<Dish>();
  136. foreach (var item in dishList)
  137. {
  138. Dish dish = ConvertHelper.ConvertToDish(item);
  139. result.Add(dish);
  140. var embryoList = Db.Queryable<EmbryoDB>().Where(it => it.dishId == item.id).ToList();
  141. if (!embryoList.Any()) continue;
  142. dish.Embryo = new List<Embryo>();
  143. foreach (var itemEmbryo in embryoList)
  144. {
  145. dish.Embryo.Add(ConvertHelper.ConvertToEmbryo(itemEmbryo));
  146. }
  147. }
  148. return result;
  149. }
  150. catch (Exception ex)
  151. {
  152. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.GetDBDishs", null, LogEnum.DbException);
  153. return new List<Dish>();
  154. }
  155. }
  156. public void AddDishs(List<DishDB> dishDbList, string tlsn)
  157. {
  158. try
  159. {
  160. Db.Deleteable<DishDB>().Where(x => x.tlSn == tlsn).ExecuteCommand();
  161. Db.Insertable(dishDbList).ExecuteCommand();
  162. return;
  163. var alldishList = Db.Queryable<DishDB>().Where(it => it.tlSn == tlsn).ToList();
  164. var oldDishList = alldishList.Where(it => !it.endTime.HasValue).ToList();
  165. List<DishDB> endDishs = new List<DishDB>();
  166. foreach (var item in oldDishList)
  167. {
  168. var aa = dishDbList.FirstOrDefault(x => x.id == item.id);
  169. if (aa == null)
  170. {
  171. item.endTime = DateTime.Now;
  172. endDishs.Add(item);
  173. }
  174. }
  175. if (endDishs.Any())
  176. {
  177. Db.Updateable(endDishs).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
  178. }
  179. List<DishDB> insertDishList = new List<DishDB>();
  180. List<DishDB> updateDishList = new List<DishDB>();
  181. foreach (var item in dishDbList)
  182. {
  183. var aa = alldishList.FirstOrDefault(x => x.id == item.id);
  184. if (aa == null)
  185. {
  186. insertDishList.Add(item);
  187. }
  188. else
  189. {
  190. if (aa.endTime.HasValue)
  191. {
  192. aa.endTime = null;
  193. updateDishList.Add(aa);
  194. }
  195. }
  196. }
  197. if (insertDishList.Any())
  198. {
  199. Db.Insertable(insertDishList).ExecuteCommand();
  200. }
  201. if (updateDishList.Any())
  202. {
  203. Db.Updateable(updateDishList).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
  204. }
  205. }
  206. catch (Exception ex)
  207. {
  208. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddDishs", null, LogEnum.DbException);
  209. }
  210. }
  211. public void AddDish(Dish dish, string tlsn)
  212. {
  213. try
  214. {
  215. Db.Deleteable<DishDB>().Where(x => x.id == dish.id && x.tlSn == tlsn).ExecuteCommand();
  216. Db.Deleteable<EmbryoDB>().Where(x => x.dishId == dish.id && x.tlSn == tlsn).ExecuteCommand();
  217. DishDB dishDB = ConvertHelper.ConvertToDishDB(dish);
  218. Db.Insertable(dishDB).ExecuteCommand();
  219. List<EmbryoDB> embryoDbList = new List<EmbryoDB>();
  220. foreach (var item in dish.Embryo)
  221. {
  222. embryoDbList.Add(ConvertHelper.ConvertToEmbryoDB(item));
  223. }
  224. Db.Insertable(embryoDbList).ExecuteCommand();
  225. }
  226. catch (Exception ex)
  227. {
  228. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddDish", null, LogEnum.DbException);
  229. }
  230. }
  231. public void EndDish(long id, DateTime endTime, string tlsn)
  232. {
  233. try
  234. {
  235. Db.Updateable<DishDB>().SetColumns(it => it.endTime == endTime).Where(it => it.id == id && it.tlSn == tlsn).ExecuteCommand();
  236. Db.Updateable<EmbryoDB>().SetColumns(it => new EmbryoDB()
  237. {
  238. state = (int)EmbryoState.End,
  239. stateTime = endTime,
  240. }).Where(it => it.dishId == id && it.tlSn == tlsn && it.state == (int)EmbryoState.None).ExecuteCommand();
  241. }
  242. catch (Exception ex)
  243. {
  244. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.EndDish", null, LogEnum.DbException);
  245. }
  246. }
  247. #endregion
  248. #region EmbryoDB
  249. public void AddEmbryos(List<EmbryoDB> embryoDbList, string tlsn)
  250. {
  251. try
  252. {
  253. Db.Deleteable<EmbryoDB>().Where(x => x.tlSn == tlsn).ExecuteCommand();
  254. Db.Insertable(embryoDbList).ExecuteCommand();
  255. return;
  256. var allEmbryoList = Db.Queryable<EmbryoDB>().Where(it => it.tlSn == tlsn).ToList();
  257. var ingEmbryoList = allEmbryoList.Where(x => x.state == (int)EmbryoState.None).ToList();
  258. var updateEmbryoList = new List<EmbryoDB>();
  259. foreach (var item in ingEmbryoList)
  260. {
  261. var a = embryoDbList.FirstOrDefault(x => x.id == item.id);
  262. if (a == null)
  263. {
  264. item.state = (int)EmbryoState.End;
  265. item.stateTime = DateTime.Now;
  266. updateEmbryoList.Add(item);
  267. }
  268. if (a.state != item.state)
  269. {
  270. item.state = a.state;
  271. item.stateTime = a.stateTime;
  272. updateEmbryoList.Add(item);
  273. }
  274. }
  275. if (updateEmbryoList.Any())
  276. {
  277. Db.Updateable(updateEmbryoList).UpdateColumns(it => new { it.state, it.stateTime }).ExecuteCommand();
  278. }
  279. updateEmbryoList.Clear();
  280. var insertEmbryoList = new List<EmbryoDB>();
  281. foreach (var item in embryoDbList)
  282. {
  283. var aa = allEmbryoList.FirstOrDefault(x => x.id == item.id);
  284. if (aa == null)
  285. {
  286. insertEmbryoList.Add(item);
  287. }
  288. else
  289. {
  290. if (aa.state != item.state)
  291. {
  292. aa.state = item.state;
  293. aa.stateTime = item.stateTime;
  294. updateEmbryoList.Add(aa);
  295. }
  296. }
  297. }
  298. if (insertEmbryoList.Any())
  299. {
  300. Db.Insertable(insertEmbryoList).ExecuteCommand();
  301. }
  302. if (updateEmbryoList.Any())
  303. {
  304. Db.Updateable(updateEmbryoList).UpdateColumns(it => new { it.state, it.stateTime }).ExecuteCommand();
  305. }
  306. }
  307. catch (Exception ex)
  308. {
  309. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddEmbryos", null, LogEnum.DbException);
  310. }
  311. }
  312. public void ChangeEmbryoState(Embryo embryo)
  313. {
  314. try
  315. {
  316. Db.Updateable<EmbryoDB>().SetColumns(it => new EmbryoDB()
  317. {
  318. state = embryo.state,
  319. stateTime = embryo.stateTime,
  320. }).Where(it => it.id == embryo.id && it.tlSn == embryo.tlSn).ExecuteCommand();
  321. }
  322. catch (Exception ex)
  323. {
  324. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.ChangeEmbryoState", null, LogEnum.DbException);
  325. }
  326. }
  327. #endregion
  328. #region BalanceDB
  329. public List<Balance> DbGetBalances(string tlsn)
  330. {
  331. try
  332. {
  333. var a = Db.Queryable<BalanceDB>().Where(it => it.tlSn == tlsn && it.endTime == null).ToList();
  334. if (!a.Any()) return new List<Balance>();
  335. List<Balance> result = new List<Balance>();
  336. foreach (var item in a)
  337. {
  338. result.Add(ConvertHelper.ConvertToBalance(item));
  339. }
  340. return result;
  341. }
  342. catch (Exception ex)
  343. {
  344. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.DbGetBalances", null, LogEnum.DbException);
  345. return new List<Balance>();
  346. }
  347. }
  348. public void AddBalances(List<BalanceDB> balanceDbList, string tlsn)
  349. {
  350. try
  351. {
  352. Db.Deleteable<BalanceDB>().Where(x => x.tlSn == tlsn).ExecuteCommand();
  353. Db.Insertable(balanceDbList).ExecuteCommand();
  354. return;
  355. var allBalanceList = Db.Queryable<BalanceDB>().Where(it => it.tlSn == tlsn).ToList();
  356. var ingBalacneList = allBalanceList.Where(x => !x.endTime.HasValue).ToList();
  357. var updateList = new List<BalanceDB>();
  358. foreach (var item in ingBalacneList)
  359. {
  360. var a = balanceDbList.FirstOrDefault(x => x.id == item.id);
  361. if (a == null)
  362. {
  363. item.endTime = DateTime.Now;
  364. updateList.Add(item);
  365. }
  366. }
  367. if (updateList.Any())
  368. {
  369. Db.Updateable(updateList).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
  370. }
  371. updateList.Clear();
  372. var insertList = new List<BalanceDB>();
  373. foreach (var item in balanceDbList)
  374. {
  375. var old = allBalanceList.FirstOrDefault(x => x.id == item.id);
  376. if (old == null)
  377. {
  378. insertList.Add(item);
  379. }
  380. else
  381. {
  382. if (old.endTime != item.endTime)
  383. {
  384. old.endTime = item.endTime;
  385. updateList.Add(old);
  386. }
  387. }
  388. }
  389. if (updateList.Any())
  390. {
  391. Db.Updateable(updateList).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
  392. }
  393. if (insertList.Any())
  394. {
  395. Db.Insertable(insertList).ExecuteCommand();
  396. }
  397. }
  398. catch (Exception ex)
  399. {
  400. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddBalances", null, LogEnum.DbException);
  401. }
  402. }
  403. public void AddBalance(Balance balance)
  404. {
  405. try
  406. {
  407. var oldBalance = Db.Queryable<BalanceDB>().First(x => x.id == balance.id);
  408. if (oldBalance == null)
  409. {
  410. BalanceDB balanceDB = ConvertHelper.ConvertToBalanceDB(balance);
  411. Db.Insertable(balanceDB).ExecuteCommand();
  412. }
  413. else
  414. {
  415. if (oldBalance.endTime.HasValue)
  416. {
  417. oldBalance.endTime = null;
  418. Db.Updateable(oldBalance).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
  419. }
  420. }
  421. }
  422. catch (Exception ex)
  423. {
  424. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddBalance", null, LogEnum.DbException);
  425. }
  426. }
  427. public void EndBalance(long id, DateTime? endTime)
  428. {
  429. try
  430. {
  431. Db.Updateable<BalanceDB>().SetColumns(it => it.endTime == endTime).Where(it => it.id == id).ExecuteCommand();
  432. return;
  433. var oldBalance = Db.Queryable<BalanceDB>().First(x => x.id == id);
  434. if (oldBalance == null) return;
  435. oldBalance.endTime = endTime;
  436. Db.Updateable(oldBalance).UpdateColumns(it => new { it.endTime }).ExecuteCommand();
  437. }
  438. catch (Exception ex)
  439. {
  440. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.EndBalance", null, LogEnum.DbException);
  441. }
  442. }
  443. #endregion
  444. #region houseDB
  445. /// <summary>
  446. /// 更新仓室信息
  447. /// </summary>
  448. /// <param name="houseList"></param>
  449. /// <param name="tlsn"></param>
  450. private void UpdateHoustInfo(List<House> houseList, string tlsn)
  451. {
  452. try
  453. {
  454. List<HouseDB> houseDBList = new List<HouseDB>();
  455. foreach (var item in houseList)
  456. {
  457. houseDBList.Add(ConvertHelper.ConvertToHouseDB(item));
  458. }
  459. if (!houseDBList.Any()) return;
  460. Db.Deleteable<HouseDB>().Where(x => x.tlSn == tlsn).ExecuteCommand();
  461. Db.Insertable(houseDBList).ExecuteCommand();
  462. }
  463. catch (Exception ex)
  464. {
  465. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdateHoustInfo", null, LogEnum.DbException);
  466. }
  467. }
  468. /// <summary>
  469. /// 初始化时更新仓室信息
  470. /// </summary>
  471. /// <param name="houseLinkDataList"></param>
  472. /// <param name="tlsn"></param>
  473. private void UpdateHouseInfo(List<HouseInitData> houseLinkDataList, string tlsn)
  474. {
  475. try
  476. {
  477. if (!houseLinkDataList.Any()) return;
  478. var houseSnList = houseLinkDataList.Select(x => x.houseSn).ToList();
  479. var oldHouseList = Db.Queryable<HouseDB>().Where(x => x.tlSn == tlsn && houseSnList.Contains(x.houseSn)).ToList();
  480. if (!oldHouseList.Any()) return;
  481. foreach (var item in oldHouseList)
  482. {
  483. var newItem = houseLinkDataList.FirstOrDefault(x => x.houseSn == item.houseSn);
  484. if (newItem == null) continue;
  485. item.housePort = newItem.housePort;
  486. item.ccdId = newItem.ccdId;
  487. item.ccdSn = newItem.ccdSn;
  488. item.inletValveOpeningTime = newItem.inletValveOpeningTime;
  489. item.temperatureLowerHeatingPlate = newItem.temperatureLowerHeatingPlate;
  490. item.verticalMotorSpacePulse = newItem.verticalMotorSpacePulse;
  491. }
  492. var updateHouseRsult = Db.Updateable(oldHouseList)
  493. .UpdateColumns(it => new
  494. {
  495. it.housePort,
  496. it.ccdId,
  497. it.ccdSn,
  498. it.inletValveOpeningTime,
  499. it.temperatureLowerHeatingPlate,
  500. it.verticalMotorSpacePulse
  501. }).ExecuteCommand();
  502. }
  503. catch (Exception ex)
  504. {
  505. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdateHouseInfo", null, LogEnum.DbException);
  506. }
  507. }
  508. #endregion
  509. #region HouseWellSettingDB
  510. private void UpdateHouseWellSetting(List<HouseWellSetting> houseWellSettingList, string tlSn)
  511. {
  512. try
  513. {
  514. List<HouseWellSettingDB> houseWellSettingDBList = new List<HouseWellSettingDB>();
  515. foreach (var item in houseWellSettingList)
  516. {
  517. houseWellSettingDBList.Add(ConvertHelper.ConvertToHouseWellSettingDB(item));
  518. }
  519. Db.Deleteable<HouseWellSettingDB>().Where(x => x.tlSn == tlSn).ExecuteCommand();
  520. Db.Insertable(houseWellSettingDBList).ExecuteCommand();
  521. }
  522. catch (Exception ex)
  523. {
  524. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdateHouseWellSetting", null, LogEnum.DbException);
  525. }
  526. }
  527. private void UpdateHouseWellSetting(List<HouseEEPROInfo> houseEEPROInfos, string tlsn)
  528. {
  529. try
  530. {
  531. if (!houseEEPROInfos.Any()) return;
  532. var houseSnList = houseEEPROInfos.Select(x => x.houseSn).ToList();
  533. var oldHouseList = Db.Queryable<HouseWellSettingDB>().Where(x => x.tlSn == tlsn && houseSnList.Contains(x.houseSn)).ToList();
  534. if (!oldHouseList.Any()) return;
  535. foreach (var item in oldHouseList)
  536. {
  537. var newItem = houseEEPROInfos.FirstOrDefault(x => x.houseSn == item.houseSn);
  538. if (newItem == null) continue;
  539. item.eepromClearPosition = newItem.eepromClearPosition;
  540. switch (item.wellSn)
  541. {
  542. case 1:
  543. item.horizontalMotorPosition = newItem.hwell1;
  544. break;
  545. case 2:
  546. item.horizontalMotorPosition = newItem.hwell2;
  547. break;
  548. case 3:
  549. item.horizontalMotorPosition = newItem.hwell3;
  550. break;
  551. case 4:
  552. item.horizontalMotorPosition = newItem.hwell4;
  553. break;
  554. case 5:
  555. item.horizontalMotorPosition = newItem.hwell5;
  556. break;
  557. case 6:
  558. item.horizontalMotorPosition = newItem.hwell6;
  559. break;
  560. case 7:
  561. item.horizontalMotorPosition = newItem.hwell7;
  562. break;
  563. case 8:
  564. item.horizontalMotorPosition = newItem.hwell8;
  565. break;
  566. case 9:
  567. item.horizontalMotorPosition = newItem.hwell9;
  568. break;
  569. case 10:
  570. item.horizontalMotorPosition = newItem.hwell10;
  571. break;
  572. case 11:
  573. item.horizontalMotorPosition = newItem.hwell11;
  574. break;
  575. case 12:
  576. item.horizontalMotorPosition = newItem.hwell12;
  577. break;
  578. case 13:
  579. item.horizontalMotorPosition = newItem.hwell13;
  580. break;
  581. case 14:
  582. item.horizontalMotorPosition = newItem.hwell14;
  583. break;
  584. case 15:
  585. item.horizontalMotorPosition = newItem.hwell15;
  586. break;
  587. case 16:
  588. item.horizontalMotorPosition = newItem.hwell16;
  589. break;
  590. }
  591. }
  592. var updateHouseRsult = Db.Updateable(oldHouseList)
  593. .UpdateColumns(it => new
  594. {
  595. it.horizontalMotorPosition,
  596. it.eepromClearPosition,
  597. }).ExecuteCommand();
  598. }
  599. catch (Exception ex)
  600. {
  601. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdateHouseWellSetting", null, LogEnum.DbException);
  602. }
  603. }
  604. #endregion
  605. #region pictureDB
  606. public void AddPictrue(PictureDB pictureDB)
  607. {
  608. try
  609. {
  610. var oldPic = Db.Queryable<PictureDB>().First(x => x.TlSn == pictureDB.TlSn && x.SourceImageName == pictureDB.SourceImageName);
  611. if (oldPic == null)
  612. {
  613. Db.Insertable(pictureDB).ExecuteCommand();
  614. }
  615. }
  616. catch (Exception ex)
  617. {
  618. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.AddPictrue", null, LogEnum.DbException);
  619. }
  620. }
  621. public void DeletePictrue(string fileName, string tlsn)
  622. {
  623. try
  624. {
  625. Db.Deleteable<PictureDB>().Where(x => x.TlSn == tlsn && x.SourceImageName == fileName).ExecuteCommand();
  626. }
  627. catch (Exception ex)
  628. {
  629. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.DeletePictrue", null, LogEnum.DbException);
  630. }
  631. }
  632. public PictureDB SearchPicture(string fileName, string tlsn)
  633. {
  634. try
  635. {
  636. var pic = Db.Queryable<PictureDB>().First(x => x.TlSn == tlsn && x.SourceImageName == fileName);
  637. return pic;
  638. }
  639. catch (Exception ex)
  640. {
  641. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.SearchPicture", null, LogEnum.DbException);
  642. return null;
  643. }
  644. }
  645. #endregion
  646. #region HouseWellPhotoDB
  647. /// <summary>
  648. /// 更新拍照位置
  649. /// </summary>
  650. /// <param name="houses"></param>
  651. /// <param name="housesn"></param>
  652. /// <param name="tlsn"></param>
  653. /// <param name="positionType">0表示自动对焦</param>
  654. public void UpdatePosition(List<HouseWellPhotoDB> houses, int housesn, string tlsn, int positionType)
  655. {
  656. try
  657. {
  658. var wellList = houses.Select(x => x.wellSn).Distinct().ToList();
  659. //Db.Deleteable<HouseWellPhotoDB>().Where(x => x.tlSn == tlsn && x.houseSn == housesn && wellList.Contains(x.wellSn) && x.positionType == positionType).ExecuteCommand();
  660. if (positionType == 0)
  661. {
  662. Db.Deleteable<HouseWellPhotoDB>().Where(x => x.tlSn == tlsn && x.houseSn == housesn && wellList.Contains(x.wellSn) && x.positionType == 0).ExecuteCommand();
  663. }
  664. else
  665. {
  666. Db.Deleteable<HouseWellPhotoDB>().Where(x => x.tlSn == tlsn && x.houseSn == housesn && wellList.Contains(x.wellSn) && x.positionType != 0).ExecuteCommand();
  667. }
  668. Db.Insertable(houses).ExecuteCommand();
  669. }
  670. catch (Exception ex)
  671. {
  672. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.UpdatePosition", null, LogEnum.DbException);
  673. }
  674. }
  675. /// <summary>
  676. /// 获取电机位置
  677. /// </summary>
  678. /// <param name="wellSn"></param>
  679. /// <param name="housesn"></param>
  680. /// <param name="tlsn"></param>
  681. /// <param name="positionType">0表示自动对焦</param>
  682. /// <returns></returns>
  683. public List<HouseWellPhotoDB> GetPosition(List<int> wellSn, int housesn, string tlsn, int positionType)
  684. {
  685. try
  686. {
  687. //return Db.Queryable<HouseWellPhotoDB>().Where(x => x.tlSn == tlsn && x.houseSn == housesn && wellSn.Contains(x.wellSn) && x.positionType == positionType).ToList();
  688. if (positionType == 0)
  689. {
  690. return Db.Queryable<HouseWellPhotoDB>().Where(x => x.tlSn == tlsn && x.houseSn == housesn && wellSn.Contains(x.wellSn) && x.positionType == 0).ToList();
  691. }
  692. else
  693. {
  694. return Db.Queryable<HouseWellPhotoDB>().Where(x => x.tlSn == tlsn && x.houseSn == housesn && wellSn.Contains(x.wellSn) && x.positionType != 0).ToList();
  695. }
  696. }
  697. catch (Exception ex)
  698. {
  699. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.GetPosition", null, LogEnum.DbException);
  700. return new List<HouseWellPhotoDB>();
  701. }
  702. }
  703. #endregion
  704. #region HouseAutofocusCalibrationDB(M2-04 标定结果库内镜像)
  705. /// <summary>
  706. /// 标定结果镜像入 house_autofocus_calibration(真相源仍为本地 calibration.json,见 12 §2.7)。
  707. /// scene=0 出厂基准:按 (tlSn,houseSn,wellSn,scene=0,deleted==null) upsert(每 well 唯一);
  708. /// scene=1 日常对焦:直接 Insertable append(留历史,清理任务只删 scene=1)。
  709. /// 入参取标定产出的原始值(WellCalib 解包后传入),本方法不依赖 IvfTl.AutoFocus 程序集。
  710. /// 写库异常被本方法吞掉(记日志),绝不向上抛,保证不崩对焦/采集线程。
  711. /// </summary>
  712. /// <param name="tlSn">tl 设备 sn</param>
  713. /// <param name="houseSn">仓室编号</param>
  714. /// <param name="wellSn">well 编号</param>
  715. /// <param name="scene">场景:0=出厂基准 1=日常对焦</param>
  716. /// <param name="focusZ">最清晰层 Z 脉冲(锚点)</param>
  717. /// <param name="exposure">曝光(×100µs)</param>
  718. /// <param name="horizontalPulse">水平电机位置脉冲</param>
  719. /// <param name="peakRatio">清晰度峰比</param>
  720. /// <param name="circleFound">是否检到 well 圆</param>
  721. /// <param name="centerOffsetPct">居中偏移百分比</param>
  722. /// <param name="note">备注</param>
  723. /// <param name="source">结果来源,默认 LOCAL_JSON</param>
  724. public void SaveAutofocusCalibration(string tlSn, int houseSn, int wellSn, int scene,
  725. int focusZ, int exposure, int horizontalPulse, decimal peakRatio, bool circleFound,
  726. decimal centerOffsetPct, string note = "", string source = "LOCAL_JSON")
  727. {
  728. try
  729. {
  730. DateTime now = DateTime.Now;
  731. if (scene == 0)
  732. {
  733. // 出厂基准:每 well 唯一,存在则更新(upsert),不存在则插入;清理任务不删 scene=0。
  734. var old = Db.Queryable<HouseAutofocusCalibrationDB>()
  735. .First(x => x.tlSn == tlSn && x.houseSn == houseSn && x.wellSn == wellSn
  736. && x.scene == 0 && x.deleted == null);
  737. if (old != null)
  738. {
  739. old.focusZ = focusZ;
  740. old.exposure = exposure;
  741. old.horizontalPulse = horizontalPulse;
  742. old.peakRatio = peakRatio;
  743. old.circleFound = circleFound ? 1 : 0;
  744. old.centerOffsetPct = centerOffsetPct;
  745. old.calibTime = now;
  746. old.source = source;
  747. old.note = note;
  748. old.updateTime = now;
  749. Db.Updateable(old).ExecuteCommand();
  750. return;
  751. }
  752. }
  753. // scene=1 日常对焦 append,或 scene=0 首次插入。
  754. var entity = new HouseAutofocusCalibrationDB
  755. {
  756. tlSn = tlSn,
  757. houseSn = houseSn,
  758. wellSn = wellSn,
  759. scene = scene,
  760. focusZ = focusZ,
  761. exposure = exposure,
  762. horizontalPulse = horizontalPulse,
  763. peakRatio = peakRatio,
  764. circleFound = circleFound ? 1 : 0,
  765. centerOffsetPct = centerOffsetPct,
  766. calibTime = now,
  767. source = source,
  768. note = note,
  769. createTime = now,
  770. };
  771. Db.Insertable(entity).ExecuteCommand();
  772. }
  773. catch (Exception ex)
  774. {
  775. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.SaveAutofocusCalibration", null, LogEnum.DbException);
  776. }
  777. }
  778. /// <summary>
  779. /// M2-06 安全门降级用:读 scene=0 出厂基准的最清晰层 Z 脉冲(FocusZ)。
  780. /// 查 house_autofocus_calibration 中 (tlSn,houseSn,wellSn,scene=0,deleted==null) 的基准行,
  781. /// 取最新一条(按 calibTime 倒序)的 focusZ;无基准/异常返回 null。
  782. /// 用于安全门关闭(local_autofocus_enabled=0)时按基准位置拍照(降级),不做实际对焦。
  783. /// 读库异常被本方法吞掉(记日志),绝不向上抛,保证不崩对焦/采集线程。
  784. /// </summary>
  785. public int? GetBaselineFocusZ(string tlSn, int houseSn, int wellSn)
  786. {
  787. try
  788. {
  789. var baseline = Db.Queryable<HouseAutofocusCalibrationDB>()
  790. .Where(x => x.tlSn == tlSn && x.houseSn == houseSn && x.wellSn == wellSn
  791. && x.scene == 0 && x.deleted == null)
  792. .OrderBy(x => x.calibTime, OrderByType.Desc)
  793. .First();
  794. return baseline?.focusZ;
  795. }
  796. catch (Exception ex)
  797. {
  798. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.GetBaselineFocusZ", null, LogEnum.DbException);
  799. return null;
  800. }
  801. }
  802. /// <summary>
  803. /// G4-1 / 需求文档12 §2.7:对焦标定数据清理任务。
  804. /// 物理删除 house_autofocus_calibration 中 scene=1(日常对焦) 且 calibTime 早于 (now - keepDays) 的记录;
  805. /// scene=0(出厂基准) 永不删除——否则日常对焦失去参照基准(12 §2.7 约束①)。
  806. /// keepDays&lt;=0 视为不清理(安全阀:避免 cutoff 异常误删全部)。
  807. /// 全 try 兜底,异常吞掉记日志,绝不向上抛(不崩采集/对焦线程)。返回删除条数。
  808. /// </summary>
  809. /// <param name="keepDays">保留天数(来自 tl_setting.clean_autofocus_data 下发值,默认30)</param>
  810. public int CleanAutofocusData(int keepDays)
  811. {
  812. try
  813. {
  814. if (keepDays <= 0) return 0;
  815. DateTime cutoff = DateTime.Now.AddDays(-keepDays);
  816. // Deleteable 物理删除(本表无逻辑删除全局过滤):retention 需真正缩表,故物理删而非置 deleted。
  817. int n = Db.Deleteable<HouseAutofocusCalibrationDB>()
  818. .Where(x => x.scene == 1 && x.calibTime < cutoff)
  819. .ExecuteCommand();
  820. return n;
  821. }
  822. catch (Exception ex)
  823. {
  824. ExceptionLogEvent?.Invoke(ex, "DBServiceImpl.CleanAutofocusData", null, LogEnum.DbException);
  825. return 0;
  826. }
  827. }
  828. #endregion
  829. }
  830. }