HbController.php 6.5 KB

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