UserActionService.class.php 1.52 KB
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 17/4/12
 * Time: 10:38
 */
namespace Common\Service;

use Common\Common\Constant;
use Common\Model\UserActionModel;

class UserActionService extends AbstractService
{
    // 构造方法
    public function __construct()
    {
        parent::__construct();
        $this->_d = new UserActionModel();
    }

    /**
     * 获取用户行为总数
     * @author tangxingguo
     * @param string $uid 用户ID
     * @param string $action_key 行为key
     * @return int
     */
    public function countByActionKey($uid, $action_key)
    {
        return $this->_d->countByActionKey($uid, $action_key);
    }

    /**
     * 记录用户动作
     * @author zhonglei
     * @param string $uid 用户ID
     * @param int $data_id 数据ID
     * @param string $action_key 动作Key
     * @return bool
     */
    public function save($uid, $data_id, $action_key)
    {
        // 单条数据只记录一次的动作Key
        $unique_acts = [
            Constant::INT_ACT_READ,
            Constant::INT_ACT_SHARED,
            Constant::INT_ACT_LIKE,
            Constant::INT_ACT_COLLECT,
        ];

        $data = [
            'uid' => $uid,
            'data_id' => $data_id,
            'action_key' => $action_key,
        ];

        if (in_array($action_key, $unique_acts)) {
            $count = $this->count_by_conds($data);

            if ($count > 0) {
                return false;
            }
        }

        $this->insert($data);
        return true;
    }
}