123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- /**
- * AgentGameDateCostModel.php UTF-8
- * 渠道游戏每日推广成本
- *
- * @date : 2019/12/5 15:52
- *
- * @license 这不是一个自由软件,未经授权不许任何使用和传播。
- * @author : dengcongshuai <dcs@huosdk.com>
- * @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;
- }
- }
|