<?php

namespace App\Services;

use App\Model\SystemInquiryConfig;
use App\Model\SystemInquiryTime;

/**
 * 系统配置
 */
class SystemService extends BaseService
{
    /**
     * 获取系统问诊时间
     * 快速问诊-问诊购药
     * @return array
     */
    public function getSystemInquiryTime(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $inquiry_type = $this->request->input('inquiry_type');
        $inquiry_mode = $this->request->input('inquiry_mode');

        if ($inquiry_type != 2 && $inquiry_type != 4){
            return fail();
        }

        $params = array();
        $params['inquiry_type'] = $inquiry_type;
        $params['inquiry_mode'] = $inquiry_mode;
        $system_inquiry_config = SystemInquiryConfig::getOne($params);
        if (empty($system_inquiry_config)){
            return fail();
        }

        $fields = [
            'inquiry_time_id',
            'start_time',
            'end_time',
            'time_interval',
        ];
        $params = array();
        $params['system_inquiry_config_id'] = $system_inquiry_config['system_inquiry_config_id'];
        $system_inquiry_time = SystemInquiryTime::getList($params,$fields);
        if (empty($system_inquiry_time)){
            return fail();
        }

        $time = [];
        foreach ($system_inquiry_time as $item){
            $start_hour = substr($item['start_time'], 0, 1);
            if ($start_hour == '0') {
                $start_hour = substr($item['start_time'], 1, 1);
            }
            $start_minute = substr($item['start_time'], 2, 2);

            $start_time = $start_hour . ":" . $start_minute;

            $end_hour = substr($item['end_time'], 0, 1);
            if ($end_hour == '0') {
                $end_hour = substr($item['end_time'], 1, 1);
            }
            $end_minute = substr($item['end_time'], 2, 2);

            $end_time = $end_hour . ":" . $end_minute;

            $time[] = $start_time . '-' . $end_time;
        }

        $result = [];
        $result['time'] = $time;
        $result['times_number'] = $system_inquiry_config['times_number']; // 沟通次数（0为不限制次数）
        $result['duration'] = $system_inquiry_config['duration']; // 沟通时长（分钟，0为不限制时长）
        $result['inquiry_price'] = $system_inquiry_config['inquiry_price']; // 接诊价格

        return success($result);
    }
}