<?php
/**
 * JYmusic音乐管理系统
 *
 * @version     2.0
 * @author      战神~~巴蒂 <jyuucn@163.com>
 * @license     http://jyuu.cn [未经授权严禁私自出售，否者承担法律责任]
 * @copyright   2014 - 2017 JYmusic
 */
namespace addons\Comment\model;

use think\Model;
use app\services\Actions;

class Comment extends Model
{
    /** 自动写入时间戳 */
    protected $autoWriteTimestamp = true;
    
    /** 关闭自动写入update_time字段 */
    protected $updateTime = false;

        public $fields = [
        [
            'name'  => 'uid',
            'title' => '用户id',
            'type'  => 'num',
        ],
        [
            'name'  => 'type',
            'title' => '类型',
            'type' => 'radio',
            'options' => [
                '1' => '音乐',
                '2' => '艺人',
                '3' => '专辑',
                '4' => '资讯',
                '5' => '用户'
            ],
            'value' => '1'
        ],
        [
            'name'  => 'reply_id',
            'title' => '回复ID',
            'type'  => 'num',
        ],
        [
            'name'  => 'page_id',
            'title' => '页面ID',
            'type'  => 'num',
            'remark' => ''
        ],
        [
            'name'  => 'digg',
            'title' => '顶次数',
            'type'  => 'num',
        ],
        [
            'name'  => 'content',
            'title' => '评论内容',
            'type'  => 'textarea',
            'remark' => ''
        ],
    ];
    
    /** 自动过滤内容  */
    protected function setContentAttr($val)
    {
        return text_filter($val);
    }
    
    /**
     * 自动获取别名
     * @param string $val
     * @return string
     */
    protected function getCreateTimeAttr($val)
    {
        return beforeTime($val);
    }
    
    /**
     * 关联用户
     * @return mixed
     */
    public function user()
    {
        return $this->belongsTo('\app\common\model\Member', 'uid')
            ->field('uid,nickname,avatar');
    }

    /**
     * 获取类型列表
     * @param int $pageId
     * @param int $type
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return mixed
     */
    public function getList($pageId, $type, $page = 1, $limit = 20, $order = 'id desc')
    {
        $map['page_id']  = $pageId;
        $map['reply_id'] = 0;
        $map['type']     = $type;
        $map['status']     = 1;

        $field = 'id,uid,content,digg,create_time';
        $list  = $this->where($map)
            ->page($page . ',' . $limit)
            ->field($field)
            ->with('user')
            ->order($order)
            ->select();

        if ($list) {
            $nums = Actions::getNum('comment', 'digg');
            foreach ($list as &$val) {
                $val['digg'] = isset($nums[$val['id']])? ($val['digg'] + $nums[$val['id']]) : $val['digg'];
                
                $reply = $this->getReply($val);
                if (!empty($reply)) {
                    $val['reply'] = $this->getReply($val);
                }
                unset($val['uid']);
            }
        }

        return $list;
    }

    protected function getReply($data)
    {
        $rootId = $data['id'];
        $list   = $this->where('root_id', $rootId)
            ->field('id,uid,content,digg,reply_id,create_time')
            ->with('user')
            ->order('id asc')
            ->select();

        if ($list) {
            $relayList = [];
            foreach ($list as $val) {
                $relayList[$val['id']] = $val;
            }
            
            $nums = Actions::getNum('comment', 'digg');
            foreach ($list as &$val) {
                $val['digg'] = isset($nums[$val['id']])? ($val['digg'] + $nums[$val['id']]) : $val['digg'];
                if ($val['reply_id'] == $rootId) {
                    $val['by_user'] = $data['user'];
                } else {
                    $val['by_user'] = $relayList[$val['reply_id']]['user'];
                }
            }
            unset($data, $relayList);
        }
        return $list;
    }
}
