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

namespace Application\Service\DB\Tmp;
use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Expression;
use Laminas\Db\Sql\Predicate\Operator;

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

    /**
     * Notes : 更新匹配数量
     * Author:唐伯虎不虎
     * Date:2024/1/30 15:53
     */
    public function changemateinfo($annex_id = 0)
    {
        //获取受试者附件产生的用药信息
        $doctoradviceWhere['where'] = [
            'is_del'=>0,
            new Operator('annex_id',Operator::OP_GT,$annex_id),
            new Operator('drug_id',Operator::OP_GT,0),
        ];

        $doctoradviceWhere['columns'] = [new Expression('COUNT(`id`) as count'),'annex_id'];

        $doctoradviceWhere['group'] = ['annex_id'];

        $datas = $this->LocalService()->TmpDoctoradviceoriginal->fetchAll($doctoradviceWhere);

        //查黑名单数量
        $doctoradviceWhere['where'] = [
            'is_del'=>0,
            new Operator('annex_id',Operator::OP_GT,$annex_id)
        ];

        $doctoradviceWhere['columns'] = [new Expression('COUNT(`id`) as count'),'annex_id'];

        $doctoradviceWhere['group'] = ['annex_id'];

        $blackdatas = $this->LocalService()->tmpDoctoradvicemateblack->fetchAll($doctoradviceWhere);

        $arr = [];
        foreach($blackdatas as $k=>$v){
            $arr[$v['annex_id']] = $v['count'];
        }

        foreach($datas as $key=>$vlaue){
            $change['black_count'] = isset($arr[$vlaue['annex_id']]) ? : 0;
            $change['mate_count'] = $vlaue['count'];
            $this->update($change,['id'=>$vlaue['annex_id']]);
        }
        echo "成功";die;
    }
    

    /**
     * Notes: 验证日期
     * User: lltyy
     * DateTime: 2023/6/25 9:31
     *
     * @param string $annex_date
     * @return array
     */
    public function validAnnexDate($annex_date=''){
        $code = 200;
        $msg = '';
        //截取日期前8位数字
        $annex_date = $this->LocalService()->ocrRawdata->replaceRawdata($annex_date,2);
        $annex_date = substr($annex_date,0,8);

        //判断报告时间是否符合要求
        if(empty($annex_date)){
            $code = 60010;
            $msg = '采集时间不能为空！';
        }else{
            $annex_date_year = substr($annex_date,0,4);
            $annex_date_month = substr($annex_date,4,2);
            $annex_date_day = substr($annex_date,6,2);
            $annex_date_str = $annex_date_year.'-'.$annex_date_month.'-'.$annex_date_day;
            $date_reg = "/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/";
            //匹配日期格式
            if (preg_match ($date_reg, $annex_date_str, $parts)) {
                //检测是否为日期,checkdate为月日年
                if (checkdate($parts[2], $parts[3], $parts[1])) {
                    $code = 200;
                }else{
                    $code = 60011;
                    $msg = '采集时间格式不正确，【正确格式如：20210607】！';
                }
            }else{
                $code = 60011;
                $msg = '采集时间格式不正确，【正确格式如：20210607】！';
            }
            if($code == 200){
                $nowYear = date('Y');
                if(intval($annex_date_year) > $nowYear){
                    $code = 60011;
                    $msg = '采集时间所属年份【'.$annex_date_year.'】不能超过当前年份【'.$nowYear.'】！';
                }else{
                    if(intval($annex_date_year) < 2020){
                        $code = 60012;
                        $msg = '采集时间所属年份【'.$annex_date_year.'】不在【2020~~'.$nowYear.'】之间！';
                    }
                }
            }
        }
        return ['code'=>$code,'msg'=>$msg,'date'=>$annex_date];
    }
}