Level.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\admin\controller\commission;
  3. use app\common\controller\Backend;
  4. use app\common\model\commission\Level as LevelModel;
  5. use think\Db;
  6. class Level extends Backend
  7. {
  8. protected $noNeedRight = ['select'];
  9. protected $model = null;
  10. /**
  11. * 快速搜索时执行查找的字段
  12. */
  13. protected $searchFields = 'level,name';
  14. /**
  15. * 是否是关联查询
  16. */
  17. protected $relationSearch = false;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new LevelModel();
  22. // 确保默认等级存在
  23. $this->ensureDefaultLevel();
  24. }
  25. /**
  26. * 确保默认等级存在
  27. */
  28. private function ensureDefaultLevel()
  29. {
  30. $defaultLevel = $this->model->find(1);
  31. if (!$defaultLevel) {
  32. $this->model->save([
  33. 'name' => '默认等级',
  34. 'level' => 1,
  35. 'commission_rules' => [
  36. 'commission_self' => '0.00',
  37. 'commission_first' => '0.00',
  38. 'commission_second' => '0.00'
  39. ]
  40. ]);
  41. }
  42. }
  43. /**
  44. * 添加
  45. */
  46. public function add()
  47. {
  48. if (!$this->request->isAjax()) {
  49. return $this->view->fetch();
  50. }
  51. $params = $this->request->only(['name', 'level', 'image', 'commission_rules', 'upgrade_type', 'upgrade_rules']);
  52. $this->model->save($params);
  53. $this->success('保存成功', null, $this->model);
  54. }
  55. /**
  56. * 编辑
  57. *
  58. * @param $id
  59. */
  60. public function edit($id = null)
  61. {
  62. if (!$this->request->isAjax()) {
  63. return $this->view->fetch('add');
  64. }
  65. $params = $this->request->only(['level', 'name', 'image', 'commission_rules', 'upgrade_type', 'upgrade_rules']);
  66. $result = Db::transaction(function () use ($id, $params) {
  67. $this->svalidate($params);
  68. $data = $this->model->where('level', $id)->find();
  69. if (!$data) {
  70. $this->error(__('No Results were found'));
  71. }
  72. return $data->save($params);
  73. });
  74. if ($result) {
  75. $this->success('更新成功', null, $result);
  76. } else {
  77. $this->error('更新失败');
  78. }
  79. }
  80. /**
  81. * 详情
  82. *
  83. * @param $id
  84. * @return \think\Response
  85. */
  86. public function detail($id)
  87. {
  88. $detail = $this->model->get($id);
  89. if (!$detail) {
  90. $this->error(__('No Results were found'));
  91. }
  92. $this->success('等级详情', null, $detail);
  93. }
  94. /**
  95. * 删除
  96. *
  97. * @param $id
  98. * @return \think\Response
  99. */
  100. public function delete($id)
  101. {
  102. if (empty($id)) {
  103. $this->error(__('Parameter %s can not be empty', 'id'));
  104. }
  105. $result = Db::transaction(function () use ($id) {
  106. return $this->model->where('level', $id)->delete();
  107. });
  108. if ($result) {
  109. $this->success('删除成功', null, $result);
  110. } else {
  111. $this->error(__('No rows were deleted'));
  112. }
  113. }
  114. // 选择分销商等级
  115. public function select()
  116. {
  117. if (!$this->request->isAjax()) {
  118. return $this->view->fetch();
  119. }
  120. $data = $this->model->sheepFilter()->field('level, name, image, commission_rules')->select();
  121. $this->success('选择等级', null, $data);
  122. }
  123. }