LevelService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace addons\OperationCenter\common\services;
  3. use addons\OperationCenter\common\models\Level;
  4. use addons\OperationCenter\common\models\UserCenter;
  5. /**
  6. * LevelService
  7. * @package addons\OperationCenter\common\services
  8. */
  9. class LevelService extends BaseService
  10. {
  11. /**
  12. * 获取分页列表
  13. *
  14. * @param array $params
  15. * @return array
  16. */
  17. public function getPageList(array $params = [])
  18. {
  19. return Level::getPageList(
  20. $this->getPage($params), $this->getLimit($params), function ($query) {
  21. $query->andWhere(['mall_id' => $this->mall_id]);
  22. },'level asc,id desc'
  23. );
  24. }
  25. /**
  26. * 获取等级列表
  27. *
  28. * @return array
  29. */
  30. public function getLevelList()
  31. {
  32. return Level::getOpenLevelList($this->mall_id);
  33. }
  34. /**
  35. * 获取等级数据
  36. *
  37. * @param integer|null $id
  38. * @return array
  39. */
  40. public function getLevelData(int $id = null)
  41. {
  42. $detail = [];
  43. $weights = Level::getLevelWeights($this->mall_id);
  44. if ($id) $detail = Level::getLevelDetail($this->mall_id, $id);
  45. if (!empty($detail)) {
  46. $detail = $detail->jsonDecode();
  47. $detail = $detail->toArray();
  48. }
  49. return compact('weights', 'detail');
  50. }
  51. /**
  52. * 编辑等级
  53. *
  54. * @param int|null $id
  55. * @param array $data
  56. * @return boolean
  57. */
  58. public function setLevelData($id, array $data)
  59. {
  60. $model = empty($id) ? new Level() : Level::getLevelDetail($this->mall_id, $id);
  61. $res = $model->setLevelData(array_merge($data, ['mall_id' => $this->mall_id]));
  62. return $res === true
  63. ? true
  64. : $this->modelError($res);
  65. }
  66. /**
  67. * 切换状态
  68. *
  69. * @param integer $id
  70. * @param integer $status
  71. * @return boolean
  72. */
  73. public function switchStatus(int $id, int $status)
  74. {
  75. $model = Level::getLevelDetail($this->mall_id, $id);
  76. if (empty($model)) return $this->error('等级不存在');
  77. $res = $model->setLevelData(['status' => $status]);
  78. return $res === true
  79. ? true
  80. : $this->modelError($res);
  81. }
  82. /**
  83. * 删除等级
  84. *
  85. * @param integer $id
  86. * @return boolean
  87. */
  88. public function delLevel(int $id)
  89. {
  90. $model = Level::getLevelDetail($this->mall_id, $id);
  91. if (empty($model)) return $this->error('等级不存在');
  92. // 被使用等级无法删除
  93. if (UserCenter::hasLevel($model)) return $this->error('该等级已被使用 无法删除');
  94. $res = $model->delLevel();
  95. return $res === true
  96. ? true
  97. : $this->modelError($res);
  98. }
  99. }