Model.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model;
  13. use think\facade\Db;
  14. use think\Validate;
  15. use think\facade\Cache;
  16. /**
  17. * 模型基类
  18. */
  19. class Model
  20. {
  21. // 查询对象
  22. private static $query_obj = null;
  23. protected $table = '';
  24. //验证规则
  25. protected $rule = [];
  26. //验证信息
  27. protected $message = [];
  28. //验证场景
  29. protected $scene = [];
  30. //错误信息
  31. protected $error;
  32. protected $is_cache = 1;
  33. public function __construct($table = '')
  34. {
  35. if ($table) {
  36. $this->table = $table;
  37. }
  38. $this->is_cache = $this->isCache();
  39. }
  40. public function isCache()
  41. {
  42. return 1;
  43. }
  44. /**
  45. * 获取列表数据
  46. * @param array $condition
  47. * @param string $field
  48. * @param string $order
  49. * @param number $page
  50. * @param array $join
  51. * @param string $group
  52. * @param string $limit
  53. * @param string $data
  54. * @return mixed
  55. */
  56. final public function getList($condition = [], $field = true, $order = '', $alias = 'a', $join = [], $group = '', $limit = null)
  57. {
  58. if ($this->is_cache && empty($join)) {
  59. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  60. $cache = Cache::get($cache_name);
  61. if (!empty($cache)) {
  62. return $cache;
  63. }
  64. }
  65. self::$query_obj = Db::name($this->table)->where($condition)->order($order);
  66. if (!empty($join)) {
  67. self::$query_obj->alias($alias);
  68. self::$query_obj = $this->parseJoin(self::$query_obj, $join);
  69. }
  70. if (!empty($group)) {
  71. self::$query_obj = self::$query_obj->group($group);
  72. }
  73. if (!empty($limit)) {
  74. self::$query_obj = self::$query_obj->limit($limit);
  75. }
  76. $result = self::$query_obj->field($field)->select()->toArray();
  77. self::$query_obj->removeOption();
  78. if ($this->is_cache && empty($join)) {
  79. Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
  80. }
  81. return $result;
  82. }
  83. final public function all()
  84. {
  85. if ($this->is_cache) {
  86. $cache_name = $this->table . '_' . __FUNCTION__ . '_';
  87. $cache = Cache::get($cache_name);
  88. if (!empty($cache)) {
  89. return $cache;
  90. }
  91. }
  92. $result = Db::name($this->table)->select()->toArray();
  93. if ($this->is_cache) {
  94. Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
  95. }
  96. return $result;
  97. }
  98. /**
  99. * 获取分页列表数据
  100. * @param unknown $where
  101. * @param string $field
  102. * @param string $order
  103. * @param number $page
  104. * @param string $list_rows
  105. * @param string $alias
  106. * @param unknown $join
  107. * @param string $group
  108. * @param string $limit
  109. */
  110. final public function pageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [], $group = null, $limit = null)
  111. {
  112. //关联查询多表无法控制不缓存,单独业务处理
  113. if ($this->is_cache && empty($join)) {
  114. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  115. $cache = Cache::get($cache_name);
  116. if (!empty($cache)) {
  117. return $cache;
  118. }
  119. }
  120. self::$query_obj = Db::name($this->table)->alias($alias)->where($condition)->order($order);
  121. $count_obj = Db::name($this->table)->alias($alias)->where($condition)->order($order);
  122. if (!empty($join)) {
  123. $db_obj = self::$query_obj;
  124. self::$query_obj = $this->parseJoin($db_obj, $join);
  125. $count_obj = $this->parseJoin($count_obj, $join);
  126. }
  127. if (!empty($group)) {
  128. self::$query_obj = self::$query_obj->group($group);
  129. $count_obj = $count_obj->group($group);
  130. }
  131. if (!empty($limit)) {
  132. self::$query_obj = self::$query_obj->limit($limit);
  133. }
  134. $count = $count_obj->count();
  135. if ($list_rows == 0) {
  136. //查询全部
  137. $result_data = self::$query_obj->field($field)->limit($count)->page($page)->select()->toArray();
  138. $result[ 'page_count' ] = 1;
  139. } else {
  140. $result_data = self::$query_obj->field($field)->limit($list_rows)->page($page)->select()->toArray();
  141. $result[ 'page_count' ] = ceil($count / $list_rows);
  142. }
  143. $result[ 'count' ] = $count;
  144. $result[ 'list' ] = $result_data;
  145. self::$query_obj->removeOption();
  146. if ($this->is_cache && empty($join)) {
  147. Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
  148. }
  149. return $result;
  150. }
  151. /**
  152. * 获取分页列表数据
  153. * @param unknown $where
  154. * @param string $field
  155. * @param string $order
  156. * @param number $page
  157. * @param string $list_rows
  158. * @param string $alias
  159. * @param unknown $join
  160. * @param string $group
  161. * @param string $limit
  162. */
  163. final public function rawPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [], $group = null, $limit = null)
  164. {
  165. //关联查询多表无法控制不缓存,单独业务处理
  166. if ($this->is_cache && empty($join)) {
  167. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  168. $cache = Cache::get($cache_name);
  169. if (!empty($cache)) {
  170. return $cache;
  171. }
  172. }
  173. if (is_array($order)) {
  174. self::$query_obj = Db::name($this->table)->alias($alias)->where($condition)->order($order);
  175. $count_obj = Db::name($this->table)->alias($alias)->where($condition)->order($order);
  176. } else {
  177. self::$query_obj = Db::name($this->table)->alias($alias)->where($condition)->orderRaw($order);
  178. $count_obj = Db::name($this->table)->alias($alias)->where($condition)->orderRaw($order);
  179. }
  180. if (!empty($join)) {
  181. $db_obj = self::$query_obj;
  182. self::$query_obj = $this->parseJoin($db_obj, $join);
  183. $count_obj = $this->parseJoin($count_obj, $join);
  184. }
  185. if (!empty($group)) {
  186. self::$query_obj = self::$query_obj->group($group);
  187. $count_obj = $count_obj->group($group);
  188. }
  189. if (!empty($limit)) {
  190. self::$query_obj = self::$query_obj->limit($limit);
  191. }
  192. $count = $count_obj->count();
  193. if ($list_rows == 0) {
  194. //查询全部
  195. $result_data = self::$query_obj->field($field)->limit($count)->page($page)->select()->toArray();
  196. $result[ 'page_count' ] = 1;
  197. } else {
  198. $result_data = self::$query_obj->field($field)->limit($list_rows)->page($page)->select()->toArray();
  199. $result[ 'page_count' ] = ceil($count / $list_rows);
  200. }
  201. $result[ 'count' ] = $count;
  202. $result[ 'list' ] = $result_data;
  203. self::$query_obj->removeOption();
  204. if ($this->is_cache && empty($join)) {
  205. Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
  206. }
  207. return $result;
  208. }
  209. /**
  210. * 获取单条数据
  211. * @param array $where
  212. * @param string $field
  213. * @param string $join
  214. * @param string $data
  215. * @return mixed
  216. */
  217. final public function getInfo($where = [], $field = true, $alias = 'a', $join = null, $data = null)
  218. {
  219. //关联查询多表无法控制不缓存,单独业务处理
  220. if ($this->is_cache && empty($join)) {
  221. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  222. $cache = Cache::get($cache_name);
  223. if (!empty($cache)) {
  224. return $cache;
  225. }
  226. }
  227. if (empty($join)) {
  228. $result = Db::name($this->table)->where($where)->field($field)->find($data);
  229. } else {
  230. $db_obj = Db::name($this->table)->alias($alias);
  231. $db_obj = $this->parseJoin($db_obj, $join);
  232. $result = $db_obj->where($where)->field($field)->find($data);
  233. }
  234. if ($this->is_cache && empty($join)) {
  235. Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
  236. }
  237. return $result;
  238. }
  239. /**
  240. * join分析
  241. * @access protected
  242. * @param array $join
  243. * @param array $options 查询条件
  244. * @return string
  245. */
  246. protected function parseJoin($db_obj, $join)
  247. {
  248. foreach ($join as $item) {
  249. list($table, $on, $type) = $item;
  250. $type = strtolower($type);
  251. switch ( $type ) {
  252. case "left":
  253. $db_obj = $db_obj->leftJoin($table, $on);
  254. break;
  255. case "inner":
  256. $db_obj = $db_obj->join($table, $on);
  257. break;
  258. case "right":
  259. $db_obj = $db_obj->rightjoin($table, $on);
  260. break;
  261. case "full":
  262. $db_obj = $db_obj->fulljoin($table, $on);
  263. break;
  264. default:
  265. break;
  266. }
  267. }
  268. return $db_obj;
  269. }
  270. /**
  271. * /**
  272. * 获取某个列的数组
  273. * @param array $where 条件
  274. * @param string $field 字段名 多个字段用逗号分隔
  275. * @param string $key 索引
  276. * @return array
  277. */
  278. final public function getColumn($where = [], $field = '', $key = '')
  279. {
  280. if ($this->is_cache) {
  281. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  282. $cache = Cache::get($cache_name);
  283. if (!empty($cache)) {
  284. return $cache;
  285. }
  286. }
  287. $result = Db::name($this->table)->where($where)->column($field, $key);
  288. if ($this->is_cache) {
  289. Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
  290. }
  291. return $result;
  292. }
  293. /**
  294. * 得到某个字段的值
  295. * @access public
  296. * @param array $where 条件
  297. * @param string $field 字段名
  298. * @param mixed $default 默认值
  299. * @param bool $force 强制转为数字类型
  300. * @return mixed
  301. */
  302. final public function getValue($where = [], $field = '', $default = null, $force = false)
  303. {
  304. if ($this->is_cache) {
  305. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  306. $cache = Cache::get($cache_name);
  307. if (!empty($cache)) {
  308. return $cache;
  309. }
  310. }
  311. $result = Db::name($this->table)->where($where)->value($field, $default, $force);
  312. if ($this->is_cache) {
  313. Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
  314. }
  315. return $result;
  316. }
  317. /**
  318. * 新增数据
  319. * @param array $data 数据
  320. * @param boolean $is_return_pk 返回自增主键
  321. */
  322. final public function add($data = [], $is_return_pk = true)
  323. {
  324. $res = Db::name($this->table)->insert($data, true, $is_return_pk);
  325. if ($this->is_cache) {
  326. Cache::tag("cache_table" . $this->table)->clear();
  327. }
  328. return $res;
  329. }
  330. /**
  331. * 新增多条数据
  332. * @param array $data 数据
  333. * @param int $limit 限制插入行数
  334. */
  335. final public function addList($data = [], $limit = null)
  336. {
  337. $res = Db::name($this->table)->insertAll($data, false, $limit);
  338. if ($this->is_cache) {
  339. Cache::tag("cache_table" . $this->table)->clear();
  340. }
  341. return $res;
  342. }
  343. /**
  344. * 更新数据
  345. * @param array $where 条件
  346. * @param array $data 数据
  347. */
  348. final public function update($data = [], $where = [])
  349. {
  350. $res = Db::name($this->table)->where($where)->update($data);
  351. if ($this->is_cache) {
  352. Cache::tag("cache_table" . $this->table)->clear();
  353. }
  354. return $res;
  355. }
  356. /**
  357. * 设置某个字段值
  358. * @param array $where 条件
  359. * @param string $field 字段
  360. * @param string $value 值
  361. */
  362. final public function setFieldValue($where = [], $field = '', $value = '')
  363. {
  364. $res = $this->update([ $field => $value ], $where);
  365. if ($this->is_cache) {
  366. Cache::tag("cache_table" . $this->table)->clear();
  367. }
  368. return $res;
  369. }
  370. /**
  371. * 设置数据列表
  372. * @param array $data_list 数据
  373. * @param boolean $replace 是否自动识别更新和写入
  374. */
  375. final public function setList($data_list = [], $replace = false)
  376. {
  377. $res = Db::name($this->table)->saveAll($data_list, $replace);
  378. if ($this->is_cache) {
  379. Cache::tag("cache_table" . $this->table)->clear();
  380. }
  381. return $res;
  382. }
  383. /**
  384. * 删除数据
  385. * @param array $where 条件
  386. */
  387. final public function delete($where = [])
  388. {
  389. $res = Db::name($this->table)->where($where)->delete();
  390. if ($this->is_cache) {
  391. Cache::tag("cache_table" . $this->table)->clear();
  392. }
  393. return $res;
  394. }
  395. /**
  396. * 统计数据
  397. * @param array $where 条件
  398. * @param string $type 查询类型 count:统计数量|max:获取最大值|min:获取最小值|avg:获取平均值|sum:获取总和
  399. */
  400. final public function stat($where = [], $type = 'count', $field = 'id')
  401. {
  402. if ($this->is_cache) {
  403. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  404. $cache = Cache::get($cache_name);
  405. if (!empty($cache)) {
  406. return $cache;
  407. }
  408. }
  409. $result = Db::name($this->table)->where($where)->$type($field);
  410. if ($this->is_cache) {
  411. Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
  412. }
  413. return $result;
  414. }
  415. /**
  416. * SQL查询
  417. * @param string $sql
  418. * @return mixed
  419. */
  420. final public function query($sql = '')
  421. {
  422. return Db::query($sql);
  423. }
  424. /**
  425. * 返回总数
  426. * @param unknown $where
  427. */
  428. final public function getCount($where = [], $field = '*', $alias = 'a', $join = null, $group = null)
  429. {
  430. if ($this->is_cache && empty($join)) {
  431. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  432. $cache = Cache::get($cache_name);
  433. if (!empty($cache)) {
  434. return $cache;
  435. }
  436. }
  437. if (empty($join)) {
  438. if (empty($group)) {
  439. $result = Db::name($this->table)->where($where)->count($field);
  440. } else {
  441. $result = Db::name($this->table)->group($group)->where($where)->count($field);
  442. }
  443. } else {
  444. $db_obj = Db::name($this->table)->alias($alias);
  445. $db_obj = $this->parseJoin($db_obj, $join);
  446. if (empty($group)) {
  447. $result = $db_obj->where($where)->count($field);
  448. } else {
  449. $result = $db_obj->group($group)->where($where)->count($field);
  450. }
  451. }
  452. if ($this->is_cache && empty($join)) {
  453. Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
  454. }
  455. return $result;
  456. }
  457. /**
  458. * 返回总数
  459. * @param unknown $where
  460. */
  461. final public function getSum($where = [], $field = '', $alias = 'a', $join = null)
  462. {
  463. if ($this->is_cache && empty($join)) {
  464. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  465. $cache = Cache::get($cache_name);
  466. if (!empty($cache)) {
  467. return $cache;
  468. }
  469. }
  470. if (empty($join)) {
  471. $result = Db::name($this->table)->where($where)->sum($field);
  472. } else {
  473. $db_obj = Db::name($this->table)->alias($alias);
  474. $db_obj = $this->parseJoin($db_obj, $join);
  475. $result = $db_obj->where($where)->sum($field);
  476. }
  477. if ($this->is_cache && empty($join)) {
  478. Cache::tag("cache_table" . $this->table)->set($cache_name, $result);
  479. }
  480. return $result;
  481. }
  482. /**
  483. * SQL执行,注意只能处理查询问题,如果执行修改需要手动删除缓存
  484. */
  485. final public function execute($sql = '')
  486. {
  487. return Db::execute($sql);
  488. }
  489. /**
  490. * 查询第一条数据
  491. * @param array $condition
  492. */
  493. final function getFirstData($condition, $field = '*', $order = "")
  494. {
  495. if ($this->is_cache) {
  496. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  497. $cache = Cache::get($cache_name);
  498. if (!empty($cache)) {
  499. return $cache;
  500. }
  501. }
  502. $data = Db::name($this->table)->where($condition)->order($order)->field($field)->find();
  503. if ($this->is_cache) {
  504. Cache::tag("cache_table" . $this->table)->set($cache_name, $data);
  505. }
  506. return $data;
  507. }
  508. /**
  509. * 查询第一条数据
  510. * @param array $condition
  511. */
  512. final function getFirstDataView($condition, $field = '*', $order = "", $alias = 'a', $join = [], $group = null)
  513. {
  514. if ($this->is_cache && empty($join)) {
  515. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  516. $cache = Cache::get($cache_name);
  517. if (!empty($cache)) {
  518. return $cache;
  519. }
  520. }
  521. self::$query_obj = Db::name($this->table)->alias($alias)->where($condition)->order($order)->field($field);
  522. if (!empty($join)) {
  523. $db_obj = self::$query_obj;
  524. self::$query_obj = $this->parseJoin($db_obj, $join);
  525. }
  526. if (!empty($group)) {
  527. self::$query_obj = self::$query_obj->group($group);
  528. }
  529. $data = self::$query_obj->find();
  530. if ($this->is_cache && empty($join)) {
  531. Cache::tag("cache_table" . $this->table)->set($cache_name, $data);
  532. }
  533. return $data;
  534. }
  535. /**
  536. * 验证
  537. * @param array $data
  538. * @param string $scene_name
  539. * @return array[$code, $error]
  540. */
  541. public function fieldValidate($data, $scene_name = '')
  542. {
  543. $validate = new Validate($this->rule, $this->message);
  544. if (empty($scene_name)) {
  545. $validate_result = $validate->batch(false)->check($data);
  546. } else {
  547. $validate->scene($this->scene);
  548. $validate_result = $validate->scene($scene_name)->batch(false)->check($data);
  549. }
  550. return $validate_result ? [ true, '' ] : [ false, $validate->getError() ];
  551. }
  552. /**
  553. * 事物开启
  554. */
  555. final public function startTrans()
  556. {
  557. return Db::startTrans();
  558. }
  559. /**
  560. * 事物提交
  561. */
  562. final public function commit()
  563. {
  564. return Db::commit();
  565. }
  566. /**
  567. * 事物回滚
  568. */
  569. final public function rollback()
  570. {
  571. Cache::clear();
  572. return Db::rollback();
  573. }
  574. /**
  575. * 获取错误信息
  576. */
  577. final public function getError()
  578. {
  579. return $this->error;
  580. }
  581. /**
  582. * 自增数据
  583. * @param array $where
  584. * @param $field
  585. * @param int $num
  586. * @return int
  587. * @throws \think\db\exception\DbException
  588. */
  589. final public function setInc($where, $field, $num = 1)
  590. {
  591. $res = Db::name($this->table)->where($where)->inc($field, $num)->update();
  592. if ($this->is_cache) {
  593. Cache::tag("cache_table" . $this->table)->clear();
  594. }
  595. return $res;
  596. }
  597. /**
  598. * 自减数据
  599. * @param $where
  600. * @param $field
  601. * @param int $num
  602. * @return int
  603. * @throws \think\db\exception\DbException
  604. */
  605. final public function setDec($where, $field, $num = 1)
  606. {
  607. $res = Db::name($this->table)->where($where)->dec($field, $num)->update();
  608. if ($this->is_cache) {
  609. Cache::tag("cache_table" . $this->table)->clear();
  610. }
  611. return $res;
  612. }
  613. /**
  614. * 获取最大值
  615. * @param array $where
  616. * @param $field
  617. * @return mixed
  618. */
  619. final public function getMax($where, $field)
  620. {
  621. if ($this->is_cache) {
  622. $cache_name = $this->table . '_' . __FUNCTION__ . '_' . serialize(func_get_args());
  623. $cache = Cache::get($cache_name);
  624. if (!empty($cache)) {
  625. return $cache;
  626. }
  627. }
  628. $data = Db::name($this->table)->where($where)->max($field);
  629. if ($this->is_cache) {
  630. Cache::tag("cache_table" . $this->table)->set($cache_name, $data);
  631. }
  632. return $data;
  633. }
  634. /**
  635. * 获取分页列表数据 只是单纯的实现部分功能 其他使用还是用pageList吧
  636. * @param unknown $where
  637. * @param string $field
  638. * @param string $order
  639. * @param number $page
  640. * @param string $list_rows
  641. * @param string $alias
  642. * @param unknown $join
  643. * @param string $group
  644. * @param string $limit
  645. */
  646. final public function Lists($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = [], $group = null, $limit = null,$gwhere='')
  647. {
  648. if(empty($gwhere)){
  649. self::$query_obj = Db::name($this->table)->alias($alias)->where($condition) ;
  650. $count_obj = Db::name($this->table)->alias($alias)->where($condition) ;
  651. }else{
  652. self::$query_obj = Db::name($this->table)->alias($alias)->where($condition)->where($gwhere) ;
  653. $count_obj = Db::name($this->table)->alias($alias)->where($condition)->where($gwhere) ;
  654. }
  655. if (!empty($join)) {
  656. $db_obj = self::$query_obj;
  657. self::$query_obj = $this->parseJoin($db_obj, $join);
  658. $count_obj = $this->parseJoin($count_obj, $join);
  659. }
  660. if (!empty($group)) {
  661. self::$query_obj = self::$query_obj->group($group);
  662. $count_obj = $count_obj->group($group);
  663. }
  664. if (!empty($limit)) {
  665. self::$query_obj = self::$query_obj->limit($limit);
  666. }
  667. $count = $count_obj->count();
  668. if ($list_rows == 0) {
  669. //查询全部
  670. $result_data = self::$query_obj->field($field)->order($order)->limit($count)->page($page)->select()->toArray();
  671. $result[ 'page_count' ] = 1;
  672. } else {
  673. $result_data = self::$query_obj->field($field)->order($order)->limit($list_rows)->page($page)->select()->toArray();
  674. $result[ 'page_count' ] = ceil($count / $list_rows);
  675. }
  676. $result[ 'count' ] = $count;
  677. $result[ 'list' ] = $result_data;
  678. self::$query_obj->removeOption();
  679. return $result;
  680. }
  681. /**
  682. * 不读取缓存--获取单条数据
  683. * @param array $where
  684. * @param string $field
  685. * @param string $join
  686. * @param string $data
  687. * @return mixed
  688. */
  689. final public function getInfoTo($where = [], $field = true, $alias = 'a', $join = null, $data = null)
  690. {
  691. if (empty($join)) {
  692. $result = Db::name($this->table)->where($where)->field($field)->find($data);
  693. } else {
  694. $db_obj = Db::name($this->table)->alias($alias);
  695. $db_obj = $this->parseJoin($db_obj, $join);
  696. $result = $db_obj->where($where)->field($field)->find($data);
  697. }
  698. return $result;
  699. }
  700. public function setIsCache($is_cache = 1){
  701. $this->is_cache = $is_cache;
  702. return $this;
  703. }
  704. }