<?php
namespace Application\Service\DB\Admin;

use Application\Service\Extension\Laminas;
use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Expression;
use Laminas\Db\Sql\Predicate\NotIn;
use Laminas\Db\Sql\Predicate\Operator;
use Zend\Stdlib\ArrayUtils;

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

    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2023/6/2 7:50
     *
     * @param $where 条件
     * @param $columns 信息
     * @return array
     */
    public function fetchDataByKey($where,$columns,$key='id',$is_more=0){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoData){
                $key_arr = explode('/',$key);
                $key_str_arr = [];
                foreach ($key_arr as $key_info){
                    $key_str_arr[] = $infoData[$key_info];
                }
                unset($key_arr);
                $key_str = implode('_',$key_str_arr);
                unset($key_str_arr);
                if($is_more == 1){
                    $datas[$key_str][] = $infoData;
                }
                if($is_more == 0){
                    $datas[$key_str] = $infoData;
                }
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }

    /**
     * Notes: 更新检查项类工作明细
     * User: lltyy
     * DateTime: 2024/12/23 12:12
     *
     */
    public function scanCheckData(){
        //获取各个项目下检查项类数据的表单
        $allCheckFormArr = [];
        $groupCode = $this->LocalService()->dictionaryFormGroup->getOneFieldVal('id','is_del = 0 and code in ("CHECKCATE")');
        if(!empty($groupCode)){
            //获取检查项类表单信息
            $formWhere = [
                'is_del' => 0,
                'group_id' => $groupCode,
                'info_collect_type' => 0,
            ];
            $allCheckFormArr = $this->LocalService()->itemForm->fetchCol('id',$formWhere);
            $allCheckFormArr = !empty($allCheckFormArr) ? array_values($allCheckFormArr) : [];
            unset($formWhere);
        }
        unset($groupCode);

        //获取已锁定的数据
        $lockWorkInfoArr = $this->fetchDataByKey([
            'is_del' => 0,
            'info_type' => 0,
            'is_lock' => 1,
        ],['patient_id','checktime_id','form_id'],'patient_id/checktime_id/form_id',0);
        $lockWorkInfoArr = !empty($lockWorkInfoArr) ? array_values(array_keys($lockWorkInfoArr)) : [];
        if(!empty($lockWorkInfoArr)){
            foreach($lockWorkInfoArr as $lockWorkInfoKey=>$lockWorkInfo){
                $lockWorkInfoArr[$lockWorkInfoKey] = $lockWorkInfo;
            }
        }

        //获取检查项类有图片的数据以及锁定情况
        $haveImgCheckArr = $checkimgArr = [];
        $checkimgWhere = [
            'is_del'=>0,
            new Operator('patient_id',Operator::OP_GT,0),
            new Operator('checktime_id',Operator::OP_GT,0),
            new Operator('form_id',Operator::OP_GT,0),
            new Operator('annex_type',Operator::OP_NE,2),
            'or'=>[
                ['lock_state'=>0],
                [
                    'lock_state'=>1,
                    new Operator('update_user_id',Operator::OP_GT,0),
                ]
            ]
        ];

        if(!empty($allCheckFormArr)){
            $checkimgWhere['form_id'] = $allCheckFormArr;
            //排除已锁定的信息数据
            if(!empty($lockWorkInfoArr)){
                $checkimgWhere[] = new NotIn( new Expression('CONCAT_WS("_", `patient_id`, `checktime_id`, `form_id` )'), $lockWorkInfoArr);
                // $checkimgWhere['CONCAT_WS("_",patient_id`,`checktime_id`,form_id ) NOT IN (?)'] = implode(',', $lockWorkInfoArr);
            }
        }else{
            $checkimgWhere = [
                'id'=>0
            ];
        }

        $checkimgDatas = $this->LocalService()->itemPatientworkannex->fetchAll([
            'where'=>$checkimgWhere,
            'columns'=>['patient_id','checktime_id','form_id','lock_state','update_user_id','update_time'],
            'order'=>['id']
        ]);
        unset($checkimgWhere);
        if(!empty($checkimgDatas)){
            foreach($checkimgDatas as $checkimgKey=>$checkimg){
                $haveImgCheckKey = $checkimg['patient_id'].'_'.$checkimg['checktime_id'].'_'.$checkimg['form_id'];
                if(!in_array($haveImgCheckKey,$haveImgCheckArr)){
                    $haveImgCheckArr[] = $haveImgCheckKey;
                }
                if($checkimg['lock_state'] == 1){
                    $checkimgArr[$haveImgCheckKey] = [
                        'update_user_id'=>$checkimg['update_user_id'],
                        'update_time'=>$checkimg['update_time']
                    ];
                }
                unset($checkimgDatas[$checkimgKey]);
            }
        }
        unset($checkimgDatas);

        //获取表单信息
        $where = [
            'is_del' => 0,
            new Operator('patient_id', Operator::OP_GT, 0),
            new Operator('form_id', Operator::OP_GT, 0),
        ];
        if(!empty($allCheckFormArr)){
            $where['form_id'] = $allCheckFormArr;
            //排除已锁定的信息数据
            if(!empty($lockWorkInfoArr)){
                $where[] = new NotIn( new Expression('CONCAT_WS("_", `patient_id`, `checktime_id`, `form_id` )'), $lockWorkInfoArr);
                //$where['CONCAT_WS("_",patient_id`,`checktime_id`,form_id ) NOT IN (?)'] = implode(',', $lockWorkInfoArr);
            }
        }else{
            $where = [
                'id' => 0
            ];
        }
        unset($allCheckFormArr);
        unset($lockWorkInfoArr);

        $bool = true;
        $page = 0;
        $limit = 1000;
        $last_id = 0;
        while ($bool) {
            $page++;
            //echo "PAGE {$page}：" . PHP_EOL;
            $last_id = $this->mateCheckData(ArrayUtils::merge($where, [
                new Operator('id', Operator::OP_GT, $last_id),
            ]),$limit,0,$checkimgArr,$haveImgCheckArr);
            if(!$last_id) $bool = false;
        }
        unset($checkimgArr);
        unset($haveImgCheckArr);
    }

    /**
     * Notes: 执行检查项类工作明细
     * User: lltyy
     * DateTime: 2024/12/19 11:19
     *
     */
    public function mateCheckData($where,$limit=0,$offset=0,&$checkimgArr=[],&$haveImgCheckArr=[]){
        //获取指定条件下的医嘱数据
        $cond = [
            'where'=>$where,
            'columns'=>['id','item_id','itemsig_id','patient_id','checktime_id','form_id','create_user_id','create_time'],
            'order'=>['id']
        ];
        if(!empty($limit) || !empty($offset)){
            $cond['limit'] = $limit;
            $cond['offset'] = $offset;
        }
        $datas = $this->LocalService()->patientFormContent->fetchAll($cond);
        $last_id = 0;
        if(!empty($datas)){
            //获取已添加的工作信息
            $workInfoArr = $this->fetchDataByKey([
                'is_del' => 0,
                'info_type' => 0,
            ],['id','patient_id','checktime_id','form_id','is_lock'],'patient_id/checktime_id/form_id',0);
            foreach($datas as $dataKey=>$data){
                $last_id = $data['id'];
                $data_item = $data['item_id'];//项目ID
                $data_patient = $data['patient_id'];//患者ID
                $data_checktime = $data['checktime_id'];//项目检查点ID
                $data_form = $data['form_id'];//表单ID
                $info_type = 0;//类型
                $old_key = $data_patient.'_'.$data_checktime.'_'.$data_form;
                if($info_type == 0 && in_array($old_key,$haveImgCheckArr)){
                    //锁定情况
                    $data_lock = $data_update_user = $data_update_time = 0;
                    //检查项类表单锁定情况
                    if(isset($checkimgArr[$old_key]) && !empty($checkimgArr[$old_key])){
                        $data_lock = 1;
                        $data_update_user = $checkimgArr[$old_key]['update_user_id'];
                        $data_update_time = $checkimgArr[$old_key]['update_time'];
                    }

                    if(isset($workInfoArr[$old_key]) && !empty($workInfoArr[$old_key])){
                        if($workInfoArr[$old_key]['is_lock'] == 0 && $data_lock == 1){
                            $this->isSetInfo(false)->update([
                                'is_lock'=>1,
                                'update_user_id'=>$data_update_user,
                                'update_time'=>$data_update_time
                            ],[
                                'id' => $workInfoArr[$old_key]['id']
                            ]);
                            $workInfoArr[$old_key]['is_lock'] = 1;
                        }
                    }else{
                        $save_data = [
                            'item_id'=>$data_item,
                            'itemsig_id'=>$data['itemsig_id'],
                            'patient_id'=>$data_patient,
                            'checktime_id'=>$data_checktime,
                            'form_id'=>$data_form,
                            'create_user_id'=>$data['create_user_id'],
                            'create_time'=>$data['create_time'],
                            'info_type'=>$info_type,
                            'is_lock'=>0
                        ];
                        if($data_lock == 1){
                            $save_data['is_lock'] = 1;
                            $save_data['update_user_id'] = $data_update_user;
                            $save_data['update_time'] = $data_update_time;
                        }
                        $new_id = $this->isSetInfo(false)->insert($save_data);
                        $workInfoArr[$old_key] = [
                            'id'=>$new_id,
                            'patient_id'=>$data_patient,
                            'checktime_id'=>$data_checktime,
                            'form_id'=>$data_form,
                            'info_type'=>$info_type,
                            'is_lock'=>$save_data['is_lock']
                        ];
                        unset($save_data);
                        unset($new_id);
                    }

                    unset($data_lock);
                    unset($data_update_user);
                    unset($data_update_time);
                }

                unset($datas[$dataKey]);
                unset($data_item);
                unset($data_patient);
                unset($data_checktime);
                unset($data_form);
                unset($info_type);
                unset($old_key);
            }
            unset($workInfoArr);
            unset($logDataArr);
        }

        //释放空间
        unset($datas);
        unset($cond);
        unset($where);

        return $last_id;

    }

    /**
     * Notes: 更新常规类识别工作明细
     * User: lltyy
     * DateTime: 2024/12/23 12:12
     *
     */
    public function scanNormalData(){
        //获取各个项目下同类【常规识别类】数据的表单
        $allNormalFormArr = $formGcoPoolArr = $gcoPoolFormArr = $formTypeArr = [];
        $groupCode = $this->LocalService()->dictionaryFormGroup->getOneFieldVal('id','is_del = 0 and code in ("GCO")');
        if(!empty($groupCode)){
            //获取病例收集类型ID
            $collectTypeArr = $this->LocalService()->collectCategory->fetchCol('id','is_del = 0 and category_code != "Medical"');
            //获取检查项类、常规类识别表单信息
            $formWhere = 'is_del = 0 and group_id = '.$groupCode.' and info_collect_type > 0';
            if(!empty($collectTypeArr)){
                $formWhere .= ' and info_collect_type in ('.implode(',',array_values($collectTypeArr)).')';
            }else{
                $formWhere = 'id < 0';
            }
            unset($collectTypeArr);
            $itemFormDatas = $this->LocalService()->itemForm->fetchAll([
                'where'=>$formWhere,
                'columns'=>['id','item_id','group_id','info_collect_type','gco_pool']
            ]);
            unset($formWhere);
            if(!empty($itemFormDatas)){
                foreach($itemFormDatas as $itemFormKey=>$itemFormData){
                    $allNormalFormArr[] = $itemFormData['id'];
                    if(!is_null($itemFormData['gco_pool']) && $itemFormData['gco_pool'] != ''){
                        $formGcoPoolArr[$itemFormData['item_id']][$itemFormData['id']] = $itemFormData['gco_pool'];
                        $gcoPoolFormArr[$itemFormData['item_id']][$itemFormData['gco_pool']][] = $itemFormData['id'];
                    }
                    $formTypeArr[$itemFormData['id']] = $itemFormData['info_collect_type'];

                    unset($itemFormDatas[$itemFormKey]);
                }
            }
            unset($itemFormDatas);
        }
        unset($groupCode);

        //获取每个检查点下的常规类识别表单
        $checkcontentFormArr = [];
        if(!empty($allNormalFormArr)) {
            $checkcontentFormDatas = $this->LocalService()->itemCheckcontent->fetchAll([
                'where' => 'is_del = 0 and form_id in (' . implode(',', $allNormalFormArr) . ')',
                'columns' => ['check_id', 'form_id']
            ]);
            if (!empty($checkcontentFormDatas)) {
                foreach($checkcontentFormDatas as $checkcontentFormKey=>$checkcontentFormData){
                    $checkcontentFormArr[$checkcontentFormData['check_id']][] = $checkcontentFormData['form_id'];
                    unset($checkcontentFormDatas[$checkcontentFormKey]);
                }
            }
            unset($checkcontentFormDatas);
        }

        //获取表单信息
        $logDataArr = [];
        $where = 'is_del = 0 and patient_id > 0 and checktime_id > 0 and form_id > 0';
        if(!empty($allNormalFormArr)){
            //获取已锁定的数据
            $lockWorkInfoArr = $this->fetchDataByKey('is_del = 0 and info_type > 0 and is_lock = 1',['patient_id','checktime_id','form_id'],'patient_id/checktime_id/form_id',0);
            $lockWorkInfoArr = !empty($lockWorkInfoArr) ? array_values(array_keys($lockWorkInfoArr)) : [];
            if(!empty($lockWorkInfoArr)){
                foreach($lockWorkInfoArr as $lockWorkInfoKey=>$lockWorkInfo){
                    $lockWorkInfoArr[$lockWorkInfoKey] = "'".$lockWorkInfo."'";
                }
            }

            $where .= ' and form_id in ('.implode(',',$allNormalFormArr).')';
            $normal_img_field_arr = $this->LocalService()->itemFormField->fetchCol('id','is_del = 0 and form_id in ('.implode(',',$allNormalFormArr).') and type = 19');
            if(!empty($normal_img_field_arr)){
                $where .= ' and field_id in ('.implode(',',array_values($normal_img_field_arr)).')';
            }else{
                $where = 'id < 0';
            }
            unset($normal_img_field_arr);

            //获取常规类识别表单的首次锁定时间
            $logWhere = 'is_del = 0 and event = "LOCK_SHARE_FORM_CONTENT" and form_id in ('.implode(',',$allNormalFormArr).')';
            if(!empty($lockWorkInfoArr)){
                //$where .= ' and CONCAT_WS("_", `patient_id`, `checktime_id`, `form_id`) NOT IN ('.implode(',',$lockWorkInfoArr).')';
                $logWhere .= ' and CONCAT_WS("_", `patient_id`, `checktime_id`, `form_id`) NOT IN ('.implode(',',$lockWorkInfoArr).')';
            }
            $logDataArr = $this->LocalService()->patientFormContentUpdatedLog->fetchDataByKey($logWhere,['patient_id','checktime_id','form_id','create_user_id','create_time'],['id desc'],'patient_id/checktime_id/form_id',0);
            unset($logWhere);
            //新调整首次锁定日志查询
            $logStartTime = strtotime(date('Y-m-d',strtotime("-1 day")).' 00:00:00');
            $logEndTime = strtotime(date('Y-m-d',strtotime("-1 day")).' 23:59:59');
            $redisLogDatas = (Laminas::$serviceManager->redisExtend->getRedisInstance()->hMGet('lockShareFormAnalyze', Laminas::$serviceManager->redisExtend->getRedisInstance()->zRangeByScore('lockShareFormAnalyzeSortSet', $logStartTime, $logEndTime))) ?: [];
            if(!empty($redisLogDatas)){
                foreach ($redisLogDatas as $redisLogDataKey => $redisLogData) {
                    $redisLogInfo = StringHelper::jsonDecode($redisLogData);
                    if(isset($redisLogInfo['forms']) && !empty($redisLogInfo['forms'])){
                        foreach ($redisLogInfo['forms'] as $redisLogInfoForm) {
                            $redisLogInfoFormKey = $redisLogInfo['patient_id'].'_'.$redisLogInfo['checktime_id'].'_'.$redisLogInfoForm;
                            if(in_array($redisLogInfoForm,$allNormalFormArr) && (empty($lockWorkInfoArr) || (!empty($lockWorkInfoArr) && !in_array($redisLogInfoFormKey,$lockWorkInfoArr)))){
                                $logDataArr[$redisLogInfoFormKey] = [
                                    'create_time' => $redisLogInfo['time'],
                                    'create_user_id' => $redisLogInfo['user']
                                ];
                            }
                            unset($redisLogInfoFormKey);
                        }
                    }
                    unset($redisLogInfo);
                    unset($redisLogDatas[$redisLogDataKey]);
                }
            }
            unset($redisLogDatas);
            unset($logStartTime);
            unset($logEndTime);
            unset($lockWorkInfoArr);

        }else{
            $where = 'id < 0';
        }
        unset($allNormalFormArr);

        $bool = true;
        $page = 0;
        $limit = 1000;
        $last_id = 0;
        while ($bool) {
            $page++;
            //echo "PAGE {$page}：" . PHP_EOL;
            $last_id = $this->mateNormalData($where.' and id > '.$last_id,$limit,0,$formGcoPoolArr,$gcoPoolFormArr,$formTypeArr,$checkcontentFormArr,$logDataArr);
            if(!$last_id) $bool = false;
        }
        unset($formGcoPoolArr);
        unset($gcoPoolFormArr);
        unset($checkcontentFormArr);
        unset($haveImgNormalArr);
        unset($logDataArr);
    }

    /**
     * Notes: 更新添加的数据
     * User: lltyy
     * DateTime: 2024/12/19 11:19
     *
     */
    public function mateNormalData($where,$limit=0,$offset=0,&$formGcoPoolArr=[],&$gcoPoolFormArr=[],&$formTypeArr=[],&$checkcontentFormArr=[],&$logDataArr=[]){
        //获取指定条件下的医嘱数据
        $cond = [
            'where'=>$where,
            'columns'=>['id','item_id','itemsig_id','patient_id','checktime_id','form_id','create_user_id','create_time'],
            'order'=>['id']
        ];
        if(!empty($limit) || !empty($offset)){
            $cond['limit'] = $limit;
            $cond['offset'] = $offset;
        }
        $datas = $this->LocalService()->patientFormContentImg->fetchAll($cond);
        $last_id = 0;
        if(!empty($datas)){
            //获取已添加的工作信息
            $workInfoArr = $this->fetchDataByKey('is_del = 0 and info_type > 0',['id','patient_id','checktime_id','form_id','is_lock'],'patient_id/checktime_id/form_id',0);
            foreach($datas as $dataKey=>$data){
                $last_id = $data['id'];
                $data_item = $data['item_id'];//项目ID
                $data_patient = $data['patient_id'];//患者ID
                $data_checktime = $data['checktime_id'];//项目检查点ID
                $data_form = $data['form_id'];//表单ID
                $info_type = isset($formTypeArr[$data_form]) && !empty($formTypeArr[$data_form]) ? $formTypeArr[$data_form] : 0;//类型
                $old_key = $data_patient.'_'.$data_checktime.'_'.$data_form;
                if($info_type > 0){
                    //锁定情况
                    $data_lock = $data_update_user = $data_update_time = 0;
                    if(isset($logDataArr[$old_key]) && !empty($logDataArr[$old_key])){
                        $data_lock = 1;
                        $data_update_user = $logDataArr[$old_key]['create_user_id'];
                        $data_update_time = $logDataArr[$old_key]['create_time'];
                    }

                    //获取共享表单信息
                    $form_gco_pool = isset($formGcoPoolArr[$data_item]) && isset($formGcoPoolArr[$data_item][$data_form]) ? $formGcoPoolArr[$data_item][$data_form] : '';
                    $gcoPoolFormDatas = isset($gcoPoolFormArr[$data_item]) && isset($gcoPoolFormArr[$data_item][$form_gco_pool]) && !empty($gcoPoolFormArr[$data_item][$form_gco_pool]) ? $gcoPoolFormArr[$data_item][$form_gco_pool] : [];
                    unset($form_gco_pool);

                    //获取共享表单中最早的锁定时间
                    if(!empty($gcoPoolFormDatas)){
                        foreach($gcoPoolFormDatas as $gcoPoolFormData){
                            if($gcoPoolFormData != $data_form && in_array($gcoPoolFormData,$checkcontentFormArr[$data_checktime])){
                                $gcoPoolFormData_key = $data_patient.'_'.$data_checktime.'_'.$gcoPoolFormData;
                                if(!isset($workInfoArr[$gcoPoolFormData_key]) && isset($formTypeArr[$gcoPoolFormData]) && !empty($formTypeArr[$gcoPoolFormData])){
                                    if(isset($logDataArr[$gcoPoolFormData_key]) && !empty($logDataArr[$gcoPoolFormData_key])){
                                        if($data_lock == 0){
                                            $data_lock = 1;
                                            $data_update_user = $logDataArr[$gcoPoolFormData_key]['create_user_id'];
                                            $data_update_time = $logDataArr[$gcoPoolFormData_key]['create_time'];
                                        }else{
                                            if($data_update_time > $logDataArr[$gcoPoolFormData_key]['create_time']){
                                                $data_lock = 1;
                                                $data_update_user = $logDataArr[$gcoPoolFormData_key]['create_user_id'];
                                                $data_update_time = $logDataArr[$gcoPoolFormData_key]['create_time'];
                                            }
                                        }
                                    }
                                }
                                unset($gcoPoolFormData_key);
                            }
                        }
                    }

                    if(isset($workInfoArr[$old_key]) && !empty($workInfoArr[$old_key])){
                        if($workInfoArr[$old_key]['is_lock'] == 0 && $data_lock == 1){
                            $this->isSetInfo(false)->update([
                                'is_lock'=>1,
                                'update_user_id'=>$data_update_user,
                                'update_time'=>$data_update_time
                            ],[
                                'id' => $workInfoArr[$old_key]['id']
                            ]);
                            $workInfoArr[$old_key]['is_lock'] = 1;
                        }
                    }else{
                        $save_data = [
                            'item_id'=>$data_item,
                            'itemsig_id'=>$data['itemsig_id'],
                            'patient_id'=>$data_patient,
                            'checktime_id'=>$data_checktime,
                            'form_id'=>$data_form,
                            'create_user_id'=>$data['create_user_id'],
                            'create_time'=>$data['create_time'],
                            'info_type'=>$info_type,
                            'is_lock'=>0
                        ];
                        if($data_lock == 1){
                            $save_data['is_lock'] = 1;
                            $save_data['update_user_id'] = $data_update_user;
                            $save_data['update_time'] = $data_update_time;
                        }
                        $new_id = $this->isSetInfo(false)->insert($save_data);
                        $workInfoArr[$old_key] = [
                            'id'=>$new_id,
                            'patient_id'=>$data_patient,
                            'checktime_id'=>$data_checktime,
                            'form_id'=>$data_form,
                            'info_type'=>$info_type,
                            'is_lock'=>$save_data['is_lock']
                        ];
                        unset($save_data);
                        unset($new_id);
                    }

                    //存储共享信息
                    if(!empty($gcoPoolFormDatas)){
                        foreach($gcoPoolFormDatas as $gcoPoolForm){
                            if($gcoPoolForm != $data_form && in_array($gcoPoolForm,$checkcontentFormArr[$data_checktime])){
                                $old_gco_key = $data_patient.'_'.$data_checktime.'_'.$gcoPoolForm;
                                if(isset($formTypeArr[$gcoPoolForm]) && !empty($formTypeArr[$gcoPoolForm])){
                                    if(isset($workInfoArr[$old_gco_key]) && !empty($workInfoArr[$old_gco_key])){
                                        if($workInfoArr[$old_gco_key]['is_lock'] == 0 && $data_lock == 1){
                                            $this->isSetInfo(false)->update([
                                                'is_lock'=>1,
                                                'info_type'=>$formTypeArr[$gcoPoolForm],
                                                'update_user_id'=>$data_update_user,
                                                'update_time'=>$data_update_time
                                            ],[
                                                'id' => $workInfoArr[$old_gco_key]['id']
                                            ]);
                                            $workInfoArr[$old_gco_key]['is_lock'] = 1;
                                        }
                                    }else{
                                        $save_gco_data = [
                                            'item_id'=>$data_item,
                                            'itemsig_id'=>$data['itemsig_id'],
                                            'patient_id'=>$data_patient,
                                            'checktime_id'=>$data_checktime,
                                            'form_id'=>$gcoPoolForm,
                                            'create_user_id'=>$data['create_user_id'],
                                            'create_time'=>$data['create_time'],
                                            'info_type'=>$formTypeArr[$gcoPoolForm],
                                            'is_lock'=>0
                                        ];
                                        if($data_lock == 1){
                                            $save_gco_data['is_lock'] = 1;
                                            $save_gco_data['update_user_id'] = $data_update_user;
                                            $save_gco_data['update_time'] = $data_update_time;
                                        }
                                        $newgco_id = $this->isSetInfo(false)->insert($save_gco_data);
                                        $workInfoArr[$old_gco_key] = [
                                            'id'=>$newgco_id,
                                            'patient_id'=>$data_patient,
                                            'checktime_id'=>$data_checktime,
                                            'form_id'=>$gcoPoolForm,
                                            'info_type'=>$save_gco_data['info_type'],
                                            'is_lock'=>$save_gco_data['is_lock']
                                        ];
                                        unset($save_gco_data);
                                    }
                                }
                                unset($old_gco_key);
                            }
                        }
                    }
                    unset($gcoPoolFormDatas);

                    unset($data_lock);
                    unset($data_update_user);
                    unset($data_update_time);
                }

                unset($datas[$dataKey]);
                unset($data_item);
                unset($data_patient);
                unset($data_checktime);
                unset($data_form);
                unset($info_type);
                unset($old_key);
            }
            unset($workInfoArr);
            unset($logDataArr);
        }

        //释放空间
        unset($datas);
        unset($cond);
        unset($where);

        return $last_id;

    }

    /**
     * Notes: 每天统计 工作信息
     * User: lltyy
     * DateTime: 2024/12/27 9:25
     *
     */
    public function scanData(){
        //统计检查项类 工作信息
        $this->LocalService()->adminWorkinfo->scanCheckData();

        //统计常规类识别 工作信息
        $this->LocalService()->adminWorkinfo->scanNormalData();

        //统计前一天所在月份的批次信息
        $this->LocalService()->adminWorkbatch->updateBatch(['now_date'=>date('Y-m',strtotime('-1 day',time()))]);

    }

    /**
     * Notes: 统计 所有工作信息
     * User: lltyy
     * DateTime: 2024/12/27 9:25
     *
     */
    public function scanALLData(){
        //统计检查项类 工作信息
        $this->LocalService()->adminWorkinfo->scanCheckData();

        //统计常规类识别 工作信息
        $this->LocalService()->adminWorkinfo->scanNormalData();

        //统计批次信息
        $this->LocalService()->adminWorkbatch->updateAllBatch();
    }

}