<?php
/**
 * 保存浏览记录到数据库
 * 执行时间：每2分钟执行一次
 */
define("APPLICATION_PATH", realpath(dirname(__FILE__) . '/../../../')); //指向public的上一级
require APPLICATION_PATH . '/scripts/crontab/common.php';
$browserDb4Redis = \Redis\Db4\GoodsBrowserRedisModel::getInstance();
while ($memberId = $browserDb4Redis->tableSPop('memberIds')){
    $records = $browserDb4Redis->tableHGAll('memberId:'.$memberId);
    if(is_array($records) && !empty($records)){
        $memberId = unserialize(current($records))['member_id'];
        $datas = [];
        foreach ($records as $k=>$v){
            $v = unserialize($v);
            $datas[] = $v;
        }
        $return = \DAO\GoodsBrowseModel::getInstance()->insertAllOrUpdate($datas);
        if($return){
            $browserDb4Redis->tableDel('memberId:'.$memberId);
            $records = $browserDb4Redis->tableHGAll('memberId:g_'.$memberId);
            if(is_array($records) && !empty($records)){
                $memberId = unserialize(current($records))['member_id'];
                $datas = [];
                foreach ($records as $k=>$v){
                    $v = unserialize($v);
                    $datas[] = $v;
                }
                $return = \DAO\GoodsBrowseModel::getInstance()->insertAllOrUpdate($datas);
                if($return){
                    $browserDb4Redis->tableDel('memberId:g_'.$memberId);
                }
            }
            \Redis\Db1\MemberCenterRedisModel::getInstance()->tableHDel($memberId,\Our\NameConst::footprint);
        }
    }else{
        $records = $browserDb4Redis->tableHGAll('memberId:g_'.$memberId);
        if(is_array($records) && !empty($records)){
            $memberId = unserialize(current($records))['member_id'];
            $datas = [];
            foreach ($records as $k=>$v){
                $v = unserialize($v);
                $datas[] = $v;
            }
            $return = \DAO\GoodsBrowseModel::getInstance()->insertAllOrUpdate($datas);
            if($return){
                $browserDb4Redis->tableDel('memberId:'.$memberId);
            }
        }
        \Redis\Db1\MemberCenterRedisModel::getInstance()->tableHDel($memberId,\Our\NameConst::footprint);
    }
}

