HbController.php 10 KB

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