LuckyDrawValidate.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\lucky_draw;
  20. use app\common\enum\CouponEnum;
  21. use app\common\enum\GoodsEnum;
  22. use app\common\enum\LuckyDrawEnum;
  23. use app\common\model\Coupon;
  24. use app\common\model\Goods;
  25. use app\common\model\LuckyDraw;
  26. use app\common\validate\BaseValidate;
  27. /**
  28. * 幸运抽奖
  29. */
  30. class LuckyDrawValidate extends BaseValidate
  31. {
  32. protected $rule = [
  33. 'id' => 'require|checkActivity',
  34. 'name' => 'require|max:60',
  35. 'start_time' => 'require',
  36. 'end_time' => 'require',
  37. 'need_integral' => 'require|integer|egt:0',
  38. 'frequency_type' => 'require|integer|in:0,1',
  39. 'frequency' => 'require|integer|egt:0',
  40. 'rule' => 'require',
  41. 'show_winning_list' => 'require|in:0,1',
  42. 'prizes' => 'require|array|length:8|checkPrizes'
  43. ];
  44. protected $message = [
  45. 'id.require' => '参数缺失',
  46. 'name.require' => '请输入活动名称',
  47. 'name.max' => '活动名称不能超过60个字符',
  48. 'start_time.require' => '选择活动开始时间',
  49. 'end_time.require' => '选择活动结束时间',
  50. 'need_integral.require' => '请输入消耗的积分',
  51. 'need_integral.integer' => '消耗的积分须为整型',
  52. 'need_integral.egt' => '消耗的积分不能为负数',
  53. 'frequency_type.require' => '请选择抽奖次数类型',
  54. 'frequency_type.integer' => '抽奖次数类型须为整数',
  55. 'frequency_type.in' => '抽奖次数类型值错误',
  56. 'frequency.require' => '请输入抽奖次数',
  57. 'frequency.integer' => '抽奖次数须数整型',
  58. 'frequency.egt' => '抽奖次数不能为负数',
  59. 'rule.require' => '请输入抽奖规则',
  60. 'show_winning_list.require' => '请选择是否显示中奖名单',
  61. 'show_winning_list.in' => '是否显示中奖名单状态值错误',
  62. 'prizes.require' => '请填写抽奖奖品',
  63. 'prizes.array' => '抽奖奖品须为数组格式',
  64. 'prizes.length' => '抽奖奖品须为8个',
  65. ];
  66. public function sceneAdd()
  67. {
  68. return $this->only(['name', 'start_time', 'end_time', 'need_integral', 'frequency_type', 'frequency', 'rule', 'show_winning_list', 'prizes']);
  69. }
  70. public function sceneDetail()
  71. {
  72. return $this->only(['id']);
  73. }
  74. public function sceneEdit()
  75. {
  76. return $this->only(['id', 'name', 'start_time', 'end_time', 'need_integral', 'frequency_type', 'frequency', 'rule', 'show_winning_list', 'prizes']);
  77. }
  78. public function sceneStart()
  79. {
  80. return $this->only(['id']);
  81. }
  82. public function sceneEnd()
  83. {
  84. return $this->only(['id']);
  85. }
  86. public function sceneDelete()
  87. {
  88. return $this->only(['id']);
  89. }
  90. public function sceneRecord()
  91. {
  92. return $this->only(['id']);
  93. }
  94. /**
  95. * @notes 校验奖品
  96. * @param $value
  97. * @param $rule
  98. * @param $data
  99. * @author Tab
  100. * @date 2021/11/24 10:42
  101. */
  102. public function checkPrizes($prizes, $rule, $data)
  103. {
  104. $sumProbability = 0;
  105. $winPrizeCount = 0;
  106. foreach ($prizes as $key => $item) {
  107. if (isset($data['id']) && !isset($item['id'])) {
  108. // 编辑的时候奖品id要携带过来
  109. return 'error01:位置为'. ($key + 1) . '的奖品格式错误';
  110. }
  111. if (!isset($item['name'], $item['image'], $item['type'], $item['type_value'], $item['num'], $item['probability'], $item['tips'])) {
  112. return 'error02:位置为'. ($key + 1) . '的奖品格式错误';
  113. }
  114. if ((int)$item['type_value'] < 0 || (int)$item['num'] < 0 || (int)$item['probability'] < 0) {
  115. return 'error03:位置为'. ($key + 1) . '的奖品不能出现负数';
  116. }
  117. if ($item['type'] == LuckyDrawEnum::COUPON && !$this->checkCoupon($item['type_value'])) {
  118. return '位置为'. ($key + 1) . '的优惠券无效';
  119. }
  120. if ($item['type'] == LuckyDrawEnum::GOODS && !$this->checkGoods($item['type_value'])) {
  121. return '位置为'. ($key + 1) . '的商品无效';
  122. }
  123. if (in_array($item['type'], LuckyDrawEnum::WIN_PRIZE_TYPE)) {
  124. // 累计奖品中奖概率(乘以100将小数转为整数)
  125. // $baseNum = isset($data['id']) ? 1 : 100; // 编辑时中奖概率已乘过基数
  126. $baseNum = 100; // 编辑时中奖概率已乘过基数
  127. if(empty($item['probability'])){
  128. return '请输入位置为'. ($key + 1) . '的中奖概率';
  129. }
  130. $sumProbability += (int)(round((float)(isset($data['id']) ? $item['probability_desc'] : $item['probability']), 2) * $baseNum);
  131. $winPrizeCount ++;
  132. }
  133. if (!is_numeric($item['type_value']) || !is_numeric($item['num']) || !is_numeric($item['probability'])) {
  134. return '位置为'. ($key + 1) . '的奖品,奖品类型的值、数量、概率须为数字';
  135. }
  136. }
  137. if ($sumProbability > 10000) {
  138. return '奖品中奖概率不能超过100%';
  139. }
  140. if ($winPrizeCount == 8 && $sumProbability != 10000) {
  141. return '全部都是奖品,中奖概率之和需为100%';
  142. }
  143. return true;
  144. }
  145. /**
  146. * @notes 校验优惠券
  147. * @param $couponId
  148. * @return bool
  149. * @author Tab
  150. * @date 2021/11/24 11:05
  151. */
  152. public function checkCoupon($couponId) {
  153. $coupon = Coupon::findOrEmpty($couponId);
  154. if ($coupon->isEmpty() || $coupon->get_type != CouponEnum::GET_TYPE_STORE || $coupon->status != CouponEnum::COUPON_STATUS_CONDUCT) {
  155. // 优惠券不存在 || 不是卖家发放形式 || 发放状态不是进行中
  156. return false;
  157. }
  158. return true;
  159. }
  160. /**
  161. * @notes 校验商品
  162. * @param $goodsId
  163. * @return bool
  164. * @author ljj
  165. * @date 2024/7/30 上午11:56
  166. */
  167. public function checkGoods($goodsId)
  168. {
  169. $goods = Goods::findOrEmpty($goodsId);
  170. if ($goods->isEmpty() || $goods->status != GoodsEnum::STATUS_SELL) {
  171. // 商品不存在 || 商品状态非上架
  172. return false;
  173. }
  174. return true;
  175. }
  176. /**
  177. * @notes 校验活动
  178. * @param $activityId
  179. * @author Tab
  180. * @date 2021/11/24 14:04
  181. */
  182. public function checkActivity($activityId)
  183. {
  184. $activity = LuckyDraw::findOrEmpty($activityId);
  185. if ($activity->isEmpty()) {
  186. return '活动不存在';
  187. }
  188. return true;
  189. }
  190. }