Commit f5c2b11c authored by clone's avatar clone

经纪人查看客户日志

parent 08f1bfa7
<?php
namespace app\api_broker\service;
use app\extra\RedisExt;
/**
* Created by PhpStorm.
* User: zw
* Date: 2019/9/17
* Time: 10:28
*/
class AgentLookUserLogService
{
private $redis;
private $nowTime;
const LOOK_USER = "agent_look_user_";//经纪人看用户key
const IS_PC = "agent_look_user_by_pc";//记录是哪里看的
public function __construct()
{
$this->redis = RedisExt::getRedis();
$this->nowTime = date("Y-m-d", time());
}
/**
* @param int $agent_id
* @param int $user_id
* @param int $is_pc
* @return array
*/
public function saveCacheAgentLookUser(int $agent_id, int $user_id, int $is_pc): array
{
//redis服务挂掉,都可以看
if (!$this->redis) {
return;
}
//判断此用户是否看过
$isExist = $this->isLooked($agent_id, $user_id);
if (!$isExist) {
$this->redis->sAdd(self::LOOK_USER . $this->nowTime . $agent_id, $user_id);//存入所看用户信息
$this->redis->set(self::LOOK_USER . $this->nowTime . $agent_id . "_" . $user_id, time());//存入第一次看的时间
$this->redis->set(self::IS_PC . $this->nowTime . $agent_id . "_" . $user_id, $is_pc);
} else {
$this->redis->set(self::LOOK_USER . $this->nowTime . $agent_id . "_" . $user_id . "_lastTime", time());//存入最后一次看的时间
$this->redis->set(self::IS_PC . $this->nowTime . $agent_id . "_" . $user_id . "_lastTime", $is_pc);
}
return;
}
/**
* 判断是否已经看过,看过返回true 否则 false
* @param int $agent_id
* @param int $user_id
* @return bool
*/
public function isLooked(int $agent_id, int $user_id): bool
{
$isExist = $this->redis->sIsMember(self::LOOK_USER . $this->nowTime . $agent_id, $user_id);
if ($isExist) {
return true;
} else {
return false;
}
}
}
\ No newline at end of file
......@@ -41,7 +41,18 @@ class TLookAgentUserModel extends BaseModel
}
return $id;
}
public function addLookUser($params)
{
Db::startTrans();
try {
$this->db_->insertAll($params);
Db::commit();
return 1;
} catch (\Exception $e) {
Db::rollback();
return 0;
}
}
public function delData($where)
{
return $this->db_->where($where)->delete();
......
<?php
namespace app\task\controller;
use app\extra\RedisExt;
use app\model\AAgents;
use app\model\TLookAgentUserModel;
use think\Exception;
use think\Log;
/**
* Created by PhpStorm.
* User : zw
* Date : 2018/5/9
* Time : 下午4:45
* Intro:
*/
class SaveLookUserHistoryTask
{
private $redis;
private $yesterday;
private $agentsModel;
private $tLookAgentUserModel;
const LOOK_USER = "agent_look_user_";//经纪人看用户key
const IS_PC = "agent_look_user_by_pc";//记录是哪里看的
function __construct()
{
$this->redis = RedisExt::getRedis();
$this->yesterday = date("Y-m-d", strtotime("-1 day"));
$this->agentsModel = new AAgents();
$this->tLookAgentUserModel = new TLookAgentUserModel();
}
/**
* 查询经纪人列表
* @return bool
*/
public function saveLookShop()
{
if (!$this->redis) {
return;
}
$day = date("Y-m-d", time());
if ($this->redis->get("run_save_agent_look_user" . $day)) {
return;
}
$this->redis->set("run_save_agent_look_user" . $day, 1, 24 * 3600);
//todo 1.拿到每个经纪人的看铺记录 2.根据看铺记录统计出已看铺数量,3.根据经纪人id 拿到可看铺总数
$total = $this->agentsModel->getAgentsCountByTask();
$pageSize = 100;
$pageTotal = ceil($total / $pageSize);
for ($pageNo = 1; $pageNo <= $pageTotal; $pageNo++) {
$resultArr = $this->agentsModel->getAgentsListByLookShop($pageNo, $pageSize, "id,name,phone");
$this->executeSave($resultArr);
unset($resultArr);
}
}
/**
* 保存看铺记录 清空redis缓存
* @param $resultArr
*/
private function executeSave($resultArr)
{
$list = [];
foreach ($resultArr as $key => $value) {
$agent_id = $value["id"];
$selectKey = self::LOOK_USER . $this->yesterday . $agent_id;
$collection = $this->redis->sMembers($selectKey);
foreach ($collection as $item) {
$getLookTimeKey = $selectKey . "_" . $item;
$getLookIsPcKey = self::IS_PC . $this->yesterday . $agent_id . "_" . $item;
$getLookLastTimeKey = $selectKey . "_" . $item . "_lastTime";
$getLookLastIsPcKey = self::IS_PC . $this->yesterday . $agent_id . "_" . $item . "_lastTime";
$lookTime = $this->redis->get($getLookTimeKey);
$getLookIsPc = $this->redis->get($getLookIsPcKey);
$getLookLastTime = $this->redis->get($getLookLastTimeKey);
$getLookLastIsPc = $this->redis->get($getLookLastIsPcKey);
if ($lookTime && $getLookIsPc) {
array_push($list, $this->tLookShopBin($agent_id, $item, $getLookIsPc, $lookTime));
}
if ($getLookLastTime && $getLookLastIsPc) {
array_push($list, $this->tLookShopBin($agent_id, $item, $getLookLastIsPc, $getLookLastTime));
}
//删除redis缓存
$this->redis->sRem($selectKey, $item);
$this->redis->del($getLookTimeKey);
$this->redis->del($getLookIsPcKey);
$this->redis->del($getLookLastTimeKey);
$this->redis->del($getLookLastIsPcKey);
}
try {
$is_insert = -1;
if (!empty($list)) {
$is_insert = $this->tLookAgentUserModel->addLookUser($list);
}
if ($is_insert == 0) {
Log::record("SaveLookUserHistoryTask--111----executeSave---save error", "info");
}
} catch (Exception $exception) {
Log::record("SaveLookUserHistoryTask---222---executeSave---save error" . $exception, "info");
}
$list = [];
}
unset($list);
}
private function tLookShopBin($agent_id, $user_id, $source, $look_time)
{
$arrBin = [];
$arrBin['user_id'] = $user_id;
$arrBin['agent_id'] = $agent_id;
$arrBin['source'] = $source;
$arrBin["create_time"] = date("Y-m-d H:i:s", $look_time);
$arrBin["update_time"] = date("Y-m-d H:i:s", time());
return $arrBin;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment