123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- /**
- * Created by PhpStorm.
- * User: hanl
- * Date: 2018/6/19
- * Time: 10:13
- */
-
- namespace App\Services;
-
-
- use DB;
- use App\Common\Api;
- use App\Models\Configs;
- use App\Models\Prizes;
- use App\Models\LotteryBoxs;
- use App\Services\ActivityService;
- use Illuminate\Support\Facades\Redis;
-
- class PrizeService
- {
- public function __construct($configs = [])
- {
-
- }
-
- /**
- * 获取奖品详情
- * @param $params
- * @return array
- */
- public static function getPrizeInfo($params){
- $activity_id = $params['activity_id'];
- $corp_id = $params['corp_id'];
- $prize_id = $params['prize_id'];
-
- $prizeInfo = Prizes::where('activity_id', $activity_id)->where('corp_id', $corp_id)->where('prize_id', $prize_id)->first();
-
- //数据库存储以千分位为单位,页面以百分位单位
- $prizeInfo['prize_weights'] = $prizeInfo['prize_weights']/10;
-
- return Api::arr(config('code.success'), trans('msg.success'), $prizeInfo);
- }
-
- /**
- * 添加/修改奖品详情
- * @param $params
- * @return array|string
- */
- public static function prizeDetail($params){
- $query = [];
- $query['activity_id'] = $params['activity_id'];
- $query['corp_id'] = $params['corp_id'];
- $query['lottery_level'] = $params['lottery_level'];
- $query['prize_name'] = $params['prize_name'];
- $query['prize_day_rule'] = $params['prize_day_rule'];
- $query['prize_count'] = $params['prize_count'];
- $query['prize_weights'] = $params['prize_weights']*10;
- $query['prize_image_url'] = $params['prize_image_url'];
- $query['update_time'] = time();
-
- if (!empty($params['prize_id'])) {
- //奖品信息判断
- $actPrizeInfo = self::getPrizeInfo($params);
- if($actPrizeInfo['code'] != 0 || empty($actPrizeInfo['data'])){
- return Api::arr(config('code.fail'), trans('msg.prize_not_exist'));
- }
- $res = Prizes::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->where('lottery_level', $params['lottery_level'])->where('prize_id', $params['prize_id'])->update($query);
-
- //奖品每日抽取逻辑优化 设置差值同步到redis
- $num = $query['prize_day_rule'] - $actPrizeInfo['data']['prize_day_rule'];
- $key = 'bs_prize_count:'.$params['prize_id'].':'.date('Ymd');
- if(Redis::exists($key) && $num != 0) {
- Redis::incrBy($key, $num);
- }
-
- } else {
- $query['create_time'] = time();
- $res = Prizes::insert($query);
- }
-
- if ($res) {
- return Api::arr(config('code.success'), trans('msg.success'));
- } else {
- return Api::arr(config('code.fail'), trans('msg.sql_error'));
- }
- }
-
- /**
- * 获取宝箱内的奖品列表
- * @param $corp_id
- * @param $activity_id
- * @param $lottery_level
- * @return array
- */
- public static function getPrizeList($corp_id, $activity_id, $lottery_level){
- $actInfo = ActivityService::getActivityInfo($activity_id, $corp_id);
- if(!$actInfo){
- return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist'));
- }
- $data = Prizes::where('activity_id', $activity_id)
- ->where('lottery_level', $lottery_level)
- ->where('status', '!=', 0)
- ->get()->toArray();
-
- foreach($data as $k=>$v){
- $data[$k]['prize_weights'] = $v['prize_weights']/10;
- }
-
- return Api::arr(config('code.success'), trans('msg.success'), $data);
- }
-
- /**
- * 奖品删除或下架
- * @param $params
- * @return array
- */
- public static function prizeSoldOut($params){
- $prizeInfo = Prizes::where('activity_id', $params['activity_id'])
- ->where('corp_id', $params['corp_id'])
- ->where('lottery_level', $params['lottery_level'])
- ->where('prize_id', $params['prize_id'])
- ->first();
- if(!$prizeInfo){
- return Api::arr(config('code.prize_not_exist'), trans('msg.prize_not_exist'));
- }
-
- $res = Prizes::where('activity_id', $params['activity_id'])
- ->where('corp_id', $params['corp_id'])
- ->where('lottery_level', $params['lottery_level'])
- ->where('prize_id', $params['prize_id'])
- ->update([
- 'status'=>$params['status'],
- 'update_time'=>time()
- ]);
- if ($res) {
- return Api::arr(config('code.success'), trans('msg.success'));
- } else {
- return Api::arr(config('code.fail'), trans('msg.sql_error'));
- }
- }
-
- /**
- * 获取活动宝箱配置
- * 2018-11-06 16:17
- * @param $params
- * @return array
- */
- public static function getLotteryBoxs($params){
- $activityId = $params['activity_id'];
- $corpId = $params['corp_id'];
- $boxInfo = LotteryBoxs::where('activity_id', $activityId)->where('corp_id', $corpId)->get()->toArray();
- return Api::arr(config('code.success'), trans('msg.success'), $boxInfo);
- }
-
- /**
- * 生成默认宝箱或保存宝箱参数
- * @param $params
- * @param bool $type
- * @return array
- */
- public static function defaultBox($params, $type=false){
- if($type){ //添加默认宝箱设置
- $boxs = [1,2,3,4];
- $querys = [];
- foreach($boxs as $k=>$v){
- $query = [];
- $query['activity_id'] = $params['activity_id'];
- $query['corp_id'] = $params['corp_id'];
- $query['lottery_level'] = $v;
- $query['open_mode'] = 'km';
- $query['create_time'] = $query['update_time'] = time();
- switch($v){
- case 1:
- $query['open_rule'] = 100;
- $query['lottery_name'] = '铜宝箱';
- $query['lottery_icon'] = '//s1-hn.aghcdn.com/brainstorming/prize/prize_one.png';
- break;
- case 2:
- $query['open_rule'] = 200;
- $query['lottery_name'] = '银宝箱';
- $query['lottery_icon'] = '//s1-hn.aghcdn.com/brainstorming/prize/prize_two.png';
- break;
- case 3:
- $query['open_rule'] = 500;
- $query['lottery_name'] = '金宝箱';
- $query['lottery_icon'] = '//s1-hn.aghcdn.com/brainstorming/prize/prize_three.png';
- break;
- case 4:
- $query['open_rule'] = 0;
- $query['lottery_name'] = '幸运宝箱';
- $query['lottery_icon'] = '//s1-hn.aghcdn.com/brainstorming/prize/prize_four.png';
- break;
- }
- $querys[] = $query;
- }
- $res = LotteryBoxs::insert($querys);
- if($res){
- $data = LotteryBoxs::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->get();
- return Api::arr(config('code.success'), trans('msg.success'), $data);
- } else {
- return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
- }
- } else { //保存宝箱配置
- $arr = collect($params['box_config'])->toArray();
- foreach($arr as $k=>$v){
- $query = [];
- $query['lottery_name'] = $v['lottery_name'];
- $query['status'] = $v['status'];
- $query['open_rule'] = $v['open_rule'];
- $query['update_time'] = time();
- $actLevelInfo = LotteryBoxs::where('activity_id', $params['activity_id'])
- ->where('corp_id',$params['corp_id'])
- ->where('lottery_level', $k)
- ->first();
- if($actLevelInfo){
- LotteryBoxs::where('activity_id', $params['activity_id'])
- ->where('corp_id',$params['corp_id'])
- ->where('lottery_level', $k)
- ->update($query);
- } else {
- $query['activity_id'] = $params['activity_id'];
- $query['corp_id'] = $params['corp_id'];
- $query['lottery_level'] = $params['lottery_level'];
- $query['create_time'] = time();
- LotteryBoxs::insert($query);
- }
- }
- return true;
- }
- }
- }
- ?>
|