PresellValidate.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\adminapi\validate\marketing;
  3. use app\common\enum\GoodsEnum;
  4. use app\common\enum\PresellEnum;
  5. use app\common\logic\CommonPresellLogic;
  6. use app\common\logic\GoodsActivityLogic;
  7. use app\common\model\Goods;
  8. use app\common\model\GoodsItem;
  9. use app\common\model\Presell;
  10. use app\common\validate\BaseValidate;
  11. class PresellValidate extends BaseValidate
  12. {
  13. protected $rule = [
  14. 'id' => [ 'require' ],
  15. 'name' => [ 'require', 'max' => 50 ],
  16. 'type' => [ 'require', 'in' => PresellEnum::TYPES ],
  17. 'start_time' => [ 'require', 'dateFormat:Y-m-d H:i:s' ],
  18. 'end_time' => [ 'require', 'dateFormat:Y-m-d H:i:s', 'checkEndTime' ],
  19. 'remark' => [ 'max' => 100 ],
  20. 'goods' => [ 'array', 'checkGoods' ],
  21. 'send_type' => [ 'require', 'in' => PresellEnum::SEND_TYPES ],
  22. 'send_type_day' => [ 'require', 'number', 'gt' => 0 ],
  23. 'buy_limit' => [ 'require', 'in' => [ 0, 1 ], 'checkBuyLimit' ],
  24. ];
  25. protected $field = [
  26. 'id' => 'ID',
  27. 'name' => '活动名称',
  28. 'type' => '预售类型',
  29. 'start_time' => '开始时间',
  30. 'end_time' => '结束时间',
  31. 'remark' => '备注',
  32. 'goods' => '商品信息',
  33. 'goods.array' => '商品信息格式错误',
  34. 'send_type' => '发货时间',
  35. 'send_type_day' => '发货天数',
  36. 'buy_limit' => '每单限制',
  37. ];
  38. protected $message = [
  39. 'type.in' => '预售类型错误',
  40. 'send_type' => '发货时间错误',
  41. 'buy_limit' => '每单限制错误',
  42. ];
  43. function sceneAdd()
  44. {
  45. return $this->only([
  46. 'name',
  47. 'type',
  48. 'start_time',
  49. 'end_time',
  50. 'remark',
  51. 'goods',
  52. 'send_type',
  53. 'send_type_day',
  54. 'buy_limit',
  55. ])->append('goods', 'require');
  56. }
  57. function sceneEdit()
  58. {
  59. return $this->only([ 'id' ])->append('id', 'checkIdEdit');
  60. }
  61. function sceneEditWait()
  62. {
  63. return $this->only([
  64. 'name',
  65. 'type',
  66. 'start_time',
  67. 'end_time',
  68. 'remark',
  69. 'goods',
  70. 'send_type',
  71. 'send_type_day',
  72. 'buy_limit',
  73. ])->append('goods', 'require');
  74. }
  75. function sceneEditStart()
  76. {
  77. return $this->only([
  78. 'name',
  79. 'remark',
  80. ]);
  81. }
  82. function sceneDetail()
  83. {
  84. return $this->only([ 'id' ]);
  85. }
  86. function checkEndTime($endTime, $rule, $data)
  87. {
  88. if (strtotime($endTime) <= strtotime($data['start_time'])) {
  89. return '活动结束时间必须大于活动开始时间';
  90. }
  91. return true;
  92. }
  93. function checkGoods($goods, $rule, $data)
  94. {
  95. if (count($data['goods']) > 25) {
  96. return '选择的商品不能大于25个';
  97. }
  98. foreach ($goods as $goodsInfo) {
  99. // 检测商品
  100. if (empty($goodsInfo)) {
  101. return '商品不能为空';
  102. }
  103. $goodsdDetail = Goods::findOrEmpty($goodsInfo['goods_id'] ?? '');
  104. if (empty($goodsdDetail['id'])) {
  105. return '商品不存在';
  106. }
  107. if (GoodsEnum::GOODS_VIRTUAL == $goodsdDetail['type']) {
  108. return '虚拟商品不能参加营销活动';
  109. }
  110. if ($this->currentScene == 'add') {
  111. $goodsActivityInfo = GoodsActivityLogic::goodsActivityInfo($goodsdDetail['id']);
  112. if ($goodsActivityInfo) {
  113. return '商品:' . $goodsdDetail['name'] . ' 正在参与' . $goodsActivityInfo['activity_text'] . ',不能添加';
  114. }
  115. }
  116. if (! empty($goodsInfo['virtual_sale']) && (! is_numeric($goodsInfo['virtual_sale']) || $goodsInfo['virtual_sale'] <= 0)) {
  117. return '虚拟销量值错误';
  118. }
  119. if (! empty($goodsInfo['virtual_click']) && (! is_numeric($goodsInfo['virtual_click']) || $goodsInfo['virtual_click'] <= 0)) {
  120. return '虚拟浏览量值错误';
  121. }
  122. // 检测商品规格
  123. foreach ($goodsInfo['items'] as $item) {
  124. if (empty($item['item_id'])) {
  125. return '商品规格item_id缺少';
  126. }
  127. $goodsItemDetail = GoodsItem::where('goods_id', $goodsdDetail['id'])->findOrEmpty($item['item_id']);
  128. if (empty($goodsItemDetail['id'])) {
  129. return '商品规格不存在';
  130. }
  131. if (empty($item['price'])) {
  132. return '商品规格预售价格缺少';
  133. }
  134. if ($item['price'] >= $goodsItemDetail['sell_price']) {
  135. return '商品规格预售价格必须低于售价';
  136. }
  137. }
  138. }
  139. return true;
  140. }
  141. function checkBuyLimit($buy_limit, $rule, $data)
  142. {
  143. if ($buy_limit == 1) {
  144. if (! isset($data['buy_limit_num'])) {
  145. return '限制数量不能为空';
  146. }
  147. if (! is_numeric($data['buy_limit_num']) || $data['buy_limit_num'] <= 0) {
  148. return '限制数量必须为大于0的数字';
  149. }
  150. }
  151. return true;
  152. }
  153. function checkIdEdit($id, $rule, $data)
  154. {
  155. $presell = Presell::findOrEmpty($id);
  156. if (empty($presell['id'])) {
  157. return '预售活动不存在';
  158. }
  159. switch ($presell['status']) {
  160. case PresellEnum::STATUS_WAIT:
  161. $validate = new self;
  162. if (! $validate->scene('editWait')->check($data)) {
  163. return $validate->getError();
  164. }
  165. break;
  166. case PresellEnum::STATUS_START:
  167. $validate = new self;
  168. if (! $validate->scene('editStart')->check($data)) {
  169. return $validate->getError();
  170. }
  171. break;
  172. default:
  173. return '预售活动当前状态不可编辑';
  174. }
  175. return true;
  176. }
  177. }