ranklist.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/7/22
  6. * Time: 下午4:31
  7. */
  8. class ranklist
  9. {
  10. private $list_sn;
  11. private $member_id;
  12. private $list_date;
  13. private $list_value;
  14. private $supports;
  15. private $top_id;
  16. private $top_money;
  17. private $list_rank;
  18. private $friends;
  19. private $update;
  20. private $dirty;
  21. private $mod_toplist;
  22. private function __construct()
  23. {
  24. $cl = __CLASS__;
  25. $this->dirty = false;
  26. $this->mod_toplist = Model('bonus_ranklist');
  27. $num = func_num_args();
  28. if($num == 1)
  29. {
  30. $param = func_get_arg(0);
  31. if(empty($param)) {
  32. throw new Exception("错误的参数 for {$cl}",errcode::ErrParamter);
  33. }
  34. if(is_array($param)) {
  35. $this->init($param);
  36. }
  37. else
  38. {
  39. $param = $this->mod_toplist->getByListSn($param);
  40. if(empty($param)) {
  41. throw new Exception("错误的参数 for {$cl}",errcode::ErrParamter);
  42. } else {
  43. $this->init($param);
  44. }
  45. }
  46. }
  47. elseif($num == 2) {
  48. $this->member_id = intval(func_get_arg(0));
  49. $this->list_date = intval(func_get_arg(1));
  50. $this->list_sn = ranklist_helper::make_sn($this->member_id,$this->list_date);
  51. $param = $this->mod_toplist->getByListSn($this->list_sn);
  52. $this->init($param);
  53. }
  54. else {
  55. throw new Exception("错误的参数 for {$cl}",errcode::ErrParamter);
  56. }
  57. }
  58. public function __destruct()
  59. {
  60. if($this->dirty)
  61. {
  62. if($this->update) {
  63. $data = array();
  64. $data['list_value'] = $this->list_value;
  65. $data['supports'] = $this->supports;
  66. $data['friends'] = serialize($this->friends);
  67. $data['top_id'] = $this->top_id;
  68. $data['top_money'] = $this->top_money;
  69. $data['list_rank'] = $this->list_rank;
  70. $this->mod_toplist->edit(array('list_sn' => $this->list_sn),$data);
  71. } else {
  72. $data = array();
  73. $data['list_value'] = $this->list_value;
  74. $data['list_sn'] = $this->list_sn;
  75. $data['member_id'] = $this->member_id;
  76. $data['list_date'] = $this->list_date;
  77. $data['supports'] = $this->supports;
  78. $data['friends'] = serialize($this->friends);
  79. $data['top_id'] = $this->top_id;
  80. $data['top_money'] = $this->top_money;
  81. $data['list_rank'] = $this->list_rank;
  82. $this->mod_toplist->insert($data);
  83. }
  84. }
  85. }
  86. public function add_money($money)
  87. {
  88. $penny = intval($money * 100 + 0.5);
  89. if($penny > 0) {
  90. $this->list_value += $money;
  91. $this->dirty = true;
  92. }
  93. }
  94. public function set_top($top_id,$top_money)
  95. {
  96. if($this->top_id != $top_id) {
  97. $this->dirty = true;
  98. $this->top_id = $top_id;
  99. }
  100. if(intval($this->top_money * 100 + 0.5) != intval($top_money * 100 + 0.5)) {
  101. $this->dirty = true;
  102. $this->top_money = $top_money;
  103. }
  104. }
  105. public function get_rank() {
  106. return $this->list_rank;
  107. }
  108. public function set_rank($rank)
  109. {
  110. if($this->list_rank != $rank) {
  111. $this->list_rank = $rank;
  112. $this->dirty = true;
  113. }
  114. }
  115. public function top_id() {
  116. return $this->top_id;
  117. }
  118. public function top_money() {
  119. return $this->top_money;
  120. }
  121. public function calculated() {
  122. return $this->top_id > 0;
  123. }
  124. public function money()
  125. {
  126. return $this->list_value;
  127. }
  128. public function list_date() {
  129. return$this->list_date;
  130. }
  131. public function list_sn() {
  132. return $this->list_sn;
  133. }
  134. public function member_id() {
  135. return $this->member_id;
  136. }
  137. public function get_friends() {
  138. return $this->friends;
  139. }
  140. public function set_friends($friends)
  141. {
  142. if(empty($this->friends) && !empty($friends)) {
  143. $this->friends = $friends;
  144. $this->dirty = true;
  145. }
  146. }
  147. public function supports()
  148. {
  149. return $this->supports;
  150. }
  151. public function support()
  152. {
  153. $this->supports += 1;
  154. $this->dirty = true;
  155. }
  156. public function unsupport()
  157. {
  158. if($this->supports >= 1) {
  159. $this->supports -= 1;
  160. $this->dirty = true;
  161. }
  162. }
  163. private function init($param)
  164. {
  165. if(!empty($param)) {
  166. $this->list_sn = $param['list_sn'];
  167. $this->member_id = intval($param['member_id']);
  168. $this->list_date = intval($param['list_date']);
  169. $this->list_value = $param['list_value'];
  170. $this->list_rank = intval($param['list_rank']);
  171. $this->top_id = intval($param['top_id']);
  172. $this->top_money = $param['top_money'];
  173. $this->supports = intval($param['supports']);
  174. $this->friends = empty($param['friends']) ? array() : unserialize(($param['friends']));
  175. $this->update = true;
  176. } else {
  177. $this->list_value = 0.00;
  178. $this->list_rank = 0;
  179. $this->top_id = 0;
  180. $this->top_money = 0.00;
  181. $this->supports = 0;
  182. $this->friends = array();
  183. $this->update = false;
  184. $this->dirty = true; //生成对象之后,为了上层需要,需要存储下来
  185. }
  186. }
  187. ///////////////////////////////////////////////////////////////////////
  188. static public function create_by_sn($list_sn)
  189. {
  190. try
  191. {
  192. return new ranklist($list_sn);
  193. } catch (Exception $ex) {
  194. return false;
  195. }
  196. }
  197. static public function create_by_date($member_id,$date)
  198. {
  199. try
  200. {
  201. return new ranklist($member_id,$date);
  202. } catch (Exception $ex) {
  203. return false;
  204. }
  205. }
  206. static public function create_by_store($row_info)
  207. {
  208. return new ranklist($row_info);
  209. }
  210. }