SlideModel.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 文件说明:用户-幻灯片
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: wuwu <15093565100@163.com>
  8. // +----------------------------------------------------------------------
  9. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | Date: 2017-5-25
  12. // +----------------------------------------------------------------------
  13. namespace huo\model\slide;
  14. use huo\model\common\CommonModel;
  15. use think\exception\PDOException;
  16. class SlideModel extends CommonModel {
  17. /**
  18. * 全局查询范围status=1显示状态
  19. *
  20. * @param $query
  21. */
  22. protected function base($query) {
  23. $query->where('status', 2)->where('delete_time', 0);
  24. }
  25. /**
  26. * SlideItemModel 一对多关联模型 关联分类下的幻灯片
  27. *
  28. */
  29. protected function items() {
  30. return $this->hasMany(SlideItemModel::className(), 'slide_id', 'id')
  31. ->order('list_order ASC');
  32. }
  33. /**
  34. * SlideList 幻灯片获取
  35. *
  36. * @param array $map
  37. *
  38. * @param string $page
  39. *
  40. * @return false|\PDOStatement|string|\think\Collection
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. * @throws \think\exception\DbException
  44. */
  45. public function getSlideList($map, $page = '1,10') {
  46. $_page = $page;
  47. if (empty($page)) {
  48. $_page = '1,10';
  49. }
  50. $data = $this->relation('items')->field(true)->where($map)->page($_page)->select();
  51. return $data;
  52. }
  53. public function getSlideIdsByDevice($device) {
  54. if (empty($device)) {
  55. return false;
  56. }
  57. return $this->where(['device' => $device])->column(['id']);
  58. }
  59. public function getSlideCodesByDevice($device) {
  60. if (empty($device)) {
  61. return false;
  62. }
  63. return $this->where(['device' => $device])->column('code');
  64. }
  65. public function getSlideIdByCode($code) {
  66. if (empty($code)) {
  67. return false;
  68. }
  69. return $this->where(['code' => $code])->value('id');
  70. }
  71. public function getCodeById($id = 0) {
  72. if (empty($id)) {
  73. return 0;
  74. }
  75. return $this->where(['id' => $id])->limit('1')->value('code');
  76. }
  77. public function getTypeIdById($id = 0) {
  78. if (empty($id)) {
  79. return 0;
  80. }
  81. return $this->where(['id' => $id])->limit('1')->value('type_id');
  82. }
  83. /**
  84. * 添加数据
  85. *
  86. * @param $data
  87. *
  88. * @return bool
  89. */
  90. public function addData($data) {
  91. if (empty($data)) {
  92. return false;
  93. }
  94. $_data = $data;
  95. $_obj = self::create($_data, true);
  96. if ($_obj) {
  97. return true;
  98. }
  99. return false;
  100. }
  101. /**
  102. * 删除数据
  103. *
  104. * @param array $_map 删除条件
  105. *
  106. * @return int
  107. */
  108. public function deleteDataByMap($_map) {
  109. $_id = self::where($_map)->column('id');
  110. self::startTrans();
  111. try {
  112. self::where($_map)->delete();
  113. (new SlideItemModel())->where(['slide_id' => ['in', $_id]])->delete();
  114. // 提交事务
  115. self::commit();
  116. } catch (\Exception $e) {
  117. // 回滚事务
  118. self::rollback();
  119. return false;
  120. }
  121. return true;
  122. }
  123. }