| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace console\services;
- use Yii;
- use yii\db\Query;
- /**
- * 替换链接
- * @package console\services
- */
- class UrlReplaceService extends BaseService
- {
- /**
- * @var string
- */
- protected $search;
- /**
- * @var string
- */
- protected $replace;
- /**
- * @param string $search
- * @param string $replace
- */
- public function __construct(string $search, string $replace)
- {
- $this->setSearch($search);
- $this->setReplace($replace);
- }
- /**
- * 需要替换的表 && 字段
- * 表名 => [字段1, 字段2, ...]
- *
- * @var array
- */
- protected $tables = [
- // 商品表
- 'qimall_goods' => [
- 'cover_pic',
- 'bannar_pic',
- 'video_url',
- 'video_cover_pic',
- 'detail',
- ],
- // 订单详情表
- 'qimall_order_detail' => [
- 'pic_url'
- ],
- // 平台设置表
- 'qimall_platform_setting' => [
- 'value'
- ],
- // 商城设置表
- 'qimall_mall_setting' => [
- 'value'
- ],
- // 插件配置表
- 'qimall_addons_setting' => [
- 'value'
- ],
- // 会员表
- 'qimall_user' => [
- 'avatar_url'
- ],
- // 自定义页面
- 'qimall_diy_page' => [
- 'content'
- ],
- // 商品规格表
- 'qimall_goods_attr' => [
- 'pic_url'
- ],
- ];
- /**
- * @param string $search
- * @return void
- */
- public function setSearch(string $search)
- {
- $this->search = $search;
- }
- /**
- * @param string $replace
- * @return void
- */
- public function setReplace(string $replace)
- {
- $this->replace = $replace;
- }
- /**
- * @param string $search 搜索目标
- * @param string $replace 替换结果
- *
- * @return boolean
- */
- public function replace()
- {
- foreach ($this->tables as $table_name => $select_fields) {
- $query = new Query();
- $batch = $query->from($table_name)
- ->select($this->getFields($select_fields))
- ->andWhere($this->getCondition($select_fields))
- ->batch(10);
- $this->batchReplace($table_name, $batch, $select_fields);
- }
- }
- /**
- * 获取查询条件
- *
- * @return array
- */
- protected function getCondition(array $fields)
- {
- $condition = ['or'];
- foreach ($fields as $field) {
- $condition[] = ['like', $field, $this->search];
- }
- return $condition;
- }
- /**
- * @param array $fields
- * @return array
- */
- protected function getFields(array $fields)
- {
- return array_merge($fields, ['id']);
- }
- protected function batchReplace($table_name, $batch, $select_fields)
- {
- foreach ($batch as $items) {
- foreach ($items as $item) {
- $update = $this->getReplaceFields($item, $select_fields);
- // 默认使用主键ID进行查询
- $this->update($table_name, ['id' => $item['id']], $update);
- }
- }
- }
- /**
- * 修改
- *
- * @param string $table
- * @param array $condition
- * @param array $update
- * @return void
- */
- protected function update($table, $condition, $update)
- {
- Yii::$app->db->createCommand()->update($table, $update, $condition)->execute();
- }
- /**
- * 获取替换字段
- *
- * @param BatchQueryResult $item
- * @param array $select_fields
- * @return array
- */
- protected function getReplaceFields($item, $select_fields)
- {
- $result = [];
- foreach ($select_fields as $field) {
- $replaced = str_replace($this->search, $this->replace, $item[$field]);
- $item[$field] != $replaced && $result[$field] = $replaced;
- }
- return $result;
- }
- }
|