RechargeHbController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. util::fail('已停用');
  21. //指定只有一二个商家有充值活动使用权
  22. $env = getenv('YII_ENV', 'local');
  23. if ($env == 'production') {
  24. $arr = [3, 5, 10, 22, 154, 209, 214];
  25. if (in_array($this->shopId, $arr) == false) {
  26. util::fail('您没有权限使用此活动');
  27. }
  28. }
  29. try {
  30. $data = Yii::$app->request->post('data');
  31. $arr = json_decode($data, true);
  32. if (is_array($arr) == false) {
  33. util::fail('数据错误');
  34. }
  35. RechargeHbClass::updateByCondition(['shopId' => $this->shopId], ['delStatus' => 1]);
  36. $arr = arrayUtil::arraySort($arr, 'amount');
  37. foreach ($arr as $key => $item) {
  38. $mini = $item['miniExpend'] ?? 0;
  39. $hbAmount = $item['hbAmount'] ?? 0;
  40. if ($mini <= $hbAmount) {
  41. util::fail('最低消费金额要大于红包金额');
  42. }
  43. $valid = $item['valid'] ?? 0;
  44. if (empty($valid)) {
  45. util::fail('请填写红包有效期');
  46. }
  47. $num = $item['num'] ?? 0;
  48. if ($num <= 0) {
  49. util::fail('请填写红包数量');
  50. }
  51. $totalHb = bcmul($hbAmount, $num, 2);
  52. $amount = $item['amount'] ?? 0;
  53. if ($totalHb >= $amount) {
  54. $index = $key + 1;
  55. util::fail('第' . $index . '个红包,红包总金额超过充值金额');
  56. }
  57. $item['shopId'] = $this->shopId;
  58. $item['sjId'] = $this->sjId;
  59. $item['totalHb'] = $totalHb;
  60. RechargeHbClass::add($item);
  61. }
  62. $rechargeSwitch = Yii::$app->request->post('rechargeSwitch', 0);
  63. $this->shop->recharge = $rechargeSwitch;
  64. $this->shop->save();
  65. $transaction->commit();
  66. util::complete();
  67. } catch (\Exception $e) {
  68. $transaction->rollBack();
  69. util::fail('没有保存成功');
  70. }
  71. }
  72. }