| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- <?php
- /**
- * Created by PhpStorm.
- * User: wangming
- * Date: 2019/1/22
- * Time: 20:54
- */
- class Table_common_guest extends Table
- {
- protected $table_name = 'common_guest';//表名
- protected $table_id = 'common_guest_id';//指定ID字段名称,必须
- protected $table_status = '';//指定状态字段名称,如果有
- protected $table_order = '';//指定排序字段名称,如果有
- //数据库结构
- protected function struct()
- {
- $attr = array();
- $attr['id'] = 'common_guest_id';
- $attr['code'] = 'common_guest_code';
- $attr['phone'] = 'common_guest_phone';
- $attr['wxopenid'] = 'common_guest_wxopenid';
- $attr['name'] = 'common_guest_name';
- $attr['sex'] = 'common_guest_sex';
- $attr['province'] = 'common_guest_province';
- $attr['city'] = 'common_guest_city';
- $attr['detailaddress'] = 'common_guest_detailaddress';
- $attr['hangye_id'] = 'common_guest_hangye_id';
- $attr['addtime'] = 'common_guest_addtime';
- $attr['count'] = 'common_guest_count';
- //外键字段
- $attr['hangye_name'] = 'hangye_name';
- return $attr;
- }
- public function getInfoById($id)
- {
- $id = $this->pdo->sql_check_input(array('number', $id));
- $sql = "select * from " . $this->table_fullname . " where common_guest_id = $id limit 1";
- $rs = $this->pdo->sqlQuery($sql);
- $r = array();
- if ($rs) {
- foreach ($rs as $key => $val) {
- $r[$key] = $this->dataToAttr($val);
- }
- return $r[0];
- } else {
- return $r;
- }
- }
- public function getInfoByPhone($phone)
- {
- $phone = $this->pdo->sql_check_input(array('string', $phone));
- $sql = "select * from " . $this->table_fullname . " where common_guest_phone = $phone limit 1";
- $rs = $this->pdo->sqlQuery($sql);
- $r = array();
- if ($rs) {
- foreach ($rs as $key => $val) {
- $r[$key] = $this->dataToAttr($val);
- }
- return $r[0];
- } else {
- return $r;
- }
- }
- public function getInfoByOpenId($openId)
- {
- $openId = $this->pdo->sql_check_input(array('string', $openId));
- $sql = "select * from " . $this->table_fullname . " where common_guest_wxopenid = $openId limit 1";
- $rs = $this->pdo->sqlQuery($sql);
- $r = array();
- if ($rs) {
- foreach ($rs as $key => $val) {
- $r[$key] = $this->dataToAttr($val);
- }
- return $r[0];
- } else {
- return $r;
- }
- }
- public function generateNewCode($id)
- {
- $id = $this->pdo->sql_check_input(array('number', $id));
- //$sql = "select count(*) as c from " . $this->table_fullname . " where (common_guest_id < $id) and (common_guest_status = 1 or common_guest_status = 0)";
- $sql = "select max(common_guest_code) as c from " . $this->table_fullname;
- $rs = $this->pdo->sqlQuery($sql);
- $count = $rs[0]['c'];
- return sprintf('%04s', $count + 1);
- }
- public function add($attr)
- {
- $param = array(
- //'common_guest_code' => array('string', $attr['code']),
- 'common_guest_phone' => array('string', $attr['phone']),
- 'common_guest_wxopenid' => array('string', $attr['wxopenid']),
- 'common_guest_name' => array('string', $attr['name']),
- 'common_guest_sex' => array('number', $attr['sex']),
- 'common_guest_province' => array('number', $attr['province']),
- 'common_guest_city' => array('number', $attr['city']),
- 'common_guest_detailaddress' => array('string', $attr['detailaddress']),
- 'common_guest_hangye_id' => array('number', $attr['hangye_id']),
- 'common_guest_addtime' => array('number', time()),
- 'common_guest_count' => array('number', 0),
- );
- return $this->pdo->sqlinsert($this->table_fullname, $param);
- }
- public function edit($id, $attr)
- {
- $param = array(
- //
- );
- $where = array(
- 'common_guest_id' => array('number', $id)
- );
- return $this->pdo->sqlupdate($this->table_fullname, $param, $where);
- }
- //获取列表(分页)
- //$count、$page和$pagesize都为0时,返回全部结果(适用于无需分页的情况)
- //
- //@param $filter array -- 过滤条件,格式见Table::filterToWhere
- //@param $count -- 0:返回列表 1:返回结果数量
- //@param $page -- 当前第几页
- //@param $pagesize -- 每页数量
- public function getList($filter = array(), $count = 0, $page = 0, $pagesize = 0)
- {
- $where = $this->filterToWhere($filter);
- if ($count == 0) {//列表
- $sql = "select aa.*, bb.hangye_name as hangye_name from " . $this->table_fullname ." as aa "
- ." left join kano_hangye as bb on aa.common_guest_hangye_id=bb.hangye_id "
- ." $where order by common_guest_addtime desc, common_guest_id desc";
- if ($page > 0) {//分页
- $startrow = ($page - 1) * $pagesize;
- $sql_limit = " limit $startrow, $pagesize";
- $sql .= $sql_limit;
- }
- $rs = $this->pdo->sqlQuery($sql);
- $r = array();
- if ($rs) {
- foreach ($rs as $key => $val) {
- $r[$key] = $this->dataToAttr($val);
- }
- return $r;
- } else {
- return $r;
- }
- } else {//统计
- $sql = "select count(*) as c from " . $this->table_fullname . " $where ";
- $rs = $this->pdo->sqlQuery($sql);
- if ($rs) {
- return $rs[0]['c'];
- } else {
- return 0;
- }
- }
- }
- }
|