<?php

namespace App\Services;

use App\Model\MessageNotice;

/**
 * 通知消息
 * 不区分各端
 */
class MessageNoticeService extends BaseService
{
    /**
     * 获取医生服务消息列表-分页
     * @return array
     */
    public function getDoctorMessageService(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $page = $this->request->input('page', 1);
        $per_page = $this->request->input('per_page', 10);

        $params = array();
        $params['user_id'] = $user_info['user_id'];
        $params['user_type'] = 2;
        $params['notice_type'] = 1; // 消息类型（1:医生服务通知 2:医生系统公告 3:患者系统消息
        $params['send_status'] = 1;

        $fields = [
            'notice_id',
            'read_status',
            'inquiry_type',
            'from_name',
            'notice_title',
            'notice_send_time',
            'notice_content',
            'button_type',
            'link_type',
            'link_params',
            'show_type',
            'show_params',
        ];

        $result = MessageNotice::getMessageNoticePage($params, $fields, $page, $per_page);
        if (!empty($result['data'])) {
            foreach ($result['data'] as &$item) {
                if (!empty($item['link_params'])) {
                    $item['link_params'] = json_decode($item['link_params']);
                }

                if (!empty($item['show_params'])) {
                    $item['show_params'] = json_decode($item['show_params']);
                }

                if (!empty($item['notice_send_time'])) {
                    $item['notice_send_time'] = date('Y-m-d H:i',strtotime($item['notice_send_time']));
                }
            }
        }
        return success($result);
    }

    /**
     * 获取医生系统公告列表-分页
     * @return array
     */
    public function getDoctorMessageSystem(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $page = $this->request->input('page', 1);
        $per_page = $this->request->input('per_page', 10);

        $params = array();
        $params['user_id'] = $user_info['user_id'];
        $params['user_type'] = 2;
        $params['notice_type'] = 2; // 消息类型（1:医生服务通知 2:医生系统公告 3:患者系统消息
        $params['send_status'] = 1;

        $fields = [
            'notice_id',
            'read_status',
            'from_name',
            'notice_title',
            'notice_send_time',
            'notice_content',
            'button_type',
            'link_type',
            'link_params',
            'show_type',
            'show_params',
        ];

        $result = MessageNotice::getMessageNoticePage($params, $fields, $page, $per_page);
        if (!empty($result['data'])) {
            foreach ($result['data'] as &$item) {
                if (!empty($item['link_params'])) {
                    $item['link_params'] = json_decode($item['link_params'], true);
                }

                if (!empty($item['show_params'])) {
                    $item['show_params'] = json_decode($item['show_params'], true);
                }

                if (!empty($item['notice_send_time'])) {
                    $item['notice_send_time'] = date('Y-m-d H:i',strtotime($item['notice_send_time']));
                }
            }
        }
        return success($result);
    }

    /**
     * 获取医生系统公告详情
     * @return array
     */
    public function getDoctorMessageSystemInfo(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $notice_id = $this->request->route('notice_id');

        $params = array();
        $params['notice_id'] = $notice_id;
        $params['user_id'] = $user_info['user_id'];
        $params['notice_type'] = 2; // 消息类型（1:医生服务通知 2:医生系统公告 3:患者系统消息）
        $params['send_status'] = 1;
        $message_notice = MessageNotice::getOne($params);
        if (empty($message_notice)){
            return fail();
        }

        if (!empty($message_notice['link_params'])) {
            $message_notice['link_params'] = json_decode($message_notice['link_params'], true);
        }

        if (!empty($message_notice['show_params'])) {
            $message_notice['show_params'] = json_decode($message_notice['show_params'], true);
        }

        if (!empty($message_notice['notice_send_time'])) {
            $message_notice['notice_send_time'] = date('Y-m-d H:i',strtotime($message_notice['notice_send_time']));
        }

        return success($message_notice->toArray());
    }

    /**
     * 消息已读
     * @return array
     */
    public function putMessageReadId(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $notice_id = $this->request->route('notice_id');

        $params = array();
        $params['notice_id'] = $notice_id;
        $params['user_id'] = $user_info['user_id'];
        $message_notice = MessageNotice::getExists($params);
        if (empty($message_notice)){
            return success();
        }

        $data = array();
        $data['read_status'] = 1;

        $params = array();
        $params['notice_id'] = $notice_id;
        MessageNotice::edit($params,$data);

        return success();
    }

    /**
     * 一键消息已读
     * @return array
     */
    public function putMessageRead(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $notice_type = $this->request->input('notice_type');

        $data = array();
        $data['read_status'] = 1;

        $params = array();
        $params['user_id'] = $user_info['user_id'];
        $params['user_type'] = $user_info['user_type'];
        $params['notice_type'] = $notice_type;
        $params['send_status'] = 1;
        MessageNotice::edit($params,$data);

        return success();
    }

    /**
     * 获取患者系统消息通知最后一条消息列表
     * @return array
     */
    public function getPatientMessageServiceLast(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $result = array();

        // 服务消息
        $params = array();
        $params['user_id'] = $user_info['user_id'];
        $params['user_id'] = 1;
        $params['user_type'] = 1;
        $params['notice_type'] = 3; // 消息类型（1:医生服务通知 2:医生系统公告 3:患者系统消息
        $params['notice_system_type'] = 1; // 系统消息类型（患者端系统消息存在 1:服务消息 2:福利消息 3:退款消息 4:物流消息）
        $params['send_status'] = 1;
        $result['service']['info'] = MessageNotice::getOrderOne($params,'notice_send_time',['*']);

        $params['read_status'] = 0;
        $result['service']['count'] = MessageNotice::getCount($params);

        // 福利消息
        $params = array();
        $params['user_id'] = $user_info['user_id'];
        $params['user_id'] = 1;
        $params['user_type'] = 1;
        $params['notice_type'] = 3; // 消息类型（1:医生服务通知 2:医生系统公告 3:患者系统消息
        $params['notice_system_type'] = 2; // 系统消息类型（患者端系统消息存在 1:服务消息 2:福利消息 3:退款消息 4:物流消息）
        $params['send_status'] = 1;
        $result['benefit']['info'] = MessageNotice::getOrderOne($params,'notice_send_time',['*']);

        $params['read_status'] = 0;
        $result['benefit']['count'] = MessageNotice::getCount($params);

        // 退款消息
        $params = array();
        $params['user_id'] = $user_info['user_id'];
        $params['user_id'] = 1;
        $params['user_type'] = 1;
        $params['notice_type'] = 3; // 消息类型（1:医生服务通知 2:医生系统公告 3:患者系统消息
        $params['notice_system_type'] = 3; // 系统消息类型（患者端系统消息存在 1:服务消息 2:福利消息 3:退款消息 4:物流消息）
        $params['send_status'] = 1;
        $result['refund']['info'] = MessageNotice::getOrderOne($params,'notice_send_time',['*']);

        $params['read_status'] = 0;
        $result['refund']['count'] = MessageNotice::getCount($params);

        // 物流消息
        $params = array();
        $params['user_id'] = $user_info['user_id'];
        $params['user_id'] = 1;
        $params['user_type'] = 1;
        $params['notice_type'] = 3; // 消息类型（1:医生服务通知 2:医生系统公告 3:患者系统消息
        $params['notice_system_type'] = 4; // 系统消息类型（患者端系统消息存在 1:服务消息 2:福利消息 3:退款消息 4:物流消息）
        $params['send_status'] = 1;
        $result['logistics']['info'] = MessageNotice::getOrderOne($params,'notice_send_time',['*']);

        $params['read_status'] = 0;
        $result['logistics']['count'] = MessageNotice::getCount($params);

        return success($result);
    }

    /**
     * 获取患者服务、福利、退款、物流消息通知列表-分页
     * @return array
     */
    public function getPatientMessageSystem(): array
    {
        $user_info = $this->request->getAttribute("userInfo") ?? [];

        $notice_system_type = $this->request->input('notice_system_type');
        $page = $this->request->input('page', 1);
        $per_page = $this->request->input('per_page', 10);


        $params = array();
        $params['user_id'] = $user_info['user_id'];
        $params['user_type'] = 1;
        $params['notice_type'] = 3; // 消息类型（1:医生服务通知 2:医生系统公告 3:患者系统消息
        $params['notice_system_type'] = $notice_system_type; // 系统消息类型（患者端系统消息存在 1:服务消息 2:福利消息 3:退款消息 4:物流消息）
        $params['send_status'] = 1;

        $fields = [
            'notice_id',
            'read_status',
            'inquiry_type',
            'from_name',
            'notice_title',
            'notice_send_time',
            'notice_content',
            'button_type',
            'link_type',
            'link_params',
            'show_type',
            'show_params',
        ];

        $result = MessageNotice::getMessageNoticePage($params, $fields, $page, $per_page);
        if (!empty($result['data'])) {
            foreach ($result['data'] as &$item) {
                if (!empty($item['link_params'])) {
                    $item['link_params'] = json_decode($item['link_params']);
                }

                if (!empty($item['show_params'])) {
                    $item['show_params'] = json_decode($item['show_params']);
                }

                if (!empty($item['notice_send_time'])) {
                    $item['notice_send_time'] = date('Y-m-d H:i',strtotime($item['notice_send_time']));
                }
            }
        }

        return success($result);
    }
}