<?php
/**
 * +------------------------------------------------------------
 * | 数据 data/model/Data.php
 * +------------------------------------------------------------
 * | @author: zhx (10630650@qq.com)
 * | @create_time: 2018-03-09 15:48:38
 * +------------------------------------------------------------
 * | @copyright: CIM信息聚合系统 (https://cimxx.com)
 * +------------------------------------------------------------
 * | @last_modified_by: zhx
 * | @last_modified_time: 2018-03-09 15:48:38
 * +------------------------------------------------------------
 * | @todo:
 * +------------------------------------------------------------
 */

namespace app\index\model;

use app\common\model\Init;
use think\model\concern\SoftDelete;

class Data extends Init
{
    use SoftDelete;

    private static $areas = [];

    // 设置json类型字段
    protected $json = ['fields'];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    protected $auto = ['thumb'];

    protected $deleteTime = 'delete_time';

    protected $defaultSoftDelete = 0;

    protected $append = ['area', 'is_top', 'is_end'];

    protected static function init()
    {
        self::$areas = load_areas(0);
    }

    public function user()
    {
        return $this->hasOne('app\user\model\User', 'id', 'user_id')->field('id,name,group_id,status,email,phone,fields');
    }

    public function image()
    {
        return $this->hasMany('Image')->order('list_order asc');
    }

    public function article()
    {
        return $this->hasOne('Article');
    }

    public function info()
    {
        return $this->hasOne('Info');
    }

    public function phone()
    {
        return $this->hasOne('Phone');
    }

    public function links()
    {
        return $this->hasOne('Links');
    }

    /**
     * 处理地区字段
     */
    public function getAreaAttr($value, $data)
    {
        if (isset(self::$areas[$data['area_id']])) return self::$areas[$data['area_id']];
        return ['id' => '', 'name' => ''];
    }

    public function getThumbAttr($value)
    {
        return action('files/api/_init_thumb', [$value, 1]);
    }

    public function setThumbAttr($value)
    {
        return action('files/api/original_thumb', [$value]);
    }

    public function setEndTimeAttr($value)
    {
        if (strtotime($value) !== false) return strtotime($value);
        return is_numeric($value) ? (time() + $value * 86400) : (time() + 7 * 86400);
    }

    public function getEndTimeAttr($value, $data)
    {
        return $value ? time_format($value) : 0;
    }

    public function getEndTimeDayAttr($value, $data)
    {
        return $data['end_time'] < time() ? 0 : (ceil(($data['end_time'] - time()) / 86400));
    }

    public function getIsEndAttr($value, $data)
    {
        return isset($data['end_time']) ? (($data['end_time'] != 0 && $data['end_time'] < time()) ? 1 : 0) : '';
    }

    public function getBehaviorAttr($value)
    {
        return empty($value) ? '' : json_decode($value, 1);
    }

    public function getIsTopAttr($value, $data)
    {
        return isset($data['top_end_time']) ? (($data['top_end_time'] > time()) ? 1 : 0) : '';
    }

}
