GoodsCategoryValidate.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\validate\goods;
  20. use app\common\model\Goods;
  21. use app\common\model\GoodsCategory;
  22. use app\common\model\GoodsCategoryIndex;
  23. use app\common\validate\BaseValidate;
  24. class GoodsCategoryValidate extends BaseValidate
  25. {
  26. protected $rule = [
  27. 'id' => 'require|checkId',
  28. 'name' => 'require|max:8|checkName',
  29. 'pid' => 'checkLevel',
  30. 'sort' => 'number|max:5',
  31. 'is_show' => 'in:0,1',
  32. 'is_recommend' => 'in:0,1',
  33. ];
  34. protected $message = [
  35. 'name.require' => '分类名称不能为空',
  36. 'name.max' => '分类名称字数不能超过8个',
  37. 'sort.number' => '排序必须为纯数字',
  38. 'sort.max' => '排序最大不能超过五位数',
  39. ];
  40. public function sceneAdd()
  41. {
  42. return $this->only(['name','pid','sort','is_show','is_recommend']);
  43. }
  44. public function sceneStatus()
  45. {
  46. return $this->only(['id','is_show'])
  47. ->append('is_show','require');
  48. }
  49. public function sceneDel()
  50. {
  51. return $this->only(['id'])
  52. ->append('id','checkDel');
  53. }
  54. public function sceneEdit()
  55. {
  56. return $this->only(['id','name','pid','sort','is_show','is_recommend'])
  57. ->append('pid','checkPid');
  58. }
  59. public function sceneDetail()
  60. {
  61. return $this->only(['id']);
  62. }
  63. /**
  64. * @notes 检查商品分类名称是否已存在
  65. * @param $value
  66. * @param $rule
  67. * @param $data
  68. * @return bool|string
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. * @author ljj
  73. * @date 2021/7/17 4:56 下午
  74. */
  75. public function checkName($value,$rule,$data)
  76. {
  77. // $where[] = ['name', '=', $value];
  78. // // 编辑的情况,要排除自身ID
  79. // if (isset($data['id'])) {
  80. // $where[] = ['id', '<>', $data['id']];
  81. // }
  82. // $result = GoodsCategory::where($where)->select()->toArray();
  83. // if ($result) {
  84. // return '该商品分类名称已存在';
  85. // }
  86. return true;
  87. }
  88. /**
  89. * @notes 检查父级分类等级
  90. * @param $value
  91. * @param $rule
  92. * @param $data
  93. * @return bool|string
  94. * @author ljj
  95. * @date 2021/7/17 5:22 下午
  96. */
  97. public function checkLevel($value,$rule,$data)
  98. {
  99. if (!isset($value)) {
  100. return true;
  101. }
  102. $level = GoodsCategory::where('id',$value)->value('level');
  103. if (empty($level)) {
  104. return '所选父级分类不存在';
  105. }
  106. if ($level > 2) {
  107. return '所选父级分类已经是最大分级';
  108. }
  109. //编辑
  110. if (isset($data['id'])) {
  111. $category_two = GoodsCategory::where('pid',$data['id'])->find();
  112. if (!empty($category_two)&& $level > 1) {
  113. return '所选父级分类超过最大分级';
  114. }
  115. $category_three = !empty($category_two) ? GoodsCategory::where('pid',$category_two['id'])->find() : [];
  116. if (!empty($category_three)) {
  117. return '目前分类已达最大分级,不能选择父级分类';
  118. }
  119. }
  120. return true;
  121. }
  122. /**
  123. * @notes 检查分类是否存在
  124. * @param $value
  125. * @param $rule
  126. * @param $data
  127. * @return bool|string
  128. * @author ljj
  129. * @date 2021/7/19 11:49 上午
  130. */
  131. public function checkId($value,$rule,$data)
  132. {
  133. $result = GoodsCategory::findOrEmpty($value);
  134. if ($result->isEmpty()) {
  135. return '分类不存在';
  136. }
  137. return true;
  138. }
  139. /**
  140. * @notes 检查商品分类能否删除
  141. * @param $value
  142. * @param $rule
  143. * @param $data
  144. * @return bool|string
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\DbException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. * @author ljj
  149. * @date 2021/7/19 12:06 下午
  150. */
  151. public function checkDel($value,$rule,$data)
  152. {
  153. $result = GoodsCategoryIndex::alias('GCI')
  154. ->join('goods G','GCI.goods_id = G.id')
  155. ->where('category_id',$value)
  156. ->whereNull('delete_time')
  157. ->field("GCI.*")
  158. ->select()
  159. ->toArray();
  160. if (!empty($result)) {
  161. return '商品分类已使用,需移除分类关联的商品后再作删除';
  162. }
  163. $result = GoodsCategory::where('pid',$value)->select()->toArray();
  164. if (!empty($result)) {
  165. return '该分类存在下级,无法删除';
  166. }
  167. return true;
  168. }
  169. /**
  170. * @notes 检验父级分类
  171. * @param $value
  172. * @param $rule
  173. * @param $data
  174. * @return bool|string
  175. * @author ljj
  176. * @date 2021/9/14 5:17 下午
  177. */
  178. public function checkPid($value,$rule,$data)
  179. {
  180. if ($value == $data['id']) {
  181. return '不能选择自己作为父级分类';
  182. }
  183. return true;
  184. }
  185. }