HbController.php 8.9 KB

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