package com.ym.mec.web.controller.education;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.MusicGroupPerformance;
import com.ym.mec.biz.dal.page.MusicGroupPerformanceQueryInfo;
import com.ym.mec.biz.service.MusicGroupPerformanceService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("eduMusicGroupPerformance")
@Api(tags = "乐团展演信息")
@RestController
public class EduMusicGroupPerformanceController extends BaseController {

	@Autowired
	private MusicGroupPerformanceService musicGroupPerformanceService;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation("新增")
	@PostMapping(value = "add")
	public Object add(@RequestBody MusicGroupPerformance musicGroupPerformance) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if(sysUser == null){
			throw new BizException("请登录");
		}
		musicGroupPerformance.setOperator(sysUser.getId());
		List<String> musicScoreNames = musicGroupPerformance.getMusicScoreNames();
		if(musicScoreNames != null && musicScoreNames.size() > 0){
			for (String musicScoreName : musicScoreNames) {
				if(musicScoreName.contains(",")){
					throw new BizException("请勿录入特殊字符");
				}
			}
			musicGroupPerformance.setMusicScoreName(StringUtils.join(musicScoreNames,","));
		}
		return succeed(musicGroupPerformanceService.insert(musicGroupPerformance));
	}

	@ApiOperation("修改")
	@PostMapping(value = "update")
	public Object update(@RequestBody MusicGroupPerformance musicGroupPerformance) {
		List<String> musicScoreNames = musicGroupPerformance.getMusicScoreNames();
		if(musicScoreNames != null && musicScoreNames.size() > 0){
			for (String musicScoreName : musicScoreNames) {
				if(musicScoreName.contains(",")){
					throw new BizException("请勿录入特殊字符");
				}
			}
			musicGroupPerformance.setMusicScoreName(StringUtils.join(musicScoreNames,","));
		}
		return succeed(musicGroupPerformanceService.update(musicGroupPerformance));
	}

	@ApiOperation("删除")
	@PostMapping(value = "del")
	public Object del(Integer id) {
		return succeed(musicGroupPerformanceService.delete(id));
	}

	@ApiOperation("分页查询")
	@PostMapping(value = "queryPage")
	public Object queryPage(MusicGroupPerformanceQueryInfo queryInfo) {
		return succeed(musicGroupPerformanceService.queryPage(queryInfo));
	}

	@ApiOperation("分页查询首页乐团展演列表")
	@PostMapping(value = "queryNoStartPage")
	public Object queryNoStartPage(MusicGroupPerformanceQueryInfo queryInfo) {
		return succeed(musicGroupPerformanceService.queryNoStartPage(queryInfo));
	}
}
