AgentGameDateCostModel.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * AgentGameDateCostModel.php UTF-8
  4. * 渠道游戏每日推广成本
  5. *
  6. * @date : 2019/12/5 15:52
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : dengcongshuai <dcs@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huomp\model\agent;
  13. use huolib\constant\CacheConst;
  14. use huomp\model\common\CommonModel;
  15. use think\Cache;
  16. class AgentGameDateCostModel extends CommonModel {
  17. protected $name = 'agent_game_date_cost';
  18. // 开启自动写入时间戳字段
  19. protected $autoWriteTimestamp = true;
  20. protected $cache_key_prefix = CacheConst::CACHE_AGENT_GAME_DATE_COST_PREFIX;
  21. /**
  22. * 添加数据
  23. *
  24. * @param $data
  25. *
  26. * @return bool
  27. */
  28. public function addData($data) {
  29. if (empty($data) || empty($data['agent_id']) || empty($data['app_id']) || empty($data['date'])
  30. || empty($data['cost'])) {
  31. return false;
  32. }
  33. $_data = $data;
  34. $_obj = self::create($_data, true);
  35. if ($_obj) {
  36. return true;
  37. }
  38. return false;
  39. }
  40. /**
  41. * 更新数据
  42. *
  43. * @param array $data 数据
  44. * @param int $id 应用ID
  45. *
  46. * @return bool
  47. */
  48. public function updateData($data, $id) {
  49. $_map['id'] = $id;
  50. $_data = $data;
  51. $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map);
  52. if (false === $_rs) {
  53. return false;
  54. }
  55. $_cache_key = $this->getCacheKeyById($id);
  56. Cache::rm($_cache_key);
  57. return true;
  58. }
  59. /***
  60. * 获取数据
  61. *
  62. * @param $id
  63. *
  64. * @return mixed
  65. */
  66. public function getInfoById($id) {
  67. $_cache_key = $this->getCacheKeyById($id);
  68. $_data = Cache::get($_cache_key);
  69. if (!empty($_data)) {
  70. return $_data;
  71. }
  72. $_map = ['id' => $id];
  73. $_data = $this->where($_map)->find();
  74. if (is_object($_data)) {
  75. $_data = $_data->toArray();
  76. }
  77. if (!empty($_data)) {
  78. Cache::set($_cache_key, $_data);
  79. }
  80. return $_data;
  81. }
  82. /**
  83. * 删除数据
  84. *
  85. * @param $id
  86. *
  87. * @return bool
  88. */
  89. public function deleteData($id) {
  90. $_map['id'] = $id;
  91. $_rs = $this->where($_map)->delete();
  92. if (false == $_rs) {
  93. return false;
  94. }
  95. $_cache_key = $this->getCacheKeyById($id);
  96. Cache::rm($_cache_key);
  97. return true;
  98. }
  99. /**
  100. * 获取缓存key
  101. *
  102. * @param $id
  103. *
  104. * @return string
  105. */
  106. public function getCacheKeyByID($id) {
  107. return $this->cache_key_prefix.$id;
  108. }
  109. /**
  110. * 获取缓存key
  111. *
  112. * @param $agent_id
  113. *
  114. * @return string
  115. */
  116. public function getCacheKeyByData($data) {
  117. return $this->cache_key_prefix.json_encode($data);
  118. }
  119. /**
  120. * 获取渠道游戏日推广成本记录id
  121. *
  122. * @param $agent_id
  123. * @param $app_id
  124. * @param $date
  125. *
  126. * @return int|mixed
  127. */
  128. public function getIdByAgentGameDate($agent_id, $app_id, $date) {
  129. if (empty($agent_id) || empty($app_id) || empty($date)) {
  130. return 0;
  131. }
  132. $_map = [
  133. 'agent_id' => $agent_id,
  134. 'app_id' => $app_id,
  135. 'date' => $date,
  136. ];
  137. $_cache_key = $this->getCacheKeyByData($_map);
  138. $_data = Cache::get($_cache_key);
  139. if (!empty($_data)) {
  140. return $_data;
  141. }
  142. $_id = $this->where($_map)->value('id', 0);
  143. if (!empty($_id)) {
  144. Cache::set($_cache_key, $_id);
  145. }
  146. return $_id;
  147. }
  148. /**
  149. * 获取渠道游戏每日推广成本
  150. *
  151. * @param $agent_id
  152. * @param $app_id
  153. * @param $date
  154. *
  155. * @return int|mixed
  156. */
  157. public function getCostByAgentGameDate($agent_id, $app_id, $date) {
  158. $_id = $this->getIdByAgentGameDate($agent_id, $app_id, $date);
  159. if (empty($_id)) {
  160. return 0;
  161. }
  162. $_info = $this->getInfoById($_id);
  163. return !empty($_info['cost']) ? $_info['cost'] : 0;
  164. }
  165. /**
  166. * 设置渠道游戏每日推广成本
  167. * @param $agent_id
  168. * @param $app_id
  169. * @param $date
  170. * @param $cost
  171. */
  172. public function updateCostByAgentGameDate($agent_id, $app_id, $date, $cost) {
  173. $_id = $this->getIdByAgentGameDate($agent_id, $app_id, $date);
  174. if (empty($_id)) {
  175. $_data = [
  176. 'agent_id' => $agent_id,
  177. 'app_id' => $app_id,
  178. 'date' => $date,
  179. 'cost' => $cost,
  180. ];
  181. $this->addData($_data);
  182. } else {
  183. $_data = [
  184. 'cost' => $cost
  185. ];
  186. $this->updateData($_data,$_id);
  187. }
  188. }
  189. /**
  190. * 获取推广成本
  191. * @param $where
  192. *
  193. * @return mixed
  194. */
  195. public function getCostByWhere($where){
  196. $_map = [];
  197. if(!empty($where['agent_id'])){
  198. $_map['agent_id'] = $where['agent_id'];
  199. }
  200. if(!empty($where['app_id'])){
  201. $_map['app_id'] = $where['app_id'];
  202. }
  203. if(!empty($where['date'])){
  204. $_map['date'] = $where['date'];
  205. }
  206. $_cost = $this->where($_map)->value('sum(cost)');
  207. if(empty($_cost)){
  208. $_cost = 0;
  209. }
  210. return $_cost;
  211. }
  212. }