package com.platform.modules.app.controller;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.platform.common.utils.PageUtils;
import com.platform.common.utils.R;
import com.platform.modules.app.annotation.Login;
import com.platform.modules.live.entity.LiveMultimediaEntity;
import com.platform.modules.live.service.LiveMultimediaService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * APP素材接口
 *
 * @author weiyan
 */

@RestController
@RequestMapping("/app/live/multimedia")
@Api(value = "APP素材接口",tags = "APP素材接口")
public class AppLiveMultimediaController extends AppAbstractController{
	
	@Autowired
	private LiveMultimediaService liveMultimediaService;
	
	/**
	 * 列表
	 */
	@Login
	@GetMapping("/list")
	@ApiOperation("素材列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "页数", defaultValue = "1", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "limit", value = "显示行数", defaultValue = "10", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "mediaType", value = "素材类型 1视频  2语音  3图片", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "title", value = "报道内容", paramType = "query", dataType = "String")})
	public R list(@ApiIgnore @RequestParam(required = false) Map<String, Object> params){
		params.put("isDelete", 0);
//		params.put("userId", getUserId());
		PageUtils page =  liveMultimediaService.queryAppPageByParams(params);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 保存
	 */
	@Login
	@PostMapping("/save")
	@ApiOperation("保存素材")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "mediaType", value = "素材类型 1视频  2语音  3图片", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "title", value = "素材标题", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "description", value = "素材描述", paramType = "query", dataType = "String")})
	public R save(String title,Integer mediaType,String description, MultipartFile mediaFile){
		if(StringUtils.isBlank(title)){
			return R.error("标题不能为空");
		}
		if(mediaType==null){
			return R.error("素材类型不能为空不能为空");
		}
		if(mediaFile==null){
			return R.error("素材文件不能为空");
		}
		LiveMultimediaEntity entity = new LiveMultimediaEntity();
		entity.setTitle(title);
		entity.setMediaType(mediaType);
		entity.setDescription(description);
		liveMultimediaService.save(entity, mediaFile, getUserId());
		return R.ok();
	}
	
	/**
	 * 信息
	 */
	@Login
	@GetMapping("/info")
	@ApiOperation("素材信息")
	public R info(@RequestParam("id") Integer id){
		return R.ok().put("info", liveMultimediaService.info(id));
	}
	
	/**
	 * 修改
	 */
	@Login
	@PostMapping("/update")
	@ApiOperation("修改素材")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "素材ID", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "title", value = "报道内容", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "description", value = "素材描述", paramType = "query", dataType = "String")})
	public R update(String title,Integer id,String description){
		if(StringUtils.isBlank(title)){
			return R.error("标题不能为空");
		}
		if(id==null){
			return R.error("ID不能为空");
		}
		LiveMultimediaEntity entity = new LiveMultimediaEntity();
		entity.setTitle(title);
		entity.setDescription(description);
		entity.setId(id);
		liveMultimediaService.update(entity,  getUserId());
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@Login
	@PostMapping("/delete")
	@ApiOperation("删除素材")
	public R delete(Integer[] ids){
		if(ids==null || ids.length==0){
			return R.error("ID不能为空");
		}
		liveMultimediaService.deleteBatch(ids,getUserId());
		return R.ok();
	}

}
