1, 'sliverPrize'=>2, 'goldPrize' =>3, ); /** * 活动复制 * @param $params * @return bool */ public static function copyActivity($params){ //活动实例信息 $act_info = Activitys::where('activity_id', $params['activity_id'])->first()->toArray(); if(!$act_info){ return Api::json(config('code.activity_not_exist'), trans('msg.activity_not_exist')); } $corp_id = empty($params['corp_id'])?$act_info['corp_id']:$params['corp_id']; unset($act_info['activity_id']); $act_query = $act_info; $act_query['corp_id'] = $corp_id; //活动参与验证方式可控 $act_query['auth_type'] = empty($params['auth_type'])?$act_info['auth_type']:$params['auth_type']; $act_query['status'] = 1; $act_query['is_temp_stop']= 0; //活动未暂停 $act_query['is_complete_set']= 0; $act_query['update_time'] = time(); $act_query['create_time'] = time(); //插入实例 $act_res = Activitys::insertGetId($act_query); //活动配置信息 $config_info = Configs::where('activity_id', $params['activity_id'])->first()->toArray(); $config_query = $config_info; $config_query['activity_id'] = $act_res; $config_query['corp_id'] = $corp_id; $config_query['update_time'] = time(); $config_query['create_time'] = time(); //插入配置 $config_res = Configs::insert($config_query); if(!$act_res || !$config_res){ return false; } //复制奖品 if(empty($params['prize'])){ $prize_res = self::copyPrize($params['activity_id'], $act_res, $corp_id); if(!$prize_res){ return false; } } //复制题库 if(empty($params['question'])){ $question_res = self::copyQuestion($params['activity_id'], $act_res, $corp_id); if(!$question_res){ return false; } } //复制用户(2018-08-08 修改为默认不复制人员) // if(!empty($params['user'])){ // $user_res = self::copyUser($params['activity_id'], $act_res, $corp_id); // if(!$user_res){ // return false; // } // } return $act_res; } /** * 复制奖品信息 * @param $old_act_id * @param $activity_id * @param $corp_id * @return bool */ public static function copyPrize($old_act_id, $activity_id, $corp_id){ //奖品信息 $prize_info = Prizes::where('activity_id', $old_act_id)->get()->toArray(); foreach ($prize_info as $k => $v) { $prize_query = []; unset($v['prize_id']); $prize_query = $v; $prize_query['activity_id'] = $activity_id; $prize_query['corp_id'] = $corp_id; $prize_query['prize_grant_count'] = 0; $prize_query['create_time'] = time(); $prize_query['update_time'] = time(); $prize_res = Prizes::insert($prize_query); if(!$prize_res){ return false; } } return true; } /** * 复制活动参与的人员信息和部门信息 * @param $old_act_id * @param $activity_id * @param $corp_id * @return bool */ public static function copyUser($old_act_id, $activity_id, $corp_id){ //部门信息 $department_info = Departments::where('activity_id', $old_act_id)->get()->toArray(); foreach ($department_info as $k => $v) { //该部门的用户信息 $user_info = Users::where('activity_id', $old_act_id)->where('department_id', $v['department_id'])->get()->toArray(); $department_query = []; unset($v['department_id']); $department_query = $v; $department_query['activity_id'] = $activity_id; $department_query['corp_id'] = $corp_id; $department_query['create_time'] = time(); $department_query['update_time'] = time(); $department_res = Departments::insertGetId($department_query); if(!$department_res){ return false; } //部门用户复制 foreach($user_info as $key=>$val){ $user_query = []; unset($val['user_id']); $user_query['activity_id'] = $activity_id; $user_query['corp_id'] = $corp_id; $user_query['guid'] = $val['guid']; $user_query['phone'] = $val['phone']; $user_query['name'] = $val['name']; $user_query['department_id']= $department_res; $user_query['create_time'] = time(); $user_query['update_time'] = time(); $user_res = Users::insert($user_query); if(!$user_res){ return false; } } } //无部门的也需要复制 $user_not_department = Users::where('activity_id', $old_act_id)->where('department_id', 0)->get()->toArray(); if(count($user_not_department)>0){ foreach($user_not_department as $key=>$val){ $user_query = []; unset($val['user_id']); $user_query['activity_id'] = $activity_id; $user_query['corp_id'] = $corp_id; $user_query['guid'] = $val['guid']; $user_query['phone'] = $val['phone']; $user_query['name'] = $val['name']; $user_query['department_id']= 0; $user_query['create_time'] = time(); $user_query['update_time'] = time(); $user_res = Users::insert($user_query); if(!$user_res){ return false; } } } return true; } /** * 复制题目和题库 * @param $old_act_id * @param $activity_id * @param $corp_id * @return bool */ public static function copyQuestion($old_act_id, $activity_id, $corp_id){ //题库信息 $category_info = Categorys::where('activity_id', $old_act_id)->get()->toArray(); //配置项中的题库信息匹配对应 $extend_configs = Configs::where('activity_id', $old_act_id)->value('extend_configs'); $extend_configs = json_decode($extend_configs, true); $blockade_category_ids = explode(',', $extend_configs['blockade_category_ids']); $must_category_ids = explode(',', $extend_configs['must_category_ids']); $pk_category_ids = explode(',', $extend_configs['pk_category_ids']); $must_category_ids_new = []; $blockade_category_ids_new = []; $pk_category_ids_new = []; foreach ($category_info as $k => $v) { //该题库的题目信息 $question_info = Questions::where('activity_id', $old_act_id)->where('category_id', $v['category_id'])->get()->toArray(); //题库复制 $category_query = []; $old_category_id = $v['category_id']; unset($v['category_id']); $category_query = $v; $category_query['activity_id'] = $activity_id; $category_query['corp_id'] = $corp_id; $category_query['create_time'] = time(); $category_query['update_time'] = time(); $category_res = Categorys::insertGetId($category_query); if(!$category_res){ return false; } //题目复制 foreach($question_info as $key=>$val){ $question_query = []; unset($val['question_id']); $question_query = $val; $question_query['activity_id'] = $activity_id; $question_query['corp_id'] = $corp_id; $question_query['category_id'] = $category_res; $question_query['create_time'] = time(); $question_query['update_time'] = time(); $question_res = Questions::insert($question_query); if(!$question_res){ return false; } } //对应匹配项入库 if(in_array($old_category_id, $must_category_ids)) $must_category_ids_new[] = $category_res; if(in_array($old_category_id, $blockade_category_ids)) $blockade_category_ids_new[] = $category_res; if(in_array($old_category_id, $pk_category_ids)) $pk_category_ids_new[] = $category_res; } //更新配置表的题库选择配置 $config = ActivityService::getActivityConfig($activity_id); $extend_configs = json_decode($config['extend_configs'], true); $extend_configs['must_category_ids'] = implode(',', $must_category_ids_new); $extend_configs['blockade_category_ids'] = implode(',', $blockade_category_ids_new); $extend_configs['pk_category_ids'] = implode(',', $pk_category_ids_new); $extend_configs = json_encode($extend_configs); $res = Configs::where('activity_id', $activity_id)->where('corp_id', $corp_id)->update(['extend_configs'=>$extend_configs]); if(!$res){ return false; } return true; } /** * 获取活动详情 * @param $activity_id * @return mixed */ public static function getActivityInfo($activity_id, $corp_id=''){ $res = Activitys::where('activity_id', $activity_id) // ->where('corp_id', $corp_id) ->where('status', 1) //活动状态 0 无效 1 有效 ->first(); if($res) { //时间格式化 $res = collect($res)->toArray(); $res = Api::dateFormat($res); } return $res; } /** * 获取活动配置 * @param $activity_id * @return array */ public static function getActivityConfig($activity_id){ $res = Configs::where('activity_id', $activity_id)->first(); if(!empty($res)) { $res = collect($res)->toArray(); $res = Api::dateFormat($res); } return $res; } /** * 获取活动参与人员的情况 * @param $corp_id * @param $activity_id * @return mixed */ public static function getUsersCount($corp_id, $activity_id){ $user_count = Users::where('activity_id', $activity_id)->where('corp_id', $corp_id)->where('status', 1)->count(); $data['total'] = $user_count; $departments = Departments::where('activity_id', $activity_id)->where('corp_id', $corp_id)->where('status', 1)->get()->toArray(); $has_department_num = 0; if($departments) { foreach ($departments as $department) { $data[$department['department_name']] = Users::where('activity_id', $activity_id)->where('corp_id', $corp_id)->where('status', 1)->where('department_id', $department['department_id'])->count(); $has_department_num += $data[$department['department_name']]; } } $data['其他'] = $data['total'] - $has_department_num; return $data; } /** * 获取应用信息 * @param string $app_id * @return mixed */ public static function getAppInfo($app_id=''){ if(empty($app_id)){ $data = Apps::get()->toArray(); } else { $data = Apps::where('app_id', $app_id)->first()->toArray(); } return $data; } /** * 删除(下架)活动 或 恢复 * @param $params * @return array */ public static function changeActStatus($params){ if(isset($params['status'])){ $query['status'] = $params['status']; } else { $query['status'] = self::ACT_STATUS_RELEASE; } $res = Activitys::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->update($query); if($res){ return Api::arr(config('code.success'), trans('msg.success')); } else { return Api::arr(config('code.sql_error'), trans('msg.sql_error')); } } /** * 暂停、开启活动 * @param $params * @return array */ public static function stopActivity($params){ $act_info = ActivityService::getActivityInfo($params['activity_id'], $params['corp_id']); if(!$act_info){ return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist')); } $is_temp_stop = empty($params['is_temp_stop'])?0:$params['is_temp_stop']; $res = Activitys::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->update(['is_temp_stop'=>$is_temp_stop]); if($res){ return Api::arr(config('code.success'), trans('msg.success')); } else { return Api::arr(config('code.sql_error'), trans('msg.sql_error')); } } /** * 新增活动(生成默认活动,返回默认数据) * 2018-06-19 17:00:00 * @param $corp_id * @return array */ public static function addAct($corp_id, $app_id){ //校验VIP权限,不符合权限则不允许创建活动 $user_info = Session::get('userInfo')->toArray(); $corpVip = CorpVip::where('corp_id', $user_info['corp_id'])->first(); if($corpVip){ if($corpVip['vip_end_time'] > time()){ $activity_num = Activitys::where('status', 1)->where('creator', $user_info['id'])->where('end_time', '>', time())->where('is_complete_set', 1)->count(); $online_act_num = VipConfig::where('vip_level', $corpVip['vip_level'])->value('online_act_num'); if($activity_num >= $online_act_num){ return Api::arr(config('code.fail'), trans('msg.vip_act_full')); } } else { return Api::arr(config('code.fail'), trans('msg.vip_time_out')); } } else { return Api::arr(config('code.fail'), trans('msg.admin_not_vip')); } //获取应用默认数据 $app_info = Apps::where('app_id', $app_id)->value('default_data'); $default_data = json_decode($app_info, true); //activitys表参数拼接 $query = []; $query['corp_id'] = $corp_id; $query['app_id'] = $app_id; $query['create_time'] = time(); $query['update_time'] = time(); $corpShortName = Corps::where('corp_id', $corp_id)->value('corp_short_name'); $query['title'] = $corpShortName.env('APP_NAME'); $query['logo'] = $default_data['logo']; $query['regulation'] = $default_data['regulation']; $query['is_jobtime_limit'] = $default_data['is_jobtime_limit']; $query['jobtimes'] = $default_data['jobtimes']; $query['start_time'] = strtotime(date('Y-m-d')); $query['end_time'] = strtotime(date('Y-m-d'))+7*3600*24-1; $query['auth_type'] = $default_data['auth_type']; $query['client_type'] = $default_data['client_type']; $query['activity_type'] = $corpVip['vip_level'] == 1 ? 1 : 2; //根据企业会员状态赋予活动收费状态 $query['creator'] = session()->get('userInfo')->id; //管理员VIP信息 // $vip_level = AdminVip::where('admin_id', session()->get('userInfo')->id)->value('vip_level'); $query['vip_level'] = $corpVip['vip_level']; //活动vip等级,用于手机端权限控制 $activity_res = Activitys::insertGetId($query); //configs表参数拼接 $configQuery = []; $configQuery['activity_id'] = $activity_res; $configQuery['corp_id'] = $corp_id; $configQuery['top_banner'] = $default_data['top_banner']; $configQuery['home_background'] = $default_data['home_background']; $configQuery['mark_background'] = $default_data['mark_background']; $configQuery['count_down_background'] = $default_data['count_down_background']; $configQuery['sponsor_corp'] = $default_data['sponsor_corp']; $configQuery['prize_banner'] = $default_data['prize_banner']; $configQuery['copper_box_km'] = $default_data['copper_box_km']; $configQuery['silver_box_km'] = $default_data['silver_box_km']; $configQuery['gold_box_km'] = $default_data['gold_box_km']; $configQuery['is_open_prize'] = $default_data['is_open_prize']; //默认不开启抽奖 $configQuery['open_department_top'] = $default_data['open_department_top']; //默认不开启单位排行 $configQuery['extend_configs'] = json_encode($default_data['extend_configs']); $configQuery['level_star_config'] = json_encode($default_data['level_star_config']); //段位的默认配置 $configQuery['update_time'] = time(); $configQuery['create_time'] = time(); $config_res = Configs::insert($configQuery); if($activity_res && $config_res){ $res = ActivityService::getConfigs($activity_res, $corp_id); QuestionService::addDefault($activity_res); //添加默认题库 return Api::arr(config('code.success'), trans('msg.success'), $res['data']); } else { return Api::arr(config('code.sql_error'), trans('msg.sql_error')); } } /** * 获取活动配置信息 * 2018-06-19 17:00:00 * @param $activity_id * @param $corp_id * @return array */ public static function getConfigs($activity_id, $corp_id){ $activity_info = self::getActivityInfo($activity_id, $corp_id); if (!$activity_info || empty($activity_info)) { return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist')); } $config_info = self::getActivityConfig($activity_id); //获取基础配置 $data['baseInfo'] = array_merge($activity_info, $config_info); $data['baseInfo']['extend_configs'] = json_decode($data['baseInfo']['extend_configs'], true); $corpInfo = Corps::where('corp_id', $corp_id)->value('has_wesuit'); $data['baseInfo']['has_wesuit'] = $corpInfo; //是否开通了企业微信 0 未开通 1 已开通 //返回对应VIP的活动时间配置范围 // $admin_vip_info = AdminVip::where('admin_id', session()->get('userInfo')->id)->where('app_id', $activity_info['app_id'])->first(); $admin_vip_info = CorpVip::where('corp_id', $corp_id)->first(); $data['baseInfo']['time_range']['vip_start_time'] = date('Y-m-d H:i:s', $admin_vip_info['vip_start_time']); $data['baseInfo']['time_range']['vip_end_time'] = date('Y-m-d H:i:s', $admin_vip_info['vip_end_time']); //人员信息 // $data['userInfo'] = self::getUsersCount($corp_id, $activity_id); //题库信息 $data['questionInfo'] = QuestionService::getCategoryCount($corp_id, $activity_id); //宝箱奖品信息 $lotteryLevels = LotteryBoxs::where('activity_id', $activity_id)->where('corp_id', $corp_id)->pluck('lottery_level', 'lottery_name'); foreach($lotteryLevels as $lotteryName=>$lotteryLevel){ $prize = PrizeService::getPrizeList($corp_id, $activity_id, $lotteryLevel); $data['prizeInfo'][$lotteryName] = $prize['data']; } // foreach (self::LOTTERY_LEVEL as $box_name => $lottery_level) { // $prize = PrizeService::getPrizeList($corp_id, $activity_id, $lottery_level); // $data['prizeInfo'][$box_name] = $prize['data']; // } //活动状态 if (strtotime($activity_info['start_time']) > time()) { $data['activityStatus'] = '活动未开始'; } elseif (strtotime($activity_info['end_time']) < time()) { $data['activityStatus'] = '活动已结束'; } else { if ($activity_info['is_temp_stop'] == 1) { $data['activityStatus'] = '活动已暂停'; } else { $data['activityStatus'] = '活动进行中'; } } //活动参与url拼接(用于生成二维码) $app_info = self::getAppInfo($activity_info['app_id']); $data['QR_url'] = env('WEB_URL') . '/' . $app_info['app_code'] . '?activity_id=' . $activity_id; $data['activity_id'] = $activity_id; if($data){ return Api::arr(config('code.success'), trans('msg.success'), $data); } else { return Api::arr(config('code.sql_error'), trans('msg.sql_error')); } } /** * 保存活动配置信息 * 2018-06-20 10:39:00 * @param $params * @return array */ public static function saveActivity($params){ //活动判断 $activityInfo = self::getActivityInfo($params['activity_id'], $params['corp_id']); if (!$activityInfo || empty($activityInfo)) { return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist')); } //activitys表参数 $activityQuery = []; $activityQuery['title'] = $params['title']; $activityQuery['logo'] = empty($params['logo']) ? '' : $params['logo']; $activityQuery['key'] = empty($params['key']) ? 1 : $params['key']; //1 手机号码 2 用户ID 3 邮箱 $activityQuery['regulation'] = empty($params['regulation']) ? '' : $params['regulation']; $activityQuery['is_complete_set'] = 1; //活动配置状态 0 未完成 1 已完成 $activityQuery['status'] = 1; //活动状态 0 无效 1 有效 $activityQuery['is_jobtime_limit'] = empty($params['is_jobtime_limit']) ? 0 : $params['is_jobtime_limit']; $activityQuery['is_weekend_limit'] = empty($params['is_weekend_limit']) ? 0 : $params['is_weekend_limit']; $activityQuery['jobtimes'] = isset($params['jobtimes']) ? $params['jobtimes'] : ''; $activityQuery['start_time'] = strtotime($params['start_time']); $activityQuery['end_time'] = strtotime($params['end_time']); //活动结束时间必须小于VIP有效期 $vipInfo = CorpVip::where('corp_id', $params['corp_id'])->first(); if($vipInfo['vip_end_time'] < $activityQuery['end_time']){ return Api::arr(config('code.fail'), trans('msg.time_out_vip')); } //体验版会员以外的会员不限制人数(activity_type改为2) if($vipInfo['vip_level'] > 1 && $activityInfo['activity_type'] == 1){ $activityQuery['activity_type'] = 2; } //完成设置时,不允许修改游戏平台和校验方式 if($activityInfo['is_complete_set'] == 0) { $activityQuery['client_type'] = empty($params['client_type']) ? 'wechat' : $params['client_type']; //根据平台类型和人员参与方式,适配登录校验方式 if ($activityQuery['client_type'] == 'wesuit') { //企业微信 $corpInfo = Corps::where('corp_id', $params['corp_id'])->first(); if ($corpInfo['has_wesuit'] == 0) { return Api::arr(config('code.corp_not_wesuit'), trans('msg.corp_not_wesuit')); } $activityQuery['auth_type'] = 'agh'; //agh:参与方式 微信+企业微信(不导入人员) } else { //微信 $activityQuery['auth_type'] = empty($params['auth_type']) ? 'wechat' : $params['auth_type']; } } $activityQuery['update_time'] = time(); //configs表参数 $configQuery = []; $configQuery['top_banner'] = empty($params['top_banner']) ? '' : $params['top_banner']; $configQuery['home_background'] = empty($params['home_background']) ? '' : $params['home_background']; $configQuery['mark_background'] = empty($params['mark_background']) ? '' : $params['mark_background']; $configQuery['count_down_background'] = $params['count_down_background']; $configQuery['sponsor_corp'] = $params['sponsor_corp']; // $configQuery['share_image'] = empty($params['share_image'])?null:$params['share_image']; // $configQuery['share_home_title'] = empty($params['share_home_title'])?'':$params['share_home_title']; // $configQuery['share_home_desc'] = empty($params['share_home_desc'])?'':$params['share_home_desc']; // $configQuery['share_pk_title'] = empty($params['share_pk_title'])?'':$params['share_pk_title']; // $configQuery['share_pk_desc'] = empty($params['share_pk_desc'])?'':$params['share_pk_desc']; $configQuery['prize_banner'] = $params['prize_banner']; $configQuery['copper_box_km'] = empty($params['copper_box_km']) ? 100 :$params['copper_box_km']; $configQuery['silver_box_km'] = empty($params['silver_box_km']) ? 500 :$params['silver_box_km']; $configQuery['gold_box_km'] = empty($params['gold_box_km']) ? 1000:$params['gold_box_km']; $configQuery['is_open_prize'] = empty($params['is_open_prize']) ? 0 :$params['is_open_prize']; //默认不开启抽奖 $configQuery['open_department_top'] = empty($params['open_department_top'])?0:$params['open_department_top']; //默认不开启单位排行 $configQuery['get_question_mode'] = !isset($params['get_question_mode'])?0:$params['get_question_mode']; //出题方式 0 随机 1 优先未达 2 优先错题 $configQuery['update_time'] = time(); $configQuery['top_background_color']= empty($params['top_background_color'])?'#242f55':$params['top_background_color']; $act_res = Activitys::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->update($activityQuery); $config_res = Configs::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->update($configQuery); //答题配置及参数校验 $questionConfig = QuestionService::questionConfigDetail($params); if(!$act_res || !$config_res || $questionConfig['code'] != 0){ return Api::arr($questionConfig['code'], $questionConfig['msg']); } //宝箱配置 if($configQuery['is_open_prize'] == 1 && !empty($params['box_config'])){ PrizeService::defaultBox($params); } //返回参数组装 $data['activity_id'] = $params['activity_id']; $actInfo = Configs::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->first()->toArray(); //全员参与时,不提示人员导入情况 if($params['auth_type'] == 'wechat_auto_add'){ $data['user_msg'] = ''; } else { $data['user_msg'] = $actInfo['is_user_upload'] == 0 ? '未上传人员':''; } $data['question_msg'] = $actInfo['is_question_upload'] == 0 ? '未上传题目' : ''; $data['prize_msg'] = ''; if($actInfo['is_open_prize'] == 1){ $prizes = Prizes::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->get(); $data['prize_msg'] = count($prizes) < 1 ? '未设置奖品' : ''; } return Api::arr(config('code.success'), trans('msg.success'), $data); } /** * 修改PK段位配置 * @param $params * @return array */ public static function modifyLevelConfig($params){ $activityInfo = self::getActivityInfo($params['activity_id']); if(!$activityInfo){ return Api::arr(config('code.fail'), trans('msg.activity_not_exist')); } $activityConfig = self::getActivityConfig($params['activity_id']); $levelConfig = json_decode($activityConfig['level_star_config'], true); $levelConfig[1]['name'] = Api::trimAll($params['level_one']); $levelConfig[2]['name'] = Api::trimAll($params['level_two']); $levelConfig[3]['name'] = Api::trimAll($params['level_three']); $levelConfig[4]['name'] = Api::trimAll($params['level_four']); $levelConfig[5]['name'] = Api::trimAll($params['level_five']); $levelConfig[6]['name'] = Api::trimAll($params['level_six']); $levelConfigNew = json_encode($levelConfig); $res = Configs::where('activity_id', $params['activity_id'])->update(['level_star_config'=>$levelConfigNew]); if($res){ return Api::arr(config('code.success'), trans('msg.success')); } else { return Api::arr(config('code.fail'), trans('msg.sql_error')); } } } ?>