Label.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\store\model;
  11. use think\facade\Db;
  12. use think\facade\Cache;
  13. use app\model\BaseModel;
  14. /**
  15. * 门店标签
  16. */
  17. class Label extends BaseModel
  18. {
  19. /**
  20. * 添加门店标签
  21. *
  22. * @param array $data
  23. */
  24. public function addStoreLabel($data)
  25. {
  26. $count = model('store_label')->getCount([ [ 'label_name', '=', $data[ 'label_name' ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
  27. if ($count) return $this->error('', '该标签名称已存在');
  28. $res = model('store_label')->add($data);
  29. Cache::tag("store_label")->clear();
  30. return $this->success($res);
  31. }
  32. /**
  33. * 修改门店标签
  34. *
  35. * @param array $data
  36. * @param array $condition
  37. */
  38. public function editStoreLabel($data, $condition)
  39. {
  40. $check_condition = array_column($condition, 2, 0);
  41. $label_id = isset($check_condition[ 'label_id' ]) ? $check_condition[ 'label_id' ] : 0;
  42. $site_id = isset($check_condition[ 'site_id' ]) ? $check_condition[ 'site_id' ] : 0;
  43. if (empty($label_id)) return $this->error('', 'label_id不能为空');
  44. if (empty($site_id)) return $this->error('', 'site_id不能为空');
  45. $count = model('store_label')->getCount([ [ 'label_id', '<>', $label_id ], [ 'label_name', '=', $data[ 'label_name' ] ], [ 'site_id', '=', $site_id ] ]);
  46. if ($count) return $this->error('', '该标签名称已存在');
  47. $old_label_name = model('store_label')->getValue($condition, 'label_name');
  48. $res = model('store_label')->update($data, $condition);
  49. model('store')->update([ 'label_name' => Db::raw("REPLACE(label_name, ',{$old_label_name},', ',{$data['label_name']},')") ], [ [ 'site_id', '=', $site_id ], [ 'label_id', 'like', "%,$label_id,%" ] ]);
  50. Cache::tag("store_label")->clear();
  51. return $this->success($res);
  52. }
  53. /**
  54. * 修改排序
  55. * @param $sort
  56. * @param $label_id
  57. * @param $site_id
  58. * @return array
  59. */
  60. public function modifySort($sort, $label_id, $site_id)
  61. {
  62. $site_id = isset($site_id) ? $site_id : '';
  63. if ($site_id === '') {
  64. return $this->error('', 'REQUEST_SITE_ID');
  65. }
  66. $res = model('store_label')->update([ 'sort' => $sort ], [ [ 'label_id', '=', $label_id ], [ 'site_id', '=', $site_id ] ]);
  67. Cache::tag("store_label")->clear();
  68. return $this->success($res);
  69. }
  70. /**
  71. * 删除门店标签
  72. * @param $condition
  73. * @return array
  74. */
  75. public function deleteStoreLabel($condition)
  76. {
  77. $check_condition = array_column($condition, 2, 0);
  78. $label_id = isset($check_condition[ 'label_id' ]) ? $check_condition[ 'label_id' ] : 0;
  79. $site_id = isset($check_condition[ 'site_id' ]) ? $check_condition[ 'site_id' ] : 0;
  80. if (empty($label_id)) return $this->error('', 'label_id不能为空');
  81. if (empty($site_id)) return $this->error('', 'site_id不能为空');
  82. $res = model('store_label')->delete($condition);
  83. $old_label_name = model('store_label')->getValue($condition, 'label_name');
  84. model('store')->update([ 'label_name' => Db::raw("REPLACE(label_name, ',{$old_label_name},', '')") ], [ [ 'site_id', '=', $site_id ], [ 'label_id', 'like', "%,$label_id,%" ] ]);
  85. Cache::tag("store_label")->clear();
  86. return $this->success($res);
  87. }
  88. /**
  89. * 获取门店标签信息
  90. * @param array $condition
  91. * @param string $field
  92. * @return array
  93. */
  94. public function getStoreLabelInfo($condition = [], $field = '*')
  95. {
  96. $data = json_encode([ $condition, $field ]);
  97. $cache = Cache::get("store_label_getStoreLabelInfo_" . $data);
  98. if (!empty($cache)) {
  99. return $this->success($cache);
  100. }
  101. $info = model('store_label')->getInfo($condition, $field);
  102. Cache::tag("store_label")->set("store_label_getStoreLabelInfo_" . $data, $info);
  103. return $this->success($info);
  104. }
  105. /**
  106. * 获取门店标签列表
  107. *
  108. * @param array $condition
  109. * @param string $field
  110. * @param string $order
  111. * @param string $limit
  112. */
  113. public function getStoreLabelList($condition = [], $field = '*', $order = 'sort asc,label_id desc', $limit = null)
  114. {
  115. $data = json_encode([ $condition, $field, $order, $limit ]);
  116. $cache = Cache::get("store_label_getStoreLabelList_" . $data);
  117. if (!empty($cache)) {
  118. return $this->success($cache);
  119. }
  120. $list = model('store_label')->getList($condition, $field, $order, '', '', '', $limit);
  121. Cache::tag("store_label")->set("store_label_getStoreLabelList_" . $data, $list);
  122. return $this->success($list);
  123. }
  124. /**
  125. * 获取门店标签分页列表
  126. *
  127. * @param array $condition
  128. * @param number $page
  129. * @param string $page_size
  130. * @param string $order
  131. * @param string $field
  132. */
  133. public function getStoreLabelPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'sort asc,label_id desc', $field = '*')
  134. {
  135. $data = json_encode([ $condition, $field, $order, $page, $page_size ]);
  136. $cache = Cache::get("store_label_getStoreLabelPageList_" . $data);
  137. if (!empty($cache)) {
  138. return $this->success($cache);
  139. }
  140. $list = model('store_label')->pageList($condition, $field, $order, $page, $page_size);
  141. Cache::tag("store_label")->set("store_label_getStoreLabelPageList_" . $data, $list);
  142. return $this->success($list);
  143. }
  144. }