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

use Application\Service\Extension\Helper\ArrayHelper;
use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Expression;


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

    /**
     * Notes: 删除真实数据
     * return int 受影响的行数
     */
    public function delete($where){
        return $this->tableGateway->delete($where);
    }

    public function patientChecktimeListData($item_id,$patientChecktimeListData){
        $whereEdcList = [
            'where'=> [
                'is_del' => 0,
                'item_id' => $item_id,
            ],
            'columns'=>['id','item_id','patient_id','create_user_id','item_checktime_list'],
        ];
        $patientChecktimeListEdcData = $this->LocalService()->patientChecktimeListEdc->fetchAll($whereEdcList);
        $patientChecktimeListEdcDatas  = [];
        if (!empty($patientChecktimeListEdcData)){
            foreach ($patientChecktimeListEdcData as $patientChecktimeListEdcDataKey=>&$patientChecktimeListEdcDatum) {
                if  (!empty($patientChecktimeListEdcDatum['item_checktime_list'])){
                    $patientChecktimeListEdcDatum['itemChecktimeData'] = json_decode($patientChecktimeListEdcDatum['item_checktime_list'],true);
                    unset($patientChecktimeListEdcDatum['item_checktime_list']);
                }
            }
            $patientChecktimeListEdcDatas = array_column($patientChecktimeListEdcData,null,'patient_id');
        }
        if (!empty($patientChecktimeListData)){
            foreach ($patientChecktimeListData as $patientChecktimeListDataKey=>&$patientChecktimeListDataDatum) {
                $patientChecktimeListDataDatum['patient_id'] = $patientChecktimeListDataDatum['id'];
                unset($patientChecktimeListDataDatum['id']);
                foreach ($patientChecktimeListDataDatum['itemChecktimeData'] as $itemChecktimeListKey=>&$itemChecktimeListValue){
                    if(isset($patientChecktimeListEdcDatas[$patientChecktimeListDataDatum['patient_id']]['itemChecktimeData'])){
                        $newItemChecktimeDataKey = $patientChecktimeListEdcDatas[$patientChecktimeListDataDatum['patient_id']]['itemChecktimeData'];
                        //匹配上
                        if(isset($newItemChecktimeDataKey[$itemChecktimeListKey])){
                            $newItemChecktimeDataDatum = $newItemChecktimeDataKey[$itemChecktimeListKey];
                            $count = $itemChecktimeListValue['count'];
                            $is_complete = $itemChecktimeListValue['is_complete'];
                            $is_complete_flag = $itemChecktimeListValue['is_complete_flag'];
                            $form_ids = $itemChecktimeListValue['form_ids'];
                            $itemChecktimeListValue = $newItemChecktimeDataDatum;
                            $itemChecktimeListValue['count'] = $count;
                            $itemChecktimeListValue['is_complete'] = $is_complete;
                            $itemChecktimeListValue['is_complete_flag'] = $is_complete_flag;
                            $itemChecktimeListValue['form_ids'] = $form_ids;
                            $patientChecktimeListDataDatum['id'] = $patientChecktimeListEdcDatas[$patientChecktimeListDataDatum['patient_id']]['id'];

                        }
                    }
                }
            }

        }
        //删除受试者
        if(count($patientChecktimeListData) != count($patientChecktimeListEdcDatas) && !empty($patientChecktimeListEdcDatas)){
            foreach ($patientChecktimeListEdcDatas as $patientChecktimeListEdcDatasKey=>&$patientChecktimeListEdcDatasDatum) {
                $update = [];
                if  (!in_array($patientChecktimeListEdcDatasDatum['patient_id'],array_column($patientChecktimeListData,'patient_id'))){
                    $update['is_del'] = 1;
                    $update['id'] = $patientChecktimeListEdcDatasDatum['id'];
                        $this->LocalService()->patientChecktimeListEdc->save($update);
                }
            }
        }
        return $patientChecktimeListData;
    }
    public function patientWorkCountData($item_id,$patientWorkCountData){
        $whereEdcCount = [
            'where'=>[
                'is_del' => 0,
                'item_id' => $item_id,
            ],
            'columns'=>['id','item_id','patient_id'],
        ];
        $patientWorkCountOldData = $this->LocalService()->patientWorkCountEdc->fetchAll($whereEdcCount);
        $patientWorkCountOldDatas = !empty($patientWorkCountOldData) ? array_column($patientWorkCountOldData,null,'patient_id'): [];
        if (!empty($patientWorkCountData)){
            foreach ($patientWorkCountData as $patientWorkCountDataKey=>&$patientWorkCountDataDatum) {
                unset($patientWorkCountDataDatum['patient_number']);
                if  (isset($patientWorkCountOldDatas[$patientWorkCountDataDatum['patient_id']])){
                    $patientWorkCountDataDatum['id'] =$patientWorkCountOldDatas[$patientWorkCountDataDatum['patient_id']]['id'];
                }
            }
        }
        //删除受试者
        if(count($patientWorkCountOldData) != count($patientWorkCountData) && !empty($patientWorkCountOldData)){
            foreach ($patientWorkCountOldData as $patientWorkCountOldDataKey=>&$patientWorkCountOldDataDatum) {
                $pudate = [];
                if  (!in_array($patientWorkCountOldDataDatum['patient_id'],array_column($patientWorkCountData,'patient_id'))){
                    $pudate['is_del'] = 1;
                    $pudate['id'] = $patientWorkCountOldDataDatum['id'];
                        $resultCount = $this->LocalService()->patientWorkCountEdc->save($pudate);

                }
            }
        }
        return $patientWorkCountData;
    }

    //处理完成质疑

    public function questionClearUpdate($whereData = []){
        if (empty($whereData) || empty($whereData['patient_id'])) return false;
        //受试者总统计

        $this->questionClearCountUpdate($whereData);


        $wherePatientWorkCount['where'] = [
            'is_del' => 0,
            'patient_id' => $whereData['patient_id'],
        ];
        !empty($whereData['item_id']) && isset($whereData['item_id']) && $wherePatientWorkCount['where']['item_id'] = $whereData['item_id'];
        $patientWorkCountInfo = $this->LocalService()->patientChecktimeListEdc->fetchOne($wherePatientWorkCount);
        if (empty($patientWorkCountInfo) || !isset($whereData['type']))  return false;
        $type = $whereData['type'];
        $itemChecktimeLists = '';
        $itemChecktimeList = json_decode($patientWorkCountInfo['item_checktime_list'],1);
        $flag = 0;
        $checktimeIdArr = (isset($whereData['checktime_id']) && !empty($whereData['checktime_id'])) ? explode(',',$whereData['checktime_id']):[];
        $checkTimeIdDatas = [];
        if (isset($whereData['form_id']) && !empty($whereData['form_id'])){
            $checkTimeIdDatas = $this->checkTimeByIdData($whereData['form_id']);
        }
        foreach ($itemChecktimeList as &$itemChecktimeListValue) {
            //r确认数据表单and添加质疑
            $formIdArr = (isset($itemChecktimeListValue['form_ids']) && !empty($itemChecktimeListValue['form_ids'])) ? explode(',',$itemChecktimeListValue['form_ids']):[];
            if (isset($whereData['error_type']) && !empty($whereData['error_type']) && isset($whereData['role_type'])){
                if (empty($type)) {
                    if ($whereData['role_type'] == 0) {
                        if (empty($whereData['checktime_id'])) {
                            if (in_array($whereData['form_id'], $formIdArr)) {
                                $itemChecktimeListValue['question_count_r'] = $itemChecktimeListValue['question_count_r'] + 1;
                                $itemChecktimeListValue['question_count'] = $itemChecktimeListValue['question_count'] + 1;
                                $flag = 1;
                            }
                        } else {
                            if (in_array($itemChecktimeListValue['id'], $checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)) {
                                $itemChecktimeListValue['question_count_r'] = $itemChecktimeListValue['question_count_r'] + 1;
                                $itemChecktimeListValue['question_count'] = $itemChecktimeListValue['question_count'] + 1;
                                $flag = 1;
                            }
                        }
                    } else if ($whereData['role_type'] == 1) {
                        if (empty($whereData['checktime_id'])) {
                            if (in_array($whereData['form_id'], $formIdArr)) {
                                $itemChecktimeListValue['question_count_s'] = $itemChecktimeListValue['question_count_s'] + 1;
                                $itemChecktimeListValue['question_count'] = $itemChecktimeListValue['question_count'] + 1;
                                $flag = 1;
                            }
                        } else {
                            if (in_array($itemChecktimeListValue['id'], $checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)) {
                                $itemChecktimeListValue['question_count_s'] = $itemChecktimeListValue['question_count_s'] + 1;
                                $itemChecktimeListValue['question_count'] = $itemChecktimeListValue['question_count'] + 1;
                                $flag = 1;
                            }
                        }
                    } else if ($whereData['role_type'] == 2) {
                        //不是cra和dm的角色，只能质疑（其它角色）
                        if (empty($whereData['checktime_id'])) {
                            if (in_array($whereData['form_id'], $formIdArr)) {
                                $itemChecktimeListValue['question_count'] = $itemChecktimeListValue['question_count'] + 1;
                                $flag = 1;
                            }
                        } else {
                            if (in_array($itemChecktimeListValue['id'], $checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)) {
                                $itemChecktimeListValue['question_count'] = $itemChecktimeListValue['question_count'] + 1;
                                $flag = 1;
                            }
                        }
                    }

                } else if ($type == 1) {

                    if ($whereData['role_type'] == 0) {
                        if (empty($whereData['checktime_id'])) {
                            if (in_array($whereData['form_id'], $formIdArr)) {
                                $itemChecktimeListValue['colse_question_count_r'] = $itemChecktimeListValue['colse_question_count_r'] + 1;
                                $itemChecktimeListValue['colse_question_count'] = $itemChecktimeListValue['colse_question_count'] + 1;
                                $flag = 1;
                            }
                        } else {
                            if (in_array($itemChecktimeListValue['id'], $checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)) {
                                $itemChecktimeListValue['colse_question_count_r'] = $itemChecktimeListValue['colse_question_count_r'] + 1;
                                $itemChecktimeListValue['colse_question_count'] = $itemChecktimeListValue['colse_question_count'] + 1;
                                $flag = 1;
                            }
                        }
                    } else if ($whereData['role_type'] == 1) {
                        if (empty($whereData['checktime_id'])) {
                            if (in_array($whereData['form_id'], $formIdArr)) {
                                $itemChecktimeListValue['colse_question_count_s'] = $itemChecktimeListValue['colse_question_count_s'] + 1;
                                $itemChecktimeListValue['colse_question_count'] = $itemChecktimeListValue['colse_question_count'] + 1;
                                $flag = 1;
                            }
                        } else {
                            if (in_array($itemChecktimeListValue['id'], $checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)) {

                                $itemChecktimeListValue['colse_question_count_s'] = $itemChecktimeListValue['colse_question_count_s'] + 1;
                                $itemChecktimeListValue['colse_question_count'] = $itemChecktimeListValue['colse_question_count'] + 1;
                                $flag = 1;
                            }
                        }
                    }else if ($whereData['role_type'] == 2) {
                        //不是cra和dm的角色，只能质疑（其它角色）
                        if (empty($whereData['checktime_id'])) {
                            if (in_array($whereData['form_id'], $formIdArr)) {
                                $itemChecktimeListValue['colse_question_count'] = $itemChecktimeListValue['colse_question_count'] + 1;
                                $flag = 1;
                            }
                        } else {
                            if (in_array($itemChecktimeListValue['id'], $checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)) {
                                $itemChecktimeListValue['colse_question_count'] = $itemChecktimeListValue['colse_question_count'] + 1;
                                $flag = 1;

                            }
                        }
                    }

                } else if ($type == 2) {

                    if ($whereData['role_type'] == 0) {
                        if (empty($whereData['checktime_id'])) {
                            if (in_array($whereData['form_id'], $formIdArr)) {
                                $itemChecktimeListValue['clear_question_count_r'] = $itemChecktimeListValue['clear_question_count_r'] + 1;
                                $itemChecktimeListValue['clear_question_count'] = $itemChecktimeListValue['clear_question_count'] + 1;
                                $flag = 1;
                            }
                        } else {
                            if (in_array($itemChecktimeListValue['id'], $checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)) {
                                $itemChecktimeListValue['clear_question_count_r'] = $itemChecktimeListValue['clear_question_count_r'] + 1;
                                $itemChecktimeListValue['clear_question_count'] = $itemChecktimeListValue['clear_question_count'] + 1;
                                $flag = 1;
                            }
                        }
                    } else if ($whereData['role_type'] == 1) {
                        if (empty($whereData['checktime_id'])) {
                            if (in_array($whereData['form_id'], $formIdArr)) {
                                $itemChecktimeListValue['clear_question_count_s'] = $itemChecktimeListValue['clear_question_count_s'] + 1;
                                $itemChecktimeListValue['clear_question_count'] = $itemChecktimeListValue['clear_question_count'] + 1;
                                $flag = 1;
                            }
                        } else {
                            if (in_array($itemChecktimeListValue['id'], $checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)) {
                                $itemChecktimeListValue['clear_question_count_s'] = $itemChecktimeListValue['clear_question_count_s'] + 1;
                                $itemChecktimeListValue['clear_question_count'] = $itemChecktimeListValue['clear_question_count'] + 1;
                                $flag = 1;
                            }
                        }
                    }else if ($whereData['role_type'] == 2) {
                        //不是cra和dm的角色，只能质疑（其它角色）
                        if (empty($whereData['checktime_id'])) {
                            if (in_array($whereData['form_id'], $formIdArr)) {
                                $itemChecktimeListValue['clear_question_count'] = $itemChecktimeListValue['clear_question_count'] + 1;
                                $flag = 1;
                            }
                        } else {
                            if (in_array($itemChecktimeListValue['id'], $checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)) {
                                $itemChecktimeListValue['clear_question_count'] = $itemChecktimeListValue['clear_question_count'] + 1;
                                $flag = 1;
                            }
                        }
                    }

                } else if ($type == 3) {
                    if (empty($whereData['checktime_id'])) {
                        if (in_array($whereData['form_id'], $formIdArr)) {
                            $itemChecktimeListValue['confirm_approve_count'] = $itemChecktimeListValue['confirm_approve_count'] + 1;
                            $flag = 1;
                        }
                    } else {
                        if (in_array($itemChecktimeListValue['id'], $checktimeIdArr)) {
                            $itemChecktimeListValue['confirm_approve_count'] = $itemChecktimeListValue['confirm_approve_count'] + 1;
                            $flag = 1;
                        }
                    }
                } else if ($type == 4) {
                    if (empty($whereData['checktime_id'])) {
                        if (in_array($whereData['form_id'], $formIdArr)) {
                            $itemChecktimeListValue['confirm_approve_count'] = $itemChecktimeListValue['confirm_approve_count'] - 1;
                            $flag = 1;
                        }
                    } else {
                        if (in_array($itemChecktimeListValue['id'], $checktimeIdArr)) {
                            $itemChecktimeListValue['confirm_approve_count'] = $itemChecktimeListValue['confirm_approve_count'] - 1;
                            $flag = 1;
                        }
                    }
                } else if (($type == 5) && isset($whereData['lock_count'])) {
                    //锁定
                    if (empty($whereData['checktime_id'])) {
                        $whereForm = (isset($whereData['form_id']) && !empty($whereData['form_id'])) ? explode(',', $whereData['form_id']) : [];
                        foreach ($whereForm as $whereFormKey => $whereFormValue) {
                            if (in_array($whereFormValue, $formIdArr)) {
                                $itemChecktimeListValue['lock_count'] = $itemChecktimeListValue['lock_count'] + $whereData['lock_count'];
                                $flag = 1;
                            }
                        }

                    } else {
                        if (in_array($itemChecktimeListValue['id'], $checktimeIdArr)) {
                            $itemChecktimeListValue['lock_count'] = $itemChecktimeListValue['lock_count'] + $whereData['lock_count'];
                            $flag = 1;
                        }
                    }
                } else if (($type == 6) && isset($whereData['lock_count'])) {
                    //锁定解锁
                    if (empty($whereData['checktime_id'])) {
                        $whereForm = (isset($whereData['form_id']) && !empty($whereData['form_id'])) ? explode(',', $whereData['form_id']) : [];
                        foreach ($whereForm as $whereFormKey => $whereFormValue) {
                            if (in_array($whereFormValue, $formIdArr)) {
                                $itemChecktimeListValue['lock_count'] = $itemChecktimeListValue['lock_count'] - $whereData['lock_count'];
                                $flag = 1;
                            }
                        }

                    } else {
                        if (in_array($itemChecktimeListValue['id'], $checktimeIdArr)) {
                            $itemChecktimeListValue['lock_count'] = $itemChecktimeListValue['lock_count'] - $whereData['lock_count'];
                            $flag = 1;
                        }
                    }
                } else if ($type == 7) {
                    if (empty($whereData['checktime_id'])) {
                        if (in_array($whereData['form_id'], $formIdArr)) {
                            $itemChecktimeListValue['clear_approve_count'] = $itemChecktimeListValue['clear_approve_count'] + 1;
                            $flag = 1;
                        }
                    } else {
                        if (in_array($itemChecktimeListValue['id'], $checktimeIdArr)) {
                            $itemChecktimeListValue['clear_approve_count'] = $itemChecktimeListValue['clear_approve_count'] + 1;
                            $flag = 1;
                        }
                    }
                } else if ($type == 8) {
                    if (empty($whereData['checktime_id'])) {
                        if (in_array($whereData['form_id'], $formIdArr)) {
                            $itemChecktimeListValue['clear_approve_count'] = $itemChecktimeListValue['clear_approve_count'] - 1;
                            $flag = 1;
                        }
                    } else {
                        if (in_array($itemChecktimeListValue['id'], $checktimeIdArr)) {
                            $itemChecktimeListValue['clear_approve_count'] = $itemChecktimeListValue['clear_approve_count'] - 1;
                            $flag = 1;
                        }
                    }
                }else if($type == 9){
                    //合并用药确认总数-r===加
                    if (empty($whereData['checktime_id'])){
                        if (in_array($whereData['form_id'],$formIdArr)){
                            $itemChecktimeListValue['batch_confirm_approve_r'] = $itemChecktimeListValue['batch_confirm_approve_r'] + 1;
                            $flag = 1;
                        }
                    }else{
                        if (in_array($itemChecktimeListValue['id'],$checktimeIdArr)){
                            $itemChecktimeListValue['batch_confirm_approve_r'] = $itemChecktimeListValue['batch_confirm_approve_r'] + 1;
                            $flag = 1;
                        }
                    }
                }else if($type == 10){
                    //合并用药确认总数-r===减
                    if (empty($whereData['checktime_id'])){
                        if (in_array($whereData['form_id'],$formIdArr)){
                            $itemChecktimeListValue['batch_confirm_approve_r'] = $itemChecktimeListValue['batch_confirm_approve_r'] - 1;
                            $flag = 1;
                        }
                    }else{
                        if (in_array($itemChecktimeListValue['id'],$checktimeIdArr)){
                            $itemChecktimeListValue['batch_confirm_approve_r'] = $itemChecktimeListValue['batch_confirm_approve_r'] - 1;
                            $flag = 1;
                        }
                    }
                }else if($type == 11){
                    //合并用药确认总数-s===加
                    if (empty($whereData['checktime_id'])){
                        if (in_array($whereData['form_id'],$formIdArr)){
                            $itemChecktimeListValue['batch_confirm_approve_s'] = $itemChecktimeListValue['batch_confirm_approve_s'] + 1;
                            $flag = 1;
                        }
                    }else{
                        if (in_array($itemChecktimeListValue['id'],$checktimeIdArr)){
                            $itemChecktimeListValue['batch_confirm_approve_s'] = $itemChecktimeListValue['batch_confirm_approve_s'] + 1;
                            $flag = 1;
                        }
                    }
                }else if($type == 12){
                    //合并用药确认总数-s===减
                    if (empty($whereData['checktime_id'])){
                        if (in_array($whereData['form_id'],$formIdArr)){
                            $itemChecktimeListValue['batch_confirm_approve_s'] = $itemChecktimeListValue['batch_confirm_approve_s'] - 1;
                            $flag = 1;
                        }
                    }else{
                        if (in_array($itemChecktimeListValue['id'],$checktimeIdArr)){
                            $itemChecktimeListValue['batch_confirm_approve_s'] = $itemChecktimeListValue['batch_confirm_approve_s'] - 1;
                            $flag = 1;
                        }
                    }
                }else if($type == 13){
                    //合并用药确认锁定总数===加
                    if (empty($whereData['checktime_id'])){
                        if (in_array($whereData['form_id'],$formIdArr)){
                            $itemChecktimeListValue['batch_confirm_lock_count'] = $itemChecktimeListValue['batch_confirm_lock_count'] + 1;
                            $flag = 1;
                        }
                    }else{
                        if (in_array($itemChecktimeListValue['id'],$checktimeIdArr)){
                            $itemChecktimeListValue['batch_confirm_lock_count'] = $itemChecktimeListValue['batch_confirm_lock_count'] + 1;
                            $flag = 1;
                        }
                    }
                }else if($type == 14){
                    //合并用药确认锁定总数===减
                    if (empty($whereData['checktime_id'])){
                        if (in_array($whereData['form_id'],$formIdArr)){
                            $itemChecktimeListValue['batch_confirm_lock_count'] = $itemChecktimeListValue['batch_confirm_lock_count'] - 1;
                            $flag = 1;
                        }
                    }else{
                        if (in_array($itemChecktimeListValue['id'],$checktimeIdArr)){
                            $itemChecktimeListValue['batch_confirm_lock_count'] = $itemChecktimeListValue['batch_confirm_lock_count'] - 1;
                            $flag = 1;
                        }
                    }
                }else if($whereData['type'] == 15){
                    //合并用药确认总数-r===清空
                    $itemChecktimeListValue['batch_confirm_approve_r'] = 0;
                    $flag = 1;

                }else if($whereData['type'] == 16){
                    //合并用药确认总数-s===清空
                    $itemChecktimeListValue['batch_confirm_approve_s'] = 0;
                    $flag = 1;

                }else if($whereData['type'] == 17){
                    //合并用药确认锁定总数===清空
                    $itemChecktimeListValue['batch_confirm_lock_count'] = 0;
                    $flag = 1;

                }
            }else{
                //自动质疑
                if (empty($type)){
                    if (empty($whereData['checktime_id'])){
                        if (in_array($whereData['form_id'],$formIdArr)){
                            $itemChecktimeListValue['question_count'] = $itemChecktimeListValue['question_count'] +1;
                            $itemChecktimeListValue['question_count_r'] = $itemChecktimeListValue['question_count_r'] +1;
                            $flag = 1;
                        }
                    }else{
                        if (in_array($itemChecktimeListValue['id'],$checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)){
                            $itemChecktimeListValue['question_count'] = $itemChecktimeListValue['question_count'] +1;
                            $itemChecktimeListValue['question_count_r'] = $itemChecktimeListValue['question_count_r'] +1;
                            $flag = 1;
                        }
                    }

                }else if($type == 1){
                    if (empty($whereData['checktime_id'])){
                        if (in_array($whereData['form_id'],$formIdArr)){
                            $itemChecktimeListValue['colse_question_count'] = $itemChecktimeListValue['colse_question_count'] +1;
                            $itemChecktimeListValue['colse_question_count_r'] = $itemChecktimeListValue['colse_question_count_r'] +1;
                            $flag = 1;
                        }
                    }else{
                        if (in_array($itemChecktimeListValue['id'],$checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)){
                            $itemChecktimeListValue['colse_question_count'] = $itemChecktimeListValue['colse_question_count'] +1;
                            $itemChecktimeListValue['colse_question_count_r'] = $itemChecktimeListValue['colse_question_count_r'] +1;
                            $flag = 1;
                        }
                    }

                }else if($type == 2){
                    if (empty($whereData['checktime_id'])){
                        if (in_array($whereData['form_id'],$formIdArr)){
                            $itemChecktimeListValue['clear_question_count'] = $itemChecktimeListValue['clear_question_count'] +1;
                            $itemChecktimeListValue['clear_question_count_r'] = $itemChecktimeListValue['clear_question_count_r'] +1;
                            $flag = 1;
                        }
                    }else{
                        if (in_array($itemChecktimeListValue['id'],$checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)){
                            $itemChecktimeListValue['clear_question_count'] = $itemChecktimeListValue['clear_question_count'] +1;
                            $itemChecktimeListValue['clear_question_count_r'] = $itemChecktimeListValue['clear_question_count_r'] +1;
                            $flag = 1;
                        }
                    }

                }
                $this->questionRuning($itemChecktimeListValue,$whereData,$formIdArr,$checktimeIdArr,$checkTimeIdDatas,$type);
            }
        }
        if (!empty($flag )){
            $itemChecktimeListArr = $itemChecktimeList;
            $itemChecktimeLists = json_encode($itemChecktimeList);
        }
        if (!empty($itemChecktimeLists)){
            $update['id'] = $patientWorkCountInfo['id'];
            $update['item_checktime_list'] = $itemChecktimeLists;
            $resultQuestion = $this->LocalService()->patientChecktimeListEdc->save($update);
        }
        return true;
    }


    //处理完成质疑

    public function questionClearCountUpdate($whereData = []){
        //$type 0为添加质疑总数 1为关闭质疑数 2为已回复质疑数
        if (empty($whereData) || empty($whereData['patient_id']) || !isset($whereData['type'])) return false;
        $wherePatientWorkCount['where'] = [
            'is_del' => 0,
            'patient_id' => $whereData['patient_id'],
        ];
        !empty($whereData['item_id']) && isset($whereData['item_id']) && $wherePatientWorkCount['where']['item_id'] = $whereData['item_id'];
        $patientWorkCountInfo = $this->LocalService()->patientWorkCountEdc->fetchOne($wherePatientWorkCount);
        if (empty($patientWorkCountInfo))  return false;
        $update = [];

        if (isset($whereData['error_type']) && !empty($whereData['error_type']) && isset($whereData['role_type'])){
            //r确认数据表单and添加质疑
            if (empty($whereData['type'])) {
                $update['question_count'] = $patientWorkCountInfo['question_count'] + 1;
                if ($whereData['role_type'] == 0) {
                    $update['question_count_r'] = $patientWorkCountInfo['question_count_r'] + 1;
                } else if ($whereData['role_type'] == 1) {
                    $update['question_count_s'] = $patientWorkCountInfo['question_count_s'] + 1;
                }
            } else if ($whereData['type'] == 1) {
                $update['colse_question_count'] = $patientWorkCountInfo['colse_question_count'] + 1;
                if ($whereData['role_type'] == 0) {
                    $update['colse_question_count_r'] = $patientWorkCountInfo['colse_question_count_r'] + 1;
                } else if ($whereData['role_type'] == 1) {
                    $update['colse_question_count_s'] = $patientWorkCountInfo['colse_question_count_s'] + 1;
                }

            } else if ($whereData['type'] == 2) {
                $update['clear_question_count'] = $patientWorkCountInfo['clear_question_count'] + 1;
                if ($whereData['role_type'] == 0) {
                    $update['clear_question_count_r'] = $patientWorkCountInfo['clear_question_count_r'] + 1;
                } else if ($whereData['role_type'] == 1) {
                    $update['clear_question_count_s'] = $patientWorkCountInfo['clear_question_count_s'] + 1;
                }
            } else if ($whereData['type'] == 3) {
                $update['confirm_approve_count'] = $patientWorkCountInfo['confirm_approve_count'] + 1;
            } else if ($whereData['type'] == 4) {
                $update['confirm_approve_count'] = $patientWorkCountInfo['confirm_approve_count'] - 1;
            } else if (($whereData['type'] == 5) && isset($whereData['lock_count'])) {
                //锁定
                $update['lock_count'] = $patientWorkCountInfo['lock_count'] + $whereData['lock_count'];

            } else if (($whereData['type'] == 6) && isset($whereData['lock_count'])) {
                //锁定解锁
                $update['lock_count'] = $patientWorkCountInfo['lock_count'] - $whereData['lock_count'];
            } else if ($whereData['type'] == 7) {
                $update['clear_approve_count'] = $patientWorkCountInfo['clear_approve_count'] + 1;

            } else if ($whereData['type'] == 8) {
                $update['clear_approve_count'] = $patientWorkCountInfo['clear_approve_count'] - 1;
            }else if($whereData['type'] == 9){
                //合并用药确认总数-r===加
                $update['batch_confirm_approve_r'] = $patientWorkCountInfo['batch_confirm_approve_r'] + 1;

            }else if($whereData['type'] == 10){
                //合并用药确认总数-r===减
                $update['batch_confirm_approve_r'] = $patientWorkCountInfo['batch_confirm_approve_r'] - 1;

            }else if($whereData['type'] == 11){
                //合并用药确认总数-s===加
                $update['batch_confirm_approve_s'] = $patientWorkCountInfo['batch_confirm_approve_s'] + 1;

            }else if($whereData['type'] == 12){
                //合并用药确认总数-s===减
                $update['batch_confirm_approve_s'] = $patientWorkCountInfo['batch_confirm_approve_s'] - 1;
            }else if($whereData['type'] == 13){
                //合并用药确认锁定总数===加
                $update['batch_confirm_lock_count'] = $patientWorkCountInfo['batch_confirm_lock_count'] + 1;
            }else if($whereData['type'] == 14){
                //合并用药确认锁定总数===减
                $update['batch_confirm_lock_count'] = $patientWorkCountInfo['batch_confirm_lock_count'] - 1;
            }else if($whereData['type'] == 15){
                //合并用药确认总数-r===清空
                $update['batch_confirm_approve_r'] = 0;
            }else if($whereData['type'] == 16){
                //合并用药确认总数-s===清空
                $update['batch_confirm_approve_s'] = 0;
            }else if($whereData['type'] == 17){
                //合并用药确认锁定总数===清空
                $update['batch_confirm_lock_count'] = 0;
            }
        }else{
            //自动质疑
            if (empty($whereData['type'])){
                $update['question_count'] = $patientWorkCountInfo['question_count'] +1;
                $update['question_count_r'] = $patientWorkCountInfo['question_count_r'] +1;
            }else if($whereData['type'] == 1){
                $update['colse_question_count'] = $patientWorkCountInfo['colse_question_count'] +1;
                $update['colse_question_count_r'] = $patientWorkCountInfo['colse_question_count_r'] +1;
            }else if($whereData['type'] == 2){
                $update['clear_question_count'] = $patientWorkCountInfo['clear_question_count'] +1;
                $update['clear_question_count_r'] = $patientWorkCountInfo['clear_question_count_r'] +1;
            }
            $this->questionRuningCount($update,$patientWorkCountInfo,$whereData);
        }

        if (!empty($update )){
            $update['id'] = $patientWorkCountInfo['id'];
            $resultQuestion = $this->LocalService()->patientWorkCountEdc->save($update);
            return true;
        }
        return false;

    }

    //贯穿全程的检查点id====同步时要调整查询库
    public function checkTimeByIdData($form_id = 0){
        if (empty($form_id)){
            return [];
        }
        $checkid = [];
        $formtype = $this->LocalService()->itemFormEdc->getOneFieldVal('group_id', [
            'id' => $form_id,
        ]);
        if($formtype == 4) $checkid = $this->LocalService()->itemCheckcontentEdc->fetchCol('check_id', [
            'is_del' => 0,
            'form_id' => $form_id,
        ]);
        return $checkid;
    }

    //自动list
    public function questionRuning(&$itemChecktimeListValue,$whereData,$formIdArr,$checktimeIdArr,$checkTimeIdDatas,$type){
        if (empty($type)){
            if (empty($whereData['checktime_id'])){
                if (in_array($whereData['form_id'],$formIdArr)){
                    $itemChecktimeListValue['question_count_run'] = $itemChecktimeListValue['question_count_run'] +1;
                    $itemChecktimeListValue['question_count_r_run'] = $itemChecktimeListValue['question_count_r_run'] +1;
                    $flag = 1;
                }
            }else{
                if (in_array($itemChecktimeListValue['id'],$checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)){
                    $itemChecktimeListValue['question_count'] = $itemChecktimeListValue['question_count'] +1;
                    $itemChecktimeListValue['question_count_r_run'] = $itemChecktimeListValue['question_count_r_run'] +1;
                    $flag = 1;
                }
            }

        }else if($type == 1){
            if (empty($whereData['checktime_id'])){
                if (in_array($whereData['form_id'],$formIdArr)){
                    $itemChecktimeListValue['colse_question_count_run'] = $itemChecktimeListValue['colse_question_count_run'] +1;
                    $itemChecktimeListValue['colse_question_count_r_run'] = $itemChecktimeListValue['colse_question_count_r_run'] +1;
                    //如果被回复过就把clear_question_count字段减1
                    if (isset($whereData['colse_type']) && ($whereData['colse_type'] == 1)){
                        if (!empty($itemChecktimeListValue['clear_question_count_run'])){
                            $itemChecktimeListValue['clear_question_count_run'] = $itemChecktimeListValue['clear_question_count_run'] - 1;
                        }
                        if (!empty($itemChecktimeListValue['clear_question_count_r_run'])){
                            $itemChecktimeListValue['clear_question_count_r_run'] = $itemChecktimeListValue['clear_question_count_r_run'] - 1;
                        }
                    }
                    $flag = 1;
                }
            }else{
                if (in_array($itemChecktimeListValue['id'],$checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)){
                    $itemChecktimeListValue['colse_question_count_run'] = $itemChecktimeListValue['colse_question_count_run'] +1;
                    $itemChecktimeListValue['colse_question_count_r_run'] = $itemChecktimeListValue['colse_question_count_r_run'] +1;
                    $flag = 1;
                }
            }

        }else if($type == 2){
            if (empty($whereData['checktime_id'])){
                if (in_array($whereData['form_id'],$formIdArr)){
                    $itemChecktimeListValue['clear_question_count_run'] = $itemChecktimeListValue['clear_question_count_run'] +1;
                    $itemChecktimeListValue['clear_question_count_r_run'] = $itemChecktimeListValue['clear_question_count_r_run'] +1;
                    $flag = 1;
                }
            }else{
                if (in_array($itemChecktimeListValue['id'],$checktimeIdArr) || in_array($itemChecktimeListValue['id'],$checkTimeIdDatas)){
                    $itemChecktimeListValue['clear_question_count_run'] = $itemChecktimeListValue['clear_question_count_run'] +1;
                    $itemChecktimeListValue['clear_question_count_r_run'] = $itemChecktimeListValue['clear_question_count_r_run'] +1;
                    $flag = 1;
                }
            }
        }
        return $itemChecktimeListValue;
    }


    //自动Count
    public function questionRuningCount(&$update,$patientWorkCountInfo,$whereData){
        //自动质疑
        if (empty($whereData['type'])){
            $update['question_count_run'] = $patientWorkCountInfo['question_count_run'] +1;
        }else if($whereData['type'] == 1){
            $update['colse_question_count_run'] = $patientWorkCountInfo['colse_question_count_run'] +1;
            //如果被回复过就把clear_question_count字段减1
            if (isset($whereData['colse_type']) && ($whereData['colse_type'] == 1)){
                if (!empty($patientWorkCountInfo['clear_question_count'])){
                    $update['clear_question_count_run'] = $patientWorkCountInfo['clear_question_count_run'] - 1;
                }
            }
        }else if($whereData['type'] == 2){
            $update['clear_question_count_run'] = $patientWorkCountInfo['clear_question_count_run'] +1;
        }
        return $update;
    }
}