table_reader.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wangming
  5. * Date: 2019/1/22
  6. * Time: 20:54
  7. */
  8. class Table_reader extends Table
  9. {
  10. protected $table_name = 'fission';//表名
  11. protected $table_id = 'fission_id';//指定ID字段名称,必须
  12. protected $table_status = '';//指定状态字段名称,如果有
  13. protected $table_order = '';//指定排序字段名称,如果有
  14. //数据库结构
  15. protected function struct()
  16. {
  17. $attr = array();
  18. $attr['id'] = 'reader_id';
  19. $attr['phone'] = 'reader_phone';
  20. $attr['wxopenid'] = 'reader_wxopenid';
  21. $attr['name'] = 'reader_name';
  22. $attr['sex'] = 'reader_sex';
  23. $attr['addtime'] = 'reader_addtime';
  24. $attr['count'] = 'reader_count';
  25. return $attr;
  26. }
  27. public function getInfoById($id)
  28. {
  29. $id = $this->pdo->sql_check_input(array('number', $id));
  30. $sql = "select * from " . $this->table_fullname . " where reader_id = $id limit 1";
  31. $rs = $this->pdo->sqlQuery($sql);
  32. $r = array();
  33. if ($rs) {
  34. foreach ($rs as $key => $val) {
  35. $r[$key] = $this->dataToAttr($val);
  36. }
  37. return $r[0];
  38. } else {
  39. return $r;
  40. }
  41. }
  42. public function getInfoByPhone($phone)
  43. {
  44. $phone = $this->pdo->sql_check_input(array('string', $phone));
  45. $sql = "select * from " . $this->table_fullname . " where reader_phone = $phone limit 1";
  46. $rs = $this->pdo->sqlQuery($sql);
  47. $r = array();
  48. if ($rs) {
  49. foreach ($rs as $key => $val) {
  50. $r[$key] = $this->dataToAttr($val);
  51. }
  52. return $r[0];
  53. } else {
  54. return $r;
  55. }
  56. }
  57. public function getInfoByOpenId($openId)
  58. {
  59. $openId = $this->pdo->sql_check_input(array('string', $openId));
  60. $sql = "select * from " . $this->table_fullname . " where reader_wxopenid = $openId limit 1";
  61. $rs = $this->pdo->sqlQuery($sql);
  62. $r = array();
  63. if ($rs) {
  64. foreach ($rs as $key => $val) {
  65. $r[$key] = $this->dataToAttr($val);
  66. }
  67. return $r[0];
  68. } else {
  69. return $r;
  70. }
  71. }
  72. public function add($attr)
  73. {
  74. $param = array(
  75. 'reader_phone' => array('string', $attr['phone']),
  76. 'reader_wxopenid' => array('string', $attr['wxopenid']),
  77. 'reader_name' => array('string', $attr['name']),
  78. 'reader_sex' => array('number', $attr['sex']),
  79. 'reader_addtime' => array('number', time()),
  80. 'reader_count' => array('number', 0),
  81. );
  82. return $this->pdo->sqlinsert($this->table_fullname, $param);
  83. }
  84. public function edit($id, $attr)
  85. {
  86. $param = array(
  87. //
  88. );
  89. $where = array(
  90. 'reader_id' => array('number', $id)
  91. );
  92. return $this->pdo->sqlupdate($this->table_fullname, $param, $where);
  93. }
  94. //获取列表(分页)
  95. //$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
  96. //
  97. //@param $filter array -- 过滤条件,格式见Table::filterToWhere
  98. //@param $count -- 0:返回列表 1:返回结果数量
  99. //@param $page -- 当前第几页
  100. //@param $pagesize -- 每页数量
  101. public function getList($filter = array(), $count = 0, $page = 0, $pagesize = 0)
  102. {
  103. $where = $this->filterToWhere($filter);
  104. if ($count == 0) {//列表
  105. $sql = "select * from " . $this->table_fullname . " $where order by reader_addtime desc, reader_id desc";
  106. if ($page > 0) {//分页
  107. $startrow = ($page - 1) * $pagesize;
  108. $sql_limit = " limit $startrow, $pagesize";
  109. $sql .= $sql_limit;
  110. }
  111. $rs = $this->pdo->sqlQuery($sql);
  112. $r = array();
  113. if ($rs) {
  114. foreach ($rs as $key => $val) {
  115. $r[$key] = $this->dataToAttr($val);
  116. }
  117. return $r;
  118. } else {
  119. return $r;
  120. }
  121. } else {//统计
  122. $sql = "select count(*) as c from " . $this->table_fullname . " $where ";
  123. $rs = $this->pdo->sqlQuery($sql);
  124. if ($rs) {
  125. return $rs[0]['c'];
  126. } else {
  127. return 0;
  128. }
  129. }
  130. }
  131. }