/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.guosen.activity.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.guosen.activity.dto.ActivityDTO;
import org.springblade.modules.guosen.activity.entity.Activity;
import org.springblade.modules.guosen.activity.service.IActivityService;
import org.springblade.modules.guosen.activity.vo.ActivityVO;
import org.springblade.modules.guosen.activity.wrapper.ActivityWrapper;
import org.springblade.modules.guosen.activityPresent.service.IActivityPresentService;
import org.springblade.modules.guosen.activityTag.service.IActivityTagService;
import org.springblade.modules.guosen.ads.entity.Ads;
import org.springblade.modules.guosen.ads.wrapper.AdsWrapper;
import org.springblade.modules.guosen.helpRecord.service.IHelpRecordService;
import org.springblade.modules.guosen.production.entity.Production;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-06-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("cyzh-activity/activity")
@Api(value = "", tags = "活动接口")
public class ActivityController extends BladeController {

	private final IActivityService activityService;

	@Resource
	private IActivityTagService activityTagService;
	@Resource
	private IActivityPresentService activityPresentService;
	@Resource
	private IHelpRecordService helpRecordService;


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	public R<ActivityVO> detail(Activity activity) {
		Activity detail = activityService.getOne(Condition.getQueryWrapper(activity));
		return R.data(ActivityWrapper.build(activityTagService,activityPresentService).entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	public R<IPage<ActivityVO>> list(@RequestParam Map<String,Object> activity, Query query) {
		IPage<Activity> pages = activityService.page(Condition.getPage(query), Condition.getQueryWrapper(activity,Activity.class));
		return R.data(ActivityWrapper.build(activityTagService,activityPresentService,helpRecordService).pageVO(pages));
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	public R<IPage<ActivityVO>> page(ActivityVO activity, Query query) {
		IPage<ActivityVO> pages = activityService.selectActivityPage(Condition.getPage(query), activity);
		return R.data(pages);
	}


	/**
	 * 新增
	 */
	@PostMapping("/save")
	public R save(@Valid @RequestBody Activity activity) {
		return R.status(activityService.save(activity));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	public R update(@Valid @RequestBody Activity activity) {
		return R.status(activityService.updateById(activity));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	public R submit(@Valid @RequestBody Activity activity) {
		return R.status(activityService.saveOrUpdate(activity));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(activityService.removeActivity(ids));
	}

	/**
	 * 创建或修改活动
	 * @param activityDTO
	 * @return
	 */
	@PostMapping("/createOrUpdate")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "后台-创建或编辑活动", notes = "传入activity")
	public R create(@Valid @RequestBody ActivityDTO activityDTO) {
		return R.status(activityService.createOrUpdate(activityDTO));
	}
	/**
	 * 分页查询字符串标签
	 */
	@GetMapping("/selectPageWithTag")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "小程序-活动列表", notes = "传入activity")
	public R<IPage<ActivityVO>> selectPageWithTag(ActivityVO activityVO, Query query) {
		IPage<ActivityVO> pages = activityService.selectPageWithTag(Condition.getPage(query), activityVO);
		return R.data(pages);
	}
	/**
	 * 歌手打榜详情
	 */
	@GetMapping("/getHitPersonDetail")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "歌手打榜详情", notes = "传入activityId")
	public R getHitPersonDetail(Long activityId, Long senderId, Long receiverId) {
		Assert.notNull(activityId, "activityId不能为空");
		Assert.notNull(receiverId, "receiverId不能为空");
		return R.data(activityService.getHitPersonDetail(activityId, senderId, receiverId));
	}

	@GetMapping("/visit")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "浏览作品", notes = "传入作活动id")
	public R visit(Long id,Long userId) {
		Assert.notNull(id, "活动id不能为空");
		activityService.visit(id, userId);
		return R.status(true);
	}

}
