* @version : HUOSDK 8.0 */ namespace huomp\model\agent; use huolib\constant\CacheConst; use huomp\model\common\CommonModel; use think\Cache; class AgentGameDateCostModel extends CommonModel { protected $name = 'agent_game_date_cost'; // 开启自动写入时间戳字段 protected $autoWriteTimestamp = true; protected $cache_key_prefix = CacheConst::CACHE_AGENT_GAME_DATE_COST_PREFIX; /** * 添加数据 * * @param $data * * @return bool */ public function addData($data) { if (empty($data) || empty($data['agent_id']) || empty($data['app_id']) || empty($data['date']) || empty($data['cost'])) { return false; } $_data = $data; $_obj = self::create($_data, true); if ($_obj) { return true; } return false; } /** * 更新数据 * * @param array $data 数据 * @param int $id 应用ID * * @return bool */ public function updateData($data, $id) { $_map['id'] = $id; $_data = $data; $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map); if (false === $_rs) { return false; } $_cache_key = $this->getCacheKeyById($id); Cache::rm($_cache_key); return true; } /*** * 获取数据 * * @param $id * * @return mixed */ public function getInfoById($id) { $_cache_key = $this->getCacheKeyById($id); $_data = Cache::get($_cache_key); if (!empty($_data)) { return $_data; } $_map = ['id' => $id]; $_data = $this->where($_map)->find(); if (is_object($_data)) { $_data = $_data->toArray(); } if (!empty($_data)) { Cache::set($_cache_key, $_data); } return $_data; } /** * 删除数据 * * @param $id * * @return bool */ public function deleteData($id) { $_map['id'] = $id; $_rs = $this->where($_map)->delete(); if (false == $_rs) { return false; } $_cache_key = $this->getCacheKeyById($id); Cache::rm($_cache_key); return true; } /** * 获取缓存key * * @param $id * * @return string */ public function getCacheKeyByID($id) { return $this->cache_key_prefix.$id; } /** * 获取缓存key * * @param $agent_id * * @return string */ public function getCacheKeyByData($data) { return $this->cache_key_prefix.json_encode($data); } /** * 获取渠道游戏日推广成本记录id * * @param $agent_id * @param $app_id * @param $date * * @return int|mixed */ public function getIdByAgentGameDate($agent_id, $app_id, $date) { if (empty($agent_id) || empty($app_id) || empty($date)) { return 0; } $_map = [ 'agent_id' => $agent_id, 'app_id' => $app_id, 'date' => $date, ]; $_cache_key = $this->getCacheKeyByData($_map); $_data = Cache::get($_cache_key); if (!empty($_data)) { return $_data; } $_id = $this->where($_map)->value('id', 0); if (!empty($_id)) { Cache::set($_cache_key, $_id); } return $_id; } /** * 获取渠道游戏每日推广成本 * * @param $agent_id * @param $app_id * @param $date * * @return int|mixed */ public function getCostByAgentGameDate($agent_id, $app_id, $date) { $_id = $this->getIdByAgentGameDate($agent_id, $app_id, $date); if (empty($_id)) { return 0; } $_info = $this->getInfoById($_id); return !empty($_info['cost']) ? $_info['cost'] : 0; } /** * 设置渠道游戏每日推广成本 * @param $agent_id * @param $app_id * @param $date * @param $cost */ public function updateCostByAgentGameDate($agent_id, $app_id, $date, $cost) { $_id = $this->getIdByAgentGameDate($agent_id, $app_id, $date); if (empty($_id)) { $_data = [ 'agent_id' => $agent_id, 'app_id' => $app_id, 'date' => $date, 'cost' => $cost, ]; $this->addData($_data); } else { $_data = [ 'cost' => $cost ]; $this->updateData($_data,$_id); } } /** * 获取推广成本 * @param $where * * @return mixed */ public function getCostByWhere($where){ $_map = []; if(!empty($where['agent_id'])){ $_map['agent_id'] = $where['agent_id']; } if(!empty($where['app_id'])){ $_map['app_id'] = $where['app_id']; } if(!empty($where['date'])){ $_map['date'] = $where['date']; } $_cost = $this->where($_map)->value('sum(cost)'); if(empty($_cost)){ $_cost = 0; } return $_cost; } }