No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

日誌.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. using UnityEngine.Networking;
  7. using System.Text.RegularExpressions;
  8. using System.Data.SqlClient;
  9. using TMPro;
  10. using System;
  11. using System.IO;
  12. using ZXing;
  13. using ZXing.QrCode;
  14. using System.Net;
  15. using SimpleJSON;
  16. using System.ComponentModel;
  17. using System.Runtime.InteropServices;
  18. public class 日誌 : MonoBehaviour
  19. {
  20. public TMP_Dropdown 年;
  21. public TMP_Dropdown 月;
  22. public TMP_Dropdown 日;
  23. public TMP_Dropdown 字體;
  24. public TMP_Dropdown 字體1;
  25. public Toggle 歷史;
  26. public Toggle 全部;
  27. public Toggle 編輯;
  28. public Toggle 編輯1;
  29. [SerializeField] GameObject 回主頁紐;
  30. [SerializeField] GameObject 回選擇書籍紐;
  31. [SerializeField] GameObject 新增紐;
  32. [SerializeField] GameObject 存檔紐;
  33. [SerializeField] GameObject 細板;
  34. [SerializeField] GameObject 寬板;
  35. [SerializeField] GameObject 選擇書籍;
  36. [SerializeField] TextMeshProUGUI 日期;
  37. [SerializeField] TMP_InputField 日誌s;
  38. [SerializeField] TextMeshProUGUI 日期1;
  39. [SerializeField] TMP_InputField 日誌s1;
  40. [SerializeField] TMP_InputField 輸入;
  41. [SerializeField] TMP_InputField 輸入1;
  42. public CanvasScaler canvasScaler;
  43. [SerializeField] GameObject 讀取面板;
  44. [SerializeField] TextMeshProUGUI 讀取;
  45. [SerializeField] GameObject 彈跳面板;
  46. [SerializeField] TextMeshProUGUI 彈跳文字;
  47. private string loadingText = "";
  48. private int dotCount = 0;
  49. JSONArray 書籍清單;
  50. public GameObject buttonPrefab;
  51. public Transform buttonParent;
  52. public GameObject buttonceneter;
  53. private List<GameObject> copy = new List<GameObject>();
  54. // Start is called before the first frame update
  55. void Start()
  56. {
  57. //Main.Global.固寬=900f;
  58. //Main.Global.固高=1900f;
  59. //Main.Global.固比=1f;
  60. //Main.Global.螢幕格式="瘦";
  61. //Main.Global.sql="Data Source=180.177.181.162,5264;Initial Catalog=BIFENG-ERP-SYS;User ID=b70340;Password=0911274990";
  62. Screen.autorotateToPortrait = false;
  63. Screen.orientation = ScreenOrientation.Portrait;
  64. canvasScaler.referenceResolution = new Vector2(Main.Global.固寬, Main.Global.固高);
  65. canvasScaler.matchWidthOrHeight=Main.Global.固比;
  66. StartCoroutine(AnimateText());
  67. 讀取年份();
  68. 讀取書籍清單();
  69. 選擇書籍.SetActive(true);
  70. 新增紐.SetActive(true);
  71. 存檔紐.SetActive(false);
  72. 細板.SetActive(false);
  73. 寬板.SetActive(false);
  74. 回主頁紐.SetActive(true);
  75. 回選擇書籍紐.SetActive(false);
  76. 字體.value = 1;
  77. 字體1.value = 1;
  78. 字體.onValueChanged.AddListener((value) => 改字());
  79. 字體1.onValueChanged.AddListener((value) => 改字1());
  80. 輸入.onSubmit.AddListener(OnSubmit);
  81. 輸入1.onSubmit.AddListener(OnSubmit1);
  82. }
  83. private void 改字()
  84. {
  85. int 字體大小值 = int.Parse(字體.options[字體.value].text);
  86. 日誌s.textComponent.fontSize = 字體大小值;
  87. }
  88. private void 改字1()
  89. {
  90. int 字體大小值 = int.Parse(字體1.options[字體1.value].text);
  91. 日誌s1.textComponent.fontSize = 字體大小值;
  92. }
  93. IEnumerator AnimateText()
  94. {
  95. string qqq = 讀取.text.ToString();
  96. while (true)
  97. {
  98. yield return new WaitForSeconds(0.5f);
  99. if (dotCount < 3)
  100. {
  101. loadingText += ".";
  102. dotCount++;
  103. }
  104. else
  105. {
  106. loadingText = "";
  107. dotCount = 0;
  108. }
  109. 讀取.text = qqq+loadingText;
  110. }
  111. }
  112. public void 讀取年份()
  113. {
  114. 讀取面板.SetActive(true);
  115. 年.options.Clear();
  116. List<TMP_Dropdown.OptionData> options = new List<TMP_Dropdown.OptionData>();
  117. using (SqlConnection connection = new SqlConnection(Main.Global.sql))
  118. {
  119. try
  120. {
  121. connection.Open();
  122. string query = "SELECT LEFT(西元, 4) AS 西元, 年 FROM 萬年曆 WHERE (RIGHT(西元, 5) LIKE '05/05') GROUP BY LEFT(西元, 4), 年 ORDER BY 西元, 年";
  123. using (SqlCommand command = new SqlCommand(query, connection))
  124. {
  125. using (SqlDataReader reader = command.ExecuteReader())
  126. {
  127. while (reader.Read()) // 假設只取前兩筆資料
  128. {
  129. options.Add(new TMP_Dropdown.OptionData(reader["西元"].ToString()));
  130. }
  131. 年.AddOptions(options);
  132. DateTime currentDate = DateTime.Now;
  133. int currentIndex = 年.options.FindIndex(option => option.text == currentDate.Year.ToString());
  134. 年.value = currentIndex;
  135. string monStr = $"{currentDate.Month:00}";
  136. int currentIndex1 = 月.options.FindIndex(option => option.text == monStr);
  137. 月.value = currentIndex1;
  138. string yearStr=年.options[年.value].text;
  139. string monthStr= 月.options[月.value].text;
  140. if (int.TryParse(yearStr, out int year) && int.TryParse(monthStr, out int month))
  141. {
  142. 日期讀取(year, month);
  143. string dayStr = $"{currentDate.Day:00}";
  144. int currentIndex2 = 日.options.FindIndex(option => option.text == dayStr);
  145. 日.value = currentIndex2;
  146. }
  147. 年.onValueChanged.AddListener((value) => OnDropdown1ValueChanged());
  148. 月.onValueChanged.AddListener((value) => OnDropdown1ValueChanged());
  149. 日.onValueChanged.AddListener((value) => OnDropdown1ValueChanged1());
  150. }
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. Debug.LogError("Error connecting to the database: " + ex.Message);
  156. 彈跳面板.SetActive(true);
  157. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  158. }
  159. finally
  160. {
  161. connection.Close();
  162. }
  163. }
  164. 讀取面板.SetActive(false);
  165. }
  166. private void OnDropdown1ValueChanged()
  167. {
  168. string yearStr = 年.options[年.value].text;
  169. string monthStr = 月.options[月.value].text;
  170. if (int.TryParse(yearStr, out int year) && int.TryParse(monthStr, out int month))
  171. {
  172. 日期讀取(year, month);
  173. if (歷史.isOn==true)
  174. {
  175. 讀取書籍清單();
  176. }
  177. }
  178. }
  179. private void OnDropdown1ValueChanged1()
  180. {
  181. if (歷史.isOn==true)
  182. {
  183. 讀取書籍清單();
  184. }
  185. }
  186. public void 日期讀取(int year , int month)
  187. {
  188. int daysInMonth = DateTime.DaysInMonth(year, month);
  189. 日.options.Clear();
  190. List<TMP_Dropdown.OptionData> options = new List<TMP_Dropdown.OptionData>();
  191. for (int i = 1; i <= daysInMonth; i++)
  192. {
  193. string dayStr = i < 10 ? "0" + i.ToString() : i.ToString();
  194. options.Add(new TMP_Dropdown.OptionData(dayStr));
  195. }
  196. 日.AddOptions(options);
  197. }
  198. public void 讀取書籍清單()
  199. {
  200. 讀取面板.SetActive(true);
  201. 書籍清單 = new JSONArray();
  202. using (SqlConnection connection = new SqlConnection(Main.Global.sql))
  203. {
  204. try
  205. {
  206. connection.Open();
  207. string query = "";
  208. if (歷史.isOn==true)
  209. {
  210. string str = 月.options[月.value].text + "/" + 日.options[日.value].text;
  211. query = "SELECT 日期, 日誌 FROM 日誌資料表 WHERE (日期 LIKE N'%" + str + "') ORDER BY 日期 DESC";
  212. }
  213. else
  214. {
  215. query = "SELECT 日期, 日誌 FROM 日誌資料表 ORDER BY 日期 DESC";
  216. }
  217. using (SqlCommand command = new SqlCommand(query, connection))
  218. {
  219. using (SqlDataReader reader = command.ExecuteReader())
  220. {
  221. while (reader.Read()) // 假設只取前兩筆資料
  222. {
  223. JSONNode newValue = new JSONObject();
  224. newValue["日期"] = reader["日期"].ToString();
  225. newValue["日誌"] = reader["日誌"].ToString();
  226. 書籍清單.Add(newValue);
  227. }
  228. }
  229. }
  230. }
  231. catch (Exception ex)
  232. {
  233. Debug.LogError("Error connecting to the database: " + ex.Message);
  234. 彈跳面板.SetActive(true);
  235. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  236. }
  237. finally
  238. {
  239. connection.Close();
  240. }
  241. }
  242. if (copy != null)
  243. {
  244. DeleteCopies(copy);
  245. }
  246. int sum = 5;
  247. for (int i = 0; i < 書籍清單.Count; i++)
  248. {
  249. GameObject buttonObj = Instantiate(buttonPrefab) as GameObject;
  250. buttonObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(2, -sum);
  251. buttonObj.transform.SetParent(buttonParent, false);
  252. TextMeshProUGUI text = buttonObj.GetComponentInChildren<TextMeshProUGUI>();
  253. Button bt = buttonObj.GetComponentInChildren<Button>();
  254. int index = i;
  255. bt.onClick.AddListener(() =>
  256. {
  257. 回主頁紐.SetActive(false);
  258. 回選擇書籍紐.SetActive(true);
  259. 選擇書籍.SetActive(false);
  260. 新增紐.SetActive(false);
  261. 存檔紐.SetActive(true);
  262. string jsonString = 書籍清單[index][1].ToString().Trim('"');
  263. string unescapedString = Regex.Unescape(jsonString);
  264. if (Main.Global.螢幕格式=="胖")
  265. {
  266. 細板.SetActive(false);
  267. 寬板.SetActive(true);
  268. 日誌s1.text = unescapedString;
  269. 日期1.text = 書籍清單[index][0].ToString().Trim('"');
  270. 日誌s1.interactable = false;
  271. }
  272. else
  273. {
  274. 細板.SetActive(true);
  275. 寬板.SetActive(false);
  276. 日誌s.text = unescapedString;
  277. 日誌s.interactable = false;
  278. 日期.text =書籍清單[index][0].ToString().Trim('"');
  279. }
  280. });
  281. text.text = 書籍清單[i][0].ToString().Trim('"');
  282. sum+=120;
  283. copy.Add(buttonObj);
  284. }
  285. buttonceneter.GetComponent<RectTransform>().sizeDelta = new Vector2(buttonceneter.GetComponent<RectTransform>().sizeDelta.x, sum);
  286. 讀取面板.SetActive(false);
  287. }
  288. public void 存檔()
  289. {
  290. string str = "";
  291. 讀取面板.SetActive(true);
  292. using (SqlConnection connection = new SqlConnection(Main.Global.sql))
  293. {
  294. try
  295. {
  296. connection.Open();
  297. string query = "";
  298. if (Main.Global.螢幕格式=="胖")
  299. {
  300. query = "SELECT 日期 FROM 日誌資料表 WHERE 日期 LIKE N'" + 日期1.text.ToString() + "'";
  301. }
  302. else
  303. {
  304. query = "SELECT 日期 FROM 日誌資料表 WHERE 日期 LIKE N'" + 日期.text.ToString() + "'";
  305. }
  306. using (SqlCommand command = new SqlCommand(query, connection))
  307. {
  308. using (SqlDataReader reader = command.ExecuteReader())
  309. {
  310. while (reader.Read()) // 假設只取前兩筆資料
  311. {
  312. str = reader["日期"].ToString();
  313. }
  314. }
  315. }
  316. }
  317. catch (Exception ex)
  318. {
  319. Debug.LogError("Error connecting to the database: " + ex.Message);
  320. 彈跳面板.SetActive(true);
  321. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  322. }
  323. finally
  324. {
  325. connection.Close();
  326. }
  327. }
  328. if (str == "")
  329. {
  330. 存檔新增();
  331. }
  332. else
  333. {
  334. 存檔修改();
  335. }
  336. 讀取書籍清單();
  337. 選擇書籍.SetActive(true);
  338. 細板.SetActive(false);
  339. 寬板.SetActive(false);
  340. 讀取面板.SetActive(false);
  341. 彈跳面板.SetActive(true);
  342. 彈跳文字.text="儲存完成";
  343. }
  344. public void 存檔新增()
  345. {
  346. using (SqlConnection connection = new SqlConnection(Main.Global.sql))
  347. {
  348. try
  349. {
  350. connection.Open();
  351. string query = "";
  352. if (Main.Global.螢幕格式=="胖")
  353. {
  354. query = "INSERT INTO 日誌資料表 (日期, 日誌) SELECT N'" + 日期1.text.ToString() + "', N'" + 日誌s1.text.ToString() + "'";
  355. }
  356. else
  357. {
  358. query = "INSERT INTO 日誌資料表 (日期, 日誌) SELECT N'" + 日期.text.ToString() + "', N'" + 日誌s.text.ToString() + "'";
  359. }
  360. using (SqlCommand command = new SqlCommand(query, connection))
  361. {
  362. using (SqlDataReader reader = command.ExecuteReader())
  363. {
  364. while (reader.Read())
  365. {
  366. }
  367. }
  368. }
  369. }
  370. catch (Exception ex)
  371. {
  372. Debug.LogError("Error connecting to the database: " + ex.Message);
  373. 彈跳面板.SetActive(true);
  374. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  375. }
  376. finally
  377. {
  378. connection.Close();
  379. }
  380. }
  381. }
  382. public void 存檔修改()
  383. {
  384. using (SqlConnection connection = new SqlConnection(Main.Global.sql))
  385. {
  386. try
  387. {
  388. connection.Open();
  389. string query = "";
  390. if (Main.Global.螢幕格式=="胖")
  391. {
  392. query = "UPDATE 日誌資料表 SET 日誌 = N'" + 日誌s1.text.ToString() + "' WHERE 日期 LIKE N'" + 日期1.text.ToString() + "'";
  393. }
  394. else
  395. {
  396. query = "UPDATE 日誌資料表 SET 日誌 = N'" + 日誌s.text.ToString() + "' WHERE 日期 LIKE N'" + 日期.text.ToString() + "'";
  397. }
  398. using (SqlCommand command = new SqlCommand(query, connection))
  399. {
  400. using (SqlDataReader reader = command.ExecuteReader())
  401. {
  402. while (reader.Read())
  403. {
  404. }
  405. }
  406. }
  407. }
  408. catch (Exception ex)
  409. {
  410. Debug.LogError("Error connecting to the database: " + ex.Message);
  411. 彈跳面板.SetActive(true);
  412. 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
  413. }
  414. finally
  415. {
  416. connection.Close();
  417. }
  418. }
  419. }
  420. public void 彈跳面板關閉()
  421. {
  422. 彈跳面板.SetActive(false);
  423. 彈跳文字.text = "";
  424. }
  425. public void 輸入文字1()
  426. {
  427. if (編輯1.isOn == false)
  428. {
  429. string str = 輸入1.text;
  430. if (日誌s1.text=="")
  431. {
  432. 日誌s1.text+=str;
  433. }
  434. else
  435. {
  436. 日誌s1.text+= "\n" +str;
  437. }
  438. 輸入1.text = "";
  439. }
  440. }
  441. public void 編輯切換1()
  442. {
  443. if (編輯1.isOn == false)
  444. {
  445. 日誌s1.interactable = false;
  446. 輸入1.interactable = true;
  447. }
  448. else
  449. {
  450. 日誌s1.interactable = true;
  451. 輸入1.interactable = false;
  452. 彈跳面板.SetActive(true);
  453. 彈跳文字.text = "如有排版需求,請至PC版,再手機上排版比較不方便";
  454. }
  455. }
  456. void OnSubmit(string inputText)
  457. {
  458. string str = 輸入.text;
  459. if (日誌s.text=="")
  460. {
  461. 日誌s.text+=str;
  462. }
  463. else
  464. {
  465. 日誌s.text+= "\n" +str;
  466. }
  467. 輸入.text = "";
  468. }
  469. void OnSubmit1(string inputText)
  470. {
  471. string str = 輸入1.text;
  472. if (日誌s1.text=="")
  473. {
  474. 日誌s1.text+=str;
  475. }
  476. else
  477. {
  478. 日誌s1.text+= "\n" +str;
  479. }
  480. 輸入1.text = "";
  481. }
  482. public void 輸入文字()
  483. {
  484. if (編輯.isOn == false)
  485. {
  486. string str = 輸入.text;
  487. if (日誌s.text=="")
  488. {
  489. 日誌s.text+=str;
  490. }
  491. else
  492. {
  493. 日誌s.text+= "\n" +str;
  494. }
  495. 輸入.text = "";
  496. }
  497. }
  498. public void 編輯切換()
  499. {
  500. if (編輯.isOn == false)
  501. {
  502. 日誌s.interactable = false;
  503. 輸入.interactable = true;
  504. }
  505. else
  506. {
  507. 日誌s.interactable = true;
  508. 輸入.interactable = false;
  509. 彈跳面板.SetActive(true);
  510. 彈跳文字.text = "如有排版需求,請至PC版,再手機上排版比較不方便";
  511. }
  512. }
  513. public void t1_click()
  514. {
  515. if (歷史.isOn==false && 全部.isOn==false )
  516. {
  517. 歷史.isOn = true;
  518. 全部.isOn = false;
  519. }
  520. else if (歷史.isOn==true && 全部==true)
  521. {
  522. 歷史.isOn = true;
  523. 全部.isOn = false;
  524. 讀取書籍清單();
  525. }
  526. }
  527. public void t2_click()
  528. {
  529. if (歷史.isOn==false && 全部.isOn==false)
  530. {
  531. 歷史.isOn = false;
  532. 全部.isOn = true;
  533. }
  534. else if (歷史.isOn==true && 全部.isOn==true)
  535. {
  536. 歷史.isOn = false;
  537. 全部.isOn = true;
  538. 讀取書籍清單();
  539. }
  540. }
  541. public void 新增日()
  542. {
  543. string str = "";
  544. 讀取面板.SetActive(true);
  545. using (SqlConnection connection = new SqlConnection(Main.Global.sql))
  546. {
  547. try
  548. {
  549. connection.Open();
  550. string query = "";
  551. string str1 = 年.options[年.value].text + "/" + 月.options[月.value].text + "/" + 日.options[日.value].text;
  552. if (Main.Global.螢幕格式=="胖")
  553. {
  554. query = "SELECT 日期 FROM 日誌資料表 WHERE 日期 LIKE N'" + str1 + "'";
  555. }
  556. else
  557. {
  558. query = "SELECT 日期 FROM 日誌資料表 WHERE 日期 LIKE N'" + str1 + "'";
  559. }
  560. using (SqlCommand command = new SqlCommand(query, connection))
  561. {
  562. using (SqlDataReader reader = command.ExecuteReader())
  563. {
  564. while (reader.Read()) // 假設只取前兩筆資料
  565. {
  566. str = reader["日期"].ToString();
  567. }
  568. }
  569. }
  570. }
  571. catch (Exception ex)
  572. {
  573. Debug.LogError("Error connecting to the database: " + ex.Message);
  574. }
  575. finally
  576. {
  577. connection.Close();
  578. }
  579. }
  580. 讀取面板.SetActive(false);
  581. if (str == "")
  582. {
  583. 回主頁紐.SetActive(false);
  584. 回選擇書籍紐.SetActive(true);
  585. 選擇書籍.SetActive(false);
  586. 新增紐.SetActive(false);
  587. 存檔紐.SetActive(true);
  588. if (Main.Global.螢幕格式=="胖")
  589. {
  590. 細板.SetActive(false);
  591. 寬板.SetActive(true);
  592. 日誌s1.text = "";
  593. 日誌s1.interactable = false;
  594. 日期1.text =年.options[年.value].text + "/" + 月.options[月.value].text + "/" + 日.options[日.value].text;
  595. }
  596. else
  597. {
  598. 細板.SetActive(true);
  599. 寬板.SetActive(false);
  600. 日誌s.text = "";
  601. 日誌s.interactable = false;
  602. 日期.text =年.options[年.value].text + "/" + 月.options[月.value].text + "/" + 日.options[日.value].text;
  603. }
  604. }
  605. else
  606. {
  607. 彈跳面板.SetActive(true);
  608. 彈跳文字.text = "已有該日日誌";
  609. }
  610. }
  611. public void 回選書()
  612. {
  613. 選擇書籍.SetActive(true);
  614. 細板.SetActive(false);
  615. 寬板.SetActive(false);
  616. 回主頁紐.SetActive(true);
  617. 回選擇書籍紐.SetActive(false);
  618. 新增紐.SetActive(true);
  619. 存檔紐.SetActive(false);
  620. 日誌s.text = "";
  621. 日期.text ="";
  622. 日誌s1.text = "";
  623. 日期1.text ="";
  624. 輸入.text ="";
  625. 輸入1.text ="";
  626. }
  627. public void 回主()
  628. {
  629. change_Secen(1);
  630. }
  631. private void DeleteCopies(List<GameObject> copyList)
  632. {
  633. foreach (GameObject copy in copyList)
  634. {
  635. Destroy(copy);
  636. }
  637. copyList.Clear();
  638. }
  639. public void change_Secen(int _screenNum)
  640. {
  641. SceneManager.LoadScene(_screenNum);
  642. }
  643. public void 截圖()
  644. {
  645. string fileName = string.Format("{0}/screenshot_{1}.png", Application.persistentDataPath, System.DateTime.Now.ToString("yyyyMMdd_HHmmss"));
  646. ScreenCapture.CaptureScreenshot(fileName);
  647. }
  648. }