HbController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace hd\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\market\classes\XrFlClass;
  5. use biz\shop\classes\ShopClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizHd\hb\classes\HbManageClass;
  8. use bizHd\order\classes\OrderClass;
  9. use bizHd\recharge\classes\RechargeShbClass;
  10. use common\components\util;
  11. use bizHd\hb\classes\HbClass;
  12. use hd\models\hb\CreateHbForm;
  13. use hd\models\hb\SendHbRuleItemForm;
  14. use hd\models\hb\UpdateHbForm;
  15. use Yii;
  16. class HbController extends BaseController
  17. {
  18. public $guestAccess = [];
  19. //获取新人红包福利 ssh 20211026
  20. public function actionGetNewGift()
  21. {
  22. $ghsId = Yii::$app->request->get('ghsId', 0);
  23. $ghs = GhsClass::getById($ghsId, true);
  24. GhsClass::valid($ghs, $this->shopId);
  25. $ghsShopId = $ghs->shopId ?? 0;
  26. $ghsShop = ShopClass::getById($ghsShopId, true);
  27. $xrFl = $ghsShop->xrFl ?? 0;
  28. if ($xrFl == 0) {
  29. util::fail('活动已结束');
  30. }
  31. $sendNewGift = $ghs->sendNewGift ?? 0;
  32. if ($sendNewGift == 1) {
  33. util::fail('已经领过了');
  34. }
  35. $new = $ghs->new ?? 0;
  36. if ($new == 0) {
  37. util::fail('新人才能领取哦');
  38. }
  39. $customId = $ghs->customId ?? 0;
  40. $custom = CustomClass::getById($customId, true);
  41. XrFlClass::giveGift($ghs, $custom);
  42. util::complete('领取成功');
  43. }
  44. /**
  45. * 获取红包列表
  46. */
  47. public function actionHbList()
  48. {
  49. $get = Yii::$app->request->get();
  50. $where = ['shopId' => $this->shopId];
  51. $customId = $get['customId'] ?? 0;
  52. if (!empty($customId)) {
  53. $where['customId'] = $customId;
  54. }
  55. $status = $get['status'] ?? '';
  56. if ($status != '') {
  57. $where['status'] = $status;
  58. }
  59. $respond = HbClass::getList('*', $where, 'id desc', 'custom');
  60. if (!empty($respond['list'])) {
  61. foreach ($respond['list'] as &$v) {
  62. $v['customName'] = $v['custom']['name'];
  63. if ($v['status'] == 0 && $v['endTime'] < time()) {
  64. //发现红包已过期,需要作废,多有处作废操作,搜索关键词 cancellation_hb
  65. $id = $v['id'];
  66. HbClass::updateById($id, ['status' => -1]);
  67. }
  68. }
  69. }
  70. util::success($respond);
  71. }
  72. // 单个门店下所有可用红包
  73. public function actionAvailableHb()
  74. {
  75. $get = Yii::$app->request->get();
  76. $where = [];
  77. $where['customId'] = $get['customId'];
  78. $where['status'] = 0;
  79. $list = HbClass::getAllByCondition($where, 'id DESC');
  80. //已经过期的就不再显示,并且作废,多有处作废操作,搜索关键词 cancellation_hb
  81. if (!empty($list)) {
  82. foreach ($list as $k => $v) {
  83. if ($v['endTime'] < time()) {
  84. unset($list[$v]);
  85. $id = $v['id'];
  86. HbClass::updateById($id, ['status' => -1]);
  87. }
  88. }
  89. }
  90. util::success($list);
  91. }
  92. /**
  93. * 获取红包详情
  94. */
  95. public function actionHbDetail()
  96. {
  97. $hbId = Yii::$app->request->get('hbId');
  98. $hb = HbClass::getById($hbId);
  99. util::success($hb);
  100. }
  101. /**
  102. * 创建红包
  103. */
  104. public function actionCreateHb()
  105. {
  106. $form = new CreateHbForm();
  107. $form->load(Yii::$app->request->post(), '');
  108. if (!$form->validateForm()) {
  109. return;
  110. }
  111. $post = $form->attributes;
  112. $duration = (int)$form->days;
  113. $count = $post['count'] ?? 1;
  114. $beginTime = time();
  115. if ($duration <= 0) {
  116. $endTime = 4102416000;
  117. } else {
  118. $endTime = $beginTime + $duration * 86400;
  119. }
  120. $data = [
  121. 'mainId' => $this->mainId,
  122. 'shopId' => $this->shopId,
  123. 'userId' => $post['userId'],
  124. 'customId' => $post['customId'],
  125. 'amount' => $post['amount'],
  126. 'minConsume' => $post['minConsume'],
  127. 'beginTime' => $beginTime,
  128. 'endTime' => $endTime,
  129. 'willTime' => $endTime,
  130. 'duration' => $duration,
  131. 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送
  132. 'remark' => $post['remark'] ?? '',
  133. ];
  134. $manageData = [
  135. 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送
  136. 'getTargetId' => 0,
  137. 'createStaffId' => $this->shopAdminId,
  138. 'createStaffName' => $this->shopAdminName,
  139. 'remark' => $data['remark'],
  140. ];
  141. for ($i = 0; $i < $count; $i++) {
  142. $hb = HbClass::add($data);
  143. $manageData['hbId'] = $hb['id'];
  144. $hbManage = HbManageClass::add($manageData);
  145. }
  146. $arr = [];
  147. if (!empty($hb) && !empty($hbManage)) {
  148. $arr = array_merge($hb, $hbManage);
  149. }
  150. util::success($arr);
  151. }
  152. /**
  153. * 更新红包
  154. * - 更新方法包含了:红包退回及其它更新
  155. */
  156. public function actionUpdateHb()
  157. {
  158. $rawPost = Yii::$app->request->post();
  159. $form = new UpdateHbForm();
  160. $form->load($rawPost, '');
  161. if (!$form->validateForm()) {
  162. return;
  163. }
  164. $id = (int)$form->id;
  165. $hb = HbClass::getById($id, true);
  166. //检测是否是本门店的红包
  167. if ($hb->shopId != $this->shopId) {
  168. util::fail('不是本门店的红包');
  169. }
  170. $connection = Yii::$app->db;
  171. $transaction = $connection->beginTransaction();
  172. try {
  173. //当是退回红包时,必须检测对应订单 actPrice 为0
  174. if (isset($form->status) && $form->status == 0) {
  175. if ($hb->status == 1) {
  176. //查询订单数据,校验 actPrice,按情况更新
  177. $order = OrderClass::getById($form->orderId, true, 'id, hbId, hbAmount, actPrice');
  178. if ($order->hbId == $id) {
  179. if ($order->actPrice == 0.00) {
  180. //更新订单数据
  181. $order->hbId = -$order->hbId;
  182. $order->save();
  183. } else {
  184. Yii::error('使用红包的订单 actPrice 不等于0,无法执行红包退回。hbId=' . $id);
  185. util::fail('使用红包的订单 actPrice 不等于0,无法执行红包退回');
  186. }
  187. } else {
  188. Yii::error("订单中的红包id与当前红包不匹配。hbId={$id}, orderId={$form->orderId}");
  189. util::fail('订单中的红包id与当前红包不匹配');
  190. }
  191. } else {
  192. Yii::error("当前红包状态不是已使用,不能变更未使用。hbId={$id}");
  193. util::fail("当前红包状态不是已使用,不能变更未使用");
  194. }
  195. }
  196. // 仅更新允许字段,且只更新请求中实际传入的字段(避免用 null 覆盖原值)
  197. $allowFields = array_keys($form->attributes);
  198. foreach ($allowFields as $field) {
  199. if ($field === 'id') {
  200. continue;
  201. }
  202. if (array_key_exists($field, $rawPost)) {
  203. $hb->$field = $form->$field;
  204. }
  205. }
  206. // 当操作作废时,status改为-1,同时将endTime改为当前时间,多有处作废操作,搜索关键词 cancellation_hb
  207. if (isset($form->status) && $form->status == -1 && $hb->status != -1) {
  208. $hb->endTime = time();
  209. //记录作废人
  210. $updateData = [
  211. 'cancelStaffId' => $this->shopAdminId,
  212. 'cancelStaffName' => $this->shopAdminName,
  213. 'cancelTime' => date('Y-m-d H:i:s'),
  214. ];
  215. HbManageClass::updateByCondition(['hbId' => $id], $updateData);
  216. }
  217. $hb->save();
  218. $transaction->commit();
  219. } catch (\Exception $e) {
  220. $transaction->rollBack();
  221. $msg = $e->getMessage();
  222. Yii::error('actionUpdateHb', $msg);
  223. util::fail($msg);
  224. }
  225. util::success($hb->attributes);
  226. }
  227. /**
  228. * 充值送红包规则列表
  229. */
  230. public function actionHbRules()
  231. {
  232. $list = RechargeShbClass::getList('*', ['shopId' => $this->shopId]);
  233. util::success($list);
  234. }
  235. /**
  236. * 充值送红包规则创建
  237. */
  238. public function actionSendHbRules()
  239. {
  240. $data = Yii::$app->request->post();
  241. $validatedRows = [];
  242. foreach ($data as $item) {
  243. if (!is_array($item)) {
  244. util::fail('参数格式错误');
  245. return;
  246. }
  247. $form = new SendHbRuleItemForm();
  248. $form->load($item, '');
  249. if (!$form->validateForm()) {
  250. return;
  251. }
  252. $validatedRows[] = $form->attributes;
  253. }
  254. $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $validatedRows);
  255. util::success($hbRules);
  256. }
  257. /**
  258. * 充值送红包规则删除
  259. */
  260. public function actionDeleteHbRule()
  261. {
  262. $id = Yii::$app->request->post('id');
  263. $re = RechargeShbClass::deleteById($id);
  264. if ($re == 1) {
  265. util::complete();
  266. } else {
  267. util::fail('删除失败');
  268. }
  269. }
  270. }