<?php
/**
 *
 * @author：llbjj
 * @DateTime：2022/5/6 10:57
 * @Description：
 *
 */

namespace Application\Service\DB\Ocr;

use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\Like;

class Ctreplace extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_ocr_ctreplace';
        parent::__construct($dbAdapter, $container);
    }

    /**
     * Notes: 影像替换
     * User: lltyy
     * DateTime: 2023/10/27 10:32
     *
     * @param array $replaceDatas
     */
    public function ctReplaceData(array $replaceDatas=[]){
        if(!empty($replaceDatas)){
            foreach ($replaceDatas as $replaceKey=>$replaceData){
                $replace_pre = $replaceData['replace_pre'];
                $columns = ['id'];
                $serch_where = [
                    'is_del' => 0,
                ];
                if(!empty($replaceData['sig_id'])){
                    $serch_where['sig_id'] = $replaceData['sig_id'];
                }
                if($replaceData['info_type'] == 1){
                    $columns = ['id','ct_result'];
                    $serch_where[] = new Like('ct_result', "%{$replace_pre}%");
                }
                if($replaceData['info_type'] == 2){
                    $columns = ['id','ct_content'];
                    $serch_where[] = new Like('ct_content', "%{$replace_pre}%");
                }
                if($replaceData['info_type'] == 3){
                    $columns = ['id','ct_result','ct_content'];
                    $serch_where[] = [
                        'or' => [
                            new Like('ct_result', "%{$replace_pre}%"),
                            new Like('ct_content', "%{$replace_pre}%"),
                        ]
                    ];
                }
                if(!empty($replaceData['annex_type'])){
                    $annex_where = [
                        'is_del' => 0,
                        'annex_type' => $replaceData['annex_type'],
                    ];
                    if(!empty($replaceData['sig_id'])){
                        $annex_where['sig_id'] = $replaceData['sig_id'];
                    }
                    $annex_arr = $this->LocalService()->ocrOcrannex->fetchCol('id',$annex_where);
                    $annex_arr = array_values($annex_arr);
                    unset($annex_where);
                    if(!empty($annex_arr)){
                        $serch_where['annex_id'] = StringHelper::toArray($annex_arr);
                    }else{
                        $serch_where = 'id < 0';
                    }
                    unset($annex_arr);
                }
                $cond = [
                    'where'=>$serch_where,
                    'columns'=>$columns
                ];
                $ctDatas = $this->LocalService()->ocrCt->fetchAll($cond);

                unset($serch_where);
                unset($columns);
                unset($cond);
                if(!empty($ctDatas)){
                    $replace_after = $replaceData['replace_after'];
                    foreach($ctDatas as $ctKey=>$ctData){
                        $update_data = [];
                        if(isset($ctData['ct_result'])){
                            $update_data['ct_result'] = str_replace($replace_pre,$replace_after,$ctData['ct_result']);
                        }
                        if(isset($ctData['ct_content'])){
                            $update_data['ct_content'] = str_replace($replace_pre,$replace_after,$ctData['ct_content']);
                        }
                        $this->LocalService()->ocrCt->update($update_data,'id = '.$ctData['id']);
                        unset($update_data);
                        unset($ctDatas[$ctKey]);
                    }
                    unset($replace_after);
                }
                unset($replace_pre);
                unset($ctDatas);
                unset($replaceDatas[$replaceKey]);
            }
        }
        return 1;
    }
}