| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 | <?php/** * IdentifyMemModel.php  UTF-8 * http://doc.huosdk.com/web/#/170?page_id=10980 * 玩家实名信息表 * * @date    : 2019/11/29 15:37 * * @license 这不是一个自由软件,未经授权不许任何使用和传播。 * @author  : chenbingling <cbl@huosdk.com> * @version : HUOSDK 8.5 */namespace huoIdentify\model;use huo\model\common\CommonModel;use huolib\constant\CacheConst;use huolib\constant\CommonConst;use huolib\constant\IdentifyConst;use think\Cache;class IdentifyMemModel extends CommonModel {    protected $name             = 'identify_mem';    protected $pk               = 'id';    protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_MEM_PREFIX;    /* 开启自动写入时间戳字段 */    protected $autoWriteTimestamp = true;    protected $type                                  = [            'id'            => 'integer',            'mem_id'        => 'integer',            'identify_type' => 'integer',            'real_name'     => 'string',            'id_card'       => 'string',            'identify_pi'   => 'string',            'identify_from' => 'string',            'create_time'   => 'timestamp',            'update_time'   => 'timestamp',        ];    /**     * 获取单条记录缓存key     *     * @param int $id ID     *     * @return string :string     */    protected function getSingleCacheKey($id) {        return $this->cache_key_prefix.$id;    }    /**     * 根据玩家id获取缓存key     *     * @param int $mem_id 玩家id     *     * @return string :string     */    protected function getCacheKeyByMemId($mem_id) {        return $this->cache_key_prefix.'mem_'.$mem_id;    }    /**     * 根据身份证号获取数量缓存key     *     * @param string $id_card 身份证号     *     * @return string :string     */    protected function getCntCacheKeyByIdCard($id_card) {        return $this->cache_key_prefix.'cntc_'.$id_card;    }    /**     * 获取玩家实名信息上报认证状态缓存key     *     * @param        $mem_id     * @param string $id_card 身份证号     *     * @return string :string     */    protected function getReportStatusCacheKeyByMemIdIdCard($mem_id, $id_card) {        return $this->cache_key_prefix.'rs_m_'.$mem_id.'_ic_'.$id_card;    }    /**     * 添加数据     *     * @param array $data 需要添加的数据     *     * @return false|int  添加失败返回 false 添加成功 返回添加的ID     */    public function addData($data) {        $_data = $data;        $_id = parent::addData($_data);        if (false === $_id) {            return false;        }        if (!empty($_data['mem_id'])) {            $_cache_key = $this->getCacheKeyByMemId($_data['mem_id']);            Cache::set($_cache_key, $_id);        }        if (!empty($_data['id_card'])) {            $_cache_key = $this->getCntCacheKeyByIdCard($_data['id_card']);            Cache::rm($_cache_key);        }        return $_id;    }    /**     * 通过ID获取信息     *     * @param int $id 主键ID     *     * @return array     */    public function getInfoById($id) {        /* 缓存操作 */        $_single_cache_key = $this->getSingleCacheKey($id);        $_data = Cache::get($_single_cache_key);        if (!empty($_data)) {            return $_data;        }        $_data = parent::getInfoById($id);        if (empty($_data)) {            return [];        }        Cache::set($_single_cache_key, $_data);        return $_data;    }    /**     * 更新单条数据     *     * @param array $data 数据     * @param int   $id   ID     *     * @return bool     */    public function updateData($data, $id) {        $_old_data = $this->getInfoById($id);        $_map[$this->pk] = $id;        $_data = $data;        $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map);        if (false === $_rs) {            return false;        }        /* 缓存操作 */        $_single_cache_key = $this->getSingleCacheKey($id);        Cache::rm($_single_cache_key);        if (isset($_data['mem_id']) && $_old_data['mem_id'] != $_data['mem_id']) {            /* 更新了玩家id 需要删除旧缓存 */            $_cache_key = $this->getCacheKeyByMemId($_old_data['mem_id']);            Cache::rm($_cache_key);        }        if (isset($_data['id_card']) && $_old_data['id_card'] != $_data['id_card']) {            $_cache_key = $this->getCntCacheKeyByIdCard($_data['id_card']);            Cache::rm($_cache_key);            $_cache_key = $this->getCntCacheKeyByIdCard($_old_data['id_card']);            Cache::rm($_cache_key);        }        return true;    }    /**     * 删除单条数据     *     * @param int  $id          ID     * @param bool $is_complete 是否完成删除     *     * @return bool     */    public function deleteData($id, $is_complete = true) {        $_old_data = $this->getInfoById($id);        $_rs = parent::deleteData($id, $is_complete);        if (false == $_rs) {            return false;        }        /* 缓存操作 */        $_single_cache_key = $this->getSingleCacheKey($id);        Cache::rm($_single_cache_key);        /* 删除玩家id 缓存 */        $_cache_key = $this->getCacheKeyByMemId($_old_data['mem_id']);        Cache::rm($_cache_key);        $_cache_key = $this->getCntCacheKeyByIdCard($_old_data['id_card']);        Cache::rm($_cache_key);        return $_rs;    }    /**     * 根据玩家id获取id     *     * @param int $mem_id 玩家id     *     * @return int     */    public function getIdByMemId($mem_id) {        $_cache_key = $this->getCacheKeyByMemId($mem_id);        if (Cache::has($_cache_key)) {            return Cache::get($_cache_key);        }        $_map = [            'mem_id' => $mem_id        ];        $_id = $this->where($_map)->value('id');        if (empty($_id)) {            $_id = CommonConst::CONST_ZERO;        }        Cache::set($_cache_key, $_id);        return $_id;    }    /**     * 根据玩家id获取实名信息     *     * @param int $mem_id 玩家id     *     * @return array     */    public function getInfoByMemId($mem_id) {        $_id = $this->getIdByMemId($mem_id);        return $this->getInfoById($_id);    }    /**     * 根据玩家id获取证件号     *     * @param $mem_id     *     * @return mixed|string     */    public function getIdCardByMemId($mem_id) {        $_info = $this->getInfoByMemId($mem_id);        return get_val($_info, 'id_card', '');    }    /**     * 根据证件号获取玩家id     *     * @param $id_card     *     * @return array     */    public function getMemIdsByIdCard($id_card) {        $_map = [            'id_card' => $id_card        ];        $_mem_ids = $this->where($_map)->column('mem_id');        return $_mem_ids;    }    /**     * 根据证件号获取数量     *     * @param $id_card     *     * @return int|string     */    public function getCntByIdCard($id_card) {        $_cache_key = $this->getCntCacheKeyByIdCard($id_card);        if (Cache::has($_cache_key)) {            return Cache::get($_cache_key);        }        $_map = [            'id_card' => $id_card        ];        $_cnt = $this->getCnt($_map);        Cache::set($_cache_key, $_cnt);        return $_cnt;    }    /**     * 根据证件号获取信息     *     * @param $id_card     *     * @return array     */    public function getInfoByIdCard($id_card) {        $_map = ['id_card' => $id_card];        $_id = $this->where($_map)->order('identify_pi DESC, id ASC')->value('id');        if (empty($_id)) {            return [];        }        return $this->getInfoById($_id);    }    /**     * 设置上报实名失败     *     * @param $mem_id     * @param $id_card     *     * @return mixed     */    public function setReportFailByMemIdIdCard($mem_id, $id_card) {        $_rs_cache_key = $this->getReportStatusCacheKeyByMemIdIdCard($mem_id, $id_card);        return Cache::set($_rs_cache_key, IdentifyConst::IDENTITY_STATUS_NO);    }    /**     * 获取上报实名状态     *     * @param $mem_id     * @param $id_card     *     * @return mixed     */    public function getReportStatusByMemIdIdCard($mem_id, $id_card) {        $_rs_cache_key = $this->getReportStatusCacheKeyByMemIdIdCard($mem_id, $id_card);        $_status = Cache::get($_rs_cache_key);        /* 获取后清理 */        Cache::rm($_rs_cache_key);        return $_status;    }    /**     * 关联玩家     */    public function mem() {        $_field = [            'id',            'username'        ];        return $this->belongsTo('\huoIdentify\model\MemberModel', 'mem_id', 'id')->field($_field);    }    /**     * 通过条件获取数量     *     * @param array $where 条件     *     * @return int     */    public function getCnt($where = []) {        $_map = $where;        $_cnt = $this->useGlobalScope(false)->where($_map)->count();        if (empty($_cnt)) {            return 0;        }        return $_cnt;    }}
 |