AgentGame.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * AgentGame.php UTF-8
  4. * 渠道游戏
  5. *
  6. * @date : 2018/4/26 14:52
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\agent;
  13. use huo\controller\common\Base;
  14. use huo\controller\game\GameCache;
  15. use huo\controller\game\Gamepack;
  16. use huo\controller\rate\Rate;
  17. use huo\controller\rate\RateCache;
  18. use huo\controller\rate\RateCheck;
  19. use huo\model\agent\AgentGameModel;
  20. use huo\model\rate\AgentGameRateModel;
  21. use huolib\constant\AgentConst;
  22. use huolib\constant\CommonConst;
  23. use huolib\constant\GameConst;
  24. use huolib\status\AgentStatus;
  25. use huolib\status\GameStatus;
  26. use huolib\status\RateStatus;
  27. use think\Log;
  28. class AgentGame extends Base {
  29. /**
  30. * 添加渠道游戏
  31. *
  32. * @param $agent_id
  33. * @param mixed $app_ids
  34. *
  35. * @return array|bool
  36. */
  37. public function addAgentGames($agent_id, $app_ids) {
  38. if (empty($app_ids)) {
  39. $_code = GameStatus::GAME_ID_EMPTY;
  40. return $this->huoError($_code, GameStatus::getMsg($_code));
  41. }
  42. $_app_ids = $app_ids;
  43. if (is_numeric($app_ids)) {
  44. $_app_ids = [$app_ids];
  45. }
  46. $_rs = false;
  47. foreach ($_app_ids as $_app_id) {
  48. $_rs = $this->addGame($agent_id, $_app_id);
  49. }
  50. if (false == $_rs) {
  51. $_code = AgentStatus::ADD_GAME_ERROR;
  52. return $this->huoError($_code, AgentStatus::getMsg($_code));
  53. }
  54. $_code = AgentStatus::NO_ERROR;
  55. return $this->huoError($_code, AgentStatus::getMsg($_code));
  56. }
  57. /**
  58. * 添加渠道游戏
  59. *
  60. * @param $agent_id
  61. * @param $app_id
  62. *
  63. * @param bool $set_qr_code
  64. *
  65. * @return bool
  66. */
  67. public function addGame($agent_id, $app_id, $set_qr_code = true) {
  68. $_game_info = GameCache::ins()->getInfoByAppId($app_id);
  69. if (empty($_game_info)) {
  70. return false;
  71. }
  72. $_ag_model = new AgentGameModel();
  73. $_ag_info = $_ag_model->getInfoByAgentIdAppId($agent_id, $app_id);
  74. if (!empty($_ag_info)) {
  75. if ($_ag_info['is_delete'] == CommonConst::CONST_DELETED) {
  76. //已被删除,则设置为正常
  77. $_ag_info['is_delete'] = CommonConst::CONST_NOT_DELETE;
  78. $_ag_info['delete_time'] = 0;
  79. $_ag_model->updateData($_ag_info, $_ag_info['id']);
  80. }
  81. //已经添加过该游戏,直接返回
  82. return true;
  83. }
  84. $_ag_data['agent_id'] = $agent_id;
  85. $_ag_data['app_id'] = $app_id;
  86. $_ag_data['agent_game'] = $_game_info['en_abbr']."_".$agent_id;
  87. $_ag_data['status'] = GameConst::AG_STATUES_SUC;
  88. if ($_game_info['classify'] == GameConst::GAME_MP) {
  89. $_ag_data['status'] = GameConst::AG_STATUES_SUC;
  90. }
  91. // if (GameConst::GAME_PROMOTE_SWITCH_CHECK == $_game_info['promote_switch']) {
  92. // $_ag_data['status'] = GameConst::AG_STATUES_CHECK;
  93. // }
  94. $_ag_id = $_ag_model->addData($_ag_data);
  95. if (empty($_ag_id)) {
  96. return false;
  97. }
  98. if ($set_qr_code == true) {
  99. /* 小程序或者小游戏 直接生成推广二维码 */
  100. (new \huomp\controller\agent\AgentGame())->setQrUrl($_ag_id);
  101. }
  102. return $this->addAgentGameRate($_ag_id, $app_id, $agent_id);
  103. }
  104. /**
  105. * 添加渠道游戏优惠信息
  106. *
  107. * @param $ag_id
  108. * @param $app_id
  109. * @param $agent_id
  110. *
  111. * @return bool
  112. */
  113. public function addAgentGameRate($ag_id, $app_id, $agent_id) {
  114. /* 添加渠道游戏折扣 */
  115. $_agr_data['ag_id'] = $ag_id;
  116. $_agr_data['agent_id'] = $agent_id;
  117. $_agr_data['app_id'] = $app_id;
  118. $_agent_class = new Agent();
  119. $_role_type = $_agent_class->getRoleType($agent_id);
  120. $_parent_id = $_agent_class->getParentId($agent_id);
  121. $_rc_class = RateCache::ins();
  122. if (AgentConst::ROLE_TYPE_GROUP == $_role_type) {
  123. $_rate_data = $_rc_class->getInfoByAppId($app_id);
  124. $_agr_data['agent_rebate'] = $_rate_data['agent_rebate'];
  125. $_agr_data['sub_agent_rebate'] = $_rate_data['sub_agent_rebate'];
  126. $_agr_data['agent_rate'] = $_rate_data['agent_rate'];
  127. $_agr_data['sub_agent_rate'] = $_rate_data['sub_agent_rate'];
  128. $_agr_data['agent_reward'] = $_rate_data['agent_reward'];
  129. $_agr_data['sub_agent_reward'] = $_rate_data['sub_agent_reward'];
  130. } elseif (AgentConst::ROLE_TYPE_AGENT == $_role_type) {
  131. $_rate_data = $_rc_class->getInfoByAppIdAgentId($app_id, $_parent_id);
  132. if (empty($_rate_data)) {
  133. $_rate_data = $_rc_class->getInfoByAppId($app_id);
  134. }
  135. $_agr_data['agent_rebate'] = $_rate_data['sub_agent_rebate'];
  136. $_agr_data['sub_agent_rebate'] = 0;
  137. $_agr_data['agent_rate'] = $_rate_data['sub_agent_rate'];
  138. $_agr_data['sub_agent_rate'] = 1;
  139. $_agr_data['agent_reward'] = $_rate_data['sub_agent_reward'];
  140. $_agr_data['sub_agent_reward'] = 0;
  141. } else {
  142. if (AgentConst::ROLE_TYPE_MEMBER == $_role_type) {
  143. $_agent_info = (new AgentCache())->getInfoByAgentId($agent_id);
  144. if (AgentConst::AGENT_ROLE_MP_MEMBER == $_agent_info['role_id']) {
  145. //小游戏玩家渠道
  146. $_rate_data = $_rc_class->getInfoByAppIdAgentId($app_id, $_parent_id);
  147. if (empty($_rate_data)) {
  148. $_rate_data = $_rc_class->getInfoByAppId($app_id);
  149. }
  150. $_agr_data['agent_rebate'] = $_rate_data['agent_rebate'];
  151. $_agr_data['sub_agent_rebate'] = $_rate_data['sub_agent_rebate'];
  152. $_agr_data['agent_rate'] = $_rate_data['agent_rate'];
  153. $_agr_data['sub_agent_rate'] = $_rate_data['sub_agent_rate'];
  154. $_agr_data['mem_agent_reward'] = $_rate_data['mem_agent_reward'];
  155. $_agr_data['mem_reward'] = $_rate_data['mem_reward'];
  156. } else {
  157. return false;
  158. }
  159. } else {
  160. return false;
  161. }
  162. }
  163. $_agr_data['benefit_type'] = $_rate_data['benefit_type'];
  164. $_agr_data['agent_benefit_type'] = $_rate_data['agent_benefit_type'];
  165. $_agr_data['mem_rate'] = $_rate_data['mem_rate'];
  166. $_agr_data['first_mem_rate'] = $_rate_data['first_mem_rate'];
  167. $_agr_data['mem_rebate'] = $_rate_data['mem_rebate'];
  168. $_agr_data['mem_rebate'] = $_rate_data['mem_rebate'];
  169. $_agr_data['first_mem_rebate'] = $_rate_data['first_mem_rebate'];
  170. $_rs = (new AgentGameRateModel())->addData($_agr_data);
  171. if (empty($_rs)) {
  172. return false;
  173. }
  174. return true;
  175. }
  176. /**
  177. * 编辑渠道优惠
  178. *
  179. * @param $ag_id
  180. * @param $sub_agent_rate
  181. * @param $sub_agent_rebate
  182. * @param $mem_first
  183. * @param $mem_refill
  184. *
  185. * @return array
  186. */
  187. public function editAgRate($ag_id, $sub_agent_rate, $sub_agent_rebate, $mem_first, $mem_refill) {
  188. if (empty($ag_id)) {
  189. $_code = RateStatus::INVALID_PARAMS;
  190. return $this->huoError($_code, RateStatus::getMsg($_code));
  191. }
  192. $_agr_data['sub_agent_rate'] = $sub_agent_rate;
  193. $_agr_data['sub_agent_rebate'] = $sub_agent_rebate;
  194. $_ac_class = AgentCache::ins();
  195. $_app_id = $_ac_class->getAppIdByAgId($ag_id);
  196. $_rate_class = new Rate($_app_id);
  197. $_benefit_type = $_rate_class->getBenefitType();
  198. if (GameConst::RATE_BENEFIT_RATE == $_benefit_type) {
  199. $_agr_data['first_mem_rebate'] = 0;
  200. $_agr_data['mem_rebate'] = 0;
  201. $_agr_data['first_mem_rate'] = $mem_first;
  202. $_agr_data['mem_rate'] = $mem_refill;
  203. } elseif (GameConst::RATE_BENEFIT_REBATE == $_benefit_type) {
  204. $_agr_data['first_mem_rebate'] = $mem_first;
  205. $_agr_data['mem_rebate'] = $mem_refill;
  206. $_agr_data['first_mem_rate'] = 1;
  207. $_agr_data['mem_rate'] = 1;
  208. }
  209. return $this->setGameRate($ag_id, $_agr_data);
  210. }
  211. /**
  212. * 编辑下级渠道折扣
  213. *
  214. * @param $ag_id
  215. * @param $agent_rate
  216. * @param $agent_rebate
  217. * @param $mem_first
  218. * @param $mem_refill
  219. *
  220. * @return array
  221. */
  222. public function editSubAgRate($ag_id, $agent_rate, $agent_rebate, $mem_first, $mem_refill) {
  223. if (empty($ag_id)) {
  224. $_code = RateStatus::INVALID_PARAMS;
  225. return $this->huoError($_code, RateStatus::getMsg($_code));
  226. }
  227. $_agr_data['agent_rate'] = $agent_rate;
  228. $_agr_data['agent_rebate'] = $agent_rebate;
  229. $_ac_class = AgentCache::ins();
  230. $_app_id = $_ac_class->getAppIdByAgId($ag_id);
  231. $_rate_class = new Rate($_app_id);
  232. $_benefit_type = $_rate_class->getBenefitType();
  233. if (GameConst::RATE_BENEFIT_RATE == $_benefit_type) {
  234. $_agr_data['first_mem_rebate'] = 0;
  235. $_agr_data['mem_rebate'] = 0;
  236. $_agr_data['first_mem_rate'] = $mem_first;
  237. $_agr_data['mem_rate'] = $mem_refill;
  238. } elseif (GameConst::RATE_BENEFIT_REBATE == $_benefit_type) {
  239. $_agr_data['first_mem_rebate'] = $mem_first;
  240. $_agr_data['mem_rebate'] = $mem_refill;
  241. $_agr_data['first_mem_rate'] = 1;
  242. $_agr_data['mem_rate'] = 1;
  243. }
  244. return $this->setGameRate($ag_id, $_agr_data);
  245. }
  246. /**
  247. * 设置游戏渠道优惠
  248. *
  249. * @param $ag_id
  250. * @param $rate_data
  251. *
  252. * @return array
  253. */
  254. public function setGameRate($ag_id, $rate_data) {
  255. if (empty($ag_id)) {
  256. $_code = RateStatus::INVALID_PARAMS;
  257. return $this->huoError($_code, RateStatus::getMsg($_code));
  258. }
  259. $_ac_class = AgentCache::ins();
  260. $_agent_id = $_ac_class->getAgentIdByAgId($ag_id);
  261. $_app_id = $_ac_class->getAppIdByAgId($ag_id);
  262. $_role_type = (new Agent())->getRoleType($_agent_id);
  263. $_rchk_class = new RateCheck();
  264. if (AgentConst::ROLE_TYPE_AGENT == $_role_type) {
  265. $_rchk_rs = $_rchk_class->checkAgentParam($_app_id, $_agent_id, $rate_data);
  266. } else {
  267. $_rchk_rs = $_rchk_class->checkGroupParam($_app_id, $_agent_id, $rate_data);
  268. }
  269. if (RateStatus::NO_ERROR != $_rchk_rs['code']) {
  270. return $this->huoReturn($_rchk_rs);
  271. }
  272. $_rc_class = RateCache::ins();
  273. $_agr_data = $_rc_class->getInfoByAppIdAgentId($_app_id, $_agent_id);
  274. $_agr_data = array_merge($_agr_data, $rate_data);
  275. $_rs = $_rc_class->updateAgRate($_app_id, $_agent_id, $_agr_data);
  276. if (false == $_rs) {
  277. $_code = RateStatus::CACHE_ERROR;
  278. return $this->huoError($_code, RateStatus::getMsg($_code));
  279. }
  280. $_code = RateStatus::NO_ERROR;
  281. return $this->huoSuccess($_code, RateStatus::getMsg($_code));
  282. }
  283. /**
  284. * 渠道游戏分包
  285. *
  286. * @param $agent_id
  287. * @param $ag_id
  288. *
  289. * @return array
  290. */
  291. public function pack($agent_id, $ag_id) {
  292. $_ag_cache = AgentCache::ins();
  293. $_ag_data = $_ag_cache->getAgInfoByAgId($agent_id);
  294. $_check_status = $_ag_data['status'];
  295. if (GameConst::AG_STATUES_CHECK == $_check_status) {
  296. $_code = GameStatus::GAME_PENDING_REVIEW;
  297. return $this->huoSuccess($_code, RateStatus::getMsg($_code));
  298. }
  299. $ag_info = $_ag_cache->getAgInfoByAgId($ag_id);
  300. if (empty($ag_info)) {
  301. $_code = GameStatus::GAME_NOT_EXISTS;
  302. return $this->huoSuccess($_code, RateStatus::getMsg($_code));
  303. }
  304. $game_info = (new GameCache())->getInfoByAppId($ag_info['app_id']);
  305. $_game_package = new Gamepack($ag_info['app_id']);
  306. $_game_package->setAgentId($agent_id);
  307. $_game_package->setPinyin($game_info['en_abbr']);
  308. $_game_package->setAgentgame($ag_info['agent_game']);
  309. $_game_package->setImageUrl($game_info['icon']);
  310. $result = $_game_package->pack();
  311. if (GameConst::GAME_PACK_SUCCESS == $result['code']) {
  312. $_ag_data = [];
  313. $_ag_data['status'] = GameConst::AG_STATUES_SUC;
  314. $_ag_data['package_url'] = $result['data']['url'];
  315. (new AgentCache())->updateAg($ag_id, $_ag_data);
  316. $result['code'] = GameStatus::NO_ERROR;
  317. } else {
  318. Log::write($result, Log::ERROR);
  319. $result['code'] = GameStatus::GAME_PACK_ERROR;
  320. }
  321. return $result;
  322. }
  323. public function getDownUrl($app_id, $agent_id) {
  324. }
  325. /**
  326. * 获取渠道的所有游戏id
  327. *
  328. * @param $agent_id
  329. *
  330. * @return array
  331. */
  332. public function getGameIds($agent_id) {
  333. $_map = $agent_id;
  334. if (!is_array($agent_id)) {
  335. $_map = ['agent_id' => $agent_id];
  336. }
  337. $_ids = (new AgentGameModel())->where($_map)->column('app_id');
  338. return $_ids;
  339. }
  340. }