<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Model;
use Think\Model;

/**
 * 分类模型
 */
class SupportModel extends Model{

    protected $_validate = array(

    );

    protected $_auto = array(
    );

    /**
     * 构造函数
     * @param string $name 模型名称
     * @param string $tablePrefix 表前缀
     * @param mixed $connection 数据库连接信息
     */
    public function __construct($name = '', $tablePrefix = '', $connection = '') {
        /* 设置默认的表前缀 */
        $this->tablePrefix ='tab_';
        /* 执行构造方法 */
        parent::__construct($name, $tablePrefix, $connection);
    }


    /**
     * 按照时间分组统计注册总数
     * @param integer           $id          数据库表行id
     */
    public function froze($id) {
        $data = [
            'freeze' => 0
        ];

        return $this->where("id=".$id)->save($data);
    }

    /**
     * 按照时间分组统计注册总数
     * @param integer           $id          数据库表行id
     */
    public function unfreeze($id)
    {
        $data = [
            'freeze' => 1
        ];
        return $this->where("id=".$id)->save($data);
    }

    public function rechangePassward($id) {

        $passward = $this->getRandomPassword(6);

        $data = [
            'user_password' => base64_encode($passward)
        ];

        if ($this->where("id=".$id)->save($data)) {
            return $passward;
        } else {
            return false;
        }

    }

    private function getRandomPassword($length, $special = true){
        $chars = array(
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
            'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
            'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
            'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2',
            '3', '4', '5', '6', '7', '8', '9'
        );

        if($special){
            $chars = array_merge($chars, array(
                '!', '@', '#', '$', '?', '|', '{', '/', ':', ';',
                '%', '^', '&', '*', '(', ')', '-', '_', '[', ']',
                '}', '<', '>', '~', '+', '=', ',', '.'
            ));
        }

        $charsLen = count($chars) - 1;
        shuffle($chars);

        $password = '';
        for($i=0; $i<$length; $i++){
            $password .= $chars[mt_rand(0, $charsLen)];
        }
        return $password;
    }

}