AgentGameRateLogic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /**
  3. * AgentGameRateLogic.php UTF-8
  4. * 渠道游戏折扣逻辑
  5. *
  6. * @date : 2019/4/20 16:43
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.5
  11. */
  12. namespace huoRate\logic;
  13. use huo\controller\agent\AgentGame;
  14. use huo\controller\common\CommonFunc;
  15. use huo\logic\game\GameLogic;
  16. use huo\model\agent\AgentGameModel;
  17. use huo\model\common\CommonModel;
  18. use huo\model\game\GameModel;
  19. use huo\model\game\GameversionModel;
  20. use huo\model\user\RoleModel;
  21. use huo\model\user\UserModel;
  22. use huolib\constant\CommonConst;
  23. use huolib\constant\GameConst;
  24. use huomp\controller\agent\AgentState;
  25. use huomp\model\game\GameMiniModel;
  26. use huoRate\model\AgentGameRateModel;
  27. class AgentGameRateLogic extends CommonModel {
  28. protected $base_field = [];
  29. /**
  30. * 转换查询条件
  31. *
  32. * @param array $param
  33. *
  34. * @return array
  35. */
  36. public function getWhere($param = []) {
  37. $_map = [];
  38. /* 时间搜索 */
  39. if (!empty($param['start_time']) && !empty($param['end_time'])) {
  40. $_map['create_time']
  41. = [
  42. 'between',
  43. [
  44. strtotime($param['start_time']),
  45. CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])
  46. ]
  47. ];
  48. } elseif (!empty($param['start_time'])) {
  49. $_map['create_time'] = ['egt', strtotime($param['start_time'])];
  50. } elseif (!empty($param['end_time'])) {
  51. $_map['create_time'] = ['elt', CommonConst::CONST_DAY_SECONDS + strtotime($param['end_time'])];
  52. }
  53. /* ID搜索 */
  54. if (!empty($param['id'])) {
  55. $_map['id'] = $param['id'];
  56. }
  57. /* 优惠类型搜索 */
  58. if (!empty($param['benefit_type'])) {
  59. $_map['benefit_type'] = $param['benefit_type'];
  60. }
  61. /* 推广模式搜索 */
  62. if (!empty($param['agent_benefit_type'])) {
  63. $_map['agent_benefit_type'] = $param['agent_benefit_type'];
  64. }
  65. /* 推广状态搜索 */
  66. if (!empty($param['promote_switch'])) {
  67. $_map['promote_switch'] = $param['promote_switch'];
  68. }
  69. /* 渠道类型搜索 */
  70. if (!empty($param['role_type'])) {
  71. $_role_ids = (new RoleModel())->getIdsByRoleType($param['role_type']);
  72. if (!empty($_role_ids)) {
  73. if (!empty($param['role_id'])) {
  74. /* 取交集 */
  75. $param['role_id'] = array_intersect($_role_ids, [$param['role_id']]);
  76. } else {
  77. $param['role_id'] = $_role_ids;
  78. }
  79. }
  80. }
  81. /* 渠道ID搜索 */
  82. if (!empty($param['role_id'])) {
  83. $_agent_ids = (new UserModel())->getIdsByRoleId($param['role_id']);
  84. $_map['agent_id'] = ['in', $_agent_ids];
  85. }
  86. /* 渠道ID搜索 */
  87. if (!empty($param['agent_id'])) {
  88. if (CommonConst::CONST_ADMIN_SEARCH_ZERO == $param['agent_id']) {
  89. $param['agent_id'] = 0;
  90. }
  91. $_map['agent_id'] = $param['agent_id'];
  92. }
  93. /* 应用ID搜索 */
  94. if (!empty($param['app_id'])) {
  95. if (CommonConst::CONST_ADMIN_SEARCH_ZERO == $param['app_id']) {
  96. $param['app_id'] = 0;
  97. }
  98. $_map['app_id'] = $param['app_id'];
  99. }
  100. if (empty($_map['app_id'])) {
  101. $_game_map = [
  102. 'delete_time' => ['eq', 0],
  103. ];
  104. if (!empty($param['game_name'])) {
  105. $_game_map['name'] = ['like', '%'.$param['game_name'].'%'];
  106. }
  107. if (!empty($param['classify'])) {
  108. $_game_map['classify'] = $param['classify'];
  109. }
  110. if (!empty($param['game_status'])) {
  111. $_game_map['status'] = $param['game_status'];
  112. }
  113. $_game_ids = (new GameModel())->getIdsByWhere($_game_map);
  114. $_exclude_app_ids = CommonFunc::getAppAppId();
  115. if (!empty($param['exclude_app_ids'])) {
  116. $_exclude_app_ids = array_merge($_exclude_app_ids, $param['exclude_app_ids']);
  117. }
  118. $_map['app_id'] = [
  119. ['notin', $_exclude_app_ids],
  120. ['in', $_game_ids],
  121. 'and'
  122. ];
  123. }
  124. return $_map;
  125. }
  126. /**
  127. * 获取列表底层函数
  128. *
  129. * @param array $where 搜索条件
  130. * @param string $page 列表个数
  131. * @param string $order 排序
  132. * @param array $field 附加字段
  133. * @param array $with 关联
  134. *
  135. * @return array ['count'=>0,'list'=>[]]
  136. */
  137. public function getList($where = [], $page = '1,10', $order = '', $field = [], $with = '') {
  138. $_map = $where;
  139. $_field = $field;
  140. $_model = new AgentGameRateModel();
  141. $_count = $_model->where($_map)->count();
  142. if (empty($_count)) {
  143. return [
  144. 'count' => 0,
  145. 'list' => []
  146. ];
  147. }
  148. $_order = $this->orderFilter($order);
  149. if (!empty($with)) {
  150. $_datas = $_model->with($with)->field($_field)->where($_map)->order($_order)->page($page)->select();
  151. } else {
  152. $_datas = $_model->field($_field)->where($_map)->order($_order)->page($page)->select();
  153. }
  154. if (is_object($_datas)) {
  155. $_datas = $_datas->toArray();
  156. }
  157. if (empty($_datas)) {
  158. return [
  159. 'count' => 0,
  160. 'list' => []
  161. ];
  162. }
  163. return [
  164. 'count' => $_count,
  165. 'list' => $_datas
  166. ];
  167. }
  168. /**
  169. * 获取后台列表
  170. *
  171. * @param array $where 搜索条件
  172. * @param string $page 列表个数
  173. * @param string $order 排序
  174. * @param array $field 附加字段
  175. *
  176. * @return array ['count'=>0,'list'=>[]]
  177. */
  178. public function getAdminList($where = [], $page = '1,10', $order = '-id', $field = []) {
  179. $_map = $this->getWhere($where);
  180. $_field = $this->base_field;
  181. if (!empty($field)) {
  182. $_field = array_merge($_field, $field);/* 获取后台字段 */
  183. }
  184. if (!empty($where['app_id']) && is_numeric($where['app_id'])) {
  185. /* 无对应游戏优惠则添加 */
  186. $_agr_model = new AgentGameRateModel();
  187. $_agent_id = 0;
  188. $_ag_id = $_agr_model->getIdByGameAgent($where['app_id'], $_agent_id);
  189. if (empty($_ag_id)) {
  190. $_add_data = [
  191. 'app_id' => $where['app_id'],
  192. 'agent_id' => $_agent_id
  193. ];
  194. $_agr_model->addData($_add_data);
  195. }
  196. if (!empty($where['agent_id']) && is_numeric($where['agent_id'])) {
  197. $_agent_id = $where['agent_id'];
  198. $_agr_id = $_agr_model->getIdByGameAgent($where['app_id'], $_agent_id);
  199. $_ag_id = (new AgentGameModel())->getIdByAgentApp($_agent_id, $where['app_id']);
  200. if (!empty($_ag_id) && empty($_agr_id)) {
  201. (new AgentGame())->addAgentGameRate($where['app_id'], $_agent_id);
  202. }
  203. }
  204. }
  205. $_rdata = $this->getList($_map, $page, $order, $_field, 'game,user');
  206. $_datas = $_rdata['list'];
  207. $_game_mini_model = new GameMiniModel();
  208. $_agent_game_model = new AgentGameModel();
  209. $_agent_state = new AgentState();
  210. $_gv_model = new GameversionModel();
  211. foreach ($_datas as $_k => $_v) {
  212. if (!empty($_v['game'])) {
  213. $_datas[$_k]['game_name'] = get_val($_v['game'], 'name', '');
  214. $_datas[$_k]['game_icon'] = get_val($_v['game'], 'icon', '');
  215. $_classify = get_val($_v['game'], 'classify', 0);
  216. $_datas[$_k]['classify'] = $_classify;
  217. $_datas[$_k]['agent_name'] = get_val($_v['user'], 'user_login', '');
  218. $_datas[$_k]['agent_nickname'] = get_val($_v['user'], 'user_nicename', '');
  219. $_datas[$_k]['role_id'] = get_val($_v['user'], 'role_id', 0);
  220. $_datas[$_k]['benefit_type_label'] = GameConst::getRateMsg($_v['benefit_type']);
  221. $_datas[$_k]['agent_benefit_type_label'] = GameConst::getAgentBenefitTypeMsg($_v['agent_benefit_type']);
  222. /* 获取小游戏id */
  223. $_datas[$_k]['mini_app_id'] = $_game_mini_model->getMpIdByAppId($_v['app_id']);
  224. /* 获取渠道游戏id */
  225. $_datas[$_k]['ag_id'] = $_agent_game_model->getIdByAgentApp($_v['agent_id'], $_v['app_id']);
  226. /* 获取游戏是否切量 */
  227. $_datas[$_k]['is_switch'] = $_agent_game_model->getIsSwitchByAgentApp($_v['agent_id'], $_v['app_id']);
  228. /* 获取渠道推广码 */
  229. $_share_code = $_agent_state->genAgentCode($_datas[$_k]['ag_id']);
  230. $_datas[$_k]['share_code'] = $_share_code;
  231. /* 获取渠道推广路径 */
  232. $_datas[$_k]['path'] = '';
  233. if (GameConst::GAME_MP == $_classify || GameConst::GAME_H5MP == $_classify) {
  234. $_gv_data = $_gv_model->getDefaultInfoByAppId($_v['app_id']);
  235. $_datas[$_k]['path'] = empty($_gv_data['package_url']) ? ''
  236. : $_gv_data['package_url'].'?state='.$_share_code;
  237. } elseif (GameConst::GAME_H5 == $_classify) {
  238. $_datas[$_k]['path'] = H5SITE.'/sdk.php/game?game_id='.$_v['app_id'].'&agent_id='.$_v['agent_id'];
  239. }
  240. unset($_datas[$_k]['game']);
  241. unset($_datas[$_k]['user']);
  242. }
  243. }
  244. $_rdata['list'] = $_datas;
  245. return $_rdata;
  246. }
  247. /***
  248. * 获取渠道可申请游戏列表
  249. *
  250. * @param int $agent_id 渠道id
  251. * @param array $where 搜索条件
  252. * @param string $page 分页数据
  253. * @param string $order 排序
  254. *
  255. * @return array ['count'=>0,'list'=>[]]
  256. */
  257. public function getNoApplyList($agent_id, $where = [], $page = '1,10', $order = '-id') {
  258. /* 排除已申请的游戏 */
  259. $_game_ids = (new AgentGameModel())->getGameIdsByAgentId($agent_id);
  260. $where['exclude_app_ids'] = $_game_ids;
  261. $where['agent_id'] = CommonConst::CONST_ADMIN_SEARCH_ZERO;
  262. $where['game_status'] = GameConst::GAME_STATUS_ON;
  263. $where['promote_switch'] = ['neq', GameConst::GAME_PROMOTE_SWITCH_NO];
  264. $_map = $this->getWhere($where);
  265. $_field = [
  266. 'app_id' => 'app_id',
  267. 'agent_rate' => 'agent_rate',
  268. 'sub_agent_rate' => 'sub_agent_rate',
  269. 'agent_rebate' => 'agent_rebate',
  270. 'sub_agent_rebate' => 'sub_agent_rebate',
  271. 'benefit_type' => 'benefit_type',
  272. 'agent_benefit_type' => 'agent_benefit_type',
  273. 'agent_reward' => 'agent_reward',
  274. 'sub_agent_reward' => 'sub_agent_reward',
  275. 'promote_switch' => 'promote_switch'
  276. ];
  277. $_rdata = $this->getList($_map, $page, $order, $_field);
  278. $_datas = $_rdata['list'];
  279. $_game_logic = new GameLogic();
  280. $_game_field = [
  281. 'app_id',
  282. 'game_name',
  283. 'icon',
  284. 'size',
  285. 'down_url',
  286. 'classify'
  287. ];
  288. foreach ($_datas as $_k => $_v) {
  289. $_game_data = $_game_logic->getFieldDataByAppId($_v['app_id'], $agent_id, 0, $_game_field);
  290. $_datas[$_k] = array_merge($_game_data, $_v);
  291. }
  292. $_rdata['list'] = $_datas;
  293. return $_rdata;
  294. }
  295. /**
  296. * 获取渠道游戏列表
  297. *
  298. * @param int $agent_id 渠道id
  299. * @param array $where 搜索条件
  300. * @param string $page 列表个数
  301. * @param string $order 排序
  302. *
  303. * @return array ['count'=>0,'list'=>[]]
  304. */
  305. public function getAgentList($agent_id, $where = [], $page = '1,10', $order = '-id') {
  306. $where['agent_id'] = $agent_id;
  307. $where['status'] = GameConst::GAME_STATUS_ON;
  308. $where['is_delete'] = CommonConst::CONST_NOT_DELETE;
  309. $_map = $this->getWhere($where);
  310. $_field = [
  311. 'app_id' => 'app_id',
  312. 'agent_rate' => 'agent_rate',
  313. 'sub_agent_rate' => 'sub_agent_rate',
  314. 'agent_rebate' => 'agent_rebate',
  315. 'sub_agent_rebate' => 'sub_agent_rebate',
  316. 'benefit_type' => 'benefit_type',
  317. 'promote_switch' => 'promote_switch'
  318. ];
  319. $_rdata = $this->getList($_map, $page, $order, $_field);
  320. $_datas = $_rdata['list'];
  321. $_ag_model = new AgentGameModel();
  322. $_game_logic = new GameLogic();
  323. $_game_field = [
  324. 'app_id',
  325. 'game_name',
  326. 'icon',
  327. 'size',
  328. 'down_url',
  329. 'classify',
  330. 'share_url',
  331. ];
  332. foreach ($_datas as $_k => $_v) {
  333. $_game_data = $_game_logic->getFieldDataByAppId($_v['app_id'], $agent_id, 0, $_game_field);
  334. $_datas[$_k] = array_merge($_game_data, $_v);
  335. $_ag_data = $_ag_model->getInfoByAgentApp($agent_id, $_v['app_id']);
  336. $_datas[$_k]['ag_id'] = get_val($_ag_data, 'id', CommonConst::CONST_ZERO);
  337. $_datas[$_k]['pack_status'] = get_val($_ag_data, 'status', CommonConst::CONST_ZERO);
  338. $_datas[$_k]['down_url'] = huo_get_down_url(get_val($_ag_data, 'package_url', CommonConst::CONST_ZERO));
  339. }
  340. $_rdata['list'] = $_datas;
  341. return $_rdata;
  342. }
  343. }