zoukankan      html  css  js  c++  java
  • TP5脚本编写

    #在application创建command文件夹
    #创建php文件,继承Command
    #示例   请根据自己需求进行修改
    <?php
    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2021/6/15
     * Time: 19:01
     */
    
    namespace appcommand;
    
    use appadminapicontrollerInsertredis;
    use thinkconsoleCommand;
    use thinkconsoleInput;
    use thinkconsoleOutput;
    use thinkDb;
    
    class Statistics extends Command
    {
        //设置php运行脚本名称
        protected function configure()
        {
            $this->setName('Statistics')->setDescription('Here is the remark ');
        }
        
    	//脚本内容
        protected function execute(Input $input, Output $output)
        {
            $sql = "CREATE TABLE IF NOT EXISTS fa_link_statistics(
                id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
                linkid INT(11) UNSIGNED NOT NULL COMMENT '链接id',
                clickCount INT(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '点击数',
                activeCount INT(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '激活数',
                nextDayCount INT(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '次留数',
                date CHAR(15) NOT NULL,
                createTime INT(11) UNSIGNED NOT NULL DEFAULT 0,
                updateTime INT(11) UNSIGNED NOT NULL DEFAULT 0
            )ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '链接统计表'";
            Db::query($sql);
    
            //当天起始时间戳
            $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
            $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
    
            //获取当日激活数
            $userLinks = Db::name('turn_user')->field('linkid,activeReportTime,morrowKeepTime,activateStatus')->where('linkid','NEQ','')->select();
            //前天开始时间
            $beginYesterday=mktime(0,0,0,date('m'),date('d')-2,date('Y'));
            //昨天结束时间
            $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
    
            $res = true;//判断是修改还是新增
            $reg = true;
            //查询所有统计
            $links = Db::name('link_statistics')->select();
            $linkArr = [];
            foreach ($links as $k=>$v) {
                $linkArr[$v['linkid']] = $v;
            }
            $insertredis = new Insertredis();
            //查询所有的链接
            $data = Db::name('link')->select();
            Db::startTrans();
            foreach ($data as $k=>$v){
                $activeCount = 0;
                $morrowKeepCount = 0;
                //判断统计是否存在
                if (isset($linkArr[$v['id']])){
                    //是否是当天第一次统计
                    if ($linkArr[$v['id']]['updateTime'] >= $beginToday && $linkArr[$v['id']]['updateTime'] <= $endToday){
                        foreach ($userLinks as $m=>$n){
                            if ($n['linkid'] == $v['id'] && $n['activeReportTime'] >= $beginToday && $n['activeReportTime'] <= $endToday && $n['activateStatus']) $activeCount++;
                        }
                    }else{
                        foreach ($userLinks as $m=>$n){
                            if ($n['linkid'] == $v['id'] && $n['activeReportTime'] >= $beginToday && $n['activeReportTime'] <= $endToday && $n['activateStatus']) $activeCount++;
                            if ($n['linkid'] == $v['id'] && $n['morrowKeepTime'] >= $beginYesterday && $n['morrowKeepTime'] <= $endYesterday) $morrowKeepCount++;
                        }
                    }
                }else{
                    foreach ($userLinks as $m=>$n){
                        if ($n['linkid'] == $v['id'] && $n['activeReportTime'] >= $beginToday && $n['activeReportTime'] <= $endToday && $n['activateStatus']) $activeCount++;
                        if ($n['linkid'] == $v['id'] && $n['morrowKeepTime'] >= $beginYesterday && $n['morrowKeepTime'] <= $endYesterday) $morrowKeepCount++;
                    }
                    $res = false;
                }
                $data1['clickCount'] = $insertredis->getMonitorClickNum($v['id']);
                $data1['activeCount'] = $activeCount;
                $data1['updateTime'] = time();
                if (!$res){
                    $data1['date'] = date('Ymd');
                    $data1['createTime'] = time();
                    $data1['linkid'] = $v['id'];
                    $data1['nextDayCount'] = $morrowKeepCount;
                    $rea = Db::name('link_statistics')->insert($data1);
                    if (!$rea) $reg = false;
                }else{
                    $rea = Db::name('link_statistics')->where('linkid',$v['id'])->update($data1);
                    if (!$rea) $reg = false;
                }
            }
            if ($reg){
                Db::commit();
                echo 1;
            }else{
                Db::rollback();
                echo 2;
            }
        }
    }
    
    
    
    #脚本名称设置好后需在application/command.php中进行定义
    <?php
    
    // +----------------------------------------------------------------------
    // | ThinkPHP [ WE CAN DO IT JUST THINK ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
    // +----------------------------------------------------------------------
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------
    // | Author: *******
    // +----------------------------------------------------------------------
    
    return [
        'appcommandStatistics'
    ];
    
    
    #脚本运行
    php think Statistics
    
  • 相关阅读:
    结构体的malloc与数组空间
    绘制K线图
    加载文件
    数据分析(绘图)
    GIT操作
    疑难杂症汇总
    Shell编程2
    shell编程1
    shell命令2
    Shell命令1
  • 原文地址:https://www.cnblogs.com/ljkltt/p/14888658.html
Copyright © 2011-2022 走看看