Lottery.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. namespace Cas\Module;
  3. use KIF\Core\Request;
  4. use KIF\Data\Convert;
  5. use KIF\Data\ResultWrapper;
  6. use KIF\Db\Transaction;
  7. use KIF\Cache\Memcached;
  8. use Cas\Dao\LotteryEvents;
  9. use Cas\Dao\LotteryPrize;
  10. use Cas\Dao\LotteryPrizeVirtualData;
  11. use Cas\Dao\LotteryData;
  12. use Cas\Dao\Platform;
  13. /**
  14. * 抽奖模型
  15. *
  16. * @author lihuanchun
  17. *
  18. */
  19. class Lottery {
  20. private $objDLotteryData;
  21. private $objDLotteryEvents;
  22. private $objDLotteryPrize;
  23. private $objDLotteryPrizeVirtualData;
  24. private $objMLotteryEventsLimitations;
  25. private $objMemcached;
  26. private $runTime ;
  27. private $objRank ;
  28. /**
  29. * 初始化
  30. */
  31. public function __construct() {
  32. $this->objDLotteryEvents = new LotteryEvents (); // 活动
  33. $this->objDLotteryPrize = new LotteryPrize (); // 奖项
  34. $this->objDLotteryPrizeVirtualData = new LotteryPrizeVirtualData (); // 奖池数据
  35. $this->objDLotteryData = new LotteryData (); // 获奖数据
  36. $this->objMemcached = new Memcached ();
  37. $this->objMLotteryEventsLimitations = new LotteryEventsLimitations();
  38. $this->runTime = time();
  39. $this->objRank = new Rank();
  40. }
  41. public function setRunTime($time){
  42. return $this->runTime=$time;
  43. }
  44. /**
  45. * 临时:抽奖
  46. */
  47. public function tmpWantToDraw($eventsId,$uid){
  48. $objLotteryDraw = new LotteryDraw($uid, $eventsId);
  49. return $objLotteryDraw-> verifyPrize();
  50. }
  51. /**
  52. * 获取抽奖锁 cache key
  53. * @return string
  54. */
  55. public function getCacheKeyOfLock($prize_id) {
  56. return "lottery_draw_lock_{$prize_id}";
  57. }
  58. /**
  59. * 开启抽奖锁
  60. * @return boolean | null
  61. */
  62. public function lock($prize_id) {
  63. return $this->objMemcached->add($this->getCacheKeyOfLock($prize_id), true, 10);
  64. }
  65. /**
  66. * 关闭抽奖锁
  67. * @return boolean | null
  68. */
  69. public function unlock($prize_id) {
  70. return $this->objMemcached->delete($this->getCacheKeyOfLock($prize_id));
  71. }
  72. /**
  73. * 检查是否是百分百中奖名单中的用户
  74. */
  75. public function ckHundredPercentList($prize_id,$uid){
  76. // 查看百分百中奖名单中的用户
  77. $hundred_percent_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_hundred_percent_list');
  78. if(!empty($hundred_percent_list)){
  79. if(isset($hundred_percent_list[$uid])){
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. /**
  86. * 检查白名单 & 黑名单
  87. */
  88. public function ckBlackAndWhiteList($prize_id,$uid){
  89. // 查看黑名单
  90. $black_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_black_list');
  91. if(!empty($black_list)){
  92. if(isset($black_list[$uid])){
  93. return false;
  94. }
  95. }
  96. // 查看白名单
  97. $white_list = $this->objMemcached -> get('lottery_prize_'.$prize_id.'_white_list');
  98. if(!empty($white_list)){
  99. if(!isset($white_list[$uid])){
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105. /**
  106. * 添加活动
  107. */
  108. public function addLotteryEvents($info) {
  109. if($info['type']==LotteryEvents::TYPE_EVENTS_NULL){
  110. return false;
  111. }
  112. if($info['type']==LotteryEvents::TYPE_EVENTS_TRY){
  113. $info['participation_user_all_num'] = 1;
  114. }
  115. if($info['type']==LotteryEvents::TYPE_EVENTS_SURVEY){
  116. $info['participation_user_all_num'] = 1;
  117. }
  118. if($info['type']==LotteryEvents::TYPE_EVENTS_INVITATION){
  119. $info['participation_user_all_num'] = 1;
  120. $info['participation_user_max_num'] = 0;//最大参与人数 0 不限制
  121. }
  122. if($info['type']==LotteryEvents::TYPE_EVENTS_CODE){
  123. $info['two_dimensional_code'] = 2;//默认不以二维码输出
  124. }
  125. $info['display'] = LotteryEvents::EVENT_DISPLAY_UNFINISHED; // 默认添加操作 显示未设置完成
  126. if($info['type']==LotteryEvents::TYPE_EVENTS_OTHER){
  127. $info['display'] = LotteryEvents::EVENT_DISPLAY_HIDE; // 当如果是其他类型 默认添加时改成 未发布(停用)
  128. }
  129. return $this->objDLotteryEvents->add ( $info );
  130. }
  131. /**
  132. * 获取活动信息
  133. */
  134. public function getOneLotteryEvents($events_id) {
  135. $events_Data = $this->objDLotteryEvents->get ( $events_id );
  136. if(isset($events_Data ['forms'])){
  137. foreach ( $events_Data ['forms'] as $tmpkey => $tmpval ) {
  138. if ($tmpval ['type'] == 'select') {
  139. // $events_Data ['forms'] [$tmpkey] ['options'] = implode ( "\n", $tmpval ['options'] );
  140. }
  141. }
  142. }
  143. return $events_Data;
  144. }
  145. /**
  146. * 获取奖项信息
  147. */
  148. public function getOneLotteryPrize($prize_id) {
  149. return $this->objDLotteryPrize->get ( $prize_id );
  150. }
  151. /**
  152. * 获取活动当前参与人数
  153. */
  154. public function getParticipationUserNum($events_id){
  155. return $this->objDLotteryData->totals(array('events_id' =>$events_id ));
  156. }
  157. /**
  158. * 获取活动列表
  159. */
  160. public function getLotteryEventsList($condition, $limit, $order){
  161. $ids = $this->objDLotteryEvents->findIdsBy($condition, $limit, $order);
  162. $events = $this->objDLotteryEvents-> gets($ids);
  163. foreach($events as $key => $data){
  164. $events[$key]['url'] = $this->getEventsUrl($data['type'],$data['create_time']);
  165. }
  166. return $events;
  167. }
  168. /**
  169. * 获取获奖数据
  170. */
  171. public function getLotteryDataList($condition, $limit = null, $order){
  172. $ids = $this->objDLotteryData->findIdsBy($condition, $limit, $order);
  173. return $this->objDLotteryData-> gets($ids);
  174. }
  175. /**
  176. * 获取获奖数据
  177. */
  178. public function getOnesLotteryData($condition){
  179. $id = $this->objDLotteryData->findIdsBy($condition);
  180. if ($id && isset($id[0])) {
  181. return $this->objDLotteryData-> get($id[0]);
  182. }
  183. return array();
  184. }
  185. /**
  186. * 获取一条获奖数据
  187. */
  188. public function getOneLotteryData($lottery_data_id){
  189. return $this->objDLotteryData->get ( $lottery_data_id );
  190. }
  191. /**
  192. * 通过IDs获取多条获奖数据
  193. */
  194. public function getLotteryDatas($lottery_data_ids){
  195. return $this->objDLotteryData->gets ( $lottery_data_ids );
  196. }
  197. /**
  198. * 修改获奖数据
  199. */
  200. public function upOneLetteryData($info,$condition){
  201. return $this->objDLotteryData->modify($info, $condition);
  202. }
  203. /**
  204. * 获取中奖数据【条数】
  205. */
  206. public function getLotteryDataListNum($condition){
  207. return $this->objDLotteryData->totals($condition);
  208. }
  209. /**
  210. * 获取活动总条数
  211. */
  212. public function getLotteryEventsListNum($condition){
  213. return $this->objDLotteryEvents->totals($condition);
  214. }
  215. /**
  216. * 获取虚拟数据
  217. */
  218. public function getLotteryPrizeVirtualDataList($condition, $limit, $order){
  219. $ids = $this->objDLotteryPrizeVirtualData->findIdsBy($condition, $limit, $order);
  220. return $this->objDLotteryPrizeVirtualData-> gets($ids);
  221. }
  222. /**
  223. * 删除虚拟数据
  224. */
  225. public function delLotteryPrizeVirtual($prize_virtua_id){
  226. $prizeVirtualData = $this->objDLotteryPrizeVirtualData->get($prize_virtua_id);
  227. $prize_id = $prizeVirtualData['prize_id'];
  228. $this->objDLotteryPrizeVirtualData->delete(array('id' => $prize_virtua_id ));
  229. $this->addLotteryPrizeNum($prize_id,-1);
  230. return $prize_virtua_id;
  231. }
  232. /**
  233. * 获取虚拟数据【条数】
  234. */
  235. public function getLotteryPrizeVirtualDataListNum($condition){
  236. return $this->objDLotteryPrizeVirtualData->totals($condition);
  237. }
  238. /**
  239. * 获取活动信息&奖项信息
  240. */
  241. public function getOneLotteryEventsAndPrize($events_id) {
  242. $returnData = array ();
  243. $returnData ['events'] = $this->getOneLotteryEvents ( $events_id );
  244. $ids = $this->objDLotteryPrize->findIdsBy ( array (
  245. 'events_id' => $events_id
  246. ) );
  247. $returnData ['prize'] = $this->objDLotteryPrize->gets ( $ids );
  248. return $returnData;
  249. }
  250. /**
  251. * 修改活动信息 & 更新中奖时间
  252. */
  253. public function upLotteryEvents($info,$condition){
  254. $events_id = $condition['id'];
  255. $this->objDLotteryEvents->modify($info,$condition);
  256. $eventsAndPrize_Data = $this ->getOneLotteryEventsAndPrize($events_id);
  257. $objMLotteryDraw = new LotteryDraw(null,$events_id);
  258. foreach($eventsAndPrize_Data['prize'] as $prize_id=> $prize){
  259. $objMLotteryDraw->clear_draw_next_time($prize_id);
  260. }
  261. return $events_id;
  262. }
  263. /**
  264. * 修改活动奖项
  265. */
  266. public function UpLotteryPrize($info,$condition){
  267. $this->objDLotteryPrize-> modify($info,$condition );
  268. $prize_id = $condition['id'];
  269. // 白黑名单&百分百中奖名单 cache
  270. $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_white_list', $info ['white_list'] );
  271. $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_hundred_percent_list', $info ['hundred_percent_list'] );
  272. $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_black_list', $info ['black_list'] );
  273. // 清除下次中奖时间
  274. $eventsData = $this->objDLotteryPrize->get($prize_id);
  275. $events_id = $eventsData['events_id'];
  276. $objMLotteryDraw = new LotteryDraw(null,$events_id);
  277. $objMLotteryDraw->clear_draw_next_time($prize_id);
  278. return $prize_id;
  279. }
  280. /**
  281. * 修改奖项数量
  282. */
  283. public function addLotteryPrizeNum($prize_id,$num=null,$virtual_data=array()){
  284. if(empty($num) && empty($virtual_data)){
  285. return false;
  286. }
  287. if(!empty($virtual_data)){
  288. foreach ( $virtual_data as $virtual ) {
  289. $virtualInfo = array (
  290. 'prize_id' => $prize_id,
  291. 'data' => $virtual
  292. );
  293. $this->objDLotteryPrizeVirtualData->add ( $virtualInfo );
  294. }
  295. $num = count($virtual_data);
  296. }
  297. $objDLotteryPrize = new LotteryPrize();
  298. $objDLotteryPrize->upPrizeNum($prize_id, $num);
  299. // 清除下次中奖时间
  300. $eventsData = $this->objDLotteryPrize->get($prize_id);
  301. $events_id = $eventsData['events_id'];
  302. $objMLotteryDraw = new LotteryDraw(null,$events_id);
  303. $objMLotteryDraw->clear_draw_next_time($prize_id);
  304. return $prize_id;
  305. }
  306. /**
  307. * 添加奖项
  308. */
  309. public function addLotteryPrize($info) {
  310. $virtual_data = $info ['virtual_data'];
  311. unset ( $info ['virtual_data'] );
  312. $prize_id = $this->objDLotteryPrize->add ( $info );
  313. // 白黑名单 cache
  314. $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_white_list', $info ['white_list'] );
  315. $this->objMemcached->set ( 'lottery_prize_' . $prize_id . '_black_list', $info ['black_list'] );
  316. if (! empty ( $virtual_data )) {
  317. foreach ( $virtual_data as $virtual ) {
  318. $virtualInfo = array (
  319. 'prize_id' => $prize_id,
  320. 'data' => $virtual
  321. );
  322. $this->objDLotteryPrizeVirtualData->add ( $virtualInfo );
  323. }
  324. }
  325. // 清除下次中奖时间
  326. $eventsData = $this->objDLotteryPrize->get($prize_id);
  327. $events_id = $eventsData['events_id'];
  328. $objMLotteryDraw = new LotteryDraw(null,$events_id);
  329. $objMLotteryDraw->clear_draw_next_time($prize_id);
  330. return $prize_id;
  331. }
  332. /**
  333. * 解析条件
  334. */
  335. public function parseCondition($condition){
  336. return $this->objDLotteryEvents -> parseCondition($condition);
  337. }
  338. /**
  339. * 页面返回不同活动的地址 “简单粗暴“
  340. * @param int $eventsId
  341. */
  342. public function getEventsUrl($type,$create_time,$z_url=null){
  343. $create_time= date('YmdHis',$create_time);
  344. if(!$z_url){
  345. $z_url = Request::g('url');
  346. }
  347. $alltype = LotteryEvents::getType();
  348. $cstr = $alltype[$type]['c'];
  349. $eventRoute = LotteryEvents::getEventRoute();
  350. $str = '';
  351. foreach($eventRoute as $key =>$data){
  352. if($data == $cstr){
  353. $str = $key;
  354. break;
  355. }
  356. }
  357. return Request::schemeDomain ().'/'."{$z_url}/{$create_time}{$str}";
  358. }
  359. }