GoodsSupplierValidate.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\api\model\Goods;
  21. use app\common\model\GoodsSupplier;
  22. use app\common\model\GoodsSupplierCategory;
  23. use app\common\validate\BaseValidate;
  24. class GoodsSupplierValidate extends BaseValidate
  25. {
  26. protected $rule = [
  27. 'id' => 'require|checkId|checkDel',
  28. 'code' => 'require|checkCode',
  29. 'name' => 'require|checkName',
  30. 'supplier_category_id' => 'require|checkCategory',
  31. 'mobile' => 'mobile',
  32. 'email' => 'email',
  33. 'sort' => 'number|max:5',
  34. ];
  35. protected $message = [
  36. 'code.require' => '供应商编码不能为空',
  37. 'name.require' => '供应商名称不能为空',
  38. 'supplier_category_id.require' => '供应商分类不能为空',
  39. 'mobile.mobile' => '无效的手机号码',
  40. 'email.email' => '邮箱格式不正确',
  41. 'sort.number' => '排序必须是纯数字',
  42. 'sort.max' => '排序最大不能超过五位数',
  43. ];
  44. public function sceneAdd()
  45. {
  46. return $this->only(['code','name','supplier_category_id','mobile','email','sort']);
  47. }
  48. public function sceneDel()
  49. {
  50. return $this->only(['id']);
  51. }
  52. public function sceneEdit()
  53. {
  54. return $this->only(['id','code','name','supplier_category_id','mobile','email','sort'])
  55. ->remove('id','checkDel');
  56. }
  57. public function sceneDetail()
  58. {
  59. return $this->only(['id'])
  60. ->remove('id','checkDel');
  61. }
  62. /**
  63. * @notes 检查供应商编码是否已存在
  64. * @param $value
  65. * @param $rule
  66. * @param $data
  67. * @return bool|string
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. * @author ljj
  72. * @date 2021/7/17 11:12
  73. */
  74. public function checkCode($value,$rule,$data)
  75. {
  76. $where[] = ['code', '=', $value];
  77. // 编辑的情况,要排除自身ID
  78. if (isset($data['id'])) {
  79. $where[] = ['id', '<>', $data['id']];
  80. }
  81. $result = GoodsSupplier::where($where)->select()->toArray();
  82. if ($result) {
  83. return '供应商编码已存在';
  84. }
  85. return true;
  86. }
  87. /**
  88. * @notes 检查供应商名称是否已存在
  89. * @param $value
  90. * @param $rule
  91. * @param $data
  92. * @return bool|string
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @author ljj
  97. * @date 2021/7/17 11:13
  98. */
  99. public function checkName($value,$rule,$data)
  100. {
  101. $where[] = ['name', '=', $value];
  102. // 编辑的情况,要排除自身ID
  103. if (isset($data['id'])) {
  104. $where[] = ['id', '<>', $data['id']];
  105. }
  106. $result = GoodsSupplier::where($where)->select()->toArray();
  107. if ($result) {
  108. return '该供应商名称已存在';
  109. }
  110. return true;
  111. }
  112. /**
  113. * @notes 检查供应商分类是否存在
  114. * @param $value
  115. * @param $rule
  116. * @param $data
  117. * @return bool|string
  118. * @author ljj
  119. * @date 2021/7/17 11:18
  120. */
  121. public function checkCategory($value,$rule,$data)
  122. {
  123. $result = GoodsSupplierCategory::findOrEmpty($value);
  124. if ($result->isEmpty()) {
  125. return '供应商分类不存在';
  126. }
  127. return true;
  128. }
  129. /**
  130. * @notes 检查供应商id是否存在
  131. * @param $value
  132. * @param $rule
  133. * @param $data
  134. * @return bool|string
  135. * @author ljj
  136. * @date 2021/7/17 2:50
  137. */
  138. public function checkId($value,$rule,$data)
  139. {
  140. $result = GoodsSupplier::findOrEmpty($value);
  141. if ($result->isEmpty()) {
  142. return '供应商不存在';
  143. }
  144. return true;
  145. }
  146. /**
  147. * @notes 检查供应商是否能删除
  148. * @param $value
  149. * @param $rule
  150. * @param $data
  151. * @return bool|string
  152. * @throws \think\db\exception\DataNotFoundException
  153. * @throws \think\db\exception\DbException
  154. * @throws \think\db\exception\ModelNotFoundException
  155. * @throws \think\exception\DbException
  156. * @author ljj
  157. * @date 2021/7/17 3:14
  158. */
  159. public function checkDel($value,$rule,$data)
  160. {
  161. $result = GoodsSupplier::hasWhere('goods',['supplier_id'=>$value])->select()->toArray();
  162. if (!empty($result)) {
  163. return '供应商正在使用中,无法删除';
  164. }
  165. return true;
  166. }
  167. }