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

namespace Application\Service\DB\Tmp;

use Interop\Container\ContainerInterface;

class Ctreplace extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_tmp_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){
                $serch_where = ['is_del'=>0];
                if(!empty($replaceData['annex_type'])){
                    $serch_where['annex_type'] = $replaceData['annex_type'];
                }
                $cond = [
                    'where'=>$serch_where,
                    'columns'=>['id','content']
                ];
                $ctDatas = $this->LocalService()->tmpPatientct->fetchAll($cond);

                unset($serch_where);
                unset($cond);
                if(!empty($ctDatas)){
                    foreach($ctDatas as $ctKey=>$ctData){
                        $ct_content = json_decode($ctData['content'],true);
                        $ct_content['result'] = isset($ct_content['result']) && $ct_content['result'] != '' ? $ct_content['result'] : '';
                        if(strpos($ct_content['result'],$replaceData['replace_pre']) !== false){
                            $ct_content['result'] = str_replace($replaceData['replace_pre'],$replaceData['replace_after'],$ct_content['result']);
                            $this->LocalService()->tmpPatientct->update(['content'=>json_encode($ct_content)],'id = '.$ctData['id']);
                        }
                        unset($ctDatas[$ctKey]);
                    }
                }
                unset($ctDatas);
                unset($replaceDatas[$replaceKey]);
            }
        }
        return 1;
    }
}