daily_records.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const mysql = require('mysql2/promise');
  2. const tools = require('../tools');
  3. const database_config = tools.getDataBaseConfig();
  4. class DailyRecordManager {
  5. constructor() {
  6. this.pool = mysql.createPool(database_config);
  7. }
  8. // 创建记录
  9. async createRecord(date, content) {
  10. try {
  11. const connection = await this.pool.getConnection();
  12. try {
  13. const [result] = await connection.execute(
  14. 'INSERT IGNORE INTO daily_records (record_date, content) VALUES (?, ?)',
  15. [date, JSON.stringify(content)]
  16. );
  17. return {
  18. success: result.affectedRows > 0,
  19. message: result.affectedRows > 0 ? '记录创建成功' : '该日期记录已存在'
  20. };
  21. } finally {
  22. connection.release();
  23. }
  24. } catch (error) {
  25. console.error('创建记录失败:', error);
  26. throw error;
  27. }
  28. }
  29. // 更新记录
  30. async updateRecord(date, content) {
  31. try {
  32. const connection = await this.pool.getConnection();
  33. try {
  34. const [result] = await connection.execute(
  35. 'UPDATE daily_records SET content = ? WHERE record_date = ?',
  36. [JSON.stringify(content), date]
  37. );
  38. return {
  39. success: result.affectedRows > 0,
  40. message: result.affectedRows > 0 ? '记录更新成功' : '记录不存在'
  41. };
  42. } finally {
  43. connection.release();
  44. }
  45. } catch (error) {
  46. console.error('更新记录失败:', error,content,date);
  47. throw error;
  48. }
  49. }
  50. async getRecord(date) {
  51. try {
  52. const connection = await this.pool.getConnection();
  53. try {
  54. const [rows] = await connection.execute(
  55. 'SELECT * FROM daily_records WHERE record_date = ?',
  56. [date]
  57. );
  58. if (rows.length === 0) {
  59. return null;
  60. }
  61. const record = rows[0];
  62. // 检查 content 的类型
  63. const content = typeof record.content === 'string'
  64. ? JSON.parse(record.content) // 如果是字符串,则解析
  65. : record.content; // 如果已经是对象,直接使用
  66. return {
  67. ...record,
  68. content
  69. };
  70. } finally {
  71. connection.release();
  72. }
  73. } catch (error) {
  74. console.error('获取记录失败:', error);
  75. throw error;
  76. }
  77. }
  78. // 获取日期范围内的记录
  79. async getRecordsInRange(startDate, endDate) {
  80. try {
  81. const connection = await this.pool.getConnection();
  82. try {
  83. const [rows] = await connection.execute(
  84. 'SELECT * FROM daily_records WHERE record_date BETWEEN ? AND ? ORDER BY record_date',
  85. [startDate, endDate]
  86. );
  87. return rows.map(row => ({
  88. ...row,
  89. content: JSON.parse(row.content)
  90. }));
  91. } finally {
  92. connection.release();
  93. }
  94. } catch (error) {
  95. console.error('获取记录范围失败:', error);
  96. throw error;
  97. }
  98. }
  99. // 检查日期是否存在记录
  100. async hasRecord(date) {
  101. try {
  102. const connection = await this.pool.getConnection();
  103. try {
  104. const [rows] = await connection.execute(
  105. 'SELECT 1 FROM daily_records WHERE record_date = ?',
  106. [date]
  107. );
  108. return rows.length > 0;
  109. } finally {
  110. connection.release();
  111. }
  112. } catch (error) {
  113. console.error('检查记录失败:', error);
  114. throw error;
  115. }
  116. }
  117. }
  118. module.exports = DailyRecordManager;
  119. // 使用示例
  120. async function example() {
  121. const manager = new DailyRecordManager();
  122. try {
  123. // 创建今天的记录
  124. const today = new Date().toISOString().split('T')[0];
  125. const result = await manager.createRecord(today, {
  126. data: '示例数据',
  127. value: 123
  128. });
  129. console.log('创建结果:', result);
  130. // 尝试再次创建今天的记录(会失败因为已存在)
  131. const duplicateResult = await manager.createRecord(today, {
  132. data: '新数据',
  133. value: 456
  134. });
  135. console.log('重复创建结果:', duplicateResult);
  136. // 更新今天的记录
  137. const updateResult = await manager.updateRecord(today, {
  138. data: '更新的数据',
  139. value: 789
  140. });
  141. console.log('更新结果:', updateResult);
  142. // 获取今天的记录
  143. const record = await manager.getRecord(today);
  144. console.log('获取的记录:', record);
  145. // 获取最近7天的记录
  146. const sevenDaysAgo = new Date();
  147. sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
  148. const rangeRecords = await manager.getRecordsInRange(
  149. sevenDaysAgo.toISOString().split('T')[0],
  150. today
  151. );
  152. console.log('范围记录:', rangeRecords);
  153. } catch (error) {
  154. console.error('操作失败:', error);
  155. }
  156. }
  157. // 执行示例
  158. // example();