HomeStoresModel.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Common\Model;
  3. use Common\Model\BaseModel;
  4. /**
  5. * 官网门店
  6. * @author: linch
  7. * Class HomeStoresModel
  8. * @package Common\Model
  9. */
  10. class HomeStoresModel extends BaseModel
  11. {
  12. //使用x-motion.cn数据库
  13. protected $connection = 'DB_CONFIG_XMOTION';
  14. //软删状态
  15. const status_del = 0; //删除
  16. const status_on = 1; //正常
  17. const status_off = 2; //下架
  18. //新旧状态
  19. const new_off = 0; // 旧店
  20. const new_on = 1; // 新店
  21. protected $mod;
  22. public function __construct() {
  23. parent::__construct();
  24. $this->mod = M('home_stores');
  25. }
  26. /**
  27. * 获取数据
  28. * @author: linch
  29. */
  30. public function get_data($search, $page_num, $page_size, $type = 0) {
  31. $where = array(
  32. 'status' => self::status_on,
  33. );
  34. if ($search['city_id'] > 0) {
  35. $where['city_id'] = $search['city_id'];
  36. }
  37. if (isset($search['is_new'])) {
  38. $where['is_new'] = $search['is_new'];
  39. }
  40. $order = 'id asc';
  41. $limit = ($page_num - 1) * $page_size . ' ,' . $page_size;
  42. $field = '*';
  43. $list = $this->where($where)->order($order)->field($field)->limit($limit)->select();
  44. $count = $this->where($where)->count();
  45. $Page = new \Common\Common\util\PageO($count, $page_size);
  46. // $Page = new \Think\Page($count, $page_size);
  47. $pager = $Page->show();
  48. if (($type == 1) && count($list) < $page_size) {
  49. $list[] = $this->get_default_store();
  50. }
  51. $store_id_arr = array();
  52. foreach ($list as $k => $v) {
  53. $store_id_arr[] = $v['id'];
  54. $list[$k]['have_news'] = 0;
  55. }
  56. //查看文章是否有分店的
  57. if (!empty($store_id_arr)) {
  58. $news_model = new \Common\Model\HomeNewModel();
  59. $news_temp = $news_model->field('distinct(store_id) as store_id')->where(array('store_id' => array('in', $store_id_arr), 'status' => $news_model::status_on))->select();
  60. $news_data = array();
  61. foreach ($news_temp as $k => $v) {
  62. $news_data[$v['store_id']] = $v;
  63. }
  64. }
  65. //整合数据
  66. foreach ($list as $k => $v) {
  67. if (!empty($news_data[$v['id']])) {
  68. $list[$k]['have_news'] = 1;
  69. }
  70. }
  71. $data = array(
  72. 'list' => $list,
  73. 'count' => $count,
  74. 'pager' => $pager,
  75. );
  76. return $data;
  77. }
  78. /**
  79. * 获取填充数据
  80. * @author: linch
  81. */
  82. public function get_default_store() {
  83. return array(
  84. 'name' => '',
  85. 'address' => '',
  86. 'img' => 'https://img.x-motion.cn/home/image/store_join.jpg',
  87. 'date' => '',
  88. 'time' => '',
  89. 'tel' => '',
  90. );
  91. }
  92. }