RechargeHbController.php 2.8 KB

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