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

namespace Application\Service\DB\Tmp;

use Interop\Container\ContainerInterface;
use Application\Common\Com;
use Laminas\Db\Sql\Predicate\IsNotNull;
use Laminas\Db\Sql\Predicate\Like;
use Laminas\Db\Sql\Predicate\Operator;

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

    /**
     * Notes:更新患者 入院日期 及 最早的影像检查日期
     * User: lltyy
     * DateTime: 2024/1/9 11:04
     *
     * @return array
     */
    public function updatePatientData(){
        $updateSql = [];
        $patient_arr = $this->LocalService()->tmpPatient->fetchCol('id',['is_del'=>0,new Operator('pid',Operator::OP_GT,0)]);
        $patient_arr = array_values($patient_arr);
        if(!empty($patient_arr)){
            //获取入院基本信息
            $enterDatas = $this->LocalService()->tmpPatienthospital->fetchDataByKey('patient_id',1,['is_del'=>0,'category_type'=>1],['patient_id','content']);
            $enterId = $this->LocalService()->tmpHospital->getOneFieldVal('id',['is_del'=>0,'category_type'=>1,new Like('name',"%入院日期%")]);
            //获取影像检查日期
            $ctDatas = $this->LocalService()->tmpPatientct->fetchDataByKey('patient_id',1,['is_del'=>0,new Operator('collect_date',Operator::OP_NE,""),new IsNotNull('collect_date')],['patient_id','collect_date']);
            foreach($patient_arr as $patient){
                //入院日期
                $enter_date = '';
                if(isset($enterDatas[$patient])){
                    $enterInfos = !empty($enterDatas[$patient]) ? $enterDatas[$patient] : [];
                    unset($enterDatas[$patient]);
                    if(!empty($enterInfos)){
                        foreach($enterInfos as $enterInfo){
                            $enter_content = json_decode($enterInfo['content'],true);
                            if(isset($enter_content[$enterId]) && isset($enter_content[$enterId]['result']) && $enter_content[$enterId]['result'] != '' && $enter_date == ''){
                                $enter_date = $enter_content[$enterId]['result'];
                            }
                            unset($enter_content);
                        }
                    }
                }

                //最早的影像检查日期
                $ct_date = '';
                if(isset($ctDatas[$patient])){
                    $ctInfos = !empty($ctDatas[$patient]) ? $ctDatas[$patient] : [];
                    unset($ctDatas[$patient]);
                    if(!empty($ctInfos)){
                        foreach($ctInfos as $ctInfo){
                            if($ctInfo['collect_date'] != ''){
                                if($ct_date == '' || ($ct_date != '' && intval($ct_date) > intval($ctInfo['collect_date']))) {
                                    $ct_date = $ctInfo['collect_date'];
                                }
                            }
                        }
                    }
                }
                $updateSql[] = 'UPDATE `yikeen_tmp_patient` SET `enter_date` = "'.$enter_date.'",`ct_date` = "'.$ct_date.'" WHERE `id` = '.$patient;
            }
            unset($enterDatas);
            unset($ctDatas);
        }
        echo "共更新《".count($updateSql)."》条数据！";
        echo "<br>";
        echo  implode(';',$updateSql);
        unset($updateSql);
        exit;
    }

}