HbController.php 9.0 KB

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