package com.ym.mec.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
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.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.ApprovalStatus;
import com.ym.mec.biz.dal.entity.MusicGroup;
import com.ym.mec.biz.dal.entity.MusicGroupQuit;
import com.ym.mec.biz.service.MusicGroupQuitService;
import com.ym.mec.biz.service.MusicGroupService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.QueryInfo;

@RequestMapping("musicGroupQuit")
@Api(tags = "退团服务")
@RestController
public class MusicGroupQuitController extends BaseController {

	@Autowired
	private MusicGroupService musicGroupService;

	@Autowired
	private MusicGroupQuitService musicGroupQuitService;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation(value = "分页查询")
	@GetMapping("/queryPage")
	@PreAuthorize("@pcs.hasPermissions('musicGroupQuit/queryPage')")
	public HttpResponseResult queryPage(QueryInfo queryInfo) throws Exception {
		return succeed(musicGroupQuitService.queryPage(queryInfo));
	}

	@ApiOperation(value = "单查询")
	@GetMapping("/query")
	@PreAuthorize("@pcs.hasPermissions('musicGroupQuit/query')")
	public HttpResponseResult query(Long id) throws Exception {
		MusicGroupQuit musicGroupQuit = musicGroupQuitService.get(id);
		if (musicGroupQuit == null) {
			return failed("参数不正确");
		}

		SysUser user = sysUserFeignService.queryUserById(musicGroupQuit.getUserId());
		musicGroupQuit.getUser().setUsername(user.getUsername());

		MusicGroup musicGroup = musicGroupService.get(musicGroupQuit.getMusicGroupId());
		musicGroupQuit.setMusicGroup(musicGroup);

		return succeed(musicGroupQuit);
	}

	@ApiOperation(value = "退团")
	@PostMapping("/quitMusicGroup")
	@PreAuthorize("@pcs.hasPermissions('musicGroupQuit/quitMusicGroup')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "退团申请id", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "status", value = "审批状态（APPROVED, DENIED, PROCESSING）", required = true, dataType = "String"),
			@ApiImplicitParam(name = "reason", value = "原因", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isRefundCourseFee", value = "是否退还课程费用", required = true, dataType = "Boolean"),
			@ApiImplicitParam(name = "isRefundInstrumentFee", value = "是否退还乐器费用（租金）", required = true, dataType = "Boolean"),
			@ApiImplicitParam(name = "isRefundTeachingAssistantsFee", value = "是否退还教辅费用", required = true, dataType = "Boolean") })
	public HttpResponseResult quitMusicGroup(Long id, ApprovalStatus status, String reason, boolean isRefundCourseFee, boolean isRefundInstrumentFee,
			boolean isRefundTeachingAssistantsFee) throws Exception {
		return succeed(musicGroupService.approveQuitMusicGroup(id, status, reason, isRefundCourseFee, isRefundInstrumentFee, isRefundTeachingAssistantsFee));
	}

	@ApiOperation(value = "一键退团")
	@PostMapping("/directQuitMusicGroup")
	@PreAuthorize("@pcs.hasPermissions('musicGroupQuit/directQuitMusicGroup')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "userId", value = "用户编号", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "reason", value = "原因", required = true, dataType = "String"),
			@ApiImplicitParam(name = "isRefundCourseFee", value = "是否退还课程费用", required = true, dataType = "Boolean"),
			@ApiImplicitParam(name = "isRefundInstrumentFee", value = "是否退还乐器费用（租金）", required = true, dataType = "Boolean"),
			@ApiImplicitParam(name = "isRefundTeachingAssistantsFee", value = "是否退还教辅费用", required = true, dataType = "Boolean") })
	public HttpResponseResult directQuitMusicGroup(String musicGroupId, Integer userId, String reason, boolean isRefundCourseFee,
			boolean isRefundInstrumentFee, boolean isRefundTeachingAssistantsFee) throws Exception {
		return succeed(musicGroupService.directQuitMusicGroup(musicGroupId, userId, reason, isRefundCourseFee, isRefundInstrumentFee,
				isRefundTeachingAssistantsFee));
	}
}
