GoodsUnitValidate.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\GoodsUnit;
  22. use app\common\validate\BaseValidate;
  23. class GoodsUnitValidate extends BaseValidate
  24. {
  25. protected $rule = [
  26. 'id' => 'require|checkId',
  27. 'name' => 'require|checkName',
  28. 'sort' => 'number|max:5',
  29. ];
  30. protected $message = [
  31. 'name.require' => '商品单位名称不能为空',
  32. 'sort.number' => '排序只能是纯数字',
  33. 'sort.max' => '排序最大不能超过五位数',
  34. ];
  35. public function sceneAdd()
  36. {
  37. return $this->only(['name','sort']);
  38. }
  39. public function sceneDel()
  40. {
  41. return $this->only(['id'])
  42. ->append('id','checkDel');
  43. }
  44. public function sceneEdit()
  45. {
  46. return $this->only(['id','name','sort']);
  47. }
  48. public function sceneDetail()
  49. {
  50. return $this->only(['id']);
  51. }
  52. /**
  53. * @notes 检查商品单位名称是否已存在
  54. * @param $value
  55. * @param $rule
  56. * @param $data
  57. * @return bool|string
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. * @author ljj
  62. * @date 2021/7/19 5:55 下午
  63. */
  64. public function checkName($value,$rule,$data)
  65. {
  66. $where[] = ['name', '=', $value];
  67. // 编辑的情况,要排除自身ID
  68. if (isset($data['id'])) {
  69. $where[] = ['id', '<>', $data['id']];
  70. }
  71. $result = GoodsUnit::where($where)->select()->toArray();
  72. if ($result) {
  73. return '商品单位名称已存在';
  74. }
  75. return true;
  76. }
  77. /**
  78. * @notes 检查商品单位ID是否存在
  79. * @param $value
  80. * @param $rule
  81. * @param $data
  82. * @return bool|string
  83. * @author ljj
  84. * @date 2021/7/19 6:39 下午
  85. */
  86. public function checkId($value,$rule,$data)
  87. {
  88. $result = GoodsUnit::findOrEmpty($value);
  89. if ($result->isEmpty()) {
  90. return '商品单位不存在';
  91. }
  92. return true;
  93. }
  94. /**
  95. * @notes 检查商品单位是否能删除
  96. * @param $value
  97. * @param $rule
  98. * @param $data
  99. * @return bool|string
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. * @author ljj
  104. * @date 2021/7/19 6:41 下午
  105. */
  106. public function checkDel($value,$rule,$data)
  107. {
  108. $result = Goods::where('unit_id',$value)->select()->toArray();
  109. if (!empty($result)) {
  110. return '该商品单位正在使用中,无法删除';
  111. }
  112. return true;
  113. }
  114. }