<?php

namespace app\api\controller\school;

use app\common\model\school\classes\hourorder\Order as OrderModel;

/**
 * 用户端：课时预约订单接口
 */
class HourOrder extends Base
{
    protected $noNeedLogin = ["get_month_by_time"];
    protected $noNeedRight = '*';

    protected $model = null;


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

        $this->model = new OrderModel;
        parent::_initialize();

        //判断登录用户是否是员工
        $this->setUrlLock();
    }








    /**
     * @ApiTitle( 订单确认/订单计算接口)
     * @ApiSummary(订单确认/订单计算接口【有过期时间】)
     * @ApiRoute(/api/school/hour_order/confirm)
     * @ApiMethod(POST)
     * @ApiParams(name = "classes_order_id", type = "int",required=true,description = "课程订单id")
     * @ApiParams(name = "classes_lib_spec_id", type = "int",required=false,description = "课时规格id")
     * @ApiParams(name = "order_no", type = "string",required=false,description = "缓存key")
     * @ApiParams(name = "is_compute", type = "int",required=false,description = "是否重新计算并更新缓存 默认传1")
     * @ApiReturn({
     *
     *})
     */
    public function confirm(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $classes_order_id =  $this->request->post('classes_order_id/d', 0); //课程id
        $classes_lib_spec_id =  $this->request->post('classes_lib_spec_id/d', 0); //课程id
//
        $param  =  []; //参数
        $order_no  =  $this->request->post('order_no/s', ''); //订单号
        $is_compute =  $this->request->post('is_compute/d', 1); //是否重新计算并更新缓存
        try{
            //当前申请状态
            $res = $this->model->confirm($user_id, $classes_order_id,$order_no,$classes_lib_spec_id,$param, $is_compute);
        }catch (\Exception $e){
//            Log::log($e->getMessage());
            $this->error($e->getMessage(),['errcode'=>$e->getCode()]);
        }
        $this->success('执行成功，可用缓存key去预约', $res);
    }


    /**
     * @ApiTitle( 订单下单接口)
     * @ApiSummary(订单下单接口)
     * @ApiRoute(/api/school/hour_order/create)
     * @ApiMethod(POST)
     * @ApiParams(name = "order_no", type = "string",required=true,description = "缓存key")
     * @ApiReturn({
     *
     *})
     */
    public function create(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $order_no =  $this->request->post('order_no/s', ''); //订单号
        $remark =  $this->request->post('remark/s', ''); //下单备注
//        repeat_filter("appointment\order\create".$user_id, 2);
        $this->setUrlLock("only");
        try{
            //当前申请状态
            $res = $this->model->cacheCreateOrder($order_no, $user_id,$remark, true);
        }catch (\Throwable $e){
//            file_put_contents("test.txt",$e->getMessage().$e->getFile().$e->getLine());//写入文件，一般做正式环境测试
//            Log::log($e->getMessage());
            $this->error($e->getMessage(),['errcode'=>$e->getCode()]);;
        }
        $this->success('订单创建成功，缓存key被消耗', $res);
    }



    /**
     * @ApiTitle( 预约课时单取消接口)
     * @ApiSummary(预约课时单取消接口（已完成的单无法取消）)
     * @ApiRoute(/api/school/hour_order/cancel)
     * @ApiMethod(POST)
     * @ApiParams(name = "order_no", type = "string",required=true,description = "订单号")
     * @ApiReturn({
     *
     *})
     */
    public function cancel(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $order_no =  $this->request->post('order_no/s', ''); //订单号
        try{
            //当前申请状态
            $res = $this->model->cancel($order_no,$user_id,true,'user',0,true);
        }catch (\Throwable $e){
            $this->error($e->getMessage());
        }
        $this->success('预约课时取消成功', $res);
    }






    /**
     * @ApiTitle( 课时订单详情)
     * @ApiSummary(课时订单详情)
     * @ApiRoute(/api/school/hour_order/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 =  OrderModel::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/hour_order/order_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 = "订单状态:-3=已取消,-1=已报名待审核,0=已预约,3=已完成")
     * @ApiParams(name = "classes_order_id", type = "int",required=true,description = "课程订单id")
     * @ApiParams(name = "classes_lib_id", type = "int",required=false,description = "课程id")
     * @ApiParams(name = "time", type = "string",required=false,description = "按开始和结束时间区间查（传值格式：Y/m/d H:M:S-Y/m/d H:M:S）")
     * @ApiParams(name = "start_time", type = "string",required=false,description = "按开始时间区间查（传值格式：Y/m/d H:M:S-Y/m/d H:M:S）")
     * @ApiParams(name = "createtime", type = "string",required=false,description = "按创建时间区间查（传值格式：Y/m/d H:M:S-Y/m/d H:M:S）")
     * @ApiReturn({
     *
     *})
     */
    public function order_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', ''); //搜索关键字
        $classes_order_id =  $this->request->get('classes_order_id/d', 0); //搜索关键字
        $classes_lib_ids =  $this->request->get('classes_lib_id/s', 0); //搜索关键字

        $start_time =  $this->request->get('start_time/s', ''); //搜索关键字
        $createtime =  $this->request->get('createtime/s', ''); //搜索关键字
        $time =  $this->request->get('time/s', ''); //搜索关键字

//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单

        try{
            //当前申请状态
            $res = $this->model::allList($user_id,$page, $limit,$keywords,$status,$classes_order_id,$classes_lib_ids,$start_time,$createtime,$time);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){

            $this->error($this->model::getLastSql());
        }
        $this->success('查询成功', $res);
    }


    /**
     * @ApiTitle( 我的课时订单数量接口)
     * @ApiSummary(返回课时订单各个数量)
     * @ApiRoute(/api/school/hour_order/order_count)
     * @ApiMethod(GET)
     * @ApiParams(name = "classes_order_id", type = "int",required=true,description = "课程订单id")
     * @ApiParams(name = "classes_lib_id", type = "int",required=false,description = "课程id")
     * @ApiReturn({
     *
     *})
     */
    public function order_count(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

        $classes_order_id =  $this->request->get('classes_order_id/d', 0); //搜索关键字
        $classes_lib_ids =  $this->request->get('classes_lib_id/s', 0); //搜索关键字

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


    /**
     * @ApiTitle(通过时间点得到当前的月份数据)
     * @ApiSummary(通过时间点得到当前的月份数据)
     * @ApiMethod(GET)
     * @ApiParams(name="time",type="string",required=true,description="选择的时间点时间戳")
     * @ApiParams(name = "status", type = "string",required=false,description = "订单状态:-3=已取消,-1=已报名待审核,0=已预约,3=已完成")
     */
    public function get_month_by_time(){
        $time = $this->request->get('time/s','');
        $status = $this->request->get('status/s','-1,0');
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
//        if(empty($time)){
//            $this->error(__('缺少必要参数'));
//        }
        try {
            $res =  $this->model::getMonthByTime($time,$user_id,$status);
        } catch (\Exception $e){
//            Log::log($e->getMessage());
            $this->error($e->getMessage(),['errcode'=>$e->getCode()]);
        }
        $this->success('生成成功', $res);
    }

}

