SeoAiResult.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. class SeoAiResult extends Base
  5. {
  6. protected $name = 'seo_ai_result';
  7. private static $uuidColumnChecked = false;
  8. public function createTableIfNotExists()
  9. {
  10. $table = config('database.prefix') . $this->name;
  11. $exists = Db::query("SHOW TABLES LIKE '{$table}'");
  12. if (!empty($exists)) {
  13. return;
  14. }
  15. Db::execute(
  16. "CREATE TABLE `{$table}` (
  17. `seo_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  18. `seo_mid` tinyint(3) unsigned NOT NULL DEFAULT '0',
  19. `seo_obj_id` int(10) unsigned NOT NULL DEFAULT '0',
  20. `seo_obj_uuid` char(36) NOT NULL DEFAULT '',
  21. `seo_title` varchar(255) NOT NULL DEFAULT '',
  22. `seo_keywords` varchar(500) NOT NULL DEFAULT '',
  23. `seo_description` varchar(500) NOT NULL DEFAULT '',
  24. `seo_provider` varchar(32) NOT NULL DEFAULT '',
  25. `seo_model` varchar(64) NOT NULL DEFAULT '',
  26. `seo_source_hash` char(40) NOT NULL DEFAULT '',
  27. `seo_error` varchar(255) NOT NULL DEFAULT '',
  28. `seo_status` tinyint(3) unsigned NOT NULL DEFAULT '1',
  29. `seo_time_add` int(10) unsigned NOT NULL DEFAULT '0',
  30. `seo_time_update` int(10) unsigned NOT NULL DEFAULT '0',
  31. PRIMARY KEY (`seo_id`),
  32. UNIQUE KEY `seo_obj` (`seo_mid`,`seo_obj_id`),
  33. UNIQUE KEY `seo_obj_uuid` (`seo_mid`,`seo_obj_uuid`),
  34. KEY `seo_time_update` (`seo_time_update`)
  35. ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8"
  36. );
  37. }
  38. public function getByObject($mid, $objId)
  39. {
  40. $mid = intval($mid);
  41. $objId = intval($objId);
  42. if ($mid < 1 || $objId < 1) {
  43. return null;
  44. }
  45. $this->ensureUuidColumn();
  46. $objUuid = $this->buildObjectUuid($mid, $objId);
  47. $row = $this->where([
  48. 'seo_mid' => $mid,
  49. 'seo_obj_uuid' => $objUuid,
  50. 'seo_status' => ['in', [1, 2]]
  51. ])->find();
  52. if (!empty($row)) {
  53. return $row;
  54. }
  55. // Backward compatibility: old rows keyed by numeric obj_id only.
  56. $row = $this->where([
  57. 'seo_mid' => $mid,
  58. 'seo_obj_id' => $objId,
  59. 'seo_status' => ['in', [1, 2]]
  60. ])->find();
  61. if (!empty($row) && empty($row['seo_obj_uuid'])) {
  62. $this->where(['seo_id' => intval($row['seo_id'])])->update(['seo_obj_uuid' => $objUuid]);
  63. }
  64. return $row;
  65. }
  66. public function saveByObject($mid, $objId, $data)
  67. {
  68. $mid = intval($mid);
  69. $objId = intval($objId);
  70. if ($mid < 1 || $objId < 1) {
  71. return false;
  72. }
  73. $this->ensureUuidColumn();
  74. $now = time();
  75. $objUuid = $this->buildObjectUuid($mid, $objId);
  76. $row = [
  77. 'seo_mid' => $mid,
  78. 'seo_obj_id' => $objId,
  79. 'seo_obj_uuid' => $objUuid,
  80. 'seo_title' => (string)$data['title'],
  81. 'seo_keywords' => (string)$data['keywords'],
  82. 'seo_description' => (string)$data['description'],
  83. 'seo_provider' => (string)$data['provider'],
  84. 'seo_model' => (string)$data['model'],
  85. 'seo_source_hash' => (string)$data['source_hash'],
  86. 'seo_error' => (string)$data['error'],
  87. 'seo_status' => intval($data['status']),
  88. 'seo_time_update' => $now,
  89. ];
  90. $old = $this->where(['seo_mid' => $mid, 'seo_obj_uuid' => $objUuid])->find();
  91. if (empty($old)) {
  92. // Backward compatibility with old numeric-only key.
  93. $old = $this->where(['seo_mid' => $mid, 'seo_obj_id' => $objId])->find();
  94. }
  95. if (empty($old)) {
  96. $row['seo_time_add'] = $now;
  97. return $this->insert($row);
  98. }
  99. return $this->where(['seo_id' => intval($old['seo_id'])])->update($row);
  100. }
  101. private function ensureUuidColumn()
  102. {
  103. if (self::$uuidColumnChecked) {
  104. return;
  105. }
  106. $table = config('database.prefix') . $this->name;
  107. $cols = Db::query("SHOW COLUMNS FROM `{$table}` LIKE 'seo_obj_uuid'");
  108. if (empty($cols)) {
  109. Db::execute("ALTER TABLE `{$table}` ADD COLUMN `seo_obj_uuid` char(36) NOT NULL DEFAULT '' AFTER `seo_obj_id`");
  110. Db::execute("ALTER TABLE `{$table}` ADD UNIQUE KEY `seo_obj_uuid` (`seo_mid`,`seo_obj_uuid`)");
  111. }
  112. self::$uuidColumnChecked = true;
  113. }
  114. private function buildObjectUuid($mid, $objId)
  115. {
  116. $hex = md5('seo:' . intval($mid) . ':' . intval($objId));
  117. return substr($hex, 0, 8) . '-' .
  118. substr($hex, 8, 4) . '-' .
  119. substr($hex, 12, 4) . '-' .
  120. substr($hex, 16, 4) . '-' .
  121. substr($hex, 20, 12);
  122. }
  123. }