HbController.php 9.4 KB

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