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

namespace Application\Service\DB\Worklist;

use Interop\Container\ContainerInterface;

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

    /**
     * Notes: 获取需要发信的沟通过程信息
     * User: lltyy
     * DateTime: 2023/2/3 10:05
     *
     * @return array
     */
    public function fetchSendMesData(){
        $arr = [];
        $where = [
            'is_del' => 0,
            'handle_result' => 1,
            'close_result' => 2,
        ];
        $cond = [
            'where' => $where,
            'columns'=>['item_id','patient_id','researchstage_id','checktime_id','patientchecktime_id','connect_remark','connect_date','create_user_id','update_user_id'],
            'order'=>['connect_date asc']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            $itemDatas = $this->LocalService()->itemInfo->fetchDataByKey('is_del = 0',['id','name','number'],['id'],['id'=>0]);
            $itemData = isset($itemDatas['id']) ? $itemDatas['id'] : [];
            $patientDatas = $this->LocalService()->patient->fetchDataByKey('is_del = 0',['id','patient_name','patient_number'],['id'],['id'=>0]);
            $patientData = isset($patientDatas['id']) ? $patientDatas['id'] : [];
            $userDatas = $this->LocalService()->adminUser->fetchDataByKey([
                'is_del' => 0,
                [
                    'or' => [
                        'status' => 0,
                        'real_status' => 0,
                    ]
                ]
            ],['id','openid','realname'],['id'],['id'=>0]);
            $userData = isset($userDatas['id']) ? $userDatas['id'] : [];
            $patientChecktimes = $this->LocalService()->itemPatientchecktime->getPatientChecktimeByKey('is_del = 0',['id','plan_start_date','plan_end_date'],['id'],['id'=>0]);
            $patientChecktimeData = isset($patientChecktimes['id']) ? $patientChecktimes['id'] : [];
            foreach($datas as $data){
                $item_id = $data['item_id'];
                $patient_id = $data['patient_id'];
                $researchstage_id = $data['researchstage_id'];
                $checktime_id = $data['checktime_id'];
                $patientchecktime_id = $data['patientchecktime_id'];
                $user_id = !empty($data['update_user_id']) ? $data['update_user_id'] : $data['create_user_id'];
                if(isset($itemData[$item_id]) && isset($patientData[$patient_id]) && isset($patientChecktimeData[$patientchecktime_id]) && isset($userData[$user_id])){
                    $send_user_openid = $userData[$user_id]['openid'];
                    $arr[$send_user_openid]['user_name'] = $userData[$user_id]['realname'];
                    $arr[$send_user_openid]['openid'] = $send_user_openid;
                    $arr[$send_user_openid]['first'] = "沟通过程待跟进通知";
                    $arr[$send_user_openid]['thing3'] = "沟通过程";
                    $arr[$send_user_openid]['connect'] = isset($arr[$send_user_openid]) && isset($arr[$send_user_openid]['connect']) && !empty($arr[$send_user_openid]['connect']) ? $arr[$send_user_openid]['connect'] : 0;
                    $arr[$send_user_openid]['connect'] += 1;
                }
            }
        }
        if(!empty($arr)){
            foreach($arr as &$arrInfo){
                $arrInfo['time5'] = date('Y年m月d日');
                $remark = '您好，'.$arrInfo['user_name'].'！截止目前待跟进沟通过程信息有【'.$arrInfo['connect'].'】条';
                $arrInfo['remark'] = $remark;
                unset($arrInfo['connect']);
                unset($arrInfo['user_name']);
            }
            $arr = array_values($arr);
        }
        return $arr;
    }
}