RechargeHbController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. try {
  21. $data = Yii::$app->request->post('data');
  22. $arr = json_decode($data, true);
  23. if (is_array($arr) == false) {
  24. util::fail('数据错误');
  25. }
  26. RechargeHbClass::updateByCondition(['shopId' => $this->shopId], ['delStatus' => 1]);
  27. $arr = arrayUtil::arraySort($arr, 'amount');
  28. foreach ($arr as $key => $item) {
  29. $mini = $item['miniExpend'] ?? 0;
  30. $hbAmount = $item['hbAmount'] ?? 0;
  31. if ($mini <= $hbAmount) {
  32. util::fail('最低消费金额要大于红包金额');
  33. }
  34. $valid = $item['valid'] ?? 0;
  35. if (empty($valid)) {
  36. util::fail('请填写红包有效期');
  37. }
  38. $num = $item['num'] ?? 0;
  39. if ($num <= 0) {
  40. util::fail('请填写红包数量');
  41. }
  42. $totalHb = bcmul($hbAmount, $num, 2);
  43. $amount = $item['amount'] ?? 0;
  44. if ($totalHb >= $amount) {
  45. $index = $key + 1;
  46. util::fail('第' . $index . '个红包,红包总金额超过充值金额');
  47. }
  48. $item['shopId'] = $this->shopId;
  49. $item['sjId'] = $this->sjId;
  50. $item['totalHb'] = $totalHb;
  51. RechargeHbClass::add($item);
  52. }
  53. $rechargeSwitch = Yii::$app->request->post('rechargeSwitch', 0);
  54. $this->shop->recharge = $rechargeSwitch;
  55. $this->shop->save();
  56. $transaction->commit();
  57. util::complete();
  58. } catch (\Exception $e) {
  59. $transaction->rollBack();
  60. util::fail('没有保存成功');
  61. }
  62. }
  63. }