-100, 'msg' => '参数有误!', 'data' => []]; } $prizeInfo = Db::table('lr_prize')->where(['mark_code' => $mark_code])->find(); if(!$prizeInfo){ return ['code' => -101, 'msg' => '礼包码标识不存在!', 'data' => []]; } $data = []; $cacheCodes = []; foreach($codeLists as $v){ $code = trim($v); $data[] = [ 'prize_id' => $prizeInfo['id'], 'code' => $code, 'status' => 1, 'craete_time' => date('Y-m-d H:i:s', NOW_TIMESTAMP) ]; $cacheCodes[] = $code; } if(count($cacheCodes) <= 0){ return ['code' => -102, 'msg' => '导入码为空!', 'data' => []]; } Db::startTrans(); try { // PS:插入时候有兑换码唯一索引,缓存不做去重处理 $res = Db::table('lr_prize_gift')->insertAll($data); if(!$res){ throw new \Exception('存储失败,请稍后再试!', -111); } $updateRes = Db::table('lr_prize')->where(['mark_code' => $mark_code])->setInc('remaining_num', count($data)); if(!$updateRes){ throw new \Exception('存储编辑失败,请稍后再试!', -112); } // 对应礼包码存储缓存 if(!$this->handleGiftCache('add', $mark_code, $cacheCodes)){ throw new \Exception('缓存失败,请稍后再试!', -113); } Db::commit(); } catch (\Exception $e) { Db::rollback(); return ['code' => -199, 'msg' => '导入失败:' . $e->getMessage(), 'data' => []]; } return ['code' => 200, 'msg' => '导入成功', 'data' => []]; } /** * 获取礼包码 * @param $mark_code * * @return array */ public function getCacheGiftCode($mark_code){ $res = $this->handleGiftCache('get', $mark_code); if($res){ return ['code' => 200, 'msg' => '', 'data' => ['code' => $res]]; } return ['code' => -100, 'msg' => '没有礼包码', 'data' => []]; } /** * 添加礼包码 * @param $mark_code * * @return array */ public function addCacheGiftCode($mark_code, $code){ if(!$mark_code || !$code){ return ['code' => -110, 'msg' => '缺少礼包码', 'data' => []]; } $prize_id = Db::table('lr_prize')->where(['mark_code' => $mark_code])->value('id'); if(!$prize_id){ return ['code' => -111, 'msg' => '错误的礼包码标识!', 'data' => []]; } $giftCount = Db::table('lr_prize_gift')->where(['prize_id' => $prize_id, 'mark_code' => $code])->count(); if($giftCount > 0){ return ['code' => -112, 'msg' => '重复添加,当前礼包码已存在!', 'data' => []]; } $res = $this->handleGiftCache('add', $mark_code, $code); if($res){ return ['code' => 200, 'msg' => '', 'data' => ['code' => $res]]; } return ['code' => -100, 'msg' => '没有礼包码', 'data' => []]; } // 获取礼包码数量 public function getCacheGiftNum(){ $prizeList = Db::table('lr_prize')->where(['type' => 'pack'])->field('name,mark_code,all_num,use_num,remaining_num')->select(); $notice = false; $noticeMsg = "## 礼包码数量预警\n"; // $redis = Cache::store('redis')->handler(); // $cacheKey = 'mlbb_detection_prize_notice:'.date("YmdH"); foreach ($prizeList as $k => &$v) { $v['cache_num'] = $this->handleGiftCache('num', $v['mark_code']); if($v['cache_num'] < 100){ $notice = true; $noticeMsg .= "- {$v['name']} - {$v['mark_code']}, 总计={$v['all_num']}, 剩余={$v['remaining_num']}, 已使用={$v['use_num']}, 缓存={$v['cache_num']}\n"; } } $noticeMsg .= "时间: ".date('Y-m-d H:i:s', NOW_TIMESTAMP)."\n"; // $res = $redis->get($cacheKey); // if($notice && !$res){ // $redis->setex($cacheKey, 60*30, 1); // 30分钟内不重复发送 // } if($notice){ curlDDMarkdown($noticeMsg, Env::get('dingtalk.notic_url'), true); } return ['code' => 200, 'msg' => '', 'data' => $prizeList]; } /** * 礼包操作缓存管理 * * @param $type string 操作类型 get-获取 add-添加 num-数量 * @param $mark_code string 礼包标识 * @param $codeLists array 礼包码列表(仅添加时使用) * * @return void|bool */ protected function handleGiftCache($type, $mark_code, $codeLists = []){ if(!$type || !$mark_code){ return false; } $cacheKey = $this->gift_cache_mark.$mark_code; $redis = Cache::store('redis')->handler(); switch ($type){ case 'get': $res = $redis->lPop($cacheKey); // string break; case 'add': $res = $redis->lPush($cacheKey, ...$codeLists); // int $redis->Expire($cacheKey, $this->gift_cache_mark_time); Db::table('lr_prize')->where(['mark_code' => $mark_code])->update(['all_num' => Db::raw('all_num + '.count($codeLists))]); break; case 'num': $res = $redis->Llen($cacheKey); // int break; default: $res = false; break; } return $res; } /** * 获取用户代金卷 * * @param $coupon_id string 代金卷ID * @param $user_id string 用户ID * * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getMemberCoupon($mark_code, $user_id){ if (!$mark_code || !$user_id) { return ['code' => -100, 'msg' => '参数有误!', 'data' => []]; } $coupon_id = Db::table('lr_prize')->where('mark_code', $mark_code)->value('mark_value'); if(!$coupon_id){ return ['code' => -110, 'msg' => '未找到当前奖品标识, '.$mark_code, 'data' => []]; } $couponInfo = model('Common/Coupon')->where('id', $coupon_id)->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2)', Db::quote(date('Y-m-d'))))->find(); if (!$couponInfo) { return ['code' => -111, 'msg' => '代金卷不存在, '. $coupon_id, 'data' => []]; } // // 判断是否已领取过{一个账户一次活动可能领取重复代金卷} // if(Db::table('cy_coupon_member')->where(['coupon_id' => $couponInfo['id'], 'member_id' => $user_id])->count() > 0){ // return ['code' => -112, 'msg' => '当前代金卷已领取!', 'data' => []]; // } // 判断类型 if ($couponInfo['type_id'] == 1) { $start_time = 0; $end_time = 0; } else { $start_time = strtotime(date('Y-m-d')); $end_time = strtotime(date('Y-m-d')) + 86400 * $couponInfo['day'] - 1; } $code = makeUniqueid('Q') . 'MLBB'; $couponMemberInfo = [ 'coupon_id' => $couponInfo['id'], 'member_id' => $user_id, 'start_time' => $start_time, 'end_time' => $end_time, 'examine' => 2, 'code' => $code, ]; Db::startTrans(); try { $saveCouponMember = Db::table('cy_coupon_member')->insert($couponMemberInfo); if(!$saveCouponMember){ throw new \Exception('代金券领取失败', -120); } $saveCoupon = Db::table('cy_coupon')->where('id', $couponInfo['id'])->update(['grant_num' => $couponInfo['grant_num'] + 1,'receive_num' => $couponInfo['receive_num'] + 1]); if(!$saveCoupon){ throw new \Exception('代金券更新失败', -121); } Db::commit(); } catch (\Exception $e) { Db::rollback(); return ['code' => -199, 'msg' => '操作异常:'.$e->getMessage(), 'data' => []]; } return ['code' => 200, 'msg' => '操作成功', 'data' => ['code' => $code]]; } // 发放道具 public function entendRewards($entendData){ $result = (new LvRenSdk())->sendRewards($entendData['sub_username'], $entendData['server_id'], $entendData['role_id'], $entendData['actId'], $entendData['actSubId']); if($result['code'] != 200){ return ['code' => -100, 'msg' => $result['msg'], 'data' => []]; } return ['code' => 200, 'msg' => '操作成功', 'data' => []]; } }