DBServiceImplNo.cs 32 KB

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