UserActionModel.class.php 1.57 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhonglei
 * Date: 17/7/25
 * Time: 11:17
 */
namespace Common\Model;

class UserActionModel extends AbstractModel
{

    // 构造方法
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 根据用户ID计算今日动作总数
     * @author zhonglei
     * @param array $action_keys 动作数组
     * @param string $uid 用户ID
     * @return array
     *          + string    action_key      动作
     *          + int       total           动作总数
     *          + int       data_total      不重复的数据总数
     */
    public function countTodayActionKeyByUid($action_keys, $uid)
    {
        if (!is_array($action_keys) || empty($action_keys) || empty($uid)) {
            return [];
        }

        $conds = [
            'uid' => $uid,
            'action_key' => $action_keys,
            'created >= ?' => rstrtotime(rgmdate(MILLI_TIME, 'Y-m-d'), 1),
        ];

        $wheres = [];
        $params = [];
        $this->_parse_where($wheres, $params, $conds);

        // 企业标记
        $wheres[] = "`{$this->prefield}domain`=?";
        $params[] = QY_DOMAIN;
        // 状态条件
        $wheres[] = "`{$this->prefield}status`<?";
        $params[] = $this->get_st_delete();

        $wheres_sql = implode(' AND ', $wheres);
        $sql = 'SELECT `action_key`, COUNT(`user_action_id`) `total`, COUNT(DISTINCT `app_data_id`) `data_total` FROM __TABLE__ ';
        $sql .= "WHERE {$wheres_sql} GROUP BY `action_key`";

        return $this->_m->fetch_array($sql, $params);
    }
}