<?php

namespace Application\Service\DB\Project;

use Application\Service\DB\Dictionary\Form;
use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;
use Application\Service\Extension\Helper\ArrayHelper;
use Application\Service\DB\Dictionary\FormGroup;
use Laminas\Db\Sql\Predicate\NotIn;

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

    public function save(array $saveData)
    {
        if (empty($saveData[$this->primaryKey])) {
            $redis = $this->LocalService()->redisExtend->getRedisInstance();

            $lockKey = sprintf('PatientworkBatch:%s:%s:%s:%s:%s', $saveData['item_id'], $saveData['itemsig_id'], $saveData['checktime_id'], $saveData['patient_id'], $saveData['form_id']);
            $lockTTL = 10;  // 锁的有效时间 10 秒

            // 新增 （数据已经存在就不要再新增了）
            if ($redis->set('Insert:' . $lockKey, 'locked', [
                'nx',
                'ex' => $lockTTL,
            ])) {
                $insertId = parent::save($saveData);
                // 将新数据的id存入redis缓存中
                $redis->set($lockKey, $insertId, [
                    'nx',
                    'ex' => $lockTTL * 6,
                ]);
                return $insertId;
            }else {
                return $redis->get($lockKey);
            }
        }

        return parent::save($saveData); // TODO: Change the autogenerated stub
    }

    /**
     * Notes: 更新受试者阶段工作进度批次信息
     * User: lltyy
     * DateTime: 2025/5/21 18:39
     *
     * @param array $params
     */
    public function multipleUpdateData(array $params) {
        $item_id = isset($params['item_id']) && !empty($params['item_id']) ? $params['item_id'] : 0;//副项目ID
        $itemsig_id = isset($params['itemsig_id']) && !empty($params['itemsig_id']) ? $params['itemsig_id'] : 0;//项目中心ID
        $patient_id = isset($params['patient_id']) && !empty($params['patient_id']) ? $params['patient_id'] : 0;//患者ID
        $checktime_id = isset($params['checktime_id']) && !empty($params['checktime_id']) ? $params['checktime_id'] : 0;//项目检查点ID
        $formGroup = isset($params['formgroup_id']) && !empty($params['formgroup_id']) ? $params['formgroup_id'] : 0;//表单分组
        $form_id = isset($params['form_id']) && !empty($params['form_id']) ? $params['form_id'] : 0;//操作表单ID
        $batch_arr = isset($params['batch_id']) && !empty($params['batch_id']) ? array_values($params['batch_id']) : [];//需要更新的进度批次ID
        $operate_type = isset($params['operate_type']) && !empty($params['operate_type']) ? $params['operate_type'] : 0;//操作类型 1、SDV 2、Review 3、锁定 4、取消Review 5、解锁 6 电子签名 7 取消电子签名 8 SDV质疑 9 Review质疑 10 新增Content 11 编辑Content 12 删除Content  13 恢复Content  14 取消SDV
        $user_id = isset($params['user_id']) && !empty($params['user_id']) ? $params['user_id'] : 0;//操作人ID
        $update_time = isset($params['time']) && !empty($params['time']) ? $params['time'] : time();//操作人时间
        if((empty($item_id) || empty($itemsig_id)) && !empty($patient_id)){
            $patientData = $this->LocalService()->patient->fetchOne([
                'where'=> [
                    'id' => $patient_id,
                ],
                'columns'=>['item_id','itemsig_id']
            ]);
            $item_id = !empty($patientData) ? $patientData['item_id'] : 0;
            $itemsig_id = !empty($patientData) ? $patientData['itemsig_id'] : 0;
            unset($patientData);
        }
        //获取表单信息
        $more_form_arr = [];
        $form_medical = 0;//表单是否为医嘱类【0 否 1 是】
        $form_score = 0;//表单是否为评分类表单【0 否 1 是】
        if(!empty($form_id) && (empty($formGroup) || empty($form_info_category) || empty($form_score) || empty($more_form_arr))){
            $formData = $this->LocalService()->itemForm->fetchOne([
                'where'=> [
                    'id' => $form_id,
                ],
                'columns'=>['group_id','info_collect_type','type','is_using_score']
            ]);
            if(!empty($formData)){
                $formGroup = !empty($formData['group_id']) ? $formData['group_id'] : 0;
                $form_score = !empty($formData['is_using_score']) ? $formData['is_using_score'] : 0;
                if($formGroup == FormGroup::NORMAL_OCR && $formData['info_collect_type'] == 3){
                    $form_medical = 1;
                }
                if($formData['type'] == Form::FORM_TYPE_MULTI){
                    $more_form_arr[] = $form_id;
                }
            }
            unset($formData);
        }else{
            //获取一对多表单ID
            $more_form_arr = $this->LocalService()->itemForm->fetchCol('id',[
                'is_del' => 0,
                'item_id' => $item_id,
                'type' => Form::FORM_TYPE_MULTI,
            ]);
            $more_form_arr = !empty($more_form_arr) ? array_values($more_form_arr) : [];
        }


        //更新明细质疑状态
        if(empty($batch_arr) && in_array($operate_type,[8,9,10,11,12,13])){
            $content_arr = isset($params['content_id']) && !empty($params['content_id']) ? $params['content_id'] : [];//需要更新的进度content
            //新增Content，获取批次ID
            $batch_id = 0;
            if(in_array($operate_type,[10,11,12,13]) && !empty($content_arr)){
                //试验总结类、AE类、SAE类、贯穿全程类表单
                if($formGroup == FormGroup::TEST_SUMMARY){//试验总结类
                    $checktime_id = $this->LocalService()->itemChecktime->getOneFieldVal('id',[
                        'is_del' => 9,
                        'item_id' => $item_id,
                    ]);
                }
                if($formGroup == FormGroup::AE){//AE类
                    $checktime_id = -1;
                }
                if($formGroup == FormGroup::SAE){//SAE类
                    $checktime_id = -2;
                }
                if($formGroup == FormGroup::THROUGH_PROCESS){//贯穿全程类
                    $checktime_id = 0;
                }
                $end_batch_id = $this->getOneFieldVal('id',[
                    'is_del' => 0,
                    'patient_id' => $patient_id,
                    'form_id' => $form_id,
                    'checktime_id' => $checktime_id,
                ]);
                if(empty($end_batch_id)){
                    $end_batch_id = $this->getOneFieldVal('id',[
                        'is_del' => 1,
                        'patient_id' => $patient_id,
                        'form_id' => $form_id,
                        'checktime_id' => $checktime_id,
                    ]);
                    if(!empty($end_batch_id)){
                        $this->isSetInfo(false)->update(['is_del'=>0,'is_sdv'=>0,'is_review'=>0,'edc_lock'=>0,'is_signature'=>0,'update_user_id'=>$user_id,'update_time'=>$update_time],[
                            'id' => $end_batch_id,
                        ]);
                    }
                }
                $batch_id = !empty($end_batch_id) ? $end_batch_id : 0;
                unset($end_batch_id);
                if(empty($batch_id)){
                    $batch_id = $this->isSetInfo(false)->save([
                        'item_id'=>$item_id,
                        'itemsig_id'=>$itemsig_id,
                        'patient_id'=>$patient_id,
                        'checktime_id'=>$checktime_id,
                        'formgroup_id'=>$formGroup,
                        'form_id'=>$form_id,
                        'is_yc'=>-1,
                        'is_lock'=>0,
                        'is_complete'=>0,
                        'is_inactive'=>0,
                        'is_sdv'=>0,
                        'is_review'=>0,
                        'edc_lock'=>0,
                        'sdv_question'=>0,
                        'review_question'=>0,
                        'is_signature'=>0,
                        'create_user_id'=>$user_id,
                        'create_time'=>$update_time
                    ]);
                }
            }

            //更新明细信息
            $batch_arr = $this->LocalService()->projectPatientworkinfo->multipleUpdateData([
                'item_id' => $item_id,
                'itemsig_id' => $itemsig_id,
                'patient_id' => $patient_id,
                'checktime_id' => $checktime_id,
                'form_id' => $form_id,
                'formgroup_id' => $formGroup,
                'operate_type'=>$operate_type,
                'content_id'=>$content_arr,
                'batch_id'=>$batch_id,
                'user_id'=>$user_id,
                'time'=>$update_time,
            ]);
        }

        //更新批次信息
        if(!empty($batch_arr) && !empty($item_id)){
            //获取该表单下的明细进度情况
            $select_columns = ['batch_id','form_id'];
            //完成状态
            if(in_array($operate_type,[10,11,12,13])){
                $select_columns[] = 'is_yc';
                $select_columns[] = 'is_lock';
                $select_columns[] = 'is_complete';
                $select_columns[] = 'is_inactive';
            }

            //SDV
            //if(in_array($operate_type,[1,8,9,10,11,12,13])){
            if(in_array($operate_type,[1,10,11,12,13])){
                $select_columns[] = 'is_sdv';
            }
            //Review、取消Review
            //if(in_array($operate_type,[2,4,8,9,10,11,12,13])){
            if(in_array($operate_type,[2,4,10,11,12,13])){
                $select_columns[] = 'is_review';
            }
            //锁定、取消Review、取消锁定
            //if(in_array($operate_type,[3,4,5,8,9,10,11,12,13])){
            if(in_array($operate_type,[3,4,5,10,11,12,13])){
                $select_columns[] = 'edc_lock';
            }
            //电子签名
            if(in_array($operate_type,[4,5,6,7,10,11,12,13])){
                $select_columns[] = 'is_signature';
            }
            //质疑
            if(in_array($operate_type,[8,9,10,11,12,13])){
                $select_columns[] = 'sdv_question';
                $select_columns[] = 'review_question';
            }

            $infos = $this->LocalService()->projectPatientworkinfo->fetchAll([
                'where'=> [
                    'is_del' => 0,
                    'batch_id' => StringHelper::toArray($batch_arr),
                ],
                'columns'=>$select_columns
            ]);
            if(!empty($infos)){
                $batch_count_arr = [];//获取明细对应的事件数量
                foreach($infos as $infoKey=>$info){
                    $info_key_arr = array_values(array_keys($info));
                    foreach($info_key_arr as $info_key){
                        if(!in_array($info_key,['batch_id','form_id'])){
                            $batch_count_arr[$info['batch_id']][$info_key][$info[$info_key]] = isset($batch_count_arr[$info['batch_id']]) && isset($batch_count_arr[$info['batch_id']][$info_key]) && isset($batch_count_arr[$info['batch_id']][$info_key][$info[$info_key]]) && !empty($batch_count_arr[$info['batch_id']][$info_key][$info[$info_key]]) ? $batch_count_arr[$info['batch_id']][$info_key][$info[$info_key]] : 0;
                            $batch_count_arr[$info['batch_id']][$info_key][$info[$info_key]]++;
                        }
                    }
                    unset($infos[$infoKey]);
                }
                //更新数据数组一对多数据 或 一对一数据【质疑信息】
                if(!empty($batch_count_arr)){
                    //获取非AE表单Patientform表信息
                    $patientFormData = [
                        'is_complete'=>0,
                        'is_inactive'=>0,
                        'is_lock'=>0
                    ];
                    if(!empty($form_id) && in_array($operate_type,[10,11,12,13]) && $formGroup != FormGroup::AE){
                        $patientFormWhere = [
                            'is_del' => 0,
                            'patient_id' => $patient_id,
                            'form_id' => $form_id,
                        ];
                        if(!in_array($formGroup,[FormGroup::SAE,FormGroup::TEST_SUMMARY,FormGroup::THROUGH_PROCESS])){
                            $patientFormWhere['checktime_id'] = $checktime_id;
                        }
                        $patientFormData = $this->LocalService()->patientForm->fetchOne([
                            'where'=>$patientFormWhere,
                            'columns'=>['is_complete','is_inactive','is_lock']
                        ]);
                    }
                    //获取非AE信息
                    foreach($batch_count_arr as $batch_count_batchid=>$batch_count_infos){
                        $upFieldData = [
                            'update_user_id'=>$user_id,
                            'update_time'=>$update_time
                        ];
                        $upInfoFieldData = [];
                        //SDV操作状态【0 未操作 1 已操作 2 操作过但已删除】
                        //if(in_array($operate_type,[1,7,8,9,10,11,12,13])){
                        if(in_array($operate_type,[1,7,10,11,12,13])){
                            $upFieldData['is_sdv'] = 0;
                            if(isset($batch_count_infos['is_sdv']) && !empty($batch_count_infos['is_sdv'])){
                                $sdv_over_count = isset($batch_count_infos['is_sdv'][1]) && !empty($batch_count_infos['is_sdv'][1]) ? $batch_count_infos['is_sdv'][1] : 0;//操作数量
                                $sdv_delete_count = isset($batch_count_infos['is_sdv'][2]) && !empty($batch_count_infos['is_sdv'][2]) ? $batch_count_infos['is_sdv'][2] : 0;//删除数量
                                if(!empty($sdv_over_count)){//有已操作
                                    $sdv_no_count = isset($batch_count_infos['is_sdv'][0]) && !empty($batch_count_infos['is_sdv'][0]) ? $batch_count_infos['is_sdv'][0] : 0;//未操作数量
                                    //无未操 其 无 操作已删除 则为 全部操作，否则为部分操作
                                    $upFieldData['is_sdv'] = empty($sdv_delete_count) && empty($sdv_no_count) ? 1 : 2;
                                    unset($sdv_no_count);
                                }else{//无已操作
                                    $upFieldData['is_sdv'] = !empty($sdv_delete_count) ? 3 : 0;//无 操作过已删除 则为  未操作，否则为 操作过但已删除
                                }
                                unset($sdv_over_count);
                                unset($sdv_delete_count);
                            }
                        }

                        //Review操作状态【0 未操作 1 已操作 2 操作过但已删除】
                        //if(in_array($operate_type,[2,4,7,8,9,10,11,12,13])){
                        if(in_array($operate_type,[2,4,7,10,11,12,13])){
                            $upFieldData['is_review'] = 0;
                            if(isset($batch_count_infos['is_review']) && !empty($batch_count_infos['is_review'])) {
                                $review_over_count = isset($batch_count_infos['is_review'][1]) && !empty($batch_count_infos['is_review'][1]) ? $batch_count_infos['is_review'][1] : 0;//操作数量
                                $review_delete_count = isset($batch_count_infos['is_review'][2]) && !empty($batch_count_infos['is_review'][2]) ? $batch_count_infos['is_review'][2] : 0;//删除数量
                                if(!empty($review_over_count)){//有已操作
                                    $review_no_count = isset($batch_count_infos['is_review'][0]) && !empty($batch_count_infos['is_review'][0]) ? $batch_count_infos['is_review'][0] : 0;//未操作数量
                                    //无未操 其 无 操作已删除 则为 全部操作，否则为部分操作
                                    $upFieldData['is_review'] = empty($review_delete_count) && empty($review_no_count) ? 1 : 2;
                                    unset($review_no_count);
                                }else{//无已操作
                                    $upFieldData['is_review'] = !empty($review_delete_count) ? 3 : 0;//无 操作过已删除 则为  未操作，否则为 操作过但已删除
                                }
                                unset($review_over_count);
                                unset($review_delete_count);
                            }
                        }

                        //EDC锁定操作状态【0 未锁定 1 已锁定】
                        //if(in_array($operate_type,[3,4,5,7,8,9,10,11,12,13])){
                        if(in_array($operate_type,[3,4,5,7,10,11,12,13])){
                            $upFieldData['edc_lock'] = 0;
                            if(isset($batch_count_infos['edc_lock']) && !empty($batch_count_infos['edc_lock'])) {
                                $lock_no_count = isset($batch_count_infos['edc_lock'][0]) && !empty($batch_count_infos['edc_lock'][0]) ? $batch_count_infos['edc_lock'][0] : 0;//未锁定数量
                                $lock_over_count = isset($batch_count_infos['edc_lock'][1]) && !empty($batch_count_infos['edc_lock'][1]) ? $batch_count_infos['edc_lock'][1] : 0;//锁定数量
                                if(empty($lock_over_count)){
                                    $lock_over_count = isset($batch_count_infos['edc_lock'][2]) && !empty($batch_count_infos['edc_lock'][2]) ? $batch_count_infos['edc_lock'][2] : 0;//锁定数量
                                }
                                if(empty($lock_no_count) && !empty($lock_over_count)){//无未锁定 且 有已锁定，则表示全部已锁定
                                    $upFieldData['edc_lock'] = 1;
                                }
                                if(!empty($lock_no_count) && !empty($lock_over_count)){//有未锁定 且 有已锁定，则表示部分已锁定
                                    $upFieldData['edc_lock'] = 2;
                                }
                                unset($lock_no_count);
                                unset($lock_over_count);
                            }

                            //如果锁定操作，该批次下锁定状态为全部锁定，则更新明细表锁定状态为 2 全部锁定
                            if($operate_type == 3 && isset($upFieldData['edc_lock']) && $upFieldData['edc_lock'] == 1){
                                $upInfoFieldData['edc_lock'] = 2;
                            }

                            //电子签名
                            if(isset($batch_count_infos['is_signature']) && !empty($batch_count_infos['is_signature'])) {
                                $upFieldData['is_signature'] = 0;
                                $signature_no_count = isset($batch_count_infos['is_signature'][0]) && !empty($batch_count_infos['is_signature'][0]) ? $batch_count_infos['is_signature'][0] : 0;//未签名数量
                                $signature_over_count = isset($batch_count_infos['is_signature'][1]) && !empty($batch_count_infos['is_signature'][1]) ? $batch_count_infos['is_signature'][1] : 0;//签名数量
                                if(empty($signature_no_count) && !empty($signature_over_count)){//无未签名 且 有已签名，则表示已签名
                                    $upFieldData['is_signature'] = 1;
                                }
                                unset($signature_no_count);
                                unset($signature_over_count);
                            }
                        }

                        //质疑状态【0 无质疑 1 全部回复 2 部分回复 3 已全部关闭 4 已全部删除】
                        if(in_array($operate_type,[8,9,10,11,12,13])){
                            //SDV质疑
                            $upFieldData['sdv_question'] = 0;
                            if(isset($batch_count_infos['sdv_question']) && !empty($batch_count_infos['sdv_question'])) {
                                $sdv_question_count2 = isset($batch_count_infos['sdv_question'][2]) && !empty($batch_count_infos['sdv_question'][2]) ? $batch_count_infos['sdv_question'][2] : 0;//部分回复
                                if(!empty($sdv_question_count2)){//有部分回复
                                    $upFieldData['sdv_question'] = 2;
                                }else{//无部分回复
                                    $sdv_question_count1 = isset($batch_count_infos['sdv_question'][1]) && !empty($batch_count_infos['sdv_question'][1]) ? $batch_count_infos['sdv_question'][1] : 0;//全部回复
                                    if(!empty($sdv_question_count1)){//有全部回复
                                        $upFieldData['sdv_question'] = 1;
                                    }else{//无全部回复
                                        $sdv_question_count3 = isset($batch_count_infos['sdv_question'][3]) && !empty($batch_count_infos['sdv_question'][3]) ? $batch_count_infos['sdv_question'][3] : 0;//全部关闭
                                        if(!empty($sdv_question_count3)){//有已全部关闭
                                            $upFieldData['sdv_question'] = 3;
                                        }else{//无已全部关闭
                                            $sdv_question_count4 = isset($batch_count_infos['sdv_question'][4]) && !empty($batch_count_infos['sdv_question'][4]) ? $batch_count_infos['sdv_question'][4] : 0;//全部删除
                                            if(!empty($sdv_question_count4)){//有已全部删除
                                                $upFieldData['sdv_question'] = 4;
                                            }
                                            unset($sdv_question_count4);
                                        }
                                        unset($sdv_question_count3);
                                    }
                                    unset($sdv_question_count1);
                                }
                                unset($sdv_question_count2);
                            }

                            //Review质疑
                            $upFieldData['review_question'] = 0;
                            if(isset($batch_count_infos['review_question']) && !empty($batch_count_infos['review_question'])) {
                                $review_question_count2 = isset($batch_count_infos['review_question'][2]) && !empty($batch_count_infos['review_question'][2]) ? $batch_count_infos['review_question'][2] : 0;//部分回复
                                if(!empty($review_question_count2)){//有部分回复
                                    $upFieldData['review_question'] = 2;
                                }else{//无部分回复
                                    $review_question_count1 = isset($batch_count_infos['review_question'][1]) && !empty($batch_count_infos['review_question'][1]) ? $batch_count_infos['review_question'][1] : 0;//全部回复
                                    if(!empty($review_question_count1)){//有全部回复
                                        $upFieldData['review_question'] = 1;
                                    }else{//无全部回复
                                        $review_question_count3 = isset($batch_count_infos['review_question'][3]) && !empty($batch_count_infos['review_question'][3]) ? $batch_count_infos['review_question'][3] : 0;//全部关闭
                                        if(!empty($review_question_count3)){//有已全部关闭
                                            $upFieldData['review_question'] = 3;
                                        }else{//无已全部关闭
                                            $review_question_count4 = isset($batch_count_infos['review_question'][4]) && !empty($batch_count_infos['review_question'][4]) ? $batch_count_infos['review_question'][4] : 0;//全部删除
                                            if(!empty($review_question_count4)){//有已全部删除
                                                $upFieldData['review_question'] = 4;
                                            }
                                            unset($review_question_count4);
                                        }
                                        unset($review_question_count3);
                                    }
                                    unset($review_question_count1);
                                }
                                unset($review_question_count2);
                            }
                        }

                        //更新表单其他信息
                        if(in_array($operate_type,[10,11,12,13])){
                            $upFieldData['is_inactive'] = !empty($patientFormData) ? $patientFormData['is_inactive'] : 0;
                            $upFieldData['is_yc'] = 0;
                            $upFieldData['is_complete'] = !empty($patientFormData) ? $patientFormData['is_complete'] : 0;
                            $upFieldData['is_lock'] = !empty($patientFormData) ? $patientFormData['is_lock'] : 0;
                            //非医嘱、评分表单 获取明细对应的Complete
                            if($form_medical != 1 && $form_score != 1 && isset($batch_count_infos['is_complete']) && !empty($batch_count_infos['is_complete'])) {
                                if(isset($batch_count_infos['is_complete'][0]) && !empty($batch_count_infos['is_complete'][0])){
                                    $upFieldData['is_complete'] = 0;
                                }else{
                                    if(isset($batch_count_infos['is_complete'][1]) && !empty($batch_count_infos['is_complete'][1])){
                                        $upFieldData['is_complete'] = 1;
                                    }
                                }
                            }
                            //获取明细对应的is_lock
                            if(isset($batch_count_infos['is_lock']) && !empty($batch_count_infos['is_lock'])) {
                                if(isset($batch_count_infos['is_lock'][0]) && !empty($batch_count_infos['is_lock'][0])){
                                    $upFieldData['is_lock'] = 0;
                                }else{
                                    if(isset($batch_count_infos['is_lock'][1]) && !empty($batch_count_infos['is_lock'][1])){
                                        $upFieldData['is_lock'] = 1;
                                    }
                                }
                            }
                            //获取明细对应的is_yc
                            if(isset($batch_count_infos['is_yc']) && !empty($batch_count_infos['is_yc'])) {
                                if(isset($batch_count_infos['is_yc'][1]) && !empty($batch_count_infos['is_yc'][1])){
                                    $upFieldData['is_yc'] = 1;
                                }else{
                                    if(isset($batch_count_infos['is_yc'][-1]) && !empty($batch_count_infos['is_yc'][-1])){
                                        $upFieldData['is_yc'] = -1;
                                    }else{
                                        if(isset($batch_count_infos['is_yc'][0]) && !empty($batch_count_infos['is_yc'][0])){
                                            $upFieldData['is_yc'] = 0;
                                        }
                                    }
                                }
                            }

                            $upInfoFieldData['is_inactive'] = $upFieldData['is_inactive'];
                            $upInfoFieldData['is_yc'] = $upFieldData['is_yc'];
                            $upInfoFieldData['is_lock'] = $upFieldData['is_lock'];
                        }
                        
                        //更新明细信息
                        if(!empty($upInfoFieldData)){
                            $upInfoFieldData['update_user_id'] = $user_id;
                            $upInfoFieldData['update_time'] = $update_time;
                            $this->LocalService()->projectPatientworkinfo->isSetInfo(false)->update($upInfoFieldData,[
                                'is_del' => 0,
                                'batch_id' => $batch_count_batchid
                            ]);
                        }
                        unset($upInfoFieldData);

                        //更新明细锁定状态
                        //if(in_array($operate_type,[8,9,10,11,12,13]) && isset($upFieldData['edc_lock']) && $upFieldData['edc_lock'] == 2){
                        if(in_array($operate_type,[10,11,12,13]) && isset($upFieldData['edc_lock']) && $upFieldData['edc_lock'] == 2){
                            $this->LocalService()->projectPatientworkinfo->isSetInfo(false)->update(['edc_lock'=>1,'update_user_id'=>$user_id,'update_time'=>$update_time],[
                                'is_del' => 0,
                                'edc_lock' => 2,
                                'batch_id' => $batch_count_batchid
                            ]);
                        }

                        //删除可以更新的batch
                        $batch_arr = array_values(array_diff_assoc($batch_arr,[$batch_count_batchid]));

                        $this->isSetInfo(false)->update($upFieldData,'id = '.$batch_count_batchid);

                        unset($batch_count_arr[$batch_count_batchid]);
                        unset($upFieldData);
                    }
                    unset($patientFormData);
                }
                unset($batch_count_arr);
            }

            //删除无明细信息的batch
            if(!empty($batch_arr) ){
                //更新一对多状态is_complete为-1
                $delete_where = [
                    'id' => StringHelper::toArray($batch_arr),
                ];
                if(!empty($more_form_arr)){
                    $delete_where[] = new NotIn('form_id', StringHelper::toArray($more_form_arr));
                    $this->isSetInfo(false)->update(['is_complete'=>-1,'is_sdv'=>0,'is_review'=>0,'edc_lock'=>0,'is_signature'=>0,'sdv_question'=>0,'review_question'=>0,'update_user_id'=>$user_id,'update_time'=>$update_time],'id in ('.implode(',',$batch_arr).') and form_id in ('.implode(',',$more_form_arr).')');
                }
                $this->isSetInfo(false)->update(['is_del'=>1,'update_user_id'=>$user_id,'update_time'=>$update_time],$delete_where);
                unset($delete_where);
            }
        }
        //释放空间
        unset($item_id);
        unset($itemsig_id);
        unset($checktime_id);
        unset($formGroup);
        unset($form_id);
        unset($batch_arr);
        unset($operate_type);
        unset($user_id);
        unset($update_time);
        unset($more_form_arr);
        unset($form_medical);
        unset($form_score);
    }


    /**
     * Notes: 创建时新增批次及明细信息受试者阶段工作进度批次信息
     * User: lltyy
     * DateTime: 2025/5/21 18:39
     *
     * @param array $params
     */
    public function generatorWorkData(array $params)
    {
        $item_id = isset($params['item_id']) && !empty($params['item_id']) ? $params['item_id'] : 0;//副项目ID
        $itemsig_id = isset($params['itemsig_id']) && !empty($params['itemsig_id']) ? $params['itemsig_id'] : 0;//项目中心ID
        $patient_id = isset($params['patient_id']) && !empty($params['patient_id']) ? $params['patient_id'] : 0;//患者ID
        $checktime_id = isset($params['checktime_id']) && !empty($params['checktime_id']) ? $params['checktime_id'] : 0;//项目检查点ID
        $form_id = isset($params['form_id']) && !empty($params['form_id']) ? $params['form_id'] : 0;//操作表单ID
        $formGroup = isset($params['formgroup_id']) && !empty($params['formgroup_id']) ? $params['formgroup_id'] : 0;//操作表单ID
        $content_id = isset($params['content_id']) && !empty($params['content_id']) ? $params['content_id'] : 0;//需要更新的进度content
        if ((empty($item_id) || empty($itemsig_id)) && !empty($patient_id)) {
            $patientData = $this->LocalService()->patient->fetchOne([
                'where' => [
                    'id' => $patient_id,
                ],
                'columns' => ['item_id', 'itemsig_id']
            ]);
            $item_id = !empty($patientData) ? $patientData['item_id'] : 0;
            $itemsig_id = !empty($patientData) ? $patientData['itemsig_id'] : 0;
            unset($patientData);
        }
        //获取表单信息
        if (!empty($form_id) && empty($formGroup)) {
            $formGroup = $this->LocalService()->itemForm->getOneFieldVal('group_id', [
                'id' => $form_id,
            ]);
        }

        //试验总结类、AE类、SAE类、贯穿全程类表单
        if ($formGroup == FormGroup::TEST_SUMMARY) {//试验总结类
            $checktime_id = $this->LocalService()->itemChecktime->getOneFieldVal('id', [
                'is_del' => 9,
                'item_id' => $item_id,
            ]);
        }
        if ($formGroup == FormGroup::AE) {//AE类
            $checktime_id = -1;
        }
        if ($formGroup == FormGroup::SAE) {//SAE类
            $checktime_id = -2;
        }
        if ($formGroup == FormGroup::THROUGH_PROCESS) {//贯穿全程类
            $checktime_id = 0;
        }

        $initWorkData = ArrayHelper::merge([
            'id' => 0,
            'item_id' => $item_id,
            'itemsig_id' => $itemsig_id,
            'patient_id' => $patient_id,
            'checktime_id' => $checktime_id,
            'form_id' => $form_id,
        ], [
            'is_del' => 0,
            'is_sdv' => 0,
            'is_review' => 0,
            'edc_lock' => 0,
            'is_signature' => 0,
            'sdv_question' => 0,
            'review_question' => 0,
            'is_complete' => 0,
            'is_lock' => 0,
            'is_yc' => -1,
            'is_inactive' => 0,
        ]);

        //获取批次ID
        $batchData = $this->fetchOne([
            'where' => [
                'item_id' => $item_id,
                'itemsig_id' => $itemsig_id,
                'patient_id' => $patient_id,
                'checktime_id' => $checktime_id,
                'form_id' => $form_id
            ],
            'columns' => [ 'id', 'is_del' ]
        ]) ;

        // 创建批次
        if ( $batchData['is_del'] ?? 1  ) {
            $saveResult = $this->save(ArrayHelper::merge($initWorkData,[
                'id' => $batchData['id'] ?? 0,
                'formgroup_id' => $formGroup,
            ]));

            $batchData['id'] = $batchData['id'] ?? $saveResult;
        }

        $infoData = $this->LocalService()->projectPatientworkinfo->fetchOne([
            'where' => [
                'content_id' => $content_id,
                'formgroup_id' => $formGroup,
            ],
            'columns' => ['id', 'is_del']
        ]);

        // 创建明细
        if ( $infoData['is_del'] ?? 1  ) {
            $this->LocalService()->projectPatientworkinfo->save(ArrayHelper::merge($initWorkData, [
                'id' => $infoData['id'] ?? 0,
                'content_id' => $content_id,
                'batch_id' => $batchData['id'],
                'formgroup_id' => $formGroup,
            ]));
        }
    }

}