CategoryLogic.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\admin\logic\shop;
  20. use app\common\basics\Logic;
  21. use app\common\model\shop\Shop;
  22. use app\common\model\shop\ShopApply;
  23. use app\common\model\shop\ShopCategory;
  24. use Exception;
  25. class CategoryLogic extends Logic
  26. {
  27. /**
  28. * NOTE: 主营类目列表
  29. * @author: 张无忌
  30. * @param $get
  31. * @return array|bool
  32. */
  33. public static function lists($get)
  34. {
  35. try {
  36. $where = [
  37. ['del', '=', 0]
  38. ];
  39. if (!empty($get['name']) and $get['name'])
  40. $where[] = ['name', 'like', '%'.$get['name'].'%'];
  41. $model = new ShopCategory();
  42. $lists = $model->field(true)
  43. ->where($where)
  44. ->order('sort', 'desc')
  45. ->paginate([
  46. 'page' => $get['page'],
  47. 'list_rows' => $get['limit'],
  48. 'var_page' => 'page'
  49. ])
  50. ->toArray();
  51. return ['count'=>$lists['total'], 'lists'=>$lists['data']];
  52. } catch (Exception $e) {
  53. return ['error'=>$e->getMessage()];
  54. }
  55. }
  56. /**
  57. * NOTE: 主营类目详细
  58. * @author: 张无忌
  59. * @param $id
  60. * @return array
  61. */
  62. public static function detail($id)
  63. {
  64. $model = new ShopCategory();
  65. return $model->field(true)->findOrEmpty((int)$id)->toArray();
  66. }
  67. /**
  68. * NOTE: 获取主营类目
  69. * @author: 张无忌
  70. * @return array
  71. */
  72. public static function getCategory()
  73. {
  74. try {
  75. $model = new ShopCategory();
  76. return $model->field(true)
  77. ->where('del', 0)
  78. ->order('id', 'desc')
  79. ->order('sort', 'desc')
  80. ->select()->toArray();
  81. } catch (\Exception $e) {
  82. return [];
  83. }
  84. }
  85. /**
  86. * NOTE: 新增主营类目
  87. * @author: 张无忌
  88. * @param $post
  89. * @return bool
  90. */
  91. public static function add($post)
  92. {
  93. try {
  94. ShopCategory::create([
  95. 'name' => $post['name'],
  96. 'image' => $post['image'] ?? '',
  97. 'sort' => $post['sort'] ?? 0
  98. ]);
  99. return true;
  100. } catch (Exception $e) {
  101. static::$error = $e->getMessage();
  102. return false;
  103. }
  104. }
  105. /**
  106. * NOTE: 编辑主营类目
  107. * @author: 张无忌
  108. * @param $post
  109. * @return bool
  110. */
  111. public static function edit($post)
  112. {
  113. try {
  114. ShopCategory::update([
  115. 'name' => $post['name'],
  116. 'image' => $post['image'] ?? '',
  117. 'sort' => $post['sort'] ?? 0
  118. ], ['id'=>(int)$post['id']]);
  119. return true;
  120. } catch (Exception $e) {
  121. static::$error = $e->getMessage();
  122. return false;
  123. }
  124. }
  125. /**
  126. * NOTE: 删除主营类目
  127. * @author: 张无忌
  128. * @param $id
  129. * @return bool
  130. */
  131. public static function del($id)
  132. {
  133. try {
  134. $shopModel = new Shop();
  135. $shopApplyModel = new ShopApply();
  136. $shop = $shopModel->where(['cid'=>(int)$id, 'del'=>0])->findOrEmpty()->toArray();
  137. $apply = $shopApplyModel->where(['cid'=>(int)$id, 'del'=>0])->findOrEmpty()->toArray();
  138. if ($shop or $apply) {
  139. static::$error = '类目已被使用,不允许删除';
  140. return false;
  141. }
  142. ShopCategory::update([
  143. 'del' => 1,
  144. 'create_time' => time()
  145. ], ['id'=>(int)$id]);
  146. return true;
  147. } catch (Exception $e) {
  148. static::$error = $e->getMessage();
  149. return false;
  150. }
  151. }
  152. }