ShopMemberLabelService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\shop\site;
  12. use app\model\member\MemberLabel;
  13. use app\service\core\member\CoreMemberLabelService;
  14. use core\base\BaseAdminService;
  15. use think\db\exception\DataNotFoundException;
  16. use think\db\exception\DbException;
  17. use think\db\exception\ModelNotFoundException;
  18. use think\Response;
  19. /**
  20. * 店铺会员标签服务层
  21. * Class ShopService
  22. * @package app\service\admin\shop\site
  23. */
  24. class ShopMemberLabelService extends BaseAdminService
  25. {
  26. protected static $cache_tag_name = 'shop_member_label_cache';
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. $this->model = new MemberLabel();
  31. }
  32. /**
  33. * 会员标签列表分页查询
  34. */
  35. public function getPage(array $where = [], string $order = 'sort desc,create_time desc')
  36. {
  37. $field = 'label_id, site_id, label_name, memo, sort, create_time, update_time';
  38. $search_model = $this->model->where([ [ 'site_id', '=', $this->site_id ] ])->withSearch([ 'label_name' ], $where)->field($field)->append([ "member_num" ])->order($order);
  39. return $this->pageQuery($search_model);
  40. }
  41. /**
  42. * 获取会员标签信息
  43. * @param int $label_id
  44. * @return array
  45. */
  46. public function getInfo(int $label_id)
  47. {
  48. $field = 'label_id, site_id, label_name, memo, sort, create_time, update_time';
  49. return $this->model->field($field)->where([ [ 'label_id', '=', $label_id ], [ 'site_id', '=', $this->site_id ] ])->findOrEmpty()->toArray();
  50. }
  51. /**
  52. * 获取标签
  53. * @return array
  54. * @throws DataNotFoundException
  55. * @throws DbException
  56. * @throws ModelNotFoundException
  57. */
  58. public function getAll()
  59. {
  60. return ( new CoreMemberLabelService() )->getAll($this->site_id);
  61. }
  62. /**
  63. * 添加会员标签
  64. * @param array $data
  65. * @return mixed
  66. */
  67. public function add(array $data)
  68. {
  69. $data[ 'site_id' ] = $this->site_id;
  70. $res = $this->model->create($data);
  71. ( new CoreMemberLabelService() )->clearCache($this->site_id);
  72. return $res->label_id;
  73. }
  74. /**
  75. * 会员标签编辑
  76. * @param int $label_id
  77. * @param array $data
  78. * @return true
  79. */
  80. public function edit(int $label_id, array $data)
  81. {
  82. $data[ 'update_time' ] = time();
  83. $this->model->where([ [ 'label_id', '=', $label_id ], [ 'site_id', '=', $this->site_id ] ])->save($data);
  84. ( new CoreMemberLabelService() )->clearCache($this->site_id);
  85. return true;
  86. }
  87. /**
  88. * 删除会员标签
  89. * @param int $label_id
  90. * @return bool
  91. */
  92. public function del(int $label_id)
  93. {
  94. $res = $this->model->where([ [ 'label_id', '=', $label_id ], [ 'site_id', '=', $this->site_id ] ])->delete();
  95. ( new CoreMemberLabelService() )->clearCache($this->site_id);
  96. return $res;
  97. }
  98. /**
  99. * 通过标签id获取标签列表
  100. * @param array $label_ids
  101. * @return Response
  102. * @throws DataNotFoundException
  103. * @throws DbException
  104. * @throws ModelNotFoundException
  105. */
  106. public function getMemberLabelListByLabelIds(array $label_ids)
  107. {
  108. $site_id = $this->site_id;
  109. sort($label_ids);
  110. $cache_name = __METHOD__ . md5(implode("_", $label_ids));
  111. return cache_remember(
  112. $cache_name,
  113. function() use ($site_id, $label_ids) {
  114. return array_keys_search($this->getAll($site_id), $label_ids, 'label_id');
  115. },
  116. self::$cache_tag_name . $site_id
  117. );
  118. }
  119. }