GoodsCategory.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\approve\controller;
  15. use app\common\constant\CommonConstant;
  16. use app\common\model\Goods;
  17. use app\common\model\GoodsCategory as model;
  18. use app\common\service\GoodsCategoryService;
  19. use library\Controller;
  20. use library\tools\Data;
  21. /**
  22. * 商品分类
  23. */
  24. class GoodsCategory extends Controller
  25. {
  26. /**
  27. * 绑定数据表
  28. * @var string
  29. */
  30. protected $table = 'GoodsCategory';
  31. /**
  32. * 控制器初始化
  33. */
  34. protected function initialize()
  35. {
  36. }
  37. /**
  38. * 列表
  39. * @auth true
  40. * @menu true
  41. * @throws \think\Exception
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. * @throws \think\exception\DbException
  45. */
  46. public function index()
  47. {
  48. $this->title = '商品分类列表';
  49. $query = $this->_query($this->table)
  50. ->field('is_deleted', true)
  51. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  52. ->order('sort desc,id asc');
  53. $query->page(false);
  54. }
  55. /**
  56. * 列表数据处理
  57. * @param array $data
  58. * @throws \Exception
  59. */
  60. protected function _index_page_filter(&$data)
  61. {
  62. if ($data) {
  63. foreach ($data as &$vo) {
  64. $vo['ids'] = join(',', Data::getArrSubIds($data, $vo['id']));
  65. }
  66. $data = Data::arr2table($data);
  67. }
  68. }
  69. /**
  70. * 添加
  71. * @auth true
  72. * @throws \think\Exception
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @throws \think\exception\DbException
  76. * @throws \think\exception\PDOException
  77. */
  78. public function add()
  79. {
  80. $this->title = '添加';
  81. $this->_form($this->table, 'form');
  82. }
  83. /**
  84. * 编辑
  85. * @auth true
  86. * @throws \think\Exception
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. * @throws \think\exception\DbException
  90. * @throws \think\exception\PDOException
  91. */
  92. public function edit()
  93. {
  94. $this->title = '编辑';
  95. $this->_form($this->table, 'form');
  96. }
  97. /**
  98. * 表单处理
  99. * @param array $data
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\ModelNotFoundException
  102. * @throws \think\exception\DbException
  103. */
  104. protected function _form_filter(&$data)
  105. {
  106. if ($this->request->isGet()) {
  107. $this->category_list = GoodsCategoryService::get_list([]);
  108. $this->category_list = array_merge([['id' => '0', 'pid' => '-1', 'name' => '顶级']], $this->category_list ? $this->category_list->toArray() : []);
  109. }
  110. if ($this->request->isPost()) {
  111. }
  112. }
  113. /**
  114. * 删除
  115. * @auth true
  116. * @throws \think\Exception
  117. * @throws \think\exception\PDOException
  118. */
  119. public function remove()
  120. {
  121. $this->applyCsrfToken();
  122. list($data) = [$this->request->post()];
  123. $is_goods = Goods::field('id')
  124. ->where('is_deleted', CommonConstant::IS_DELETED_0)
  125. ->when($data, function ($query) use ($data) {
  126. if ($data['pid'] == 0) {
  127. $query->where('goods_category_first', $data['id']);
  128. } else {
  129. $query->where('goods_category_id', $data['id']);
  130. }
  131. })
  132. ->find();
  133. if ($is_goods) {
  134. $this->error("该分类下有商品,不能删除!");
  135. }
  136. $this->_delete($this->table);
  137. }
  138. /**
  139. * 删除结果处理
  140. * @param boolean $result
  141. * @throws \think\Exception
  142. * @throws \think\exception\PDOException
  143. */
  144. protected function _remove_delete_result($result)
  145. {
  146. if ($result) {
  147. list($data) = [$this->request->post()];
  148. if ($data['pid'] == 0) {
  149. model::where('pid', $data['id'])->update(['is_deleted' => CommonConstant::IS_DELETED_1]);
  150. }
  151. $this->success("删除成功!", '');
  152. } else {
  153. $this->error("删除失败,请稍候再试!");
  154. }
  155. }
  156. }