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

namespace Application\Service\DB\Collect;

use Interop\Container\ContainerInterface;

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


    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2023/4/25 10:16
     *
     * @param $where
     * @return array
     */
    public function fetchDataById($where,$columns){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoData){
                $id = $infoData['id'];
                $datas[$id] = $infoData;
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }

    /**
     * Notes:拆分介入报告信息
     * User: lltyy
     * DateTime: 2024/1/5 14:22
     *
     */
    public function mateData(){
        $insertSql = [
            0=>[],
            1=>[],
            2=>[]
        ];
        $updateSql = [];
        $cond = [
            'where'=>'is_del = 0',
            'columns'=>['id','sig_id','patient_id','content'],
            'order'=>['id']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach($datas as $dataKey=>$data){
                $data_sig = $data['sig_id'];
                $data_patient = $data['patient_id'];
                $content_arr = json_decode($data['content'],true);

                //更新患者【冠状动脉分布、术式】
                $intervene_result = $intervene_operation = '';
                if(isset($content_arr['result'])){
                    $intervene_result = $content_arr['result'] != '' ? $content_arr['result'] : '';
                    unset($content_arr['result']);
                }
                if(isset($content_arr['operation'])){
                    $intervene_operation =  $content_arr['operation'] != '' ? $content_arr['operation'] : '';
                    unset($content_arr['result']);
                }
                $updateSql[] = 'UPDATE `yikeen_tmp_patient` set `intervene_result` = "'.$intervene_result.'",`intervene_operation` = "'.$intervene_operation.'" WHERE `id` = '.$data_patient;

                //拆分数据【病态部位、置入支架、药物球囊】
                if(!empty($content_arr)){
                    foreach($content_arr as $content_num=>$content_datas){
                        if(count($content_datas) > 1){
                            $now_filed_num = 0;
                            foreach($content_datas as $content_key=>$content_data){
                                if($content_key == 0){
                                    $now_filed_num = count($content_data);
                                }else{
                                    if(count($content_data) == $now_filed_num){
                                        $save_data = '('.$data_sig.','.$data_patient.',';
                                        foreach($content_data as $content_str_key=>$content_str){
                                            if($content_str_key < $now_filed_num-1){
                                                $save_data .= '"'.$content_str.'",';
                                            }else{
                                                $save_data .= '"'.$content_str.'")';
                                            }
                                            unset($content_data[$content_str_key]);
                                            unset($content_datas[$content_key][$content_str_key]);
                                            unset($content_arr[$content_num][$content_key][$content_str_key]);
                                        }
                                        $insertSql[$content_num][] = $save_data;
                                    }
                                }
                                unset($content_datas[$content_key]);
                                unset($content_arr[$content_num][$content_key]);
                            }
                        }
                        unset($content_arr[$content_num]);
                    }
                }
                unset($content_arr);
            }
        }

        /*if(!empty($updateSql)){
            echo "【患者】共更新《".count($updateSql)."》条数据！<br>";
            echo "【患者SQL】 = ".implode(';',$updateSql)."<br><br><br><br><br>";
            exit;
        }*/
        unset($updateSql);
        /*if(!empty($insertSql[0])){
            echo "【病变部位】共插入《".count($insertSql[0])."》条数据！<br>";
            $insertSql0 = 'INSERT INTO `yikeen_tmp_intervene0` (`itemsig_id`, `patient_id`, `filed0`, `filed1`, `filed2`, `filed3`, `filed4`, `filed5`) VALUES '.implode(',',$insertSql[0]);
            echo "【病变部位SQL】 = ".$insertSql0."<br><br><br><br><br>";
            exit;
        }*/
        unset($insertSql[0]);

        /*if(!empty($insertSql[1])){
            echo "【置入支架】共插入《".count($insertSql[1])."》条数据！<br>";
            $insertSql1 = 'INSERT INTO `yikeen_tmp_intervene1` (`itemsig_id`, `patient_id`, `filed0`, `filed1`, `filed2`, `filed3`, `filed4`) VALUES '.implode(',',$insertSql[1]);
            $insertSql1 = str_replace('")"','","',$insertSql1);
            echo "【置入支架SQL】 = ".$insertSql1;
            exit;
        }*/
        unset($insertSql[1]);
        if(!empty($insertSql[2])){
            echo "【药物球囊】共插入《".count($insertSql[2])."》条数据！<br>";
            $insertSql2 = 'INSERT INTO `yikeen_tmp_intervene2` (`itemsig_id`, `patient_id`, `filed0`, `filed1`, `filed2`, `filed3`, `filed4`, `filed5`, `filed6`, `filed7`, `filed8`) VALUES '.implode(',',$insertSql[2]);
            $insertSql2 = str_replace('")"','","',$insertSql2);
            echo "【药物球囊SQL】 = ".$insertSql2;
            exit;
        }
        unset($insertSql[2]);

        exit;
    }

}