HbController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. $endTime = $beginTime + $duration * 86400;
  104. $data = [
  105. 'mainId' => $this->mainId,
  106. 'shopId' => $this->shopId,
  107. 'userId' => $post['userId'],
  108. 'customId' => $post['customId'],
  109. 'amount' => $post['amount'],
  110. 'minConsume' => $post['minConsume'],
  111. 'beginTime' => $beginTime,
  112. 'endTime' => $endTime,
  113. 'willTime' => $endTime,
  114. 'duration' => $duration,
  115. 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送
  116. 'remark' => $post['remark'] ?? '',
  117. ];
  118. $manageData = [
  119. 'getType' => 1, //取得方式 0新人领取 1门店发放 2充值赠送
  120. 'getTargetId' => 0,
  121. 'createStaffId' => $this->shopAdminId,
  122. 'createStaffName' => $this->shopAdminName,
  123. 'remark' => $data['remark'],
  124. ];
  125. for ($i = 0; $i < $count; $i++) {
  126. $hb = HbClass::add($data);
  127. $manageData['hbId'] = $hb['id'];
  128. $hbManage = HbManageClass::add($manageData);
  129. }
  130. $arr = [];
  131. if(!empty($hb) && !empty($hbManage)){
  132. $arr = array_merge($hb, $hbManage);
  133. }
  134. util::success($arr);
  135. }
  136. /**
  137. * 更新红包
  138. */
  139. public function actionUpdateHb()
  140. {
  141. $rawPost = Yii::$app->request->post();
  142. $form = new UpdateHbForm();
  143. $form->load($rawPost, '');
  144. if (!$form->validateForm()) {
  145. return;
  146. }
  147. $id = (int)$form->id;
  148. $hb = HbClass::getById($id, true);
  149. //检测是否是本门店的红包
  150. if($hb->shopId != $this->shopId){
  151. util::fail('不是本门店的红包');
  152. }
  153. // 仅更新允许字段,且只更新请求中实际传入的字段(避免用 null 覆盖原值)
  154. $allowFields = array_keys($form->attributes);
  155. foreach ($allowFields as $field) {
  156. if ($field === 'id') {
  157. continue;
  158. }
  159. if (array_key_exists($field, $rawPost)) {
  160. $hb->$field = $form->$field;
  161. }
  162. }
  163. // 当操作作废时,status改为-1,同时将endTime改为当前时间
  164. if($hb->status == -1){
  165. $hb->endTime = time();
  166. //记录作废人
  167. $updateData = [
  168. 'cancelStaffId' => $this->shopAdminId,
  169. 'cancelStaffName' => $this->shopAdminName,
  170. 'cancelTime' => date('Y-m-d H:i:s'),
  171. ];
  172. HbManageClass::updateByCondition(['hbId'=>$id], $updateData);
  173. }
  174. $hb->save();
  175. util::success($hb->attributes);
  176. }
  177. /**
  178. * 充值送红包规则列表
  179. */
  180. public function actionHbRules()
  181. {
  182. $list = RechargeShbClass::getList('*', ['shopId'=>$this->shopId]);
  183. util::success($list);
  184. }
  185. /**
  186. * 充值送红包规则创建
  187. */
  188. public function actionSendHbRules()
  189. {
  190. $data = Yii::$app->request->post();
  191. $validatedRows = [];
  192. foreach ($data as $item) {
  193. if (!is_array($item)) {
  194. util::fail('参数格式错误');
  195. return;
  196. }
  197. $form = new SendHbRuleItemForm();
  198. $form->load($item, '');
  199. if (!$form->validateForm()) {
  200. return;
  201. }
  202. $validatedRows[] = $form->attributes;
  203. }
  204. $hbRules = RechargeShbClass::setRules($this->shopId, $this->mainId, $validatedRows);
  205. util::success($hbRules);
  206. }
  207. /**
  208. *
  209. */
  210. public function actionDeleteHbRule()
  211. {
  212. $id = Yii::$app->request->post('id');
  213. $re = RechargeShbClass::deleteById($id);
  214. if($re == 1){
  215. util::complete();
  216. }else{
  217. util::fail('删除失败');
  218. }
  219. }
  220. }