| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696 |
- <?php
- namespace app\common\service;
- use app\admin\model\dao\BoxMidHandle;
- use app\common\model\BoxHandleAllModel;
- use app\common\model\BoxHandleModel;
- use app\common\model\BoxMidHandleModel;
- use app\common\model\BoxModel;
- use app\common\model\BoxRecordModel;
- use app\common\model\MoneyLogModel;
- use app\common\model\ScoreLogModel;
- use app\common\model\ShopGoodsModel;
- use app\common\model\UserModel;
- use app\common\model\UserUnmoneyModel;
- use think\facade\Db;
- use utils\RedisCache;
- /**
- * 盒子预约记录服务 by wes
- * Class BoxRecordService
- * @package app\common\service
- */
- class BoxRecordService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new BoxRecordModel();
- }
- /**
- * 静态化入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 获取待处理福袋
- * @param $params
- * @param int $pageSize
- */
- public function getCatchList($params, $pageSize = 20)
- {
- $where = ['r.status' => 1];
- $uid = isset($params['uid']) ? $params['ud'] : 0;
- if ($uid > 0) {
- $where['r.uid'] = $uid;
- }
- $mobile = isset($params['mobile']) ? trim($params['mobile']) : '';
- if (!empty($mobile)) {
- $where['r.mobile'] = $mobile;
- }
- $boxId = isset($params['box_id']) ? trim($params['box_id']) : '';
- if (!empty($boxId)) {
- $where['r.box_id'] = $boxId;
- }
- $qiCount = isset($params['qi_count']) ? $params['qi_count'] : 0;
- if ($qiCount > 0) {
- $where['r.qi_count'] = $qiCount;
- }
- $lunCount = isset($params['lun_count']) ? $params['lun_count'] : 0;
- if ($lunCount > 0) {
- $where['r.lun_count'] = $lunCount;
- }
- $isMatch = isset($params['is_set_match']) ? $params['is_set_match'] : 0;
- if ($isMatch > 0) {
- $where['r.is_set_match'] = $isMatch;
- }
- $list = $this->model->alias('r')
- ->where($where)
- ->where(function ($query) use ($params) {
- $box10 = isset($params['box10']) ? $params['box10'] : 0;
- $box20 = isset($params['box20']) ? $params['box20'] : 0;
- $box30 = isset($params['box30']) ? $params['box30'] : 0;
- $box40 = isset($params['box40']) ? $params['box40'] : 0;
- if ($box10 > 0) {
- $query->where('r.box10', '>=', $box10);
- }
- if ($box20 > 0) {
- $query->where('r.box20', '>=', $box20);
- }
- if ($box30 > 0) {
- $query->where('r.box30', '>=', $box30);
- }
- if ($box40 > 0) {
- $query->where('r.box40', '>=', $box40);
- }
- })
- ->where('r.create_time', '>=', date('Y-m-d'))
- ->leftJoin('user u', 'u.id = r.uid')
- ->field(Db::raw('r.*,u.mobile,u.total_null_box,sum(r.num) as num,u.total_free,u.total_appoint_count,u.total_income,u.yesterday_money,u.box10 as ubox10,u.box20 as ubox20,u.box30 as ubox30,u.box40 as ubox40,u.path'))
- ->group('group_key')
- ->order('r.create_time desc')
- ->order('r.qi_count desc')
- ->order('r.lun_count desc')
- ->order('r.num desc')
- ->paginate($pageSize)
- ->each(function($item, $k){
- $path = isset($item['path'])? $item['path'] : '';
- $path = $path? explode(',', $path):[];
- $path[] = $item['uid'];
- $item['last_team_profit'] = UserUnmoneyModel::whereIn('uid',$path)->whereDay('create_time','yesterday')->sum('money');
- $item['today_team_profit'] = UserUnmoneyModel::whereIn('uid',$path)->whereDay('create_time','today')->sum('money');
- });
- $list = $list ? $list->toArray() : [];
- return $list;
- }
- /**
- * 统计小于某预约数量的渔业次数
- * @param $uid 用户ID
- * @param $boxId 福袋ID
- * @param int $num 数量
- * @return array|mixed|null
- * @throws \think\db\exception\DbException
- */
- public function getCountByUser($uid, $boxId, $num = 5)
- {
- $cacheKey = "caches:boxRecord:count:{$uid}_{$boxId}_{$num}";
- $data = RedisCache::get($cacheKey);
- if ($data) {
- return $data;
- }
- $count = $this->model->where(['uid' => $uid, 'box_id' => $boxId, 'status' => 1])
- ->where('num', '<', $num)
- ->count('id');
- if ($count) {
- RedisCache::set($cacheKey, $count, rand(3, 5));
- }
- return $data;
- }
- /**
- * 预约福袋处理
- * @param $uid
- * @param $params
- * @param $user_info
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function applyBox($uid, $params)
- {
- $boxCount = isset($params['buy_count']) ? intval($params['buy_count']) : 0;
- $boxId = isset($params['box_id']) ? intval($params['box_id']) : 0;
- $payType = isset($params['pay_type']) ? intval($params['pay_type']) : 0;
- if ($boxCount <= 0) {
- sr_throw('请选择预约福袋类型');
- }
- if ($boxId <= 0 || $payType <= 0) {
- sr_throw('预约参数错误');
- }
- if (!in_array($payType, [1, 2])) {
- sr_throw('支付类型错误');
- }
- // 验证福袋功能是否开启
- $boxIsApply = SystemConfigService::make()->getConfigByName('fudai_is_apply', 1, 'fudai');
- if ($boxIsApply != 1) {
- sr_throw('福袋预约功能暂未开放');
- }
- // 福袋信息和验证
- $boxInfo = BoxModel::where('id', $boxId)->find();
- $openStatus = isset($boxInfo['open_status']) ? $boxInfo['open_status'] : 0;
- if (!$boxInfo) {
- sr_throw('福袋参数错误');
- }
- if ($openStatus != 1) {
- sr_throw('该期福袋预约未开启');
- }
- if ($boxCount > 20) {
- sr_throw('每次最多可预约20个');
- }
- // 如果预约数量小于5
- if ($boxCount < 5 && $this->getCountByUser($uid, $boxId, 5) >= 4) {
- sr_throw('每一场数量小于5最多只能预约4次');
- }
- $lun = 1;
- $time = time();
- $canBuy = false;
- $times = explode('|', $boxInfo['time_set']);
- $curTimeDay = sr_getcurtime(time(), 'Y-m-d');
- $openTime = '';
- foreach ($times as $key => $val) {
- $timesarr = explode('-', $val);
- $beginTime = strtotime($curTimeDay . ' ' . $timesarr[0]);
- $endTime = strtotime($curTimeDay . ' ' . $timesarr[1]);
- if ($time > $beginTime && $time < $endTime) {
- $canBuy = true;
- $lun = $key + 1;
- $openTime = date('Y-m-d H:i:s', $endTime + 30 * 60);
- }
- }
- // 预约数量限制
- $applyCount = $this->model->where(['uid' => $uid,'box_id'=>$boxId,'lun_count'=>$lun, 'status' => 1])->sum('num');
- if ($applyCount + $boxCount> 100) {
- sr_throw("每一场最多可预约100个,您已预约".($applyCount)."个");
- }
- if (!$canBuy) {
- sr_throw('no_open');
- }
- // 总金额
- $totalPay = $boxCount * env('boxsetting.one_box_price', 288);
- $userInfo = UserModel::where(['id' => $uid])->field('id,score,money,has_fd')->find();
- $userMoney = isset($userInfo['money']) ? floatval($userInfo['money']) : 0;
- $userScore = isset($userInfo['score']) ? floatval($userInfo['score']) : 0;
- $hasFd = isset($userInfo['has_fd']) ? intval($userInfo['has_fd']) : 0;
- // 积分支付
- if ($payType == 1) {
- if ($userScore < $totalPay) {
- sr_throw('积分不足,预约失败');
- }
- // 扣除用户积分
- if (!UserModel::where('id', $uid)->dec('score', $totalPay)->update()) {
- sr_throw('扣除账户积分失败');
- }
- // 积分流水
- $data = [
- 'uid' => $uid,
- 'type' => 1,
- 'score' => $totalPay,
- 'create_at' => sr_getcurtime(time()),
- 'state' => 2,
- 'before_score' => $userScore,
- 'after_score' => max(0, $userScore - $totalPay),
- 'from_id' => 0,
- 'uid2' => 0,
- 'remark' => '预约福袋:' . $boxCount . '个'
- ];
- if (!ScoreLogModel::insertGetId($data)) {
- sr_throw('扣除积分处理失败');
- }
- }
- // 余额支付
- if ($payType == 2) {
- if ($userMoney < $totalPay) {
- sr_throw('余额不足,预约失败');
- }
- // 扣除用户余额
- if (!UserModel::where('id', $uid)->dec('money', $totalPay)->update()) {
- sr_throw('扣除账户余额失败');
- }
- // 余额流水
- $data = [
- 'uid' => $uid,
- 'type' => 1,
- 'money' => $totalPay,
- 'create_at' => sr_getcurtime(time()),
- 'state' => 2,
- 'before_money' => $userMoney,
- 'after_money' => max(0, $userMoney - $totalPay),
- 'from_id' => 0,
- 'uid2' => 0,
- 'remark' => '预约福袋:' . $boxCount . '个'
- ];
- if (!MoneyLogModel::insertGetId($data)) {
- sr_throw('扣除余额处理失败');
- }
- }
- // 预约记录
- $recordData = [
- 'box_id' => $params['box_id'],
- 'num' => $params['buy_count'],
- 'lun_count' => $lun,
- 'uid' => $uid,
- 'create_time' => sr_getcurtime($time),
- 'open_time' => $openTime ? $openTime : sr_getcurtime(time() + 30 * 60),
- 'box_img' => $boxInfo['box_img'],
- 'group_key' => $boxId . '_' . $boxInfo['qi_count'] . '_' . $lun . '_' . $uid,
- 'box_title' => $boxInfo['box_title'],
- 'qi_count' => $boxInfo['qi_count'],
- 'pay_type' => $params['pay_type']
- ];
- if (!$this->model->insertGetId($recordData)) {
- sr_throw('预约处理失败');
- }
- // 统计用户预约次数
- if (!UserModel::where('id', $uid)->inc('total_appoint_count', $params['buy_count'])->update()) {
- sr_throw('用户预约数据处理失败');
- }
- // 清除拆袋缓存
- RedisCache::clear("caches:box:open:user_" . $uid);
- return true;
- }
- /**
- * 设置福袋匹配数据
- * @param $ids
- * @param $data
- * @return bool
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function setBoxMatch($ids, $data)
- {
- $ids = $ids ? explode(',', $ids) : [];
- $box10 = isset($data['box10']) ? intval($data['box10']) : 0;
- $box20 = isset($data['box20']) ? intval($data['box20']) : 0;
- $box30 = isset($data['box30']) ? intval($data['box30']) : 0;
- $box40 = isset($data['box40']) ? intval($data['box40']) : 0;
- $boxList = BoxRecordModel::whereIn('id', $ids)->where('status', 1)
- ->field('uid,box_id,group_key,box10,box20,box30,box40,is_set_match')
- ->select();
- $boxList = $boxList ? $boxList->toArray() : [];
- if (empty($boxList)) {
- return '请选择有效的用户进行操作';
- }
- if (count($ids) != count($boxList)) {
- return '存在不可设置的用户';
- }
- $matchCount = intval($box10 + $box20 + $box30 + $box40);
- foreach ($boxList as $item) {
- $uid = isset($item['uid']) ? $item['uid'] : 0;
- $groupKey = isset($item['group_key']) ? $item['group_key'] : '';
- $applyCount = $this->model->where(['group_key'=>$groupKey,'status'=>1])->sum('num');
- if ($matchCount > $applyCount) {
- return "用户[{$uid}]不能设置超过最大预约量{$applyCount}个";
- }
- }
- $updateData = ['box10' => $box10, 'box20' => $box20, 'box30' => $box30, 'box40' => $box40, 'is_set_match' => 1, 'update_time' => date('Y-m-d H:i:s')];
- $hasXy = isset($data['hs_xy'])? $data['hs_xy'] : '';
- if ($hasXy == 'on') {
- $updateData['hs_xy'] = 1;
- }else {
- $updateData['hs_xy'] = 0;
- }
- Db::startTrans();
- if (!$this->model->whereIn('id', $ids)->update($updateData)) {
- Db::rollback();
- return '设置匹配数据失败';
- }
- Db::commit();
- return true;
- }
- /**
- * 开奖处理
- * @param $ids
- * @param $post
- * @return string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function openBox($ids, $post)
- {
- set_time_limit(0);
- if (empty($ids)) {
- return '请先选择要开出的用户';
- }
- $cacheTime = 7200;
- $key = md5(json_encode($ids));
- $date = date('Y-m-d H:i:s');
- $cacheKey = "caches:boxOpen:" . date('mdHi') . "_{$key}:";
- if (RedisCache::get($cacheKey . 'lock')) {
- return '正在开奖中,请不要关闭页面或重复提交,若失败请20秒后再试';
- }
- RedisCache::setnx($cacheKey . 'lock', 1, rand(10, 20));
- if ($this->model->whereIn('id', $ids)->where('is_set_match', 2)->value('id')) {
- RedisCache::set($cacheKey . 'error', ['ids' => $ids, 'date' => $date, 'msg' => '存在未设置匹配数据的记录'], $cacheTime);
- return '存在未设置匹配数据的记录';
- }
- // 盒子验证
- $boxInfo = BoxService::make()->getCacheInfo();
- if (empty($boxInfo)) {
- RedisCache::set($cacheKey . 'error', ['ids' => $ids, 'date' => $date, 'msg' => '进行中的福袋数据不存在或预约中'], $cacheTime);
- return '当前福袋场次未开或预约中';
- }
- // 处理的预约记录
- $recordList = $this->model->alias('a')->whereIn('a.id', $ids)
- ->leftJoin('user u', 'u.id=a.uid')
- ->where('a.status', 1)
- ->field('a.id,a.box_id,a.num,a.uid,a.group_key,a.hs_xy,a.box10,a.box20,a.box30,a.box40,a.pay_type,a.open_time,u.has_fd,u.path,u.pid')
- ->select();
- $recordList = $recordList? $recordList->toarray() : [];
- if (empty($recordList)) {
- RedisCache::set($cacheKey . 'error', ['ids' => $ids, 'date' => $date, 'msg' => '暂无有效的预约记录'], $cacheTime);
- return '暂无有效的预约记录';
- }
- if (count($ids) != count($recordList)) {
- RedisCache::set($cacheKey . 'error', ['ids' => $ids,'count'=>count($recordList), 'date' => $date, 'msg' => '存在已处理记录,请刷新后重试'], $cacheTime);
- return '存在已处理记录,请刷新后重试~';
- }
- // 盒子商品
- $appointGoods = ShopGoodsService::make()->getBoxMatchGoods(20, 0,20);
- $boxGoods[10] = ShopGoodsService::make()->getBoxMatchGoods(10, 1,20);
- $boxGoods[20] = ShopGoodsService::make()->getBoxMatchGoods(20, 1,20);
- $boxGoods[30] = ShopGoodsService::make()->getBoxMatchGoods(30, 1,20);
- $boxGoods[40] = ShopGoodsService::make()->getBoxMatchGoods(40, 1,20);
- if (empty($appointGoods) && empty($boxGoods[10]) && empty($boxGoods[20]) && empty($boxGoods[30]) && empty($boxGoods[40])) {
- RedisCache::set($cacheKey . 'error', ['ids' => $ids, 'date' => $date, 'msg' => '请先设置福袋商品'], $cacheTime);
- return '请先设置福袋商品';
- }
- // 处理预约记录
- Db::startTrans();
- try {
- $boxMidList = [];
- $boxOpenArr = [];
- $awardUids = []; // 中奖的用户
- $groupKeys = []; // 已处理的记录分组
- $nullAwardCount = 0;
- $allAwardCount = 0;
- $boxCount = 0;
- foreach ($recordList as $key => $val) {
- $rid = isset($val['id']) ? $val['id'] : 0;
- $uid = isset($val['uid']) ? $val['uid'] : 0;
- $boxId = isset($val['box_id']) ? $val['box_id'] : 0;
- $groupKey = isset($val['group_key']) ? $val['group_key'] : '';
- $hasXy = isset($val['hs_xy']) ? $val['hs_xy'] : 0;
- $payType = isset($val['pay_type']) ? $val['pay_type'] : 0;
- $openTime = isset($val['open_time']) && $val['open_time'] ? $val['open_time'] : sr_getcurtime(time() + 120);
- if (!$rid || !$uid || !$boxId || empty($groupKey)) {
- Db::rollback();
- RedisCache::set($cacheKey . "r{$rid}_{$groupKey}_error", ['ids' => $ids, 'info' => $val, 'date' => $date, 'msg' => "抱歉用户[{$uid}]存在错误的预约数据,开袋失败"], $cacheTime);
- return "抱歉用户[{$uid}]存在错误的预约数据,开袋失败";
- }
- // 本次总预约数量
- $total = $this->model->where(['group_key' => $groupKey,'status'=> 1])->sum('num');
- $scorePayNum = $this->model->where(['group_key' => $groupKey,'status'=> 1,'pay_type'=>1])->sum('num');
- $box10 = isset($val['box10']) ? intval($val['box10']) : 0;
- $box20 = isset($val['box20']) ? intval($val['box20']) : 0;
- $box30 = isset($val['box30']) ? intval($val['box30']) : 0;
- $box40 = isset($val['box40']) ? intval($val['box40']) : 0;
- $openBoxNum = intval($box10 + $box20 + $box30 + $box40);
- $boxCount = $boxCount + $total;
- // 验证开出的盒子数和预约数量
- $tempScorePayNum = $scorePayNum;
- $nullCount = max(0, $total - $openBoxNum); // 空盒数量
- if ($openBoxNum > $total) {
- Db::rollback();
- RedisCache::set($cacheKey . "r{$rid}_{$groupKey}_error", ['ids' => $ids, 'total' => $total,'scoreNum'=>$tempScorePayNum, 'openNum' => $openBoxNum, 'info' => $val, 'date' => $date, 'msg' => "开袋失败:用户[{$uid}]的匹配数量设置超出预约数量"], $cacheTime);
- return "开袋失败:用户[{$uid}]的匹配数量设置超出预约数量";
- }
- // 处理空盒
- if ($nullCount > 0) {
- $time = sr_getcurtime(time() + $key);
- for ($i = 0; $i < $nullCount; $i++) {
- $boxMidList[] = [
- 'h_sn' => createdHandleOrderSn(),
- 'uid' => $uid,
- 'rid' => $rid,
- 'status' => 1,
- 'goods_id' => 0,
- 'create_time' => $time,
- 'box_settle_time' => $openTime,
- 'box_type' => 0,
- 'goods_price' => 0,
- 'pay_type' => $scorePayNum>0? 1 : 2
- ];
- $nullAwardCount++;
- $scorePayNum--;
- }
- }
- // 中奖处理
- $awardCount = 0;
- $openGoodsIds = [];
- if ($openBoxNum > 0) {
- $boxArr = [10 => '普通', 20 => '稀有', 30 => '史诗', 40 => '传说'];
- foreach ($boxArr as $bk => $bName) {
- $goodsArr = isset($boxGoods[$bk]) ? $boxGoods[$bk] : [];
- $awardNum = isset($val['box' . $bk]) ? intval($val['box' . $bk]) : 0;
- $time = sr_getcurtime(strtotime($openTime) + ($bk / 10));
- // 有中奖
- if ($awardNum > 0) {
- for ($i = 0; $i < $awardNum; $i++) {
- // 指定稀有
- $openGoods = $goodsArr;
- if ($hasXy == 1 && $i != 0 && $bk == 20) {
- $openGoods = isset($appointGoods) ? $appointGoods : [];
- }
- $rand = $openGoods ? rand(0, count($openGoods) - 1) : 0;
- $goods = isset($openGoods[$rand]) ? $openGoods[$rand] : [];
- $goodsId = isset($goods['goods_id']) ? $goods['goods_id'] : 0;
- if (empty($openGoods) || empty($goods) || $goodsId <= 0) {
- Db::rollback();
- RedisCache::set($cacheKey . "r{$rid}_{$groupKey}_error", ['ids' => $ids, 'total' => $total,'scoreNum'=>$tempScorePayNum, 'openNum' => $openBoxNum, 'info' => $val, 'date' => $date, 'msg' => "开袋失败:[{$bName}]福袋商品没有库存可以开奖,请先设置"], $cacheTime);
- return "开袋失败:[{$bName}]福袋商品没有足够库存可以开奖,请先设置";
- }
- $boxMidList[] = [
- 'h_sn' => createdHandleOrderSn(),
- 'uid' => $uid,
- 'rid' => $rid,
- 'status' => 2,
- 'goods_id' => $goodsId,
- 'create_time' => $time<$openTime?$openTime:$time,
- 'box_settle_time' => $openTime,
- 'box_type' => $bk,
- 'goods_price' => isset($goods['price']) ? $goods['price'] : 0,
- 'pay_type' => $scorePayNum>0? 1 : 2
- ];
- // 中奖数据
- $boxOpenArr[] = [
- 'status' => 1,
- 'h_sn' => createdHandleOrderSn(),
- 'uid' => $uid,
- 'rid' => $rid,
- 'handle_type' => 0,
- 'goods_id' => $goodsId,
- 'create_time' => $time<$openTime?$openTime:$time,
- 'open_time' => $openTime,
- 'box_type' => $bk,
- 'goods_price' => isset($goods['price']) ? $goods['price'] : 0
- ];
- $openGoodsIds[] = $goodsId;
- // 用户已中奖
- $awardUids[$uid] = 1;
- // 累计中奖数
- $awardCount++;
- $allAwardCount++;
- $scorePayNum--;
- }
- }
- }
- // 如果设置了盒子但是未开出有,则开奖失败
- if (empty($openGoodsIds)) {
- Db::rollback();
- RedisCache::set($cacheKey . "r{$rid}_{$groupKey}_error", ['ids' => $ids, 'total' => $total,'scoreNum'=>$tempScorePayNum, 'openNum' => $openBoxNum, 'info' => $val, 'date' => $date, 'msg' => "开袋失败:用户[{$uid}]福袋开奖处理错误"], $cacheTime);
- return "开袋失败:用户[{$uid}]福袋开奖处理错误";
- }
- }
- // 更新开奖记录的开出商品ID
- $updateData = ['status' => 3, 'update_time' => date('Y-m-d H:i:s')];
- if ($openGoodsIds) {
- $updateData['goods_ids'] = implode(',', $openGoodsIds);
- }
- if (!$this->model->where('id', $rid)->update($updateData)) {
- Db::rollback();
- RedisCache::set($cacheKey . "r{$rid}_{$groupKey}_error", ['ids' => $ids, 'total' => $total,'scoreNum'=>$tempScorePayNum, 'openNum' => $openBoxNum, 'info' => $val, 'date' => $date, 'msg' => "开袋失败:用户[{$uid}]更新开袋状态失败"], $cacheTime);
- return "开袋失败:用户[{$uid}]更新开袋状态失败";
- }
- // 处理用户开盒数据统计
- $hasFd = isset($val['has_fd']) ? $val['has_fd'] : 0;
- $isUpdate = UserModel::where('id', $uid)
- // 中奖数据
- ->inc('total_null_box', $nullCount)
- ->inc('box10', $box10)
- ->inc('box20', $box20)
- ->inc('box30', $box30)
- ->inc('box40', $box40)
- // 团队数据
- ->inc('today_box', $awardCount)
- ->inc('today_team_box', $awardCount)
- ->inc('total_box', $awardCount)
- ->inc('total_team_box', $awardCount)
- // 是否中过福袋
- ->inc('has_fd', $awardCount && !$hasFd ? 1 : 0)
- ->update();
- if (!$isUpdate) {
- Db::rollback();
- RedisCache::set($cacheKey . "r{$rid}_{$groupKey}_error", ['ids' => $ids, 'total' => $total,'scoreNum'=>$tempScorePayNum, 'openNum' => $openBoxNum, 'info' => $val, 'date' => $date, 'msg' => "开袋失败:更新用户[{$uid}]的中奖数据错误"], $cacheTime);
- return "开袋失败:更新用户[{$uid}]的中奖数据错误";
- }
- // 团毒统计中奖次数更新
- $upperPath = isset($val['path']) ? $val['path'] : '';
- $pid = isset($val['pid']) ? $val['pid'] : 0;
- if ($awardCount > 0 && $upperPath) {
- if (!UserModel::where('id', 'in', $upperPath)->inc('total_team_box', $awardCount)->inc('has_update_level', (!$hasFd? 1:0))->update()) {
- Db::rollback();
- RedisCache::set($cacheKey . "r{$rid}_{$groupKey}_error", ['ids' => $ids, 'total' => $total,'scoreNum'=>$tempScorePayNum, 'path' => $upperPath, 'openNum' => $openBoxNum, 'info' => $val, 'date' => $date, 'msg' => "开袋失败:更新用户[{$uid}]的团队中奖数据错误"], $cacheTime);
- return "开袋失败:更新用户[{$uid}]的团队中奖数据错误";
- }
- // 如果当前用户第一次中奖,且有直推上级,上级有效直推累计
- if($pid>0 && !$hasFd){
- //UserModel::where('id', 'in', $pid)->inc('zt_num_valid', 1)->update();
- }
- // 清理有效直推和团队人数缓存
- RedisCache::keyDel("caches:users:counts*");
- }
- // 处理成功
- $groupKeys[] = $groupKey;
- RedisCache::set($cacheKey . "r{$rid}_{$groupKey}_catch", ['ids' => $ids, 'total' => $total,'scoreNum'=>$tempScorePayNum, 'awardCount' => $awardCount, 'openNum' => $openBoxNum, 'null' => $nullCount, 'info' => $val, 'date' => $date, 'msg' => "开袋处理成功:更新用户[{$uid}]的数据处理成功"], $cacheTime);
- }
- if (empty($groupKeys) || empty($boxMidList)) {
- Db::rollback();
- RedisCache::set($cacheKey . "error", ['ids' => $ids, 'count' => count($boxMidList),'scoreNum'=>$tempScorePayNum, 'groupKeys' => $groupKeys, 'date' => $date, 'msg' => "开袋失败:没有开出有效福袋"], $cacheTime);
- return "开袋失败:没有开出有效福袋";
- }
- // 更新同组盒子状态
- if(count($groupKeys) == 1){
- if (!$this->model->where('group_key', $groupKeys[0])->update(['status' => 3, 'update_time' => date('Y-m-d H:i:s')])) {
- Db::rollback();
- RedisCache::set($cacheKey . "error", ['ids' => $ids,'sql'=>$this->model->getLastSql(),'keys'=>$groupKeys, 'count' => count($boxMidList),'scoreNum'=>$tempScorePayNum, 'date' => $date, 'msg' => "开袋失败:更新福袋开袋状态数据错误"], $cacheTime);
- return "开袋失败:更新福袋开袋状态数据错误";
- }
- }else{
- if (!$this->model->whereIn('group_key', $groupKeys)->update(['status' => 3, 'update_time' => date('Y-m-d H:i:s')])) {
- Db::rollback();
- RedisCache::set($cacheKey . "error", ['ids' => $ids,'sql'=>$this->model->getLastSql(),'keys'=>$groupKeys, 'count' => count($boxMidList),'scoreNum'=>$tempScorePayNum, 'date' => $date, 'msg' => "开袋失败:更新福袋开袋状态数据错误"], $cacheTime);
- return "开袋失败:更新福袋开袋状态数据错误";
- }
- }
- // 插入匹配数据
- if (!BoxMidHandleModel::insertAll($boxMidList)) {
- Db::rollback();
- RedisCache::set($cacheKey . "error", ['ids' => $ids, 'count' => count($boxMidList),'scoreNum'=>$tempScorePayNum, 'date' => $date, 'msg' => "开袋失败:写入福袋处理数据错误"], $cacheTime);
- return "开袋失败:写入福袋处理数据错误";
- }
- // 插入备份中奖盒子数据
- if ($boxOpenArr && !BoxHandleModel::insertAll($boxOpenArr)) {
- Db::rollback();
- RedisCache::set($cacheKey . "error", ['ids' => $ids, 'count' => count($boxMidList),'scoreNum'=>$tempScorePayNum, 'award' => count($boxOpenArr), 'date' => $date, 'msg' => "开袋失败:写入福袋中奖数据错误"], $cacheTime);
- return "开袋失败:写入福袋中奖数据错误";
- }
- // 插入中奖盒子数据
- if ($boxOpenArr && !BoxHandleAllModel::insertAll($boxOpenArr)) {
- Db::rollback();
- RedisCache::set($cacheKey . "errora", ['ids' => $ids, 'count' => count($boxMidList),'scoreNum'=>$tempScorePayNum, 'award' => count($boxOpenArr), 'date' => $date, 'msg' => "开袋失败:写入备份福袋中奖数据错误"], $cacheTime);
- return "开袋失败:写入福袋中奖数据错误";
- }
- Db::commit();
- RedisCache::clear($cacheKey . 'lock');
- RedisCache::set($cacheKey . "error", ['ids' => $ids, 'count' => count($boxMidList),'scoreNum'=>$tempScorePayNum, 'null' => $nullAwardCount, 'award' => $allAwardCount, 'date' => $date, 'msg' => "开袋失败:写入福袋中奖数据错误"], $cacheTime);
- return ['code' => 0, 'msg' => "开袋成功,本次提交处理数据" . count($ids) . "条,累计福袋{$boxCount}个,开出空袋:{$nullAwardCount}个,中奖{$allAwardCount}个。", 'data' => $ids];
- } catch (\Exception $exception){
- Db::rollback();
- RedisCache::set($cacheKey.'fail', ['ids'=> $ids,'error'=>['msg'=>$exception->getMessage(),'trace'=>$exception->getTrace()]], 3600);
- return '开奖失败:'.$exception->getMessage();
- }
- }
- }
|