HbController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. if (isset($this->shopAdmin->founder) == false || $this->shopAdmin->founder != 2) {
  106. util::fail('管理员才能操作');
  107. }
  108. $duration = (int)$form->days;
  109. $count = $post['count'] ?? 1;
  110. $effectiveDate = $post['effectiveDate'];
  111. $beginTime = $effectiveDate == '' ? time() : strtotime($effectiveDate);
  112. if ($duration <= 0) {
  113. $endTime = 4102416000;
  114. } else {
  115. $endTime = $beginTime + $duration * 86400;
  116. }
  117. $data = [
  118. 'mainId' => $this->mainId,
  119. 'shopId' => $this->shopId,
  120. 'userId' => $post['userId'],
  121. 'customId' => $post['customId'],
  122. 'amount' => $post['amount'],
  123. 'minConsume' => $post['minConsume'],
  124. 'beginTime' => $beginTime,
  125. 'endTime' => $endTime,
  126. 'willTime' => $endTime,
  127. 'duration' => $duration,
  128. 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送
  129. 'remark' => $post['remark'] ?? '',
  130. ];
  131. $manageData = [
  132. 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送
  133. 'getTargetId' => 0,
  134. 'createStaffId' => $this->shopAdminId,
  135. 'createStaffName' => $this->shopAdminName,
  136. 'remark' => $data['remark'],
  137. ];
  138. for ($i = 0; $i < $count; $i++) {
  139. $hb = HbClass::add($data);
  140. $manageData['hbId'] = $hb['id'];
  141. $hbManage = HbManageClass::add($manageData);
  142. }
  143. $arr = [];
  144. if (!empty($hb) && !empty($hbManage)) {
  145. $arr = array_merge($hb, $hbManage);
  146. }
  147. util::success($arr);
  148. }
  149. /**
  150. * 更新红包
  151. * - 更新方法包含了:红包退回及其它更新
  152. */
  153. public function actionUpdateHb()
  154. {
  155. $rawPost = Yii::$app->request->post();
  156. $form = new UpdateHbForm();
  157. $form->load($rawPost, '');
  158. if (!$form->validateForm()) {
  159. return;
  160. }
  161. $id = (int)$form->id;
  162. $hb = HbClass::getById($id, true);
  163. //检测是否是本门店的红包
  164. if ($hb->shopId != $this->shopId) {
  165. util::fail('不是本门店的红包');
  166. }
  167. if (isset($this->shopAdmin->founder) == false || $this->shopAdmin->founder != 2) {
  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 actionHbRuleDetail()
  239. {
  240. $id = Yii::$app->request->get('id');
  241. $rule = RechargeShbClass::getById($id);
  242. util::success($rule);
  243. }
  244. /**
  245. * 充值送红包规则创建
  246. */
  247. public function actionSendHbRules()
  248. {
  249. $data = Yii::$app->request->post();
  250. $id = $data['id'];
  251. $amount = $data['amount'];
  252. $rules = $data['rules'];
  253. $validatedRules = [];
  254. foreach ($rules as $item) {
  255. if (!is_array($item)) {
  256. util::fail('参数格式错误');
  257. return;
  258. }
  259. $form = new SendHbRuleItemForm();
  260. $form->load($item, '');
  261. if (!$form->validateForm()) {
  262. return;
  263. }
  264. $validatedRules[] = $form->attributes;
  265. }
  266. $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $id, $amount, $validatedRules);
  267. util::success($hbRules);
  268. }
  269. /**
  270. * 充值送红包规则删除
  271. */
  272. public function actionDeleteHbRule()
  273. {
  274. $id = Yii::$app->request->post('id');
  275. $re = RechargeShbClass::deleteById($id);
  276. if ($re == 1) {
  277. util::complete();
  278. } else {
  279. util::fail('删除失败');
  280. }
  281. }
  282. }