HbController.php 9.4 KB

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