HbController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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\recharge\classes\RechargeShbClass;
  9. use common\components\util;
  10. use bizHd\hb\classes\HbClass;
  11. use hd\models\hb\CreateHbForm;
  12. use hd\models\hb\SendHbRuleItemForm;
  13. use hd\models\hb\UpdateHbForm;
  14. use Yii;
  15. class HbController extends BaseController
  16. {
  17. public $guestAccess = [];
  18. //获取新人红包福利 ssh 20211026
  19. public function actionGetNewGift()
  20. {
  21. $ghsId = Yii::$app->request->get('ghsId', 0);
  22. $ghs = GhsClass::getById($ghsId, true);
  23. GhsClass::valid($ghs, $this->shopId);
  24. $ghsShopId = $ghs->shopId ?? 0;
  25. $ghsShop = ShopClass::getById($ghsShopId, true);
  26. $xrFl = $ghsShop->xrFl ?? 0;
  27. if ($xrFl == 0) {
  28. util::fail('活动已结束');
  29. }
  30. $sendNewGift = $ghs->sendNewGift ?? 0;
  31. if ($sendNewGift == 1) {
  32. util::fail('已经领过了');
  33. }
  34. $new = $ghs->new ?? 0;
  35. if ($new == 0) {
  36. util::fail('新人才能领取哦');
  37. }
  38. $customId = $ghs->customId ?? 0;
  39. $custom = CustomClass::getById($customId, true);
  40. XrFlClass::giveGift($ghs, $custom);
  41. util::complete('领取成功');
  42. }
  43. /**
  44. * 获取红包列表
  45. */
  46. public function actionHbList()
  47. {
  48. $where = ['shopId' => $this->shopId];
  49. $customId = Yii::$app->request->get('customId', '');
  50. if (!empty($customId)) {
  51. $where['customId'] = $customId;
  52. }
  53. $status = Yii::$app->request->get('status', '');
  54. if (isset($status) && $status != '') {
  55. if ($status == -1) { // 已失效包含:1作废 与 过期(0未使用但endTime<当前时间) --- 所以当操作作废时,status改为-1,同时将endTime改为当前时间
  56. $where['endTime<'] = time();
  57. } else {
  58. $where['status'] = $status;
  59. }
  60. }
  61. $list = HbClass::getList('*', $where, 'id desc', 'custom');
  62. if (!empty($list['list'])) {
  63. foreach ($list['list'] as &$v) {
  64. $v['customName'] = $v['custom']['name'];
  65. }
  66. }
  67. util::success($list);
  68. }
  69. // 单个门店下所有可用红包
  70. public function actionAvailableHb()
  71. {
  72. $get = Yii::$app->request->get();
  73. $where = [];
  74. $where['customId'] = $get['customId'];
  75. $where['status'] = 0;
  76. $where['endTime>'] = time();
  77. $list = HbClass::getAllByCondition($where, 'id DESC');
  78. util::success($list);
  79. }
  80. /**
  81. * 获取红包详情
  82. */
  83. public function actionHbDetail()
  84. {
  85. $hbId = Yii::$app->request->get('hbId');
  86. $hb = HbClass::getById($hbId);
  87. util::success($hb);
  88. }
  89. /**
  90. * 创建红包
  91. */
  92. public function actionCreateHb()
  93. {
  94. $form = new CreateHbForm();
  95. $form->load(Yii::$app->request->post(), '');
  96. if (!$form->validateForm()) {
  97. return;
  98. }
  99. $post = $form->attributes;
  100. $duration = (int)$form->days;
  101. $count = $post['count'] ?? 1;
  102. $beginTime = time();
  103. if ($duration <= 0) {
  104. $endTime = 4102416000;
  105. } else {
  106. $endTime = $beginTime + $duration * 86400;
  107. }
  108. $data = [
  109. 'mainId' => $this->mainId,
  110. 'shopId' => $this->shopId,
  111. 'userId' => $post['userId'],
  112. 'customId' => $post['customId'],
  113. 'amount' => $post['amount'],
  114. 'minConsume' => $post['minConsume'],
  115. 'beginTime' => $beginTime,
  116. 'endTime' => $endTime,
  117. 'willTime' => $endTime,
  118. 'duration' => $duration,
  119. 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送
  120. 'remark' => $post['remark'] ?? '',
  121. ];
  122. $manageData = [
  123. 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送
  124. 'getTargetId' => 0,
  125. 'createStaffId' => $this->shopAdminId,
  126. 'createStaffName' => $this->shopAdminName,
  127. 'remark' => $data['remark'],
  128. ];
  129. for ($i = 0; $i < $count; $i++) {
  130. $hb = HbClass::add($data);
  131. $manageData['hbId'] = $hb['id'];
  132. $hbManage = HbManageClass::add($manageData);
  133. }
  134. $arr = [];
  135. if (!empty($hb) && !empty($hbManage)) {
  136. $arr = array_merge($hb, $hbManage);
  137. }
  138. util::success($arr);
  139. }
  140. /**
  141. * 更新红包
  142. */
  143. public function actionUpdateHb()
  144. {
  145. $rawPost = Yii::$app->request->post();
  146. $form = new UpdateHbForm();
  147. $form->load($rawPost, '');
  148. if (!$form->validateForm()) {
  149. return;
  150. }
  151. $id = (int)$form->id;
  152. $hb = HbClass::getById($id, true);
  153. //检测是否是本门店的红包
  154. if ($hb->shopId != $this->shopId) {
  155. util::fail('不是本门店的红包');
  156. }
  157. // 仅更新允许字段,且只更新请求中实际传入的字段(避免用 null 覆盖原值)
  158. $allowFields = array_keys($form->attributes);
  159. foreach ($allowFields as $field) {
  160. if ($field === 'id') {
  161. continue;
  162. }
  163. if (array_key_exists($field, $rawPost)) {
  164. $hb->$field = $form->$field;
  165. }
  166. }
  167. // 当操作作废时,status改为-1,同时将endTime改为当前时间
  168. if ($hb->status == -1) {
  169. $hb->endTime = time();
  170. //记录作废人
  171. $updateData = [
  172. 'cancelStaffId' => $this->shopAdminId,
  173. 'cancelStaffName' => $this->shopAdminName,
  174. 'cancelTime' => date('Y-m-d H:i:s'),
  175. ];
  176. HbManageClass::updateByCondition(['hbId' => $id], $updateData);
  177. }
  178. $hb->save();
  179. util::success($hb->attributes);
  180. }
  181. /**
  182. * 充值送红包规则列表
  183. */
  184. public function actionHbRules()
  185. {
  186. $list = RechargeShbClass::getList('*', ['shopId' => $this->shopId]);
  187. util::success($list);
  188. }
  189. /**
  190. * 充值送红包规则创建
  191. */
  192. public function actionSendHbRules()
  193. {
  194. $data = Yii::$app->request->post();
  195. $validatedRows = [];
  196. foreach ($data as $item) {
  197. if (!is_array($item)) {
  198. util::fail('参数格式错误');
  199. return;
  200. }
  201. $form = new SendHbRuleItemForm();
  202. $form->load($item, '');
  203. if (!$form->validateForm()) {
  204. return;
  205. }
  206. $validatedRows[] = $form->attributes;
  207. }
  208. $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $validatedRows);
  209. util::success($hbRules);
  210. }
  211. /**
  212. *
  213. */
  214. public function actionDeleteHbRule()
  215. {
  216. $id = Yii::$app->request->post('id');
  217. $re = RechargeShbClass::deleteById($id);
  218. if ($re == 1) {
  219. util::complete();
  220. } else {
  221. util::fail('删除失败');
  222. }
  223. }
  224. }