table_reader.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wangming
  5. * Date: 2019/1/22
  6. * Time: 20:54
  7. */
  8. class Table_common_guest extends Table
  9. {
  10. protected $table_name = 'common_guest';//表名
  11. protected $table_id = 'common_guest_id';//指定ID字段名称,必须
  12. protected $table_status = '';//指定状态字段名称,如果有
  13. protected $table_order = '';//指定排序字段名称,如果有
  14. //数据库结构
  15. protected function struct()
  16. {
  17. $attr = array();
  18. $attr['id'] = 'common_guest_id';
  19. $attr['code'] = 'common_guest_code';
  20. $attr['phone'] = 'common_guest_phone';
  21. $attr['wxopenid'] = 'common_guest_wxopenid';
  22. $attr['name'] = 'common_guest_name';
  23. $attr['sex'] = 'common_guest_sex';
  24. $attr['province'] = 'common_guest_province';
  25. $attr['city'] = 'common_guest_city';
  26. $attr['detailaddress'] = 'common_guest_detailaddress';
  27. $attr['hangye_id'] = 'common_guest_hangye_id';
  28. $attr['addtime'] = 'common_guest_addtime';
  29. $attr['count'] = 'common_guest_count';
  30. //外键字段
  31. $attr['hangye_name'] = 'hangye_name';
  32. return $attr;
  33. }
  34. public function getInfoById($id)
  35. {
  36. $id = $this->pdo->sql_check_input(array('number', $id));
  37. $sql = "select * from " . $this->table_fullname . " where common_guest_id = $id limit 1";
  38. $rs = $this->pdo->sqlQuery($sql);
  39. $r = array();
  40. if ($rs) {
  41. foreach ($rs as $key => $val) {
  42. $r[$key] = $this->dataToAttr($val);
  43. }
  44. return $r[0];
  45. } else {
  46. return $r;
  47. }
  48. }
  49. public function getInfoByPhone($phone)
  50. {
  51. $phone = $this->pdo->sql_check_input(array('string', $phone));
  52. $sql = "select * from " . $this->table_fullname . " where common_guest_phone = $phone limit 1";
  53. $rs = $this->pdo->sqlQuery($sql);
  54. $r = array();
  55. if ($rs) {
  56. foreach ($rs as $key => $val) {
  57. $r[$key] = $this->dataToAttr($val);
  58. }
  59. return $r[0];
  60. } else {
  61. return $r;
  62. }
  63. }
  64. public function getInfoByOpenId($openId)
  65. {
  66. $openId = $this->pdo->sql_check_input(array('string', $openId));
  67. $sql = "select * from " . $this->table_fullname . " where common_guest_wxopenid = $openId limit 1";
  68. $rs = $this->pdo->sqlQuery($sql);
  69. $r = array();
  70. if ($rs) {
  71. foreach ($rs as $key => $val) {
  72. $r[$key] = $this->dataToAttr($val);
  73. }
  74. return $r[0];
  75. } else {
  76. return $r;
  77. }
  78. }
  79. public function generateNewCode($id)
  80. {
  81. $id = $this->pdo->sql_check_input(array('number', $id));
  82. //$sql = "select count(*) as c from " . $this->table_fullname . " where (common_guest_id < $id) and (common_guest_status = 1 or common_guest_status = 0)";
  83. $sql = "select max(common_guest_code) as c from " . $this->table_fullname;
  84. $rs = $this->pdo->sqlQuery($sql);
  85. $count = $rs[0]['c'];
  86. return sprintf('%04s', $count + 1);
  87. }
  88. public function add($attr)
  89. {
  90. $param = array(
  91. //'common_guest_code' => array('string', $attr['code']),
  92. 'common_guest_phone' => array('string', $attr['phone']),
  93. 'common_guest_wxopenid' => array('string', $attr['wxopenid']),
  94. 'common_guest_name' => array('string', $attr['name']),
  95. 'common_guest_sex' => array('number', $attr['sex']),
  96. 'common_guest_province' => array('number', $attr['province']),
  97. 'common_guest_city' => array('number', $attr['city']),
  98. 'common_guest_detailaddress' => array('string', $attr['detailaddress']),
  99. 'common_guest_hangye_id' => array('number', $attr['hangye_id']),
  100. 'common_guest_addtime' => array('number', time()),
  101. 'common_guest_count' => array('number', 0),
  102. );
  103. return $this->pdo->sqlinsert($this->table_fullname, $param);
  104. }
  105. public function edit($id, $attr)
  106. {
  107. $param = array(
  108. //
  109. );
  110. $where = array(
  111. 'common_guest_id' => array('number', $id)
  112. );
  113. return $this->pdo->sqlupdate($this->table_fullname, $param, $where);
  114. }
  115. //获取列表(分页)
  116. //$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
  117. //
  118. //@param $filter array -- 过滤条件,格式见Table::filterToWhere
  119. //@param $count -- 0:返回列表 1:返回结果数量
  120. //@param $page -- 当前第几页
  121. //@param $pagesize -- 每页数量
  122. public function getList($filter = array(), $count = 0, $page = 0, $pagesize = 0)
  123. {
  124. $where = $this->filterToWhere($filter);
  125. if ($count == 0) {//列表
  126. $sql = "select aa.*, bb.hangye_name as hangye_name from " . $this->table_fullname ." as aa "
  127. ." left join kano_hangye as bb on aa.common_guest_hangye_id=bb.hangye_id "
  128. ." $where order by common_guest_addtime desc, common_guest_id desc";
  129. if ($page > 0) {//分页
  130. $startrow = ($page - 1) * $pagesize;
  131. $sql_limit = " limit $startrow, $pagesize";
  132. $sql .= $sql_limit;
  133. }
  134. $rs = $this->pdo->sqlQuery($sql);
  135. $r = array();
  136. if ($rs) {
  137. foreach ($rs as $key => $val) {
  138. $r[$key] = $this->dataToAttr($val);
  139. }
  140. return $r;
  141. } else {
  142. return $r;
  143. }
  144. } else {//统计
  145. $sql = "select count(*) as c from " . $this->table_fullname . " $where ";
  146. $rs = $this->pdo->sqlQuery($sql);
  147. if ($rs) {
  148. return $rs[0]['c'];
  149. } else {
  150. return 0;
  151. }
  152. }
  153. }
  154. }