<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/4/6
 * Time: 下午8:47
 */

namespace fcode;

require_once (BASE_ROOT_PATH . '/helper/goods/commonid_helper.php');

use Exception;
use commonid_helper;

class mfcode
{
    private $mParams;

    public function __construct($param)
    {
        if(is_array($param))
        {
            $this->mParams = $param;
        }
        elseif(is_numeric($param))
        {
            $fc_id = intval($param);
            if($fc_id <= 0) {
                throw new Exception("错误的参数");
            }
            else
            {
                $mod_fcode = Model('goods_fcode');
                $result = $mod_fcode->find($fc_id);
                if(empty($result)) {
                    throw new Exception("没有这个F码");
                } else {
                    $this->mParams = $result;
                }
            }
        }
        elseif(is_string($param))
        {
            $fc_code = $param;
            $mod_fcode = Model('goods_fcode');
            $fcode = $mod_fcode->where(array('fc_code' => $fc_code))->find();
            if(empty($fcode)) {
                throw new Exception("没有这个F码");
            } else {
                $this->mParams = $fcode;
            }
        }
        else {
            throw new Exception("错误的参数");
        }
    }

    public function params() {
        return $this->mParams;
    }

    public function format()
    {
        $result = [];
        $result['fcode_id'] = $this->fc_id();

        $gid = commonid_helper::instance()->one_goods($this->commonid());
        if($gid == false) return false;

        $result['goods_id'] = $gid;
        $result['fcode']    = $this->fcode();
        $result['key']      = $this->user_key();
        $result['usable_time'] = $this->usable_time();
        $result['state'] = $this->state();
        $result['used'] = $this->used();
        $result['expired'] = $this->expired();

        return $result;
    }

    private function state()
    {
        if($this->used()) return 1;
        if($this->expired()) return 2;
        if($this->locked()) return 3;

        return 0;
    }

    public function fc_id() {
        return intval($this->mParams['fc_id']);
    }
    public function commonid() {
        return intval($this->mParams['goods_commonid']);
    }
    public function batch_code() {
        return ($this->mParams['batch_code']);
    }
    public function fcode() {
        return $this->mParams['fc_code'];
    }
    public function used() {
        return (intval($this->mParams['fc_state']) == 1);
    }
    public function user_key() {
        return $this->mParams['user_key'];
    }
    public function mobile() {
        return $this->mParams['mobile'];
    }
    public function grabed() {
        return intval($this->mParams['grab_state']) == 1;
    }
    public function binded() {
        return intval($this->mParams['grab_state']) == 2;
    }
    public function usable_time() {
        return intval($this->mParams['usable_time']);
    }
    public function expired()
    {
        $tm = $this->usable_time();
        return ($tm > 0 && $tm <= time());
    }
    public function locked() {
        return (intval($this->mParams['fc_state']) == 3);
    }
    public function can_use() {
        return ($this->commonid() > 0 && $this->expired() == false && $this->used() == false && $this->locked() == false);
    }
    public function un_used() {
        return ($this->commonid() > 0 && $this->expired() == false && $this->used() == false);
    }
}