<?php

namespace app\api\controller\school;

use app\common\model\school\classes\order\Order as OrderModel;
use app\common\model\school\classes\Teacher as Teachermodel;

/**
 * 用户端:站内信接口
 */
class Message extends Base
{
    protected $noNeedLogin = ['message_count'];
    protected $noNeedRight = '*';

    protected $model = null;

    /**
     * 初始化操作
     * @access protected
     */
    protected function _initialize()
    {

        $this->model = new \app\common\model\school\Message();
        parent::_initialize();

        //判断登录用户是否是员工
    }


    /**
     * @ApiTitle( 站内信详情-确认已读)
     * @ApiSummary(站内信详情-确认已读)
     * @ApiRoute(/api/school/message/detail)
     * @ApiMethod(GET)
     * @ApiParams(name = "id", type = "int",required=true,description = "消息id")
     * @ApiReturn({
     *
     *})
     */
    public function detail(){
        $id = $this->request->get('id/d','');

        if(empty($id)){
            $this->error(__('缺少必要参数'));
        }

        try {
            $res =  $this->model::getDetail($id);
        } catch (\Exception $e){
//            Log::log($e->getMessage());
            $this->error($e->getMessage(),['errcode'=>$e->getCode()]);
        }
        $this->success('获取成功', ['detail' => $res]);
    }





    /**
     * @ApiTitle( 我的消息列表接口)
     * @ApiSummary(我的消息列表接口)
     * @ApiRoute(/api/school/message/message_list)
     * @ApiMethod(GET)
     * @ApiParams(name = "keywords", type = "string",required=false,description = "搜索关键字")
     * @ApiParams(name = "page", type = "string",required=true,description = "页数")
     * @ApiParams(name = "limit", type = "string",required=true,description = "条数")
     * @ApiParams(name = "status", type = "string",required=false,description = "消息总类型:system=系统消息,classes=课程消息,order=订单消息")
     * @ApiParams(name = "mini_type", type = "string",required=false,description = "小消息类型:order_notice=课程订单通知,classes_auth=课程报名审核,classes_apply=课程上新审核,shop_apply=机构审核,classes_order_notice=课时预约,user_auth=机构授权用户信息,aftercare=售后服务,other=其他")
     * @ApiParams(name = "type", type = "string",required=false,description = "已读状态:1=未读,2=已读")
     * @ApiParams(name = "time", type = "string",required=false,description = "复合时间查询:today今天,week本周,month本月,year本年,yesterday昨天,last year上一年,last week上周,last month上個月,lately7最近7天 , lately30最近30天,按开始时间区间查传值格式:Y/m/d H:M:S-Y/m/d H:M:S")
     *
     * @ApiReturn({
     *
     *})
     */
    public function message_list()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $page  =  $this->request->get('page/d', 0); //页数
        $limit =  $this->request->get('limit/d', 0); //条数
        $keywords =  $this->request->get('keywords/s', ''); //搜索关键字
        $status =  $this->request->get('status/s', ''); //搜索关键字
        $mini_type =  $this->request->get('mini_type/s', ''); //搜索关键字
        $type =  $this->request->get('type/s', ''); //搜索关键字
        $time =  $this->request->get('time/s', ''); //时间
//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单
        $to_id = $user_id;
        try{
            //当前申请状态
            $res = $this->model::messageList($page, $limit,$to_id,$keywords,$status,$mini_type,$type,$time);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }


    /**
     * @ApiTitle( 我的消息数量接口)
     * @ApiSummary(我的消息数量接口)
     * @ApiRoute(/api/school/message/message_count)
     * @ApiMethod(GET)
     * @ApiReturn({
     *
     *})
     */
    public function message_count(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

        try{
            $res = $this->model::messageCount($user_id);
        }catch (\Throwable $e){
            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }



     /**
     * @ApiTitle( 设置一键已读)
     * @ApiSummary(设置一键已读)
     * @ApiMethod(GET)
     * @ApiParams(name = "id", type = "string",required=false,description = "默认* 多值逗号拼接")
     * @ApiParams(name = "event", type = "string",required=false,description = "要删的事件")
     * @ApiParams(name = "status", type = "string",required=false,description = "消息总类型:system=系统消息,classes=课程消息,order=订单消息")
     * @ApiParams(name = "mini_type", type = "string",required=false,description = "小消息类型:order_notice=课程订单通知,classes_auth=课程报名审核,classes_apply=课程上新审核,shop_apply=机构审核,classes_order_notice=课时预约,user_auth=机构授权用户信息,aftercare=售后服务,other=其他")
     * @ApiReturn({
     *
     *})
     */
    public function batch()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

        $id =  $this->request->get('id/s', '*'); //条数
        $event =  $this->request->get('event/s', ''); //搜索关键字
        $status =  $this->request->get('status/s', ''); //搜索关键字
        $mini_type =  $this->request->get('mini_type/s', ''); //搜索关键字

        try{
            //当前申请状态
            $res = $this->model::batchRead($id,$user_id,$status,$mini_type,$event,'user',$user_id,true);

        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('操作成功');
    }


}