UrlReplaceService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace console\services;
  3. use Yii;
  4. use yii\db\Query;
  5. /**
  6. * 替换链接
  7. * @package console\services
  8. */
  9. class UrlReplaceService extends BaseService
  10. {
  11. /**
  12. * @var string
  13. */
  14. protected $search;
  15. /**
  16. * @var string
  17. */
  18. protected $replace;
  19. /**
  20. * @param string $search
  21. * @param string $replace
  22. */
  23. public function __construct(string $search, string $replace)
  24. {
  25. $this->setSearch($search);
  26. $this->setReplace($replace);
  27. }
  28. /**
  29. * 需要替换的表 && 字段
  30. * 表名 => [字段1, 字段2, ...]
  31. *
  32. * @var array
  33. */
  34. protected $tables = [
  35. // 商品表
  36. 'qimall_goods' => [
  37. 'cover_pic',
  38. 'bannar_pic',
  39. 'video_url',
  40. 'video_cover_pic',
  41. 'detail',
  42. ],
  43. // 订单详情表
  44. 'qimall_order_detail' => [
  45. 'pic_url'
  46. ],
  47. // 平台设置表
  48. 'qimall_platform_setting' => [
  49. 'value'
  50. ],
  51. // 商城设置表
  52. 'qimall_mall_setting' => [
  53. 'value'
  54. ],
  55. // 插件配置表
  56. 'qimall_addons_setting' => [
  57. 'value'
  58. ],
  59. // 会员表
  60. 'qimall_user' => [
  61. 'avatar_url'
  62. ],
  63. // 自定义页面
  64. 'qimall_diy_page' => [
  65. 'content'
  66. ],
  67. // 商品规格表
  68. 'qimall_goods_attr' => [
  69. 'pic_url'
  70. ],
  71. ];
  72. /**
  73. * @param string $search
  74. * @return void
  75. */
  76. public function setSearch(string $search)
  77. {
  78. $this->search = $search;
  79. }
  80. /**
  81. * @param string $replace
  82. * @return void
  83. */
  84. public function setReplace(string $replace)
  85. {
  86. $this->replace = $replace;
  87. }
  88. /**
  89. * @param string $search 搜索目标
  90. * @param string $replace 替换结果
  91. *
  92. * @return boolean
  93. */
  94. public function replace()
  95. {
  96. foreach ($this->tables as $table_name => $select_fields) {
  97. $query = new Query();
  98. $batch = $query->from($table_name)
  99. ->select($this->getFields($select_fields))
  100. ->andWhere($this->getCondition($select_fields))
  101. ->batch(10);
  102. $this->batchReplace($table_name, $batch, $select_fields);
  103. }
  104. }
  105. /**
  106. * 获取查询条件
  107. *
  108. * @return array
  109. */
  110. protected function getCondition(array $fields)
  111. {
  112. $condition = ['or'];
  113. foreach ($fields as $field) {
  114. $condition[] = ['like', $field, $this->search];
  115. }
  116. return $condition;
  117. }
  118. /**
  119. * @param array $fields
  120. * @return array
  121. */
  122. protected function getFields(array $fields)
  123. {
  124. return array_merge($fields, ['id']);
  125. }
  126. protected function batchReplace($table_name, $batch, $select_fields)
  127. {
  128. foreach ($batch as $items) {
  129. foreach ($items as $item) {
  130. $update = $this->getReplaceFields($item, $select_fields);
  131. // 默认使用主键ID进行查询
  132. $this->update($table_name, ['id' => $item['id']], $update);
  133. }
  134. }
  135. }
  136. /**
  137. * 修改
  138. *
  139. * @param string $table
  140. * @param array $condition
  141. * @param array $update
  142. * @return void
  143. */
  144. protected function update($table, $condition, $update)
  145. {
  146. Yii::$app->db->createCommand()->update($table, $update, $condition)->execute();
  147. }
  148. /**
  149. * 获取替换字段
  150. *
  151. * @param BatchQueryResult $item
  152. * @param array $select_fields
  153. * @return array
  154. */
  155. protected function getReplaceFields($item, $select_fields)
  156. {
  157. $result = [];
  158. foreach ($select_fields as $field) {
  159. $replaced = str_replace($this->search, $this->replace, $item[$field]);
  160. $item[$field] != $replaced && $result[$field] = $replaced;
  161. }
  162. return $result;
  163. }
  164. }