HbController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\UpdateHbForm;
  13. use Yii;
  14. class HbController extends BaseController
  15. {
  16. public $guestAccess = [];
  17. //获取新人红包福利 ssh 20211026
  18. public function actionGetNewGift()
  19. {
  20. $ghsId = Yii::$app->request->get('ghsId', 0);
  21. $ghs = GhsClass::getById($ghsId, true);
  22. GhsClass::valid($ghs, $this->shopId);
  23. $ghsShopId = $ghs->shopId ?? 0;
  24. $ghsShop = ShopClass::getById($ghsShopId, true);
  25. $xrFl = $ghsShop->xrFl ?? 0;
  26. if ($xrFl == 0) {
  27. util::fail('活动已结束');
  28. }
  29. $sendNewGift = $ghs->sendNewGift ?? 0;
  30. if ($sendNewGift == 1) {
  31. util::fail('已经领过了');
  32. }
  33. $new = $ghs->new ?? 0;
  34. if ($new == 0) {
  35. util::fail('新人才能领取哦');
  36. }
  37. $customId = $ghs->customId ?? 0;
  38. $custom = CustomClass::getById($customId, true);
  39. XrFlClass::giveGift($ghs, $custom);
  40. util::complete('领取成功');
  41. }
  42. /**
  43. * 获取红包列表
  44. */
  45. public function actionHbList()
  46. {
  47. $where = ['shopId' => $this->shopId];
  48. $customId = Yii::$app->request->get('customId', '');
  49. if (!empty($customId)) {
  50. $where['customId'] = $customId;
  51. }
  52. $status = Yii::$app->request->get('status', '');
  53. if (isset($status) && $status != '') {
  54. if($status == -1){ // 已失效包含:1作废 与 过期(0未使用但endTime<当前时间) --- 所以当操作作废时,status改为-1,同时将endTime改为当前时间
  55. $where['endTime<'] = time();
  56. }else{
  57. $where['status'] = $status;
  58. }
  59. }
  60. $list = HbClass::getList('*', $where, 'id desc', 'custom');
  61. if (!empty($list['list'])) {
  62. foreach ($list['list'] as &$v) {
  63. $v['customName'] = $v['custom']['name'];
  64. }
  65. }
  66. util::success($list);
  67. }
  68. // 单个门店下所有可用红包
  69. public function actionAvailableHb()
  70. {
  71. $get = Yii::$app->request->get();
  72. $where = [];
  73. $where['customId'] = $get['customId'];
  74. $where['status'] = 0;
  75. $where['endTime>'] = time();
  76. $list = HbClass::getAllByCondition($where, 'id DESC');
  77. util::success($list);
  78. }
  79. /**
  80. * 获取红包详情
  81. */
  82. public function actionHbDetail()
  83. {
  84. $hbId = Yii::$app->request->get('hbId');
  85. $hb = HbClass::getById($hbId);
  86. util::success($hb);
  87. }
  88. /**
  89. * 创建红包
  90. */
  91. public function actionCreateHb()
  92. {
  93. $form = new CreateHbForm();
  94. $form->load(Yii::$app->request->post(), '');
  95. if (!$form->validateForm()) {
  96. return;
  97. }
  98. $post = $form->attributes;
  99. $duration = intval($post['days']);
  100. $count = $post['count'] ?? 1;
  101. $beginTime = time();
  102. $endTime = $beginTime + $duration * 86400;
  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. public function actionUpdateHb()
  139. {
  140. $rawPost = Yii::$app->request->post();
  141. $form = new UpdateHbForm();
  142. $form->load($rawPost, '');
  143. if (!$form->validateForm()) {
  144. return;
  145. }
  146. $id = (int)$form->id;
  147. $hb = HbClass::getById($id, true);
  148. //检测是否是本门店的红包
  149. if($hb->shopId != $this->shopId){
  150. util::fail('不是本门店的红包');
  151. }
  152. // 仅更新允许字段,且只更新请求中实际传入的字段(避免用 null 覆盖原值)
  153. $allowFields = array_keys($form->attributes);
  154. foreach ($allowFields as $field) {
  155. if ($field === 'id') {
  156. continue;
  157. }
  158. if (array_key_exists($field, $rawPost)) {
  159. $hb->$field = $form->$field;
  160. }
  161. }
  162. // 当操作作废时,status改为-1,同时将endTime改为当前时间
  163. if($hb->status == -1){
  164. $hb->endTime = time();
  165. //记录作废人
  166. $updateData = [
  167. 'cancelStaffId' => $this->shopAdminId,
  168. 'cancelStaffName' => $this->shopAdminName,
  169. 'cancelTime' => date('Y-m-d H:i:s'),
  170. ];
  171. HbManageClass::updateByCondition(['hbId'=>$id], $updateData);
  172. }
  173. $hb->save();
  174. util::success($hb->attributes);
  175. }
  176. /**
  177. * 充值送红包规则列表
  178. */
  179. public function actionHbRules()
  180. {
  181. $list = RechargeShbClass::getList('*', ['shopId'=>$this->shopId]);
  182. util::success($list);
  183. }
  184. /**
  185. * 充值送红包规则创建
  186. */
  187. public function actionSendHbRules()
  188. {
  189. $data = Yii::$app->request->post();
  190. //var_dump($data); die;
  191. $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $data);
  192. util::success($hbRules);
  193. }
  194. /**
  195. *
  196. */
  197. public function actionDeleteHbRule()
  198. {
  199. $id = Yii::$app->request->post('id');
  200. $re = RechargeShbClass::deleteById($id);
  201. if($re == 1){
  202. util::complete();
  203. }else{
  204. util::fail('删除失败');
  205. }
  206. }
  207. }