BargainActivityValidate.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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\bargain;
  20. use app\common\enum\BargainEnum;
  21. use app\common\enum\GoodsEnum;
  22. use app\common\logic\CommonPresellLogic;
  23. use app\common\model\BargainActivity;
  24. use app\common\model\Goods;
  25. use app\common\validate\BaseValidate;
  26. /**
  27. * 砍价活动验证器
  28. * Class BargainActivityValidate
  29. * @package app\adminapi\validate\bargain
  30. */
  31. class BargainActivityValidate extends BaseValidate
  32. {
  33. protected $rule = [
  34. 'goods_ids' => 'require|array|checkCount',
  35. 'name' => 'require|length:1,100',
  36. 'start_time' => 'require|checkTime',
  37. 'end_time' => 'require|checkTime',
  38. 'remark' => 'length:1,100',
  39. // 'is_distribution' => 'require|in:0,1',
  40. 'buy_condition' => 'require|in:1,2',
  41. 'valid_period' => 'require|integer|gt:0',
  42. 'help_num' => 'require|integer|gt:0',
  43. 'knife_amount_type' => 'require|in:1,2',
  44. 'self' => 'require|in:0,1',
  45. 'count' => 'require|integer|gt:0',
  46. // 'buy_limit' => 'require|integer',
  47. 'order_limit' => 'require|integer',
  48. // 'use_coupon' => 'require|in:0,1',
  49. 'goods' => 'require|array|checkGoods',
  50. 'id' => 'require'
  51. ];
  52. protected $message = [
  53. 'goods_ids.require' => '请选择商品',
  54. 'goods_ids.array' => '商品数据须为数组格式',
  55. 'name.require' => '请输入砍价活动名称',
  56. 'name.length' => '砍价活动名称长度过长',
  57. 'start_time.require' => '请选择活动开始时间',
  58. 'end_time.require' => '请选择活动结束时间',
  59. 'remark.length' => '备注长度过长',
  60. 'is_distribution.require' => '请选择是否参与分销',
  61. 'is_distribution.in' => '分销状态值错误',
  62. 'buy_condition.require' => '请选择购买条件',
  63. 'buy_condition.in' => '购买条件状态值错误',
  64. 'valid_period.require' => '请填写有效期',
  65. 'valid_period.integer' => '有效期须为整型',
  66. 'valid_period.gt' => '有效期须大于0',
  67. 'help_num.require' => '请填写帮砍人数',
  68. 'help_num.integer' => '帮砍人数须为整型',
  69. 'help_num.gt' => '帮砍人数须大于0',
  70. 'knife_amount_type.require' => '请选择每刀金额类型',
  71. 'knife_amount_type.in' => '每刀金额类型值有误',
  72. 'self.require' => '请选择自己是否能砍价',
  73. 'self.in' => '自已砍价状态值错误',
  74. 'count.require' => '请填写最大可发起砍价次数',
  75. 'count.integer' => '最大可发起砍价次数须为整型',
  76. 'count.gt' => '最大可发起砍价次数须大于0',
  77. 'buy_limit.require' => '请选择起购限制',
  78. 'buy_limit.integer' => '起购限制值须为整型',
  79. 'order_limit.require' => '请选择每单限制',
  80. 'order_limit.integer' => '每单限制须为整型',
  81. 'use_coupon.require' => '请选择是否可使用优惠券',
  82. 'use_coupon.in' => '优惠券类型状态值错误',
  83. 'goods.require' => '请选择活动商品',
  84. 'goods.array' => '活动商品格式须为数组',
  85. 'id.require' => '参数缺失',
  86. ];
  87. /**
  88. * @notes 选择商品场景
  89. * @return BargainActivityValidate
  90. * @author Tab
  91. * @date 2021/8/26 16:15
  92. */
  93. public function sceneChooseGoods()
  94. {
  95. return $this->only(['goods_ids']);
  96. }
  97. /**
  98. * @notes 添加砍价活动场景
  99. * @return BargainActivityValidate
  100. * @author Tab
  101. * @date 2021/8/27 11:00
  102. */
  103. public function sceneAdd()
  104. {
  105. return $this->remove('goods_ids', 'require|array|checkCount')
  106. ->remove('id', 'require');
  107. }
  108. /**
  109. * @notes 查看砍价详情场景
  110. * @return BargainActivityValidate
  111. * @author Tab
  112. * @date 2021/8/27 11:57
  113. */
  114. public function sceneDetail()
  115. {
  116. return $this->only(['id']);
  117. }
  118. /**
  119. * @notes 编辑场景
  120. * @return BargainActivityValidate
  121. * @author Tab
  122. * @date 2021/8/27 15:12
  123. */
  124. public function sceneEdit()
  125. {
  126. return $this->only(['id']);
  127. }
  128. /**
  129. * @notes 编辑进行中的砍价活动场景
  130. * @return BargainActivityValidate
  131. * @author Tab
  132. * @date 2021/8/27 16:01
  133. */
  134. public function sceneEditIng()
  135. {
  136. return $this->only(['id', 'name', 'end_time', 'remark']);
  137. }
  138. /**
  139. * @notes 编辑未开始的砍价活动场景
  140. * @return BargainActivityValidate
  141. * @author Tab
  142. * @date 2021/8/27 16:18
  143. */
  144. public function sceneEditWait()
  145. {
  146. return $this->remove('goods_ids', 'require|array|checkCount');
  147. }
  148. /**
  149. * @notes 确认砍价活动场景
  150. * @return BargainActivityValidate
  151. * @author Tab
  152. * @date 2021/8/27 17:10
  153. */
  154. public function sceneConfirm()
  155. {
  156. return $this->only(['id']);
  157. }
  158. /**
  159. * @notes 结束砍价活动场景
  160. * @return BargainActivityValidate
  161. * @author Tab
  162. * @date 2021/8/27 17:27
  163. */
  164. public function sceneStop()
  165. {
  166. return $this->only(['id']);
  167. }
  168. /**
  169. * @notes 删除砍价活动
  170. * @return BargainActivityValidate
  171. * @author Tab
  172. * @date 2021/8/27 17:40
  173. */
  174. public function sceneDelete()
  175. {
  176. return $this->only(['id']);
  177. }
  178. /**
  179. * @notes 活动数据
  180. * @return BargainActivityValidate
  181. * @author Tab
  182. * @date 2021/9/23 18:54
  183. */
  184. public function sceneActivityData()
  185. {
  186. return $this->only(['id']);
  187. }
  188. /**
  189. * @notes 活动记录
  190. * @return BargainActivityValidate
  191. * @author Tab
  192. * @date 2021/9/24 15:37
  193. */
  194. public function sceneActivityRecord()
  195. {
  196. return $this->only(['id']);
  197. }
  198. /**
  199. * @notes 结束砍价记录
  200. * @return BargainActivityValidate
  201. * @author Tab
  202. * @date 2021/9/24 18:11
  203. */
  204. public function sceneStopInitiate()
  205. {
  206. return $this->only(['id']);
  207. }
  208. /**
  209. * @notes 校验所选商品数量
  210. * @param $goodsIds
  211. * @return bool|string
  212. * @author Tab
  213. * @date 2021/8/26 16:03
  214. */
  215. public function checkCount($goodsIds)
  216. {
  217. if(count($goodsIds) > 25) {
  218. return '最多可选25个商品';
  219. }
  220. return true;
  221. }
  222. /**
  223. * @notes 校验时间格式
  224. * @param $time
  225. * @return bool|string
  226. * @author Tab
  227. * @date 2021/8/27 10:19
  228. */
  229. public function checkTime($time)
  230. {
  231. if(!strtotime($time)) {
  232. return '时间格式错误';
  233. }
  234. return true;
  235. }
  236. /**
  237. * @notes 校验参与活动的砍价商品
  238. * @param $goods
  239. * @return bool|string
  240. * @author Tab
  241. * @date 2021/8/27 14:28
  242. */
  243. public function checkGoods($goods, $rule, $data)
  244. {
  245. if(!count($goods) > 25) {
  246. return '最多可选25个商品';
  247. }
  248. $goodsIds = array_column($goods, 'goods_id');
  249. $where = [
  250. ['ba.status', 'in', [BargainEnum::ACTIVITY_STATUS_ING, BargainEnum::ACTIVITY_STATUS_WAIT]],
  251. ['bg.goods_id', 'in', $goodsIds],
  252. ];
  253. // 编辑时排除当前活动
  254. if (isset($data['id'])) {
  255. $where[] = ['ba.id', '<>', $data['id']];
  256. }
  257. $goodsVirtual = Goods::where(['id'=>$goodsIds,'type'=>GoodsEnum::GOODS_VIRTUAL])->find();
  258. if($goodsVirtual){
  259. return '虚拟商品不能参加营销活动';
  260. }
  261. $count = BargainActivity::alias('ba')
  262. ->leftJoin('bargain_goods bg', 'bg.activity_id = ba.id')
  263. ->where($where)
  264. ->count();
  265. if($count > 0) {
  266. return '部分商品正在参与砍价活动中';
  267. }
  268. foreach ($goodsIds as $goodsId) {
  269. if ($this->currentScene == 'add' && CommonPresellLogic::checkGoodsHas($goodsId)) {
  270. $goods = Goods::findOrEmpty($goodsId);
  271. return '商品:' . ($goods['name'] ?? '') . ' 正在参与预售活动,不能添加';
  272. }
  273. }
  274. return true;
  275. }
  276. }