<?php

namespace app\api\controller;
use addons\epay\library\Service;
use app\common\controller\Api;
use app\common\library\NightSchoolBigData;
use app\common\model\school\classes\ClassesLib;
use app\common\model\school\classes\order\Order;
use app\common\model\school\classes\VisitDistribution;
use bw\UrlLock;
use think\Cache;

/** 定时任务控制器
 * Class Crontab
 * @package addons\shopro\controller
 */
class Crontab extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 每天执行的任务
     */
    public function day()
    {



       try{
          $res = Order::timeoutCheck(true);
           $res = \app\common\model\school\classes\activity\order\Order::timeoutCheck(true);

                 //得到近七天的开始结束时间 "start_date","end_date"
           $seven_date = NightSchoolBigData::getLastSevenDaysDate();
           $seven_days_start_date = $seven_date["start_date"];
           $seven_days_end_date = $seven_date["end_date"];
           $seven_date = NightSchoolBigData::getLastMonthDate();
           $month_start_date = $seven_date["start_date"];
           $month_end_date = $seven_date["end_date"];
           $seven_date = NightSchoolBigData::getLastDaysDate();
           $last_days_start_date = $seven_date["start_date"];
           $last_days_end_date = $seven_date["end_date"];
           $seven_date = NightSchoolBigData::getLastWeekDate();
           $last_week_start_date = $seven_date["start_date"];
           $last_week_end_date = $seven_date["end_date"];

           //更新画像
           $weMiniTotal = new \WeMini\Total(Service::wechatConfig());
           $res = $weMiniTotal->getWeanalysisAppidUserportrait($seven_days_start_date, $seven_days_end_date);
           $cacheNmae = 'getWeanalysisAppidUserportrait' . $seven_days_start_date . $seven_days_end_date;
           Cache::set($cacheNmae, $res, (3600 * 24 * 32));

           $res = $weMiniTotal->getWeanalysisAppidUserportrait($last_days_start_date, $last_days_end_date);
           $cacheNmae = 'getWeanalysisAppidUserportrait' . $last_days_start_date . $last_days_end_date;
           Cache::set($cacheNmae, $res, (3600 * 24 * 32));
           //微信小程序访问页面
           $res = $weMiniTotal->getWeanalysisAppidVisitPage($last_days_start_date, $last_days_end_date);
           $cacheNmae = 'getWeanalysisAppidVisitPage' . $last_days_start_date . $last_days_end_date;
           Cache::set($cacheNmae, $res, (3600 * 24 * 32));
           //月留存
           $res = $weMiniTotal->getWeanalysisAppidMonthlyRetaininfo($month_start_date, $month_end_date);
           $cacheNmae = 'getWeanalysisAppidMonthlyRetaininfo' . $month_start_date . $month_end_date;
           Cache::set($cacheNmae, $res, (3600 * 24 * 32));
           //周留存
           $res = $weMiniTotal->getWeanalysisAppidWeeklyRetaininfo($last_week_start_date, $last_week_end_date);
           $cacheNmae = 'getWeanalysisAppidWeeklyRetaininfo' . $last_week_start_date . $last_week_end_date;
           Cache::set($cacheNmae, $res, (3600 * 24 * 32));
           //日留存
           $res = $weMiniTotal->getWeanalysisAppidDailyRetaininfo($last_days_start_date, $last_days_end_date);
           $cacheNmae = 'getWeanalysisAppidDailyRetaininfo' . $last_days_start_date . $last_days_end_date;
           Cache::set($cacheNmae, $res, (3600 * 24 * 32));
           //访问分布
           $res = $weMiniTotal->getWeanalysisAppidVisitdistribution($last_days_start_date, $last_days_end_date);
           $cacheNmae = 'getWeanalysisAppidVisitdistribution' . $last_days_start_date . $last_days_end_date;
           Cache::set($cacheNmae, $res, (3600 * 24 * 32));
           VisitDistribution::setlog($last_days_start_date,$last_days_end_date,$res);

           //微信小程序月趋势
           $res = $weMiniTotal->getWeanalysisAppidMonthlyVisittrend($month_start_date, $month_end_date);
           $cacheNmae = 'getWeanalysisAppidMonthlyVisittrend' . $month_start_date . $month_end_date;
           Cache::set($cacheNmae, $res, (3600 * 24 * 32));
           //微信小程序周趋势
           $res = $weMiniTotal->getWeanalysisAppidWeeklyVisittrend($last_week_start_date, $last_week_end_date);
           $cacheNmae = 'getWeanalysisAppidWeeklyVisittrend' . $last_week_start_date . $last_week_end_date;
           Cache::set($cacheNmae, $res, (3600 * 24 * 32));
           //微信小程序访问分析
           $res = $weMiniTotal->getWeanalysisAppidDailyVisittrend($last_days_start_date, $last_days_end_date);
           $cacheNmae = 'getWeanalysisAppidDailyVisittrend' . $last_days_start_date . $last_days_end_date;
           Cache::set($cacheNmae, $res, (3600 * 24 * 32));
           //微信小程序数据分析
           $res = $weMiniTotal->getWeanalysisAppidDailySummarytrend($last_days_start_date, $last_days_end_date);
           $cacheNmae = 'getWeanalysisAppidDailySummarytrend' . $last_days_start_date . $last_days_end_date;
           Cache::set($cacheNmae, $res, (3600 * 24 * 32));
       }catch (\Exception $e){
           $this->error("执行失败：".$e->getMessage());
       }
        $this->success("执行成功");
    }


    /**
     * 每五分钟执行的任务
     */
    public function minute()
    {
        try{

            $res = Order::timeoutCheck(true);
            $res = \app\common\model\school\classes\activity\order\Order::timeoutCheck(true);

//            $lock = new UrlLock(2,"mock-lock-suffix",5,"您的请求过于频繁，请您稍后再试！");
//            $lock->lock();
//            var_dump("進入時間：".date("Y-m-d H:i:s "));
//            sleep(2);
//            $lock->free();
//             var_dump("釋放時間：".date("Y-m-d H:i:s "));
//            MockOrder::distributionFailSmsNotice(MockOrder::where("id",438)->find());
            //${name}，手机号${phone}有${number}圈已被分配到您的${car_number}号车上，请及时查看！
//            $event = "distribution_success";
////            $event = "distribution_fail";
//            $config = get_addon_config('alisms');
//            $template_id = $config['template'][$event];
//             $res = Smslib::notice(15090180611, ['name'=>"小绿",'phone'=>"15555555555",'number'=>"3",'car_number'=>"22"], $template_id);
////            $params = [
////                'mobile'   => 15090180611,
////                'msg'      => ['name'=>"小明",'phone'=>"15555555555",'number'=>"3",'car_number'=>"22"],
////                'template' => "SMS_275410075"
////            ];
////            $result = \think\Hook::listen('sms_notice', $params, null, true);
//            var_dump($res);die;
//
//            //得到返解析的内容
//            $qrcode = new \Zxing\QrReader( cdnurl("/uploads/20221117/f8c6c1ba12938daf9767719fa022d6b8.png",true));   //绝对路径
//            $common_content = $qrcode->decode(); //返回二维码的内容


//            $qrcode = new \Zxing\QrReader(cdnurl("/uploads/20221116/ffcf9f4bd725ec49ebb53d97166a486b.png",true));  //二维码图片路径
//            $text = $qrcode->text(); //返回识别后的文本
//             $ids = AppointmentOrder::where("mock_code_url",'not null')->page(1,500)->column("id");
//             $num = count($ids);
//            foreach ($ids as $id) {
//                //取消模拟资格
//                AppointmentOrder::cancelMockCode($id, true);
//                //插入订单日志
//                OrderAction::log($id, "预约订单取消模拟资格，原小票练车码（如果有）作废", "admin", 1);
//            }
//            $qrcode = new QrReader(cdnurl("/uploads/20221117/f8c6c1ba12938daf9767719fa022d6b8.png",true));  //图片路径
//            $text = $qrcode->decode(); //返回识别后的文本

//            $res = MockOrder::repairOrders(1000,$trans = false);

//           $sss = Common::getLinkMiniQrCode(29716);
//           var_dump($sss->toArray());die;

        }catch (\Exception $e){
            $this->error("执行失败：".$e->getMessage());
        }
        $this->success("执行成功");
    }
}
