<?php

namespace Application\Service\DB\Item;

use Application\Service\Extension\Params\Params;
use Interop\Container\Containerinterface;
use Laminas\Db\Sql\Expression;

/**
 * 记录表单数据修改轨迹
 * ```
 *  现在会根据 `itemId` 自动分表
 *  没有 `item_id` 则会自动使用 `yikeen_item_patient_form_content_updated_log` 表进行操作
 *
 *  强制使用表
 *  ->patientFormContentUpdatedLog(5) // 强制使用 `yikeen_item_patient_form_content_updated_log_5` 表进行操作
 * ```
 */
class PatientFormContentUpdatedLog extends \Application\Service\DB\AbstractDb
{
    const EVENT_PATIENT_FORM_ACTIVE = 'EVENT_PATIENT_FORM_ACTIVE'; // 表单激活事件
    const EVENT_PATIENT_FORM_INACTIVE = 'EVENT_PATIENT_FORM_INACTIVE'; // 表单失活事件

    public function __construct($dbAdapter, ContainerInterface $container, ?array $options = null)
    {
        if (Params::app()->getValue('item_id')) {
            $this->tableName = 'yikeen_item_patient_form_content_updated_log_' . intval(Params::app()->itemId) % 10;
        } else {
            $this->tableName = 'yikeen_item_patient_form_content_updated_log' . (isset($options['index']) ? ('_' . $options['index']) : '');
        }

        parent::__construct($dbAdapter, $container);
    }

    /**
     * @note 更新日志表的unikey
     * @return void
     */
    public function updateUnikey() {
        $this->isSetInfo(false)->update([
           'unikey' => new Expression("CAST(CONCAT(CAST(`create_time` AS CHAR), '.', `id`) AS DECIMAL(20, 10))")
        ], [
            'unikey' => '0'
        ]);
    }

    /**
     * Notes:日志类型数组
     * @param
     * @return
     * @author haojinhua
     * @date 2024-08-05
     */
    public function geteventArr(){
        $event_arr = ['EVENT_CREATE' => '添加表单','EVENT_EDIT' => '修改表单','FORM_CONTENT_CREATE'=>'添加表单','FORM_CONTENT_EDIT'=>'修改表单','FORM_CONTENT_FLUSH'=>'重置表单','FORM_CONTENT_EDIT_BY_QUESTION'=>'质疑沟通编辑表单内容','UNLOCK_SHARE_FORM_CONTENT'=>'常规识别解锁','LOCK_SHARE_FORM_CONTENT'=>'常规识别锁定','FORM_CONTENT_TEMP_STORAGE'=>'常规识别保存'];
        $event_arr['FORM_CHECK_SAVE'] = '检查项保存';
        $event_arr['FORM_CHECK_LOCK'] = '检查项锁定';
        $event_arr['FORM_CHECK_UNLOCK'] = '检查项解锁';
        $event_arr['FORM_CHECK_REPAIR'] = '检查项补单';
        $event_arr['EVENT_FORM_CONTENT_EDIT_BY_LOGIC_REPLY'] = '常规识别类型的表单, 通过逻辑核查回复修改表单内容';
        $event_arr['SDV'] = 'SDV';
        $event_arr['Un-SDV'] = '取消SDV';
        $event_arr['Review'] = 'Review';
        $event_arr['Un-Review'] = '取消Review';
        $event_arr['Lock'] = '锁定';
        $event_arr['Un-Lock'] = '解锁';
        $event_arr['Sign'] = '签名';
        $event_arr['Un-Sign'] = '取消签名';
        $event_arr['EVENT_FORM_CONTENT_APPLY_UNLOCK'] = '表单申请解锁';
        $event_arr['EVENT_FORM_CONTENT_REVOKE_UNLOCK'] = '取消表单申请解锁';
        $event_arr['EVENT_MULTI_SHARE_FORM_UPLOAD_IMAGE'] = '共享图片新增';
        $event_arr['EVENT_MULTI_SHARE_FORM_UPDATE_IMAGE'] = '共享图片编辑';
        $event_arr['EVENT_PATIENT_FORM_ACTIVE'] = '表单激活';
        $event_arr['EVENT_PATIENT_FORM_INACTIVE'] = '表单失活';
        $event_arr['FORM_CONTENT_DELETE'] = '表单内容删除';
        $event_arr['FORM_CONTENT_RECOVERY'] = '表单内容恢复';
        $event_arr['EVENT_FORM_CONTENT_CONFIRM_APPLY_UNLOCK'] = '确认表单解锁申请';
        return $event_arr;
    }

    public function getlogevent(){
        $res = ['SDV','Un-SDV','Review','Un-Review','Lock','Un-Lock','Sign','Un-Sign'];
        return $res;
    }

    /**
     * Notes: 获取检查项修改字段
     * User: lltyy
     * DateTime: 2024/9/23 10:31
     *
     * @return array
     */
    public function getCheckFieldArr(){
        $arr = [];
        $arr['unit'] = [
            'name'=>'单位',
            'type'=>0
        ];
        $arr['result'] = [
            'name'=>'结果',
            'type'=>0
        ];
        $arr['reference_range_min'] = [
            'name'=>'最小值',
            'type'=>0
        ];
        $arr['min_symbol'] = [
            'name'=>'最小值符号',
            'type'=>0
        ];
        $arr['max_symbol'] = [
            'name'=>'最大值符号',
            'type'=>0
        ];
        $arr['reference_range_max'] = [
            'name'=>'最大值/正常值',
            'type'=>0
        ];
        $arr['lock_state_small'] = [
            'name'=>'锁定',
            'type'=>1,
            'select'=>[
                1=>'已锁定',
                0=>'未锁定'
            ]
        ];
        $arr['is_normal_type'] = [
            'name'=>'异常值判断',
            'type'=>1,
            'select'=>[
                1=>'正常',
                2=>'CS',
                3=>'NCS'
            ]
        ];
        return $arr;
    }

    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2023/6/2 7:50
     *
     * @param $where 条件
     * @param $columns 信息
     * @return array
     */
    public function fetchDataByKey($where,$columns,$order=['id'],$key='id',$is_more=0){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>$order
        ];
        $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;
    }

}