<?php
/**
 * @desc: 访问统计
 * @Author: Dr.Xing <ezfwork@foxmail.com>
 * @Since: 2017/12/14 12:09
 */

namespace app\common\model;


use think\Model;

class Counter extends Model
{
    /**
     * 增/减访问计数器
     * @param int    $item_id   被统计数据的ID
     * @param string $item_type 被统计数据的类型
     * @param int    $added     计数器增长数（默认+1）
     */
    public static function added($item_id, $item_type='', $added=1)
    {
        $count = self::get(['item_id'=>$item_id]);
        if($count){
            $count->views=$count->views+$added;
            $count->save();
        } else {
            self::create(['item_id'=>$item_id, 'item_type'=>$item_type, 'views'=>$added]);
        }
    }

    //点赞+1
    public static function good($item_id, $item_type='', $added=1)
    {
        $count = self::get(['item_id'=>$item_id]);
        if($count){
            $count->good=$count->good+$added;
            $count->save();
        } else {
            self::create(['item_id'=>$item_id, 'item_type'=>$item_type, 'good'=>$added]);
        }
    }

    //点踩+1
    public static function bad($item_id, $item_type='', $added=1)
    {
        $count = self::get(['item_id'=>$item_id]);
        if($count){
            $count->bad=$count->bad+$added;
            $count->save();
        } else {
            self::create(['item_id'=>$item_id, 'item_type'=>$item_type, 'bad'=>$added]);
        }
    }
}