RechargeHbController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\recharge\classes\RechargeHbClass;
  4. use common\components\arrayUtil;
  5. use common\components\util;
  6. use Yii;
  7. class RechargeHbController extends BaseController
  8. {
  9. public function actionDetail()
  10. {
  11. $list = RechargeHbClass::getAllByCondition(['shopId' => $this->shopId, 'delStatus' => 0], 'amount DESC', '*');
  12. $recharge = $this->shop->recharge ?? 0;
  13. util::success(['list' => $list, 'recharge' => $recharge]);
  14. }
  15. //保存红包内容 shish 20210930
  16. public function actionSaveHb()
  17. {
  18. $connection = Yii::$app->db;//事务处理
  19. $transaction = $connection->beginTransaction();
  20. //指定只有一二个商家有充值活动使用权
  21. $env = getenv('YII_ENV', 'local');
  22. if ($env == 'production') {
  23. $arr = [3, 5, 10, 22, 154, 209, 214];
  24. if (in_array($this->shopId, $arr) == false) {
  25. util::fail('您没有权限使用此活动');
  26. }
  27. }
  28. try {
  29. $data = Yii::$app->request->post('data');
  30. $arr = json_decode($data, true);
  31. if (is_array($arr) == false) {
  32. util::fail('数据错误');
  33. }
  34. RechargeHbClass::updateByCondition(['shopId' => $this->shopId], ['delStatus' => 1]);
  35. $arr = arrayUtil::arraySort($arr, 'amount');
  36. foreach ($arr as $key => $item) {
  37. $mini = $item['miniExpend'] ?? 0;
  38. $hbAmount = $item['hbAmount'] ?? 0;
  39. if ($mini <= $hbAmount) {
  40. util::fail('最低消费金额要大于红包金额');
  41. }
  42. $valid = $item['valid'] ?? 0;
  43. if (empty($valid)) {
  44. util::fail('请填写红包有效期');
  45. }
  46. $num = $item['num'] ?? 0;
  47. if ($num <= 0) {
  48. util::fail('请填写红包数量');
  49. }
  50. $totalHb = bcmul($hbAmount, $num, 2);
  51. $amount = $item['amount'] ?? 0;
  52. if ($totalHb >= $amount) {
  53. $index = $key + 1;
  54. util::fail('第' . $index . '个红包,红包总金额超过充值金额');
  55. }
  56. $item['shopId'] = $this->shopId;
  57. $item['sjId'] = $this->sjId;
  58. $item['totalHb'] = $totalHb;
  59. RechargeHbClass::add($item);
  60. }
  61. $rechargeSwitch = Yii::$app->request->post('rechargeSwitch', 0);
  62. $this->shop->recharge = $rechargeSwitch;
  63. $this->shop->save();
  64. $transaction->commit();
  65. util::complete();
  66. } catch (\Exception $e) {
  67. $transaction->rollBack();
  68. util::fail('没有保存成功');
  69. }
  70. }
  71. }