<?php

namespace app\admin\model\wechat;

use think\Model;


class RoutineQrcode extends Model
{

    

    

    // 表名
    protected $name = 'routine_qrcode';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'add_time_text',
        'url_time_text'
    ];
    

    



    public function getAddTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['add_time']) ? $data['add_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUrlTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['url_time']) ? $data['url_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setAddTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUrlTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    /**
     * TODO 添加二维码  存在直接获取
     * @param int $thirdId
     * @param string $thirdType
     * @param string $page
     * @param string $qrCodeLink
     * @return array|false|object|\PDOStatement|string|\think\Model
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function routineQrCodeForever($thirdId = 0, $thirdType = 'spread', $page = '', $qrCodeLink = '')
    {
        $count = self::where('third_id', $thirdId)->where('third_type', $thirdType)->count();
        if ($count) return self::where('third_id', $thirdId)->where('third_type', $thirdType)->field('id')->find();
        return self::setRoutineQrcodeForever($thirdId, $thirdType, $page, $qrCodeLink);
    }

    /**
     * 添加二维码记录
     * @param string $thirdType
     * @param int $thirdId
     * @return object
     */
    public static function setRoutineQrcodeForever($thirdId = 0, $thirdType = 'spread', $page = '', $qrCodeLink = '')
    {
        $data['third_type'] = $thirdType;
        $data['third_id'] = $thirdId;
        $data['status'] = 1;
        $data['add_time'] = time();
        $data['page'] = $page;
        $data['url_time'] = '';
        $data['qrcode_url'] = $qrCodeLink;
        return self::create($data);
    }

    /**
     * 修改二维码地址
     * @param int $id
     * @param array $data
     * @return bool
     */
    public static function setRoutineQrcodeFind($id = 0, $data = array())
    {
        if (!$id) return false;
        $count = self::getRoutineQrcodeFind($id);
        if (!$count) return false;
        return self::edit($data, $id, 'id');
    }

    /**
     * 获取二维码是否存在
     * @param int $id
     * @return int|string
     */
    public static function getRoutineQrcodeFind($id = 0)
    {
        if (!$id) return 0;
        return self::where('id', $id)->count();
    }

    /**
     * 获取小程序二维码信息
     * @param int $id
     * @param string $field
     * @return array|bool|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getRoutineQrcodeFindType($id = 0, $field = 'third_type,third_id,page')
    {
        if (!$id) return false;
        $count = self::getRoutineQrcodeFind($id);
        if (!$count) return false;
        return self::where('id', $id)->where('status', 1)->field($field)->find();
    }

}
