ActivityService.php 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hanl
  5. * Date: 2018/6/5
  6. * Time: 17:13
  7. */
  8. namespace App\Services;
  9. use App\Common\Api;
  10. use App\Models\Activitys;
  11. use App\Models\AdminVip;
  12. use App\Models\Apps;
  13. use App\Models\Configs;
  14. use App\Models\Corps;
  15. use App\Models\Prizes;
  16. use App\Models\Departments;
  17. use App\Models\Users;
  18. use App\Models\Questions;
  19. use App\Models\Categorys;
  20. use App\Models\LotteryBoxs;
  21. use App\Models\CorpVip;
  22. use App\Models\VipConfig;
  23. use DB;
  24. use Excel;
  25. use Illuminate\Support\Facades\Session;
  26. class ActivityService
  27. {
  28. public function __construct($configs=[])
  29. {
  30. }
  31. const ACT_STATUS_RELEASE = 1; //活动已发布
  32. const ACT_STATUS_NOT_RELEASE = 1; //活动未发布
  33. const ACT_NOT_COMPLETE = 0; //活动设置未完成
  34. const ACT_COMPLETE = 1; //活动设置完成
  35. //宝箱设置
  36. const LOTTERY_LEVEL = array(
  37. 'copperPrize'=>1,
  38. 'sliverPrize'=>2,
  39. 'goldPrize' =>3,
  40. );
  41. /**
  42. * 活动复制
  43. * @param $params
  44. * @return bool
  45. */
  46. public static function copyActivity($params){
  47. //活动实例信息
  48. $act_info = Activitys::where('activity_id', $params['activity_id'])->first()->toArray();
  49. if(!$act_info){
  50. return Api::json(config('code.activity_not_exist'), trans('msg.activity_not_exist'));
  51. }
  52. $corp_id = empty($params['corp_id'])?$act_info['corp_id']:$params['corp_id'];
  53. unset($act_info['activity_id']);
  54. $act_query = $act_info;
  55. $act_query['corp_id'] = $corp_id;
  56. //活动参与验证方式可控
  57. $act_query['auth_type'] = empty($params['auth_type'])?$act_info['auth_type']:$params['auth_type'];
  58. $act_query['status'] = 1;
  59. $act_query['is_temp_stop']= 0; //活动未暂停
  60. $act_query['is_complete_set']= 0;
  61. $act_query['update_time'] = time();
  62. $act_query['create_time'] = time();
  63. //插入实例
  64. $act_res = Activitys::insertGetId($act_query);
  65. //活动配置信息
  66. $config_info = Configs::where('activity_id', $params['activity_id'])->first()->toArray();
  67. $config_query = $config_info;
  68. $config_query['activity_id'] = $act_res;
  69. $config_query['corp_id'] = $corp_id;
  70. $config_query['update_time'] = time();
  71. $config_query['create_time'] = time();
  72. //插入配置
  73. $config_res = Configs::insert($config_query);
  74. if(!$act_res || !$config_res){
  75. return false;
  76. }
  77. //复制奖品
  78. if(empty($params['prize'])){
  79. $prize_res = self::copyPrize($params['activity_id'], $act_res, $corp_id);
  80. if(!$prize_res){
  81. return false;
  82. }
  83. }
  84. //复制题库
  85. if(empty($params['question'])){
  86. $question_res = self::copyQuestion($params['activity_id'], $act_res, $corp_id);
  87. if(!$question_res){
  88. return false;
  89. }
  90. }
  91. //复制用户(2018-08-08 修改为默认不复制人员)
  92. // if(!empty($params['user'])){
  93. // $user_res = self::copyUser($params['activity_id'], $act_res, $corp_id);
  94. // if(!$user_res){
  95. // return false;
  96. // }
  97. // }
  98. return $act_res;
  99. }
  100. /**
  101. * 复制奖品信息
  102. * @param $old_act_id
  103. * @param $activity_id
  104. * @param $corp_id
  105. * @return bool
  106. */
  107. public static function copyPrize($old_act_id, $activity_id, $corp_id){
  108. //奖品信息
  109. $prize_info = Prizes::where('activity_id', $old_act_id)->get()->toArray();
  110. foreach ($prize_info as $k => $v) {
  111. $prize_query = [];
  112. unset($v['prize_id']);
  113. $prize_query = $v;
  114. $prize_query['activity_id'] = $activity_id;
  115. $prize_query['corp_id'] = $corp_id;
  116. $prize_query['prize_grant_count'] = 0;
  117. $prize_query['create_time'] = time();
  118. $prize_query['update_time'] = time();
  119. $prize_res = Prizes::insert($prize_query);
  120. if(!$prize_res){
  121. return false;
  122. }
  123. }
  124. return true;
  125. }
  126. /**
  127. * 复制活动参与的人员信息和部门信息
  128. * @param $old_act_id
  129. * @param $activity_id
  130. * @param $corp_id
  131. * @return bool
  132. */
  133. public static function copyUser($old_act_id, $activity_id, $corp_id){
  134. //部门信息
  135. $department_info = Departments::where('activity_id', $old_act_id)->get()->toArray();
  136. foreach ($department_info as $k => $v) {
  137. //该部门的用户信息
  138. $user_info = Users::where('activity_id', $old_act_id)->where('department_id', $v['department_id'])->get()->toArray();
  139. $department_query = [];
  140. unset($v['department_id']);
  141. $department_query = $v;
  142. $department_query['activity_id'] = $activity_id;
  143. $department_query['corp_id'] = $corp_id;
  144. $department_query['create_time'] = time();
  145. $department_query['update_time'] = time();
  146. $department_res = Departments::insertGetId($department_query);
  147. if(!$department_res){
  148. return false;
  149. }
  150. //部门用户复制
  151. foreach($user_info as $key=>$val){
  152. $user_query = [];
  153. unset($val['user_id']);
  154. $user_query['activity_id'] = $activity_id;
  155. $user_query['corp_id'] = $corp_id;
  156. $user_query['guid'] = $val['guid'];
  157. $user_query['phone'] = $val['phone'];
  158. $user_query['name'] = $val['name'];
  159. $user_query['department_id']= $department_res;
  160. $user_query['create_time'] = time();
  161. $user_query['update_time'] = time();
  162. $user_res = Users::insert($user_query);
  163. if(!$user_res){
  164. return false;
  165. }
  166. }
  167. }
  168. //无部门的也需要复制
  169. $user_not_department = Users::where('activity_id', $old_act_id)->where('department_id', 0)->get()->toArray();
  170. if(count($user_not_department)>0){
  171. foreach($user_not_department as $key=>$val){
  172. $user_query = [];
  173. unset($val['user_id']);
  174. $user_query['activity_id'] = $activity_id;
  175. $user_query['corp_id'] = $corp_id;
  176. $user_query['guid'] = $val['guid'];
  177. $user_query['phone'] = $val['phone'];
  178. $user_query['name'] = $val['name'];
  179. $user_query['department_id']= 0;
  180. $user_query['create_time'] = time();
  181. $user_query['update_time'] = time();
  182. $user_res = Users::insert($user_query);
  183. if(!$user_res){
  184. return false;
  185. }
  186. }
  187. }
  188. return true;
  189. }
  190. /**
  191. * 复制题目和题库
  192. * @param $old_act_id
  193. * @param $activity_id
  194. * @param $corp_id
  195. * @return bool
  196. */
  197. public static function copyQuestion($old_act_id, $activity_id, $corp_id){
  198. //题库信息
  199. $category_info = Categorys::where('activity_id', $old_act_id)->get()->toArray();
  200. //配置项中的题库信息匹配对应
  201. $extend_configs = Configs::where('activity_id', $old_act_id)->value('extend_configs');
  202. $extend_configs = json_decode($extend_configs, true);
  203. $blockade_category_ids = explode(',', $extend_configs['blockade_category_ids']);
  204. $must_category_ids = explode(',', $extend_configs['must_category_ids']);
  205. $pk_category_ids = explode(',', $extend_configs['pk_category_ids']);
  206. $must_category_ids_new = [];
  207. $blockade_category_ids_new = [];
  208. $pk_category_ids_new = [];
  209. foreach ($category_info as $k => $v) {
  210. //该题库的题目信息
  211. $question_info = Questions::where('activity_id', $old_act_id)->where('category_id', $v['category_id'])->get()->toArray();
  212. //题库复制
  213. $category_query = [];
  214. $old_category_id = $v['category_id'];
  215. unset($v['category_id']);
  216. $category_query = $v;
  217. $category_query['activity_id'] = $activity_id;
  218. $category_query['corp_id'] = $corp_id;
  219. $category_query['create_time'] = time();
  220. $category_query['update_time'] = time();
  221. $category_res = Categorys::insertGetId($category_query);
  222. if(!$category_res){
  223. return false;
  224. }
  225. //题目复制
  226. foreach($question_info as $key=>$val){
  227. $question_query = [];
  228. unset($val['question_id']);
  229. $question_query = $val;
  230. $question_query['activity_id'] = $activity_id;
  231. $question_query['corp_id'] = $corp_id;
  232. $question_query['category_id'] = $category_res;
  233. $question_query['create_time'] = time();
  234. $question_query['update_time'] = time();
  235. $question_res = Questions::insert($question_query);
  236. if(!$question_res){
  237. return false;
  238. }
  239. }
  240. //对应匹配项入库
  241. if(in_array($old_category_id, $must_category_ids)) $must_category_ids_new[] = $category_res;
  242. if(in_array($old_category_id, $blockade_category_ids)) $blockade_category_ids_new[] = $category_res;
  243. if(in_array($old_category_id, $pk_category_ids)) $pk_category_ids_new[] = $category_res;
  244. }
  245. //更新配置表的题库选择配置
  246. $config = ActivityService::getActivityConfig($activity_id);
  247. $extend_configs = json_decode($config['extend_configs'], true);
  248. $extend_configs['must_category_ids'] = implode(',', $must_category_ids_new);
  249. $extend_configs['blockade_category_ids'] = implode(',', $blockade_category_ids_new);
  250. $extend_configs['pk_category_ids'] = implode(',', $pk_category_ids_new);
  251. $extend_configs = json_encode($extend_configs);
  252. $res = Configs::where('activity_id', $activity_id)->where('corp_id', $corp_id)->update(['extend_configs'=>$extend_configs]);
  253. if(!$res){
  254. return false;
  255. }
  256. return true;
  257. }
  258. /**
  259. * 获取活动详情
  260. * @param $activity_id
  261. * @return mixed
  262. */
  263. public static function getActivityInfo($activity_id, $corp_id=''){
  264. $res = Activitys::where('activity_id', $activity_id)
  265. // ->where('corp_id', $corp_id)
  266. ->where('status', 1) //活动状态 0 无效 1 有效
  267. ->first();
  268. if($res) {
  269. //时间格式化
  270. $res = collect($res)->toArray();
  271. $res = Api::dateFormat($res);
  272. }
  273. return $res;
  274. }
  275. /**
  276. * 获取活动配置
  277. * @param $activity_id
  278. * @return array
  279. */
  280. public static function getActivityConfig($activity_id){
  281. $res = Configs::where('activity_id', $activity_id)->first();
  282. if(!empty($res)) {
  283. $res = collect($res)->toArray();
  284. $res = Api::dateFormat($res);
  285. }
  286. return $res;
  287. }
  288. /**
  289. * 获取活动参与人员的情况
  290. * @param $corp_id
  291. * @param $activity_id
  292. * @return mixed
  293. */
  294. public static function getUsersCount($corp_id, $activity_id){
  295. $user_count = Users::where('activity_id', $activity_id)->where('corp_id', $corp_id)->where('status', 1)->count();
  296. $data['total'] = $user_count;
  297. $departments = Departments::where('activity_id', $activity_id)->where('corp_id', $corp_id)->where('status', 1)->get()->toArray();
  298. $has_department_num = 0;
  299. if($departments) {
  300. foreach ($departments as $department) {
  301. $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();
  302. $has_department_num += $data[$department['department_name']];
  303. }
  304. }
  305. $data['其他'] = $data['total'] - $has_department_num;
  306. return $data;
  307. }
  308. /**
  309. * 获取应用信息
  310. * @param string $app_id
  311. * @return mixed
  312. */
  313. public static function getAppInfo($app_id=''){
  314. if(empty($app_id)){
  315. $data = Apps::get()->toArray();
  316. } else {
  317. $data = Apps::where('app_id', $app_id)->first()->toArray();
  318. }
  319. return $data;
  320. }
  321. /**
  322. * 删除(下架)活动 或 恢复
  323. * @param $params
  324. * @return array
  325. */
  326. public static function changeActStatus($params){
  327. if(isset($params['status'])){
  328. $query['status'] = $params['status'];
  329. } else {
  330. $query['status'] = self::ACT_STATUS_RELEASE;
  331. }
  332. $res = Activitys::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->update($query);
  333. if($res){
  334. return Api::arr(config('code.success'), trans('msg.success'));
  335. } else {
  336. return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
  337. }
  338. }
  339. /**
  340. * 暂停、开启活动
  341. * @param $params
  342. * @return array
  343. */
  344. public static function stopActivity($params){
  345. $act_info = ActivityService::getActivityInfo($params['activity_id'], $params['corp_id']);
  346. if(!$act_info){
  347. return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist'));
  348. }
  349. $is_temp_stop = empty($params['is_temp_stop'])?0:$params['is_temp_stop'];
  350. $res = Activitys::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->update(['is_temp_stop'=>$is_temp_stop]);
  351. if($res){
  352. return Api::arr(config('code.success'), trans('msg.success'));
  353. } else {
  354. return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
  355. }
  356. }
  357. /**
  358. * 新增活动(生成默认活动,返回默认数据)
  359. * 2018-06-19 17:00:00
  360. * @param $corp_id
  361. * @return array
  362. */
  363. public static function addAct($corp_id, $app_id){
  364. //校验VIP权限,不符合权限则不允许创建活动
  365. $user_info = Session::get('userInfo')->toArray();
  366. $corpVip = CorpVip::where('corp_id', $user_info['corp_id'])->first();
  367. if($corpVip){
  368. if($corpVip['vip_end_time'] > time()){
  369. $activity_num = Activitys::where('status', 1)->where('creator', $user_info['id'])->where('end_time', '>', time())->where('is_complete_set', 1)->count();
  370. $online_act_num = VipConfig::where('vip_level', $corpVip['vip_level'])->value('online_act_num');
  371. if($activity_num >= $online_act_num){
  372. return Api::arr(config('code.fail'), trans('msg.vip_act_full'));
  373. }
  374. } else {
  375. return Api::arr(config('code.fail'), trans('msg.vip_time_out'));
  376. }
  377. } else {
  378. return Api::arr(config('code.fail'), trans('msg.admin_not_vip'));
  379. }
  380. //获取应用默认数据
  381. $app_info = Apps::where('app_id', $app_id)->value('default_data');
  382. $default_data = json_decode($app_info, true);
  383. //activitys表参数拼接
  384. $query = [];
  385. $query['corp_id'] = $corp_id;
  386. $query['app_id'] = $app_id;
  387. $query['create_time'] = time();
  388. $query['update_time'] = time();
  389. $corpShortName = Corps::where('corp_id', $corp_id)->value('corp_short_name');
  390. $query['title'] = $corpShortName.env('APP_NAME');
  391. $query['logo'] = $default_data['logo'];
  392. $query['regulation'] = $default_data['regulation'];
  393. $query['is_jobtime_limit'] = $default_data['is_jobtime_limit'];
  394. $query['jobtimes'] = $default_data['jobtimes'];
  395. $query['start_time'] = strtotime(date('Y-m-d'));
  396. $query['end_time'] = strtotime(date('Y-m-d'))+7*3600*24-1;
  397. $query['auth_type'] = $default_data['auth_type'];
  398. $query['client_type'] = $default_data['client_type'];
  399. $query['activity_type'] = $corpVip['vip_level'] == 1 ? 1 : 2; //根据企业会员状态赋予活动收费状态
  400. $query['creator'] = session()->get('userInfo')->id;
  401. //管理员VIP信息
  402. // $vip_level = AdminVip::where('admin_id', session()->get('userInfo')->id)->value('vip_level');
  403. $query['vip_level'] = $corpVip['vip_level']; //活动vip等级,用于手机端权限控制
  404. $activity_res = Activitys::insertGetId($query);
  405. //configs表参数拼接
  406. $configQuery = [];
  407. $configQuery['activity_id'] = $activity_res;
  408. $configQuery['corp_id'] = $corp_id;
  409. $configQuery['top_banner'] = $default_data['top_banner'];
  410. $configQuery['home_background'] = $default_data['home_background'];
  411. $configQuery['mark_background'] = $default_data['mark_background'];
  412. $configQuery['count_down_background'] = $default_data['count_down_background'];
  413. $configQuery['sponsor_corp'] = $default_data['sponsor_corp'];
  414. $configQuery['prize_banner'] = $default_data['prize_banner'];
  415. $configQuery['copper_box_km'] = $default_data['copper_box_km'];
  416. $configQuery['silver_box_km'] = $default_data['silver_box_km'];
  417. $configQuery['gold_box_km'] = $default_data['gold_box_km'];
  418. $configQuery['is_open_prize'] = $default_data['is_open_prize']; //默认不开启抽奖
  419. $configQuery['open_department_top'] = $default_data['open_department_top']; //默认不开启单位排行
  420. $configQuery['extend_configs'] = json_encode($default_data['extend_configs']);
  421. $configQuery['level_star_config'] = json_encode($default_data['level_star_config']); //段位的默认配置
  422. $configQuery['update_time'] = time();
  423. $configQuery['create_time'] = time();
  424. $config_res = Configs::insert($configQuery);
  425. if($activity_res && $config_res){
  426. $res = ActivityService::getConfigs($activity_res, $corp_id);
  427. QuestionService::addDefault($activity_res); //添加默认题库
  428. return Api::arr(config('code.success'), trans('msg.success'), $res['data']);
  429. } else {
  430. return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
  431. }
  432. }
  433. /**
  434. * 获取活动配置信息
  435. * 2018-06-19 17:00:00
  436. * @param $activity_id
  437. * @param $corp_id
  438. * @return array
  439. */
  440. public static function getConfigs($activity_id, $corp_id){
  441. $activity_info = self::getActivityInfo($activity_id, $corp_id);
  442. if (!$activity_info || empty($activity_info)) {
  443. return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist'));
  444. }
  445. $config_info = self::getActivityConfig($activity_id);
  446. //获取基础配置
  447. $data['baseInfo'] = array_merge($activity_info, $config_info);
  448. $data['baseInfo']['extend_configs'] = json_decode($data['baseInfo']['extend_configs'], true);
  449. $corpInfo = Corps::where('corp_id', $corp_id)->value('has_wesuit');
  450. $data['baseInfo']['has_wesuit'] = $corpInfo; //是否开通了企业微信 0 未开通 1 已开通
  451. //返回对应VIP的活动时间配置范围
  452. // $admin_vip_info = AdminVip::where('admin_id', session()->get('userInfo')->id)->where('app_id', $activity_info['app_id'])->first();
  453. $admin_vip_info = CorpVip::where('corp_id', $corp_id)->first();
  454. $data['baseInfo']['time_range']['vip_start_time'] = date('Y-m-d H:i:s', $admin_vip_info['vip_start_time']);
  455. $data['baseInfo']['time_range']['vip_end_time'] = date('Y-m-d H:i:s', $admin_vip_info['vip_end_time']);
  456. //人员信息
  457. // $data['userInfo'] = self::getUsersCount($corp_id, $activity_id);
  458. //题库信息
  459. $data['questionInfo'] = QuestionService::getCategoryCount($corp_id, $activity_id);
  460. //宝箱奖品信息
  461. $lotteryLevels = LotteryBoxs::where('activity_id', $activity_id)->where('corp_id', $corp_id)->pluck('lottery_level', 'lottery_name');
  462. foreach($lotteryLevels as $lotteryName=>$lotteryLevel){
  463. $prize = PrizeService::getPrizeList($corp_id, $activity_id, $lotteryLevel);
  464. $data['prizeInfo'][$lotteryName] = $prize['data'];
  465. }
  466. // foreach (self::LOTTERY_LEVEL as $box_name => $lottery_level) {
  467. // $prize = PrizeService::getPrizeList($corp_id, $activity_id, $lottery_level);
  468. // $data['prizeInfo'][$box_name] = $prize['data'];
  469. // }
  470. //活动状态
  471. if (strtotime($activity_info['start_time']) > time()) {
  472. $data['activityStatus'] = '活动未开始';
  473. } elseif (strtotime($activity_info['end_time']) < time()) {
  474. $data['activityStatus'] = '活动已结束';
  475. } else {
  476. if ($activity_info['is_temp_stop'] == 1) {
  477. $data['activityStatus'] = '活动已暂停';
  478. } else {
  479. $data['activityStatus'] = '活动进行中';
  480. }
  481. }
  482. //活动参与url拼接(用于生成二维码)
  483. $app_info = self::getAppInfo($activity_info['app_id']);
  484. $data['QR_url'] = env('WEB_URL') . '/' . $app_info['app_code'] . '?activity_id=' . $activity_id;
  485. $data['activity_id'] = $activity_id;
  486. if($data){
  487. return Api::arr(config('code.success'), trans('msg.success'), $data);
  488. } else {
  489. return Api::arr(config('code.sql_error'), trans('msg.sql_error'));
  490. }
  491. }
  492. /**
  493. * 保存活动配置信息
  494. * 2018-06-20 10:39:00
  495. * @param $params
  496. * @return array
  497. */
  498. public static function saveActivity($params){
  499. //活动判断
  500. $activityInfo = self::getActivityInfo($params['activity_id'], $params['corp_id']);
  501. if (!$activityInfo || empty($activityInfo)) {
  502. return Api::arr(config('code.activity_not_exist'), trans('msg.activity_not_exist'));
  503. }
  504. //activitys表参数
  505. $activityQuery = [];
  506. $activityQuery['title'] = $params['title'];
  507. $activityQuery['logo'] = empty($params['logo']) ? '' : $params['logo'];
  508. $activityQuery['key'] = empty($params['key']) ? 1 : $params['key']; //1 手机号码 2 用户ID 3 邮箱
  509. $activityQuery['regulation'] = empty($params['regulation']) ? '' : $params['regulation'];
  510. $activityQuery['is_complete_set'] = 1; //活动配置状态 0 未完成 1 已完成
  511. $activityQuery['status'] = 1; //活动状态 0 无效 1 有效
  512. $activityQuery['is_jobtime_limit'] = empty($params['is_jobtime_limit']) ? 0 : $params['is_jobtime_limit'];
  513. $activityQuery['is_weekend_limit'] = empty($params['is_weekend_limit']) ? 0 : $params['is_weekend_limit'];
  514. $activityQuery['jobtimes'] = isset($params['jobtimes']) ? $params['jobtimes'] : '';
  515. $activityQuery['start_time'] = strtotime($params['start_time']);
  516. $activityQuery['end_time'] = strtotime($params['end_time']);
  517. //活动结束时间必须小于VIP有效期
  518. $vipInfo = CorpVip::where('corp_id', $params['corp_id'])->first();
  519. if($vipInfo['vip_end_time'] < $activityQuery['end_time']){
  520. return Api::arr(config('code.fail'), trans('msg.time_out_vip'));
  521. }
  522. //体验版会员以外的会员不限制人数(activity_type改为2)
  523. if($vipInfo['vip_level'] > 1 && $activityInfo['activity_type'] == 1){
  524. $activityQuery['activity_type'] = 2;
  525. }
  526. //完成设置时,不允许修改游戏平台和校验方式
  527. if($activityInfo['is_complete_set'] == 0) {
  528. $activityQuery['client_type'] = empty($params['client_type']) ? 'wechat' : $params['client_type'];
  529. //根据平台类型和人员参与方式,适配登录校验方式
  530. if ($activityQuery['client_type'] == 'wesuit') { //企业微信
  531. $corpInfo = Corps::where('corp_id', $params['corp_id'])->first();
  532. if ($corpInfo['has_wesuit'] == 0) {
  533. return Api::arr(config('code.corp_not_wesuit'), trans('msg.corp_not_wesuit'));
  534. }
  535. $activityQuery['auth_type'] = 'agh'; //agh:参与方式 微信+企业微信(不导入人员)
  536. } else { //微信
  537. $activityQuery['auth_type'] = empty($params['auth_type']) ? 'wechat' : $params['auth_type'];
  538. }
  539. }
  540. $activityQuery['update_time'] = time();
  541. //configs表参数
  542. $configQuery = [];
  543. $configQuery['top_banner'] = empty($params['top_banner']) ? '' : $params['top_banner'];
  544. $configQuery['home_background'] = empty($params['home_background']) ? '' : $params['home_background'];
  545. $configQuery['mark_background'] = empty($params['mark_background']) ? '' : $params['mark_background'];
  546. $configQuery['count_down_background'] = $params['count_down_background'];
  547. $configQuery['sponsor_corp'] = $params['sponsor_corp'];
  548. // $configQuery['share_image'] = empty($params['share_image'])?null:$params['share_image'];
  549. // $configQuery['share_home_title'] = empty($params['share_home_title'])?'':$params['share_home_title'];
  550. // $configQuery['share_home_desc'] = empty($params['share_home_desc'])?'':$params['share_home_desc'];
  551. // $configQuery['share_pk_title'] = empty($params['share_pk_title'])?'':$params['share_pk_title'];
  552. // $configQuery['share_pk_desc'] = empty($params['share_pk_desc'])?'':$params['share_pk_desc'];
  553. $configQuery['prize_banner'] = $params['prize_banner'];
  554. $configQuery['copper_box_km'] = empty($params['copper_box_km']) ? 100 :$params['copper_box_km'];
  555. $configQuery['silver_box_km'] = empty($params['silver_box_km']) ? 500 :$params['silver_box_km'];
  556. $configQuery['gold_box_km'] = empty($params['gold_box_km']) ? 1000:$params['gold_box_km'];
  557. $configQuery['is_open_prize'] = empty($params['is_open_prize']) ? 0 :$params['is_open_prize']; //默认不开启抽奖
  558. $configQuery['open_department_top'] = empty($params['open_department_top'])?0:$params['open_department_top']; //默认不开启单位排行
  559. $configQuery['get_question_mode'] = !isset($params['get_question_mode'])?0:$params['get_question_mode']; //出题方式 0 随机 1 优先未达 2 优先错题
  560. $configQuery['update_time'] = time();
  561. $configQuery['top_background_color']= empty($params['top_background_color'])?'#242f55':$params['top_background_color'];
  562. $act_res = Activitys::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->update($activityQuery);
  563. $config_res = Configs::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->update($configQuery);
  564. //答题配置及参数校验
  565. $questionConfig = QuestionService::questionConfigDetail($params);
  566. if(!$act_res || !$config_res || $questionConfig['code'] != 0){
  567. return Api::arr($questionConfig['code'], $questionConfig['msg']);
  568. }
  569. //宝箱配置
  570. if($configQuery['is_open_prize'] == 1 && !empty($params['box_config'])){
  571. PrizeService::defaultBox($params);
  572. }
  573. //返回参数组装
  574. $data['activity_id'] = $params['activity_id'];
  575. $actInfo = Configs::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->first()->toArray();
  576. //全员参与时,不提示人员导入情况
  577. if($params['auth_type'] == 'wechat_auto_add'){
  578. $data['user_msg'] = '';
  579. } else {
  580. $data['user_msg'] = $actInfo['is_user_upload'] == 0 ? '未上传人员':'';
  581. }
  582. $data['question_msg'] = $actInfo['is_question_upload'] == 0 ? '未上传题目' : '';
  583. $data['prize_msg'] = '';
  584. if($actInfo['is_open_prize'] == 1){
  585. $prizes = Prizes::where('activity_id', $params['activity_id'])->where('corp_id', $params['corp_id'])->get();
  586. $data['prize_msg'] = count($prizes) < 1 ? '未设置奖品' : '';
  587. }
  588. return Api::arr(config('code.success'), trans('msg.success'), $data);
  589. }
  590. /**
  591. * 修改PK段位配置
  592. * @param $params
  593. * @return array
  594. */
  595. public static function modifyLevelConfig($params){
  596. $activityInfo = self::getActivityInfo($params['activity_id']);
  597. if(!$activityInfo){
  598. return Api::arr(config('code.fail'), trans('msg.activity_not_exist'));
  599. }
  600. $activityConfig = self::getActivityConfig($params['activity_id']);
  601. $levelConfig = json_decode($activityConfig['level_star_config'], true);
  602. $levelConfig[1]['name'] = Api::trimAll($params['level_one']);
  603. $levelConfig[2]['name'] = Api::trimAll($params['level_two']);
  604. $levelConfig[3]['name'] = Api::trimAll($params['level_three']);
  605. $levelConfig[4]['name'] = Api::trimAll($params['level_four']);
  606. $levelConfig[5]['name'] = Api::trimAll($params['level_five']);
  607. $levelConfig[6]['name'] = Api::trimAll($params['level_six']);
  608. $levelConfigNew = json_encode($levelConfig);
  609. $res = Configs::where('activity_id', $params['activity_id'])->update(['level_star_config'=>$levelConfigNew]);
  610. if($res){
  611. return Api::arr(config('code.success'), trans('msg.success'));
  612. } else {
  613. return Api::arr(config('code.fail'), trans('msg.sql_error'));
  614. }
  615. }
  616. }
  617. ?>