activityser.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/9/26 0026
  6. * Time: 下午 6:28
  7. */
  8. class ActivitySer extends Mysqldb
  9. {
  10. private static $mysql;
  11. protected $redis = null;
  12. private static $appid = null;
  13. protected $game_db_name = 'gank_activites';
  14. protected $redis_prefix = 'gank_wechat_activites';
  15. protected $activity_reward_table = 'app_user_reward';
  16. protected $redis_prize_key = null;
  17. protected static $activity_user_table = null;
  18. protected static $activity_prize_dbname = 'wechat_prize_config';
  19. protected $login_table = 'receivelogin';
  20. protected static $consignee_table = 'wechat_user_consignee';
  21. protected static $virtual_item_prize_table = 'wechat_virtual_item_prize';
  22. protected static $table = null;
  23. public static $prize_info = null;
  24. protected static $wechat_stat_lottery_tab = 'wechat_stat_lottery_info';
  25. protected static $wechat_stat_webclick_tab = 'wechat_stat_web_click';
  26. const LIMIT_ONE = 1;
  27. const K_EXPICE_TIME = 3600;
  28. function __construct($appid = null)
  29. {
  30. log_message::info("初始 act sers ...");
  31. self::$appid = $appid;
  32. self::$activity_user_table = 'wechat_user';
  33. $this->redis_prize_key = "prize_info:wechat";
  34. $xredis_conf = array(
  35. 'redis_prefix' => $this->redis_prefix,
  36. 'k_expire_time' => self::K_EXPICE_TIME
  37. );
  38. $this->redis = new Xredis($xredis_conf);
  39. self::$mysql = new Mysqldb();
  40. self::$prize_info = $this->getCachePrizeInfo();
  41. self::$table = $this->game_db_name . '.' . self::$activity_user_table;
  42. }
  43. //wechat_sign_activity
  44. public function GetActivitySign($openId)
  45. {
  46. $sql = "SELECT id,openid,rule,create_at FROM wechat_sign_activity WHERE openid='" . $openId . "' limit 1 ";
  47. if (self::$mysql->query($sql)) {
  48. return self::$mysql->fetch_row();
  49. }
  50. return false;
  51. }
  52. public function SetActivitySign($openId, $ruleinfo)
  53. {
  54. $sql = "INSERT INTO wechat_sign_activity (openid,rule,create_at) VALUES('" . $openId . "','" . $ruleinfo . "',NOW())";
  55. if (self::$mysql->query($sql)) {
  56. error_log('1' . "\n", 3, 'D:\xampp\htdocs\wechat\log\wechat-log.txt');
  57. return true;
  58. }
  59. return false;
  60. }
  61. public function EditActivitySign($infostr, $id)
  62. {
  63. $up = self::$mysql->update2('wechat_sign_activity', ['rule' => $infostr], 'id=:id', array('id' => $id));
  64. if ($up) {
  65. return true;
  66. }
  67. return false;
  68. }
  69. /***
  70. * 礼包码变更
  71. */
  72. public function EditGiftCode($id)
  73. {
  74. $up = self::$mysql->update2('wechat_gift_code', ['status' => 1], 'id=:id', array('id' => $id));
  75. if ($up) {
  76. return true;
  77. }
  78. return false;
  79. }
  80. /**
  81. * 礼包码获取 1
  82. */
  83. public function GetGiftCode($type)
  84. {
  85. $sql = "SELECT type,giftcode,status FROM wechat_gift_code WHERE type=" . $type . " AND status=GIFTCODE_STATUS limit 1 ";
  86. error_log($sql . "\n", 3, 'D:\xampp\htdocs\wechat\log\wechat-sql-log.txt');
  87. if (self::$mysql->query($sql)) {
  88. return self::$mysql->fetch_row();
  89. }
  90. return false;
  91. }
  92. /**
  93. * 礼包码清理
  94. */
  95. public function ClearGiftCode($id)
  96. {
  97. $sql = "DELETE FROM wechat_gift_code WHERE id=$id";
  98. if (self::$mysql->query($sql)) {
  99. return true;
  100. }
  101. return false;
  102. }
  103. /***
  104. * @param type 对应不同类型获取不同的配置属性
  105. * 活动配置获取
  106. */
  107. public function ActivityConfigList($type = null)
  108. {
  109. $sql = "SELECT type,title,desc FROM wechat_activity_config";
  110. if (self::$mysql->query($sql)) {
  111. return self::$mysql->fetch_all();
  112. }
  113. return false;
  114. }
  115. /**
  116. * 设置活动cache
  117. * @param $key
  118. * @param $data
  119. */
  120. public function setConfigCache($key, $app_id, $data)
  121. {
  122. // get redis cache
  123. // if false get mysql data
  124. // and set redis
  125. // and return redis
  126. //$redis->hSet('paylog', $orderid, $payLogStr);
  127. $this->redis->hSet($key, $app_id, $data);
  128. }
  129. public function setRankingCache()
  130. {
  131. }
  132. /***
  133. * 后期联合 appid openid channelid
  134. * @param $openid
  135. * @return bool|null|string
  136. *
  137. */
  138. public function getCacheUserInfo($uid)
  139. {
  140. $userinfo = $this->redis->get(self::$activity_user_table . ":" . $uid);
  141. if ($userinfo) {
  142. $this->setCacheEX(self::$activity_user_table . ":" . $uid);
  143. return $userinfo;
  144. }
  145. log_message::info('cache user info is null ');
  146. return null;
  147. }
  148. /***
  149. * @param $key
  150. * @return bool
  151. */
  152. public function setCacheEX($key)
  153. {
  154. $ttl = $this->redis->ttl($key);
  155. if ($ttl < self::K_EXPICE_TIME) {
  156. return $this->redis->expire($key, self::K_EXPICE_TIME);
  157. }
  158. $this->redis->expire($key, self::K_EXPICE_TIME);
  159. }
  160. /***
  161. * @param $data
  162. * @return int
  163. */
  164. public function setCacheUserInfo($data)
  165. {
  166. $uid = $data['uid'];
  167. $data_str = json_encode($data, JSON_UNESCAPED_UNICODE);
  168. $ret = $this->redis->setex(self::$activity_user_table . ":" . $uid, self::K_EXPICE_TIME, $data_str);
  169. if (!$ret) {
  170. return log_message::info('set cache user info false');
  171. }
  172. return true;
  173. log_message::info('set cache user info true');
  174. }
  175. /***
  176. * 获取用户信息
  177. * @param $openid
  178. * @return array|bool
  179. */
  180. public function getdbUserInfo($uid,$gameid=null)
  181. {
  182. $field = "uid,gameid,respce_lottery_total,surplus_lottery_num,
  183. act_lottery_end_at,lottery_total,create_at,lucky_draw_total,lottery_up_limit,login_at";
  184. $where = " WHERE uid=:uid AND gameid=:gameid LIMIT " . self::LIMIT_ONE;
  185. $sql = "SELECT $field FROM " . self::$activity_user_table . $where;
  186. $prepare = array('uid' => $uid,'gameid'=>$gameid);
  187. if (self::$mysql->query($sql, $prepare) && self::$mysql->rowcount() > ZERO) {
  188. return self::$mysql->fetch_row();;
  189. }
  190. return false;
  191. }
  192. /**
  193. *
  194. */
  195. /*public function getUserPrizelotteryUplimit($uid)
  196. {
  197. $prepare = [
  198. 'uid' => $uid,
  199. 'type' => ONE,
  200. 'date' => date('Ymd', time())
  201. ];
  202. $were_str = 'WHERE ' . self::$mysql->formatSqlWhere($prepare);
  203. $sql = "SELECT count(*) as lottery_cnt FROM " . self::$consignee_table . $were_str;
  204. if (self::$mysql->query($sql, $prepare) && self::$mysql->rowcount() > ZERO) {
  205. return self::$mysql->fetch_row();;
  206. }
  207. return false;
  208. }*/
  209. /***
  210. * 修改用户数据
  211. * @param $data
  212. * @param $prepare_array
  213. * @return bool
  214. */
  215. public function updateUserInfo($updata, $get_prepare_array)
  216. {
  217. $where_str = self::$mysql->formatSqlWhere($get_prepare_array);
  218. $where_val = $get_prepare_array;
  219. $up_ret = self::$mysql->update2(self::$activity_user_table, $updata, $where_str, $where_val);
  220. if ($up_ret) {
  221. return true;
  222. log_message::info("update info ok");
  223. }
  224. log_message::info("update info false");
  225. return false;
  226. }
  227. public static function formatSqlWhere($data, $addition = null)
  228. {
  229. if (isDatas($data)) {
  230. $where = null;
  231. foreach ($data as $field_key => $field_val) {
  232. if (!empty($where)) {
  233. $where .= ' AND ' . $field_key . '=:' . $field_key;
  234. } else {
  235. $where .= $field_key . '=:' . $field_key;
  236. }
  237. }
  238. return $where . $addition;
  239. }
  240. return false;
  241. }
  242. /***
  243. * Save Userinfo
  244. * @param $data ['openid']
  245. * @return array|bool|mixed|null
  246. */
  247. public function saveUserInfo($data)
  248. {
  249. //$openid = isset($data['openid']) ? $data['openid'] : ZERO;
  250. $uid = isset($data['uid']) ? $data['uid'] : ZERO;
  251. $gameid = isset($data['gameid']) ? $data['gameid'] : ZERO;
  252. $user_info = $this->getCacheUserInfo($uid);
  253. if ($user_info) {
  254. log_message::info('return cache user info .');
  255. return Utils::decodeDate($user_info);
  256. }
  257. $db_user_info = $this->getdbUserInfo($uid,$gameid);
  258. // 如果db 也不存在那么 cache 与 redis 都有进行录入set
  259. if (empty($db_user_info)) {
  260. // 这个时候注意参数 $data 是客户端新用户第一次进来初始的参数
  261. if (!self::$mysql->insert(self::$table, $data)) {
  262. log_message::info('insert indo db saveuserinfo false');
  263. }
  264. if (!$this->setCacheUserInfo($data)) {
  265. log_message::info('insert indo cache saveuserinfo false');
  266. }
  267. return $data;
  268. }
  269. // 否则之前录入的redis 已经失效大db存在,这时候要重新设置下cache
  270. // 如果db 有的话直接set cache 的是 get db 的数据 $db_user_info
  271. if (!$this->setCacheUserInfo($db_user_info)) {
  272. log_message::info('insert indo cache saveuserinfo2 false');
  273. }
  274. return $db_user_info;
  275. }
  276. /***
  277. * 更新用户抽奖数据
  278. * @param $data
  279. * @param bool $lottery
  280. * @param bool $initial true 初始加载 default false
  281. * @return array|bool
  282. */
  283. public function updateUserlotteryInfo($data, $lottery = false, $lottery_up_limit = ZERO)
  284. {
  285. $uid = isset($data['uid']) ? $data['uid'] : ZERO;
  286. $gameid = isset($data['gameid']) ? $data['gameid'] : ZERO;
  287. // 登录次数总次数 累计
  288. $respce_lottery_total = isset($data['respce_lottery_total']) ? $data['respce_lottery_total'] : ZERO;
  289. /* $login_frequency = isset($data['login_frequency']) ? $data['login_frequency'] : ZERO;
  290. // 登录累计天数
  291. $login_successive_day = isset($data['login_successive_day']) ? $data['login_successive_day'] : ZERO;
  292. // 登录总天数
  293. $login_total_day = isset($data['login_total_day']) ? $data['login_total_day'] : ZERO;
  294. // 付费次数
  295. $pay_frequency = isset($data['pay_frequency']) ? $data['pay_frequency'] : ZERO;
  296. // 付费金额
  297. $pay_amount = isset($data['pay_amount']) ? $data['pay_amount'] : ZERO;*/
  298. // 抽奖的剩余次数为 付费金额 + 登录天数 - 抽奖次数
  299. // -- 符合条件的抽奖次数 减去 已经抽了的次数 = 剩余的次数
  300. if ($uid != '0366415e26930fa2f42dae66fce21f1b') {
  301. // respace
  302. $sql = "UPDATE " . self::$table . " SET
  303. respce_lottery_total = respce_lottery_total+($respce_lottery_total-respce_lottery_total),
  304. surplus_lottery_num = respce_lottery_total - lottery_total
  305. WHERE uid = " . $uid ." AND gameid=".$gameid;
  306. }
  307. if ($lottery == true) {
  308. $sql = "UPDATE " . self::$table . " SET surplus_lottery_num = surplus_lottery_num-1,
  309. lottery_total=lottery_total+1,lucky_draw_total=lucky_draw_total+1,lottery_up_limit=lottery_up_limit+" . $lottery_up_limit . "
  310. WHERE uid = " . $uid." AND gameid=".$gameid;;
  311. }
  312. $uid = isset($data['uid']) ? $data['uid'] : ZERO;
  313. //$ret = $this->mysql->update($this->table, $data, $where, array('openid' => $openid));
  314. if (!empty($sql)) {
  315. log_message::info("lottery", $sql);
  316. $ret = self::$mysql->query($sql);
  317. if ($ret->rowCount() > ZERO) {
  318. $data = $this->getdbUserInfo($uid,$gameid);
  319. // 重置缓存
  320. // $this->getdbUserInfo($openid);
  321. $this->setCacheUserInfo($data);
  322. //surplus_lottery_num 剩余次数
  323. return $data;
  324. }
  325. return false;
  326. }
  327. }
  328. /**
  329. * 获取登录次数 登录天数 连续登录天数
  330. * @param $appid
  331. * @param $channelid
  332. * @param $sid
  333. * @param $uid
  334. * @param $act_start_at
  335. * @param $act_end_at
  336. * @return array|bool
  337. */
  338. public function getOnlineNum($appid, $channelid, $sid, $uid, $act_start_at, $act_end_at)
  339. {
  340. $login_data = [];
  341. $dbconfg = Utils::config('game_stat_db');
  342. $appid = '6';
  343. $serverid = '378';
  344. $channelid = 'xcx1970';
  345. $who = '1000002587037899';
  346. $act_start_at = '2018-08-22 00:00:00';
  347. $act_end_at = '2018-10-22 23:59:59';
  348. $mysql = new Mysqldb($dbconfg);
  349. $field = "who,DATE(begdate) as login_at,COUNT(*) as login_frequency";
  350. $where = " WHERE who=:who AND begdate>=:act_start_at
  351. AND begdate<=:act_end_at AND appid=:appid
  352. AND channelid=:channelid AND serverid=:serverid ";
  353. $sql = "SELECT $field FROM (SELECT * FROM " .
  354. $this->login_table . $where . " ) as a GROUP BY DATE(a.begdate)";
  355. $prepare = array(
  356. 'who' => $who,
  357. 'appid' => $appid,
  358. 'channelid' => $channelid,
  359. 'act_start_at' => $act_start_at,
  360. 'act_end_at' => $act_end_at,
  361. 'serverid' => $serverid
  362. );
  363. if ($mysql->query($sql, $prepare) && $mysql->rowcount() > ZERO) {
  364. log_message::info('login 查询成功!!!!');
  365. $data = $mysql->fetch_all();
  366. log_message::info("6666666", json_encode($data));
  367. $login_day = [];
  368. $login_num = [];
  369. $login_successive_day = [];
  370. foreach ($data as $var) {
  371. $login_day[] = $var['login_at']; // 登录天数
  372. $login_num[] = $var['login_frequency']; // 没登录一次记一次
  373. }
  374. $login_data = [
  375. 'login_day' => count($login_day), // 累计登录天数
  376. 'login_frequency' => array_sum($login_num), // 登录次数
  377. 'login_successive_day' => count(Utils::successiveTime($login_day)), // 连续登录天数
  378. ];
  379. log_message::info(json_encode($login_data));
  380. return $login_data;
  381. }
  382. return false;
  383. }
  384. /***
  385. * 活动付费记录
  386. * @param $appid
  387. * @param $channelid
  388. * @param $sid
  389. * @param $uid
  390. * @param $act_start_at
  391. * @param $act_end_at
  392. * @return array|bool
  393. */
  394. public function getUserPayInfo($appid, $channelid, $sid, $uid, $act_start_at, $act_end_at)
  395. {
  396. log_message::info("get **************");
  397. $login_data = [];
  398. $dbconfg = Utils::config('game_stat_db');
  399. $appid = '6';
  400. $serverid = '378';
  401. $channelid = 'xcx1970';
  402. $who = '1000002587037899';
  403. $act_start_at = '2018-08-22 00:00:00';
  404. $act_end_at = '2018-10-22 23:59:59';
  405. // 每日 收充 + 1 count(disct begdate)
  406. $mysql = new Mysqldb($dbconfg);
  407. $field = "COUNT(*) paynum,SUM(currencyamount) as amount ";
  408. $where = " WHERE who=:who AND begdate>=:act_start_at
  409. AND begdate<=:act_end_at AND appid=:appid AND
  410. channelid=:channelid AND serverid=:serverid LIMIT " . self::LIMIT_ONE;
  411. $sql = "SELECT $field FROM todaypayment " . $where;
  412. $prepare = array(
  413. 'who' => $who,
  414. 'appid' => $appid,
  415. 'channelid' => $channelid,
  416. 'act_start_at' => $act_start_at,
  417. 'act_end_at' => $act_end_at,
  418. 'serverid' => $serverid
  419. );
  420. log_message::info("ddddddddddd");
  421. if ($mysql->query($sql, $prepare) && $mysql->rowcount() > ZERO) {
  422. log_message::info('pay 查询成功!!!!');
  423. $data = $mysql->fetch_row();
  424. log_message::info("&&&&&&JHHHHHHHHH", json_encode($data));
  425. $pay_frequency = ZERO;
  426. $pay_amount = ZERO;
  427. $pay_frequency = empty($data['pay_frequency']) ? ZERO : $data['pay_frequency'];
  428. $pay_amount = empty($data['pay_amount']) ? ZERO : $data['pay_amount'];
  429. $pay_data = [
  430. 'pay_frequency' => $pay_frequency,
  431. 'pay_amount' => $pay_amount,
  432. ];
  433. log_message::info("555555555555555555&&&&&&JHHHHHHHHH", json_encode($pay_data));
  434. return $pay_data;
  435. }
  436. return false;
  437. }
  438. //===========================================================================
  439. /***抽奖
  440. * 获取奖励db配置信息
  441. * @param $openid
  442. * @return array|bool
  443. */
  444. //===========================================================================
  445. public function getdbPrizeInfo()
  446. {
  447. $appid = self::$appid;
  448. //appid,prize_id,prize,rate,number,upper_limit,prize_type
  449. $field = "appid,prize_id,prize,rate,number,commodity,upper_limit,creat_at";
  450. $where = " WHERE appid=:appid";
  451. $sql = "SELECT $field FROM " . self::$activity_prize_dbname;
  452. $prepare = array('appid' => $appid);
  453. if (self::$mysql->query($sql, $prepare) && self::$mysql->rowcount() > ZERO) {
  454. return self::$mysql->fetch_all();
  455. }
  456. return false;
  457. }
  458. /***
  459. *
  460. */
  461. public function respcedbLottery()
  462. {
  463. }
  464. /***
  465. * @return array|bool|null
  466. */
  467. public function getCachePrizeInfo()
  468. {
  469. $prize_info = null;
  470. $data = null;
  471. $redis_data = null;
  472. $cache_data = null;
  473. $data = Utils::config('game_prize');
  474. // 直接覆盖重置数据
  475. $prize_info = $this->redis->hGetAll($this->redis_prize_key);
  476. if ($prize_info) {
  477. foreach ($prize_info as $key => $var) {
  478. $redis_data[] = json_decode($var, true);
  479. }
  480. $cache_data = Utils::prizeFormat($redis_data);
  481. log_message::info('getCachePrizeInfo is cache info ', json_encode($cache_data));
  482. return $cache_data;
  483. }
  484. $dbdata = $this->getdbPrizeInfo();
  485. if ($dbdata) {
  486. // grt db set cache
  487. $this->setCachePrizeInfo($dbdata);
  488. $dbdata = Utils::prizeFormat($dbdata);
  489. log_message::info('cache prize info is null ');
  490. return $dbdata;
  491. } else {
  492. // DB CACHE 都没有获取配置文件
  493. if ($data) {
  494. $data = Utils::prizeFormat($data);
  495. log_message::info("getCachePrizeInfo set db prize ");
  496. $this->respaceLottery($data);
  497. return $data;
  498. }
  499. }
  500. return false;
  501. }
  502. /***
  503. * @param $respace_data
  504. * @return bool
  505. */
  506. public function respaceLottery($respace_data)
  507. {
  508. if ($respace_data) {
  509. $res_db = $this->setdbPrizeInfo($respace_data);
  510. $res_cache = $this->setCachePrizeInfo($respace_data);
  511. if ($res_db && $res_cache) {
  512. log_message::info("更新 prize db 与 cache 成功");
  513. return $respace_data;
  514. }
  515. log_message::info("更新 prize db 与 cache 失败");
  516. return false;
  517. }
  518. log_message::info(" respace_data is null ");
  519. return false;
  520. }
  521. public function setdbPrizeInfo($data)
  522. {
  523. $prize_out = null;
  524. $fields = 'prize_id,prize,rate,number,upper_limit,prize_type,status';
  525. foreach ($data as $key => $var) {
  526. $prize_out[$key] = [
  527. "prize_id" => $var['prize_id'],
  528. "prize" => '"' . $var['prize'] . '"',
  529. "rate" => $var['rate'],
  530. "number" => $var['number'],
  531. "upper_limit" => $var['upper_limit'],
  532. "prize_type" => $var['prize_type'],
  533. "status" => $var['status'],// DEFAULT 0 1 实物需发货
  534. ];
  535. }
  536. if (isDatas($prize_out)) {
  537. if (self::$mysql->insertBatch(self::$activity_prize_dbname, $fields, $prize_out)) {
  538. return true;
  539. }
  540. log_message::info("setdbPrizeInfo false ");
  541. return false;
  542. }
  543. log_message::info("prize_out is null");
  544. return false;
  545. }
  546. /***
  547. * @param $data
  548. * @param null $appid
  549. * @return int
  550. */
  551. public function setCachePrizeInfo($data)
  552. {
  553. // 整理hmset 索规定的格式 并录入缓存 ..[key=>string],[key,string]
  554. // 后期同步db 后台可同步
  555. $data = Utils::prizeAssembly($data);
  556. // ..[key=>string],[key,string]
  557. $ret = $this->redis->hMset($this->redis_prize_key, $data);
  558. if (!$ret) {
  559. log_message::info('set cache prize info false');
  560. return false;
  561. }
  562. return true;
  563. }
  564. /***
  565. * 覆盖抽奖配置规则
  566. * @param $appid
  567. * @return bool|mixed|null
  568. */
  569. public function setLotteryFrequency($prize_id)
  570. {
  571. if ($this->redis->exists($this->redis_prize_key)) {
  572. $data = json_decode($this->redis->hGet($this->redis_prize_key, $prize_id), true);
  573. if (isDatas($data)) {
  574. $upper_limit = (int)$data['upper_limit'];
  575. $frequency = $data['frequency'];
  576. // 重置cache 物品抽奖的累计次数 递增 | frequency
  577. return $this->setUpperLimit($frequency, $upper_limit, $data);
  578. }
  579. log_message::info("setLotteryFrequency false");
  580. return false;
  581. }
  582. log_message::info("setLotteryFrequency false cache is null");
  583. return false;
  584. }
  585. /***
  586. * 重置cache 物品抽奖的累计次数
  587. * @param $role_upper_limit
  588. * @param $game_upper_limit
  589. * @param $data
  590. * @return bool|mixed|null
  591. */
  592. public function setUpperLimit($role_upper_limit, $game_upper_limit, $data)
  593. {
  594. log_message::info('-----------------', json_encode($data, JSON_UNESCAPED_UNICODE));
  595. $commodity = $data['status']; // 0 虚拟 1 实物
  596. $prize_id = $data['prize_id'];
  597. $prize_value = null;
  598. $isNayUpperLimit = self::isNayUpperLimit($role_upper_limit, $game_upper_limit);
  599. // 如果奖品抽取次数小于规定的限制次数
  600. if ($isNayUpperLimit) {
  601. $role_upper_limit += ONE;
  602. $data['frequency'] = $role_upper_limit;
  603. $prize_value = json_encode($data, JSON_UNESCAPED_UNICODE);
  604. log_message::info("########################", $prize_value);
  605. // 重置prize cache 次数
  606. log_message::info('^^^^^^^^^^^^^^^^^^^^^cishu', $prize_id, $prize_value);
  607. $this->redis->hSet($this->redis_prize_key, $prize_id, $prize_value);
  608. return self::prizeIsCommodity($data, $commodity);
  609. } else {
  610. log_message::info('set uo limit is nay InterventionLottery');
  611. // 如果抽奖达到上限则重新随机奖励
  612. return self::interventionLottery();
  613. }
  614. }
  615. /***
  616. * 抽到实物需要收货地址弹框相应
  617. * 此类型key 不会再 cache 里设置并返回,只允许代码里判断有效类型
  618. * @param $appid
  619. * @return bool|mixed|null
  620. */
  621. public static function prizeIsCommodity($data, $commodity)
  622. {
  623. $id = $data['prize_id'];
  624. $status = (int)$data['status'];
  625. $up_limit = $data['upper_limit'];
  626. if ($commodity == ONE) {
  627. $data['prize_type'] = (int)ONE;
  628. }
  629. if ($up_limit != ZERO && $status != ONE) {
  630. $ret = self::getdbVirtualItemPrize($id);
  631. log_message::info("******************", json_encode($ret, JSON_UNESCAPED_UNICODE));
  632. if ($ret) {
  633. $card_info = '卡密:' . $ret['password'];
  634. $data['prize_desc'] = $card_info;
  635. //$data['prize_desc'] = '卡号:'.$ret['account'].' 卡密:'.$ret['account'];
  636. $item_prize_id = $ret['id'];
  637. self::setdbVirtualItemPrize($item_prize_id);
  638. log_message::info("prizeIsCommodity VirtualItem data is true");
  639. } else {
  640. log_message::info("prizeIsCommodity VirtualItem data is pres");
  641. unset($data);
  642. /*foreach (self::$prize_info as $val) {
  643. if ($val['prize_id'] ==SIX){
  644. $data = $val;
  645. }
  646. }*/
  647. $data = self::$prize_info[SIX];
  648. }
  649. // 最后重置一个吧
  650. //
  651. log_message::info("prizeIsCommodity VirtualItem data is null");
  652. }
  653. // 如果虚拟判断是否卡密 对应 prize id 获取卡密配置信息
  654. return json_encode($data, JSON_UNESCAPED_UNICODE);
  655. }
  656. /***
  657. * 干预奖品信息 如果抽奖达到上限则重新随机奖励
  658. */
  659. public function interventionLottery($data = [])
  660. {
  661. $prize_all = self::$prize_info;
  662. foreach ($prize_all as $key => $val) {
  663. $arr[$val['prize_id']] = $val['rate'];
  664. }
  665. do {
  666. log_message::info("********************* prize_all ", json_encode($arr));
  667. $rid = Utils::getPrizeRand($arr);
  668. log_message::info("********************* rid ", $rid);
  669. //$pirze_lottery = $prize_all[$rid - ONE];
  670. $pirze_lottery = $prize_all[$rid];
  671. $game_upper_limit = (int)$pirze_lottery['upper_limit'];
  672. $role_upper_limit = (int)$pirze_lottery['frequency'];
  673. return $this->setLotteryFrequency($pirze_lottery['prize_id']);
  674. } while (Utils::isUpperlimit($role_upper_limit, $game_upper_limit));
  675. }
  676. /***
  677. * 获取
  678. * @param $id
  679. * @return bool|false|string
  680. */
  681. public function byPrizeidInfo($id = null, $all = false)
  682. {
  683. $data = $this->redis->hGet($this->redis_prize_key, $id);
  684. if ($all == true) {
  685. $data = $this->redis->hGetAll($this->redis_prize_key);
  686. }
  687. if (isDatas($data)) {
  688. return $data;
  689. }
  690. log_message::info("getPrizeidInfo is null");
  691. return false;
  692. }
  693. /***
  694. * 干预判断 如果 不是无限制的类型并且 game 上限大于或等于用户已抽取的次数
  695. * 表示已经达到上限符合干预条件需要系统重新随机奖品
  696. * @param $role_upper_limit
  697. * @param $game_upper_limit
  698. * @return bool
  699. */
  700. public static function isUpperlimit($role_upper_limit, $game_upper_limit)
  701. {
  702. if (($game_upper_limit != ZERO && $game_upper_limit >= $role_upper_limit)) {
  703. return true;
  704. }
  705. return false;
  706. }
  707. /****
  708. * 判断有效范围内可增加抽取次数并返回客户端
  709. * @param $role_upper_limit
  710. * @param $game_upper_limit
  711. * @return bool
  712. */
  713. public static function isNayUpperLimit($role_upper_limit, $game_upper_limit)
  714. {
  715. if ($game_upper_limit == ZERO || ($game_upper_limit != ZERO && $role_upper_limit < $game_upper_limit)) {
  716. return true;
  717. }
  718. return false;
  719. }
  720. /***
  721. * 重置上限次数
  722. */
  723. public function replaceUpperLimit()
  724. {
  725. $where_data = ['appid' => self::$appid];
  726. $edit_data = ['frequency' => ZERO];
  727. $where_str = self::$mysql->formatSqlWhere($where_data);
  728. $up = self::$mysql->update2(self::$activity_prize_dbname, $edit_data, $where_str, $where_data);
  729. if ($up) {
  730. log_message::info('抽奖上限次数重置成功!');
  731. return true;
  732. }
  733. log_message::info('抽奖上限次数重置失败!');
  734. return false;
  735. }
  736. public function getdbLotteryInfo()
  737. {
  738. }
  739. // --------------------------------------------------------------------
  740. // 抽奖活动用户信息,用户抽取次数
  741. // --------------------------------------------------------------------
  742. /***
  743. * @param $appid
  744. * @param $channelid
  745. * @param $uid
  746. * @param $act_start_at
  747. * @param $act_end_at
  748. * @param $openid
  749. * @return array|bool
  750. */
  751. public function getdbLotteryTotal($appid, $channelid, $uid, $act_start_at, $act_end_at, $openid)
  752. {
  753. $dbconfg = Utils::config('game_stat_db');
  754. $mysql = new Mysqldb($dbconfg);
  755. $field = " COUNT(*) as cont_frequency,COUNT(DISTINCT DATE(begdate)) cont_day";
  756. $where = " WHERE who=:who appid=:appid AND channelid=:channelid AND begdate>=:start_at
  757. AND begdate<=:end_at LIMIT" . self::LIMIT_ONE;
  758. $sql = "SELECT $field FROM " . $this->login_table . $where;
  759. $prepare = array(
  760. 'who' => $uid,
  761. 'appid' => $appid,
  762. 'channelid' => $channelid,
  763. 'start_at' => $act_start_at,
  764. 'end_at' => $act_end_at,
  765. );
  766. if ($mysql->query($sql, $prepare) && $mysql->rowcount() > ZERO) {
  767. return $mysql->fetch_row();
  768. }
  769. return false;
  770. }
  771. /***
  772. * 设置用户收货地址
  773. * @param $data
  774. * @return bool
  775. */
  776. public static function savePirzeConsignee($data)
  777. {
  778. /*$addItion = " ON DUPLICATE KEY UPDATE
  779. type=1,
  780. consignee_address ={$data['consignee_address']} ,
  781. consignee_name = {$data['consignee_name']},
  782. consignee_phone={$data['consignee_phone']},";*/
  783. $ret = self::$mysql->insert(self::$consignee_table, $data);
  784. if ($ret) {
  785. return true;
  786. }
  787. return false;
  788. }
  789. /***
  790. * @param $data
  791. * @return bool
  792. */
  793. public static function editPirzeConsignee($data)
  794. {
  795. $uid = $data['uid'];
  796. $gameid = $data['gameid'];
  797. $prize_order = $data['prize_order'];
  798. $type = $data['type'];
  799. $where_data = [
  800. 'uid' => $uid,
  801. 'gameid' => $gameid,
  802. 'prize_order' => $prize_order
  803. ];
  804. $where_str = self::$mysql->formatSqlWhere($where_data);
  805. $edit_data = [
  806. 'consignee_address' => $data['consignee_address'],
  807. 'consignee_phone' => $data['consignee_phone'],
  808. 'type' => $type
  809. ];
  810. $up = self::$mysql->update2(self::$consignee_table, $edit_data, $where_str, $where_data);
  811. if ($up) {
  812. log_message::info('地址更新成功!');
  813. return true;
  814. }
  815. log_message::info('地址更新失败!');
  816. return false;
  817. }
  818. /***
  819. * 获取用户抽奖记录
  820. */
  821. public function userPrizeInfo($uid,$gameid=null)
  822. {
  823. $sql = " SELECT * FROM " . self::$consignee_table . "
  824. WHERE uid=:uid AND gameid=:gameid";
  825. $prepare = array(
  826. 'uid' => $uid,
  827. 'gameid' => $gameid,
  828. );
  829. if (self::$mysql->query($sql, $prepare) && self::$mysql->rowcount() > ZERO) {
  830. return self::$mysql->fetch_all();
  831. }
  832. return false;
  833. }
  834. /***
  835. * 获取用户抽奖记录
  836. */
  837. public function byuserPrizeOrderInfo($uid,$gameid, $prize_order)
  838. {
  839. $sql = " SELECT * FROM " . self::$consignee_table . "
  840. WHERE uid=:uid AND gameid =:gameid AND prize_order=:prize_order";
  841. $prepare = array(
  842. 'uid' => $uid,
  843. 'gameid' => $gameid,
  844. 'prize_order' => $prize_order
  845. );
  846. if (self::$mysql->query($sql, $prepare) && self::$mysql->rowcount() > ZERO) {
  847. return self::$mysql->fetch_row();
  848. }
  849. return false;
  850. }
  851. /**
  852. * @param $where_data
  853. * @return bool|mixe
  854. */
  855. public static function globalConfig($where_data)
  856. {
  857. $sql = "SELECT * FROM tb_stat_back_ranking ";
  858. $addition = 'ORDER BY id desc limit 1';
  859. $sql .= ' WHERE ' . self::$mysql->formatSqlWhere($where_data, $addition);
  860. if (self::$mysql->query($sql, $where_data) && self::$mysql->rowcount() > ZERO) {
  861. return self::$mysql->fetch_row();
  862. log_message::info("globalconfig true");
  863. }
  864. log_message::info("globalconfig false", $sql);
  865. return false;
  866. }
  867. public function getRankingList($data)
  868. {
  869. $sql = "select * from WHERE ";
  870. //self::$mysql-
  871. }
  872. // get item acccount info
  873. /***
  874. * @param $itemid
  875. * @return array|bool
  876. */
  877. public static function getdbVirtualItemPrize($itemid)
  878. {
  879. // AND status=:status
  880. $sql = " SELECT id,account,password,`desc`,itemid FROM " . self::$virtual_item_prize_table . "
  881. WHERE itemid=:itemid AND status=:status LIMIT " . self::LIMIT_ONE;
  882. $prepare = array(
  883. 'itemid' => $itemid,
  884. 'status' => ZERO, // 0待领取 1 已领取 获取new prize itme
  885. );
  886. if (self::$mysql->query($sql, $prepare) && self::$mysql->rowcount() > ZERO) {
  887. return self::$mysql->fetch_row();
  888. }
  889. return false;
  890. }
  891. /***
  892. * @param $itemid
  893. * @return bool
  894. */
  895. public static function setdbVirtualItemPrize($id)
  896. {
  897. $where_data = ['id' => $id];
  898. $edit_data = ['status' => ONE];
  899. $where_str = self::$mysql->formatSqlWhere($where_data);
  900. $up = self::$mysql->update2(self::$virtual_item_prize_table, $edit_data, $where_str, $where_data);
  901. if ($up) {
  902. log_message::info('重置虚拟配置成功!');
  903. return true;
  904. }
  905. log_message::info('重置虚拟配置失败!');
  906. return false;
  907. }
  908. // 统计 抽奖信息
  909. public function setLotteryLog($data)
  910. {
  911. $ret = self::$mysql->insert(self::$wechat_stat_lottery_tab, $data);
  912. if ($ret) {
  913. return true;
  914. }
  915. return false;
  916. }
  917. // 统计 页面点击
  918. public function setWebClickLog($data)
  919. {
  920. $ret = self::$mysql->insert(self::$wechat_stat_webclick_tab, $data);
  921. if ($ret) {
  922. return true;
  923. }
  924. return false;
  925. }
  926. }