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

namespace Application\Service\DB\Ocr;

use Interop\Container\ContainerInterface;
use Application\Common\Com;
use Laminas\Db\Sql\Predicate\Operator;
use Laminas\Stdlib\ArrayUtils;

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


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


    /**
     * Notes:医嘱数据 - 日期差
     * User: lltyy
     * DateTime: 2023/5/12 14:51
     *
     */
    public function scanDateDay(array $where= []) {

        if(empty($where)){
            $where = [
                'is_del' => 0,
            ];
        }

        $bool = true;
        $page = 0;
        $limit = 1000;
        $last_medical_id = 0;
        while ($bool) {
            $page++;
            echo "dayPAGE {$page}：" . PHP_EOL;
            $last_medical_id = $this->mateDateDay(ArrayUtils::merge($where, [
                new Operator('id', Operator::OP_GT, $last_medical_id),
            ]),$limit,0);
            if(!$last_medical_id) $bool = false;
        }
        unset($white_word_arr);
        unset($black_word_arr);
        unset($black_white_word_arr);
        unset($drugDatas);
        unset($takeDatas);
    }

    /**
     * Notes:获取指定的医嘱数据 各个日期差，存入匹配信息表
     * User: lltyy
     * DateTime: 2023/5/26 15:00
     *
     * @param $where//条件
     * @param int $limit
     * @param int $offset
     * @return int
     */
    public function mateDateDay($where,$limit=0,$offset=0){
        //获取指定条件下的医嘱数据
        $cond = [
            'where'=>$where,
            'columns'=>['id','group_time','first_raw_date','start_date','end_date','special_start_date'],
            'order'=>['id']
        ];
        if(!empty($limit) || !empty($offset)){
            $cond['limit'] = $limit;
            $cond['offset'] = $offset;
        }
        $datas = $this->fetchAll($cond);
        $last_id = 0;
        if(!empty($datas)){
            foreach($datas as $dataKey=>$data){
                $last_id = $data['id'];
                $diff_raw = Com::diffDateDay($data['start_date'],$data['first_raw_date']);
                $diff_group = Com::diffDateDay($data['start_date'],$data['group_time']);
                $diff_end = Com::diffDateDay($data['start_date'],$data['end_date']);
                $diff_special = Com::diffDateDay($data['start_date'],$data['special_start_date']);
                $this->update(['diff_raw'=>$diff_raw,'diff_group'=>$diff_group,'diff_end'=>$diff_end,'diff_special'=>$diff_special],'id = '.$data['id']);
                unset($datas[$dataKey]);
            }
        }
        unset($cond);
        unset($datas);

        return $last_id;
    }
}