<?php

namespace Application\Service\DB\Item;

use Application\Service\Extension\Helper\SDMHelper;
use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\Containerinterface;

class PatientFormLogicErrorQuery extends \Application\Service\DB\AbstractDb
{
    public const PLATFORM_EDC = 1;

    public const PLATFORM_SDM = 2;

    public function __construct($dbAdapter, ContainerInterface $container)
    {
        if (!$this->tableName) {
            $this->tableName = 'yikeen_item_patient_form_logic_error_query';
        }

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

    /**
     * Notes: 获取质疑对应的回复信息
     * @param
     * @return
     * @author haojinhua
     * @date 2024-09-04
     */
    public function getreplayinfo(array $where = [],$userNameArr = []){
        $res = [];
        $where  = !empty($where) ? $where : [
            'is_reply' => 1,
        ];
        $cond = [
            'where' => $where,
            'columns' => ['id','content','error_id','pointer_id','create_user_id','create_time','is_reply','is_auditor','platform'],
        ];

        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            $error_id = $this->fetchCol('error_id',$where);
            $error_datas = $this->LocalService()->itemPatientFormLogicError->getErrorInfo([
                'id' => StringHelper::toArray($error_id)
            ]);

            //查询人员姓名数组
            if(empty($userNameArr)) $userNameArr = $this->LocalService()->adminUser->fetchCol('realname', 'is_del = 0');

            foreach ($datas as &$data){
                $error_infos = isset($error_datas[$data['error_id']]) && !empty($error_datas[$data['error_id']]) ? $error_datas[$data['error_id']] : [];
                 if ($data['create_user_id'] == 0) {
                     $role_name = 'System';
                 } else {
                     $role_name = SDMHelper::app()->user->getRoleNameString($data['create_user_id'], $error_infos['sign_id'], $error_infos['item_id']);
                 }
                if($data['is_auditor'] == 1){
                    $role_name = '图片审核员';
                }

                $submitRole = $role_name;
                if($data['create_user_id'] > 0 && $error_infos['is_closed'] == 1 && $data['content'] == '关闭质疑'){
                    //关闭 查询的是edc的角色 是单角色  手动质疑只有提出质疑的人可以关 系统质疑可以自动关或者DM关  系统质疑的话 提质疑角色是空的
                    $submit_role = $error_infos['submit_role'];
                    if(!empty($submit_role)){
                        $submitRole = $submit_role;
                    }else{
                        $submitRole = 'DM';
                    }
                }
                $create_user = isset($userNameArr[$data['create_user_id']]) && !empty($userNameArr[$data['create_user_id']]) ? $userNameArr[$data['create_user_id']] : 'System';
                $new_create_user = $create_user == 'System' ? $create_user : $create_user.'【'.$submitRole.'】';
                $create_time = date('Y-m-d H:i:s', $data['create_time']);;
                $res[$data['error_id']][$data['pointer_id']]['replay'][] =$data['id'].'_'.$data['content']."(".$new_create_user.$create_time.")";
                $res[$data['error_id']][$data['pointer_id']]['colsed'] = $data;
                $res[$data['error_id']][$data['pointer_id']]['colsed_id'] = $data['id'];
                if($data['content'] == '数据更新' && empty($data['create_user_id'])){
                    //系统关闭了质疑 如常规识别的补充数据后自动关闭质疑会记录数据更新
                    continue;
                }else{
                    $res[$data['error_id']][$data['pointer_id']]['infos'][] = $data;
                }

            }

        }

        return $res;
    }
}