123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- namespace app\common\model;
- use app\common\service\helperService;
- use think\Model;
- class BaseModel extends Model
- {
- public function __construct($data=""){
- parent::__construct($data);
- if(!is_array($data) || empty($data)){
- //实例化,将产品促销过期数据更新
- ProductPromoModel::updateExpireTs();
- //将订单超过半小时的退单
- OrderModel::checkOrder();
- }
- }
- protected function BaseModel($res)
- {
- return helperService::modelDataToArr($res);
- }
- public function getPage($page,$pageSize,$condition=[],$is_count = false,$order=''){
- $this->alias('a');
- if(!empty($condition)){
- $this->where($condition);
- }
- if(!empty($order)){
- $this->order($order);
- }
- if($is_count){
- return $this->count();
- }
- return $this->page($page,$pageSize)->column('*');
- }
- /**
- * 保存数据
- * @param $data
- * @param array $condition
- * @return $this
- */
- public function saveData($data,$condition=[]){
- if($condition){
- return $this->where($condition)->update($data);
- }
- return $this->insertGetId($data);
- }
- /**
- * 保存多条数据
- * @param $data
- * @return bool|int|string
- */
- public function insertAllDate($data){
- if(empty($data) && !is_array($data)){
- return false;
- }
- return $this->insertAll($data);
- }
- /**
- * 获取一条数据信息
- * @param array $condition
- * @param bool $is_cache
- * @param string $order
- * @return array|false|\PDOStatement|string|Model
- */
- public function getOne($condition=[],$is_cache=false,$order=''){
- if($condition){
- $this->where($condition);
- }
- if($is_cache){
- $this->cache($is_cache,60);
- }
- if($order){
- $this->order($order);
- }
- return $this->BaseModel($this->find());
- }
- /**
- * 获取总条数据信息
- * @param array $condition
- * @return array|false|\PDOStatement|string|Model
- */
- public function getCount($condition=[]){
- if($condition){
- return $this->where($condition)->count();
- }
- return $this->count();
- }
- /**
- * 获取总条数据信息
- * @param array $condition
- * @param string $field
- * @return int|string
- */
- public function getSum($condition=[],$field=''){
- if($condition){
- $this->where($condition);
- }
- return $this->sum($field);
- }
- /**
- * 获取多条数据信息
- * @param array $condition 条件
- * @param string $order 排序
- * @param int $limit 限制数
- * @return array
- */
- public function getMulti($condition=[],$order='',$limit=0){
- if($order){
- $this->order($order);
- }
- if($limit){
- $this->limit($limit);
- }
- if($condition){
- return $this->BaseModel($this->where($condition)->select());
- }
- return $this->BaseModel($this->select());
- }
- /**
- * 根据条件删除数据
- * @param array $condition
- * @return bool|int
- */
- public function removeData($condition=[]){
- //删除功能要严格控制
- if(empty($condition)){
- return false;
- }
- return $this->where($condition)->delete();
- }
- /**
- *增/改
- *@param array $data 数据
- *@param array $where 添加条件
- *@return array $res 返回结果
- */
- public function kuyun_insert($data,$where){
- if(!empty($where)){
- $where = array_filter($where);
- $res = $this->where($where)->update($data);
- }else{
- $res = $this->insertGetId($data);
- }
- return $res;
- }
- /**
- *多查
- *@param array $where 条件
- *@param array $join 联合查询
- *@param string $order 排序
- *@param number $limit
- *@param number $page 是否分页
- *@param bull $count 是否查数量
- *@return array number
- */
- public function kuyun_select($where=[],$join=[],$order='',$field='',$limit=0,$page=0,$is_count=false){
- if(!empty($where)){
- $where = array_filter($where);
- $this->where($where);
- }
- if(!empty($join)){
- $this->join($join);
- }
- if(!empty($order)){
- $this->order($order);
- }
- if(!empty($field)){
- $this->field($field);
- }
- if(!empty($limit)){
- $this->limit($limit);
- }
- if($page!=0){
- $pageSize = 10;
- $this->page($page,$pageSize);
- }
- if($is_count){
- return $this->count();
- }else{
- return $this->select();
- }
- }
- /**
- *单查
- *@param array $where 查询条件
- *@param string $field 查询指定字段
- *@param array $join 联合查询
- */
- public function kuyun_find($where,$field,$join){
- if(!empty($where)){
- $where = array_filter($where);
- $this->where($where);
- }
- if(!empty($field)){
- $this->field($field);
- }
- if(!empty($join)){
- $this->join($join);
- }
- return $this->find();
- }
- /**
- *物理删除
- *@param array $where删除条件
- *@return number 删除id
- */
- public function kuyun_delete($where){
- return $this->where($where)->delete();
- }
- }
|