| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <?php
- namespace hd\controllers;
- use biz\ghs\classes\GhsClass;
- use biz\market\classes\XrFlClass;
- use biz\shop\classes\ShopClass;
- use bizGhs\custom\classes\CustomClass;
- use bizHd\hb\classes\HbManageClass;
- use bizHd\order\classes\OrderClass;
- use bizHd\recharge\classes\RechargeShbClass;
- use common\components\util;
- use bizHd\hb\classes\HbClass;
- use hd\models\hb\CreateHbForm;
- use hd\models\hb\SendHbRuleItemForm;
- use hd\models\hb\UpdateHbForm;
- use Yii;
- class HbController extends BaseController
- {
- public $guestAccess = [];
- //获取新人红包福利 ssh 20211026
- public function actionGetNewGift()
- {
- util::complete('领取成功');
- }
- /**
- * 获取红包列表
- */
- public function actionHbList()
- {
- $get = Yii::$app->request->get();
- $where = ['shopId' => $this->shopId];
- $customId = $get['customId'] ?? 0;
- if (!empty($customId)) {
- $where['customId'] = $customId;
- }
- $status = $get['status'] ?? '';
- if ($status != '') {
- $where['status'] = $status;
- }
- $respond = HbClass::getList('*', $where, 'id desc', 'custom');
- if (!empty($respond['list'])) {
- foreach ($respond['list'] as &$v) {
- $v['customName'] = $v['custom']['name'] ?? '';
- if ($v['status'] == 0 && $v['endTime'] < time()) {
- //发现红包已过期,需要作废,多有处作废操作,搜索关键词 cancellation_hb
- $id = $v['id'];
- HbClass::updateById($id, ['status' => -1]);
- }
- }
- }
- util::success($respond);
- }
- // 单个门店下所有可用红包
- public function actionAvailableHb()
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['customId'] = $get['customId'];
- $where['status'] = 0;
- $list = HbClass::getAllByCondition($where, 'id DESC');
- //已经过期的就不再显示,并且作废,多有处作废操作,搜索关键词 cancellation_hb
- if (!empty($list)) {
- foreach ($list as $k => $v) {
- if ($v['status'] == 0 && $v['endTime'] < time()) {
- unset($list[$k]);
- $id = $v['id'];
- HbClass::updateById($id, ['status' => -1]);
- }
- }
- }
- util::success($list);
- }
- /**
- * 获取红包详情
- */
- public function actionHbDetail()
- {
- $hbId = Yii::$app->request->get('hbId');
- $hb = HbClass::getById($hbId);
- util::success($hb);
- }
- /**
- * 创建红包
- */
- public function actionCreateHb()
- {
- $form = new CreateHbForm();
- $form->load(Yii::$app->request->post(), '');
- if (!$form->validateForm()) {
- return;
- }
- $post = $form->attributes;
- $adminId = $this->shopAdminId;
- util::checkRepeatCommit($adminId, 5);
- $duration = (int)$form->days;
- $count = $post['count'] ?? 1;
- $beginTime = time();
- if ($duration <= 0) {
- $endTime = 4102416000;
- } else {
- $endTime = $beginTime + $duration * 86400;
- }
- $data = [
- 'mainId' => $this->mainId,
- 'shopId' => $this->shopId,
- 'userId' => $post['userId'],
- 'customId' => $post['customId'],
- 'amount' => $post['amount'],
- 'minConsume' => $post['minConsume'],
- 'beginTime' => $beginTime,
- 'endTime' => $endTime,
- 'willTime' => $endTime,
- 'duration' => $duration,
- 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送
- 'remark' => $post['remark'] ?? '',
- ];
- $manageData = [
- 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送
- 'getTargetId' => 0,
- 'createStaffId' => $this->shopAdminId,
- 'createStaffName' => $this->shopAdminName,
- 'remark' => $data['remark'],
- ];
- for ($i = 0; $i < $count; $i++) {
- $hb = HbClass::add($data);
- $manageData['hbId'] = $hb['id'];
- $hbManage = HbManageClass::add($manageData);
- }
- $arr = [];
- if (!empty($hb) && !empty($hbManage)) {
- $arr = array_merge($hb, $hbManage);
- }
- util::success($arr);
- }
- /**
- * 更新红包
- * - 更新方法包含了:红包退回及其它更新
- */
- public function actionUpdateHb()
- {
- $rawPost = Yii::$app->request->post();
- $form = new UpdateHbForm();
- $form->load($rawPost, '');
- if (!$form->validateForm()) {
- return;
- }
- $id = (int)$form->id;
- $hb = HbClass::getById($id, true);
- //检测是否是本门店的红包
- if ($hb->shopId != $this->shopId) {
- util::fail('不是本门店的红包');
- }
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- //当是退回红包时,必须检测对应订单 actPrice 为0
- if (isset($form->status) && $form->status == 0) {
- if ($hb->status == 1) {
- //查询订单数据,校验 actPrice,按情况更新
- $order = OrderClass::getById($form->orderId, true, 'id, hbId, hbAmount, actPrice');
- if ($order->hbId == $id) {
- if ($order->actPrice == 0.00) {
- //更新订单数据
- $order->hbId = -$order->hbId;
- $order->save();
- } else {
- Yii::error('使用红包的订单 actPrice 不等于0,无法执行红包退回。hbId=' . $id);
- util::fail('使用红包的订单 actPrice 不等于0,无法执行红包退回');
- }
- } else {
- Yii::error("订单中的红包id与当前红包不匹配。hbId={$id}, orderId={$form->orderId}");
- util::fail('订单中的红包id与当前红包不匹配');
- }
- } else {
- Yii::error("当前红包状态不是已使用,不能变更未使用。hbId={$id}");
- util::fail("当前红包状态不是已使用,不能变更未使用");
- }
- }
- // 仅更新允许字段,且只更新请求中实际传入的字段(避免用 null 覆盖原值)
- $allowFields = array_keys($form->attributes);
- foreach ($allowFields as $field) {
- if ($field === 'id') {
- continue;
- }
- if (array_key_exists($field, $rawPost)) {
- $hb->$field = $form->$field;
- }
- }
- // 当操作作废时,status改为-1,同时将endTime改为当前时间,多有处作废操作,搜索关键词 cancellation_hb
- if (isset($form->status) && $form->status == -1 && $hb->status != -1) {
- $hb->endTime = time();
- //记录作废人
- $updateData = [
- 'cancelStaffId' => $this->shopAdminId,
- 'cancelStaffName' => $this->shopAdminName,
- 'cancelTime' => date('Y-m-d H:i:s'),
- ];
- HbManageClass::updateByCondition(['hbId' => $id], $updateData);
- }
- $hb->save();
- $transaction->commit();
- } catch (\Exception $e) {
- $transaction->rollBack();
- $msg = $e->getMessage();
- Yii::error('actionUpdateHb', $msg);
- util::fail($msg);
- }
- util::success($hb->attributes);
- }
- /**
- * 充值送红包规则列表
- */
- public function actionHbRules()
- {
- $list = RechargeShbClass::getList('*', ['shopId' => $this->shopId]);
- util::success($list);
- }
- /**
- * 充值送红包规则创建
- */
- public function actionSendHbRules()
- {
- $data = Yii::$app->request->post();
- $validatedRows = [];
- foreach ($data as $item) {
- if (!is_array($item)) {
- util::fail('参数格式错误');
- return;
- }
- $form = new SendHbRuleItemForm();
- $form->load($item, '');
- if (!$form->validateForm()) {
- return;
- }
- $validatedRows[] = $form->attributes;
- }
- $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $validatedRows);
- util::success($hbRules);
- }
- /**
- * 充值送红包规则删除
- */
- public function actionDeleteHbRule()
- {
- $id = Yii::$app->request->post('id');
- $re = RechargeShbClass::deleteById($id);
- if ($re == 1) {
- util::complete();
- } else {
- util::fail('删除失败');
- }
- }
- }
|