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

namespace Application\Service\DB\Item;

use Application\Service\Exceptions\InvalidArgumentException;
use Application\Service\Extension\Helper\StringHelper;
use Application\Service\Extension\Laminas;
use Application\Service\DB\Dictionary\FormGroup;
use Interop\Container\ContainerInterface;
use Application\Service\Extension\Helper\Edcloghelper;

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

    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2024/8/8 8:54
     *
     * @param array $arr
     * @return array
     */
    public function fetchDataByKey($arr=[]){
        $where = isset($arr['where']) && !empty($arr['where']) ? $arr['where'] : 'id > 0';
        $columns = isset($arr['columns']) && !empty($arr['columns']) ? $arr['columns'] : [];
        $order = isset($arr['order']) && !empty($arr['order']) ? $arr['order'] : ['id'];
        $key_arr = isset($arr['key_arr']) && !empty($arr['key_arr']) ? $arr['key_arr'] : ['id'=>0];
        unset($arr);

        $datas = [];
        $cond = [
            'where'=>$where
        ];
        unset($where);
        if(!empty($columns)){
            $cond['columns'] = $columns;
        }
        unset($columns);
        if(!empty($order)){
            $cond['order'] = $order;
        }
        unset($order);
        $infoDatas = $this->fetchAll($cond);
        unset($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoKey=>$infoData){
                foreach($key_arr as $key=>$is_more){
                    $key_str_arr = explode('/',$key);
                    $key_value = [];
                    if(!empty($key_str_arr)){
                        foreach($key_str_arr as $key_str){
                            $key_value[] = isset($infoData[$key_str]) && !empty($infoData[$key_str]) ? $infoData[$key_str] : '';
                        }
                    }
                    $data_key = implode('_',$key_value);
                    if($is_more == 1){
                        $datas[$data_key][] = $infoData;
                    }else{
                        $datas[$data_key] = $infoData;
                    }
                }
                unset($infoDatas[$infoKey]);
            }
        }
        unset($infoDatas);

        return $datas;
    }

    /**
     * Notes: 表单解锁时删除电子签名标识
     * User: lltyy
     * DateTime: 2024/8/7 14:28
     *
     * @param array $arr
     */
    public function delPatientFormSignature($arr=[]){
        $Journal = new Edcloghelper;

        $item_id = isset($arr['item_id']) && !empty($arr['item_id']) ? $arr['item_id'] : 0;
        $patient_id = isset($arr['patient_id']) && !empty($arr['patient_id']) ? $arr['patient_id'] : 0;
        $form_id = isset($arr['form_id']) && !empty($arr['form_id']) ? $arr['form_id'] : 0;
        $checktime_id = isset($arr['checktime_id']) && !empty($arr['checktime_id']) ? $arr['checktime_id'] : 0;
        $is_more_operate = isset($arr['is_more_operate']) && !empty($arr['is_more_operate']) ? $arr['is_more_operate'] : 0;
        $formGroup = $this->LocalService()->itemForm->getOneFieldVal('group_id',[
            'id' => $form_id,
        ]);
        $signature_detail_where = [
            'is_del' => 0,
            'item_id' => $item_id,
            'patient_id' => $patient_id,
            'checktime_id' => StringHelper::toArray($checktime_id),
            'form_id' => $form_id,
        ];
        if(in_array($formGroup,[FormGroup::TEST_SUMMARY,FormGroup::SAE,FormGroup::AE])){
            $signature_detail_where = [
                'is_del' => 0,
                'item_id' => $item_id,
                'patient_id' => $patient_id,
                'form_id' => $form_id,
            ];
        }
        $signature_details = $this->fetchAll(['where'=>$signature_detail_where]);
        unset($formGroup);
        unset($signature_detail_where);
        if(!empty($signature_details)){
            $batchArr = [];
            $sigEvent = $is_more_operate == 1 ? 'MoreUn-Sign' : 'Un-Sign';
            //删除表单电子签名标识
            foreach($signature_details as $signature_detail_key=>$signature_detail){
                $this->update(['is_del'=>1],'id = '.$signature_detail['id']);
                $Journal->setContentId($signature_detail['id'])->setEvent($sigEvent)->setPostData($arr)->flush($signature_detail);
                //日志
//                $log_type = 1;
//                $log_remark = '电子签名 - 表单解锁删除电子签名';
//                $this->LocalService()->log->saveFileLog($signature_detail['id'],'ItemSignaturedetail',['id'=>$signature_detail['id'],'is_del'=>1],$log_type,$log_remark,$item_id,' ',['item_id'=>$item_id]);
                unset($log_type);
                unset($log_remark);

                if(!in_array($signature_detail['batch_id'],$batchArr)){
                    $batchArr[] = $signature_detail['batch_id'];
                }
                unset($signature_details[$signature_detail_key]);
            }
            unset($sigEvent);

            //更新签名批次状态
            $batch_have_detail_arr = $this->fetchCol('batch_id',[
                'is_del' => 0,
                'batch_id' => StringHelper::toArray($batchArr),
            ]);
            $batch_have_detail_arr = !empty($batch_have_detail_arr) ? array_values($batch_have_detail_arr) : [];
            foreach($batchArr as $batchKey=>$batchid){
                if(!in_array($batchid,$batch_have_detail_arr)){
                    $this->localService()->itemSignaturebatch->update(['operate_state'=>2],[
                        'id' => $batchid,
                    ]);
                    //日志
                    $log_type = 1;
                    $log_remark = $is_more_operate == 1 ? '电子签名 - 表单批量解锁作废批次' : '电子签名 - 表单解锁作废批次';
                    $this->LocalService()->log->saveFileLog($batchid,'ItemSignaturebatch',['id'=>$batchid,'operate_state'=>2],$log_type,$log_remark,$item_id,' ',['item_id'=>$item_id]);
                    unset($log_type);
                    unset($log_remark);
                }
                unset($batchArr[$batchKey]);
            }
            unset($batchArr);
            unset($batch_have_detail_arr);
        }
        unset($signature_details);
        return 1;
    }

}