package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.biz.dal.dto.MusicTagSaveDto;
import com.yonge.cooleshow.biz.dal.dto.search.MusicTagSearch;
import com.yonge.cooleshow.biz.dal.entity.MusicTag;
import com.yonge.cooleshow.biz.dal.service.MusicTagService;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.MusicTagVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.mybatis.page.PageInfo;
import com.yonge.toolset.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/MusicTag")
@Api(value = "音乐标签表", tags = "音乐标签表")
public class MusicTagController extends BaseController {

    @Autowired
    private MusicTagService musicTagService;

	/**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
	@ApiOperation(value = "详情", notes = "传入id")
    public HttpResponseResult<MusicTagVo> detail(@ApiParam(value = "标签编号", required = true) @PathVariable("id") Long id) {
    	return succeed(musicTagService.detail(id));
	}

    /**
     * 查询分页tree
     */
    @PostMapping(value = "/page", consumes="application/json", produces="application/json")
    @ApiOperation(value = "查询分页", httpMethod="POST", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<MusicTagVo>> page(@Valid @RequestBody MusicTagSearch query) {
		IPage<MusicTagVo> pages = musicTagService.selectPage(PageUtil.getPage(query), query);
		return succeed(PageUtil.pageInfo(pages));
	}


	/**
	 * 查询分页tree
	 */
	@GetMapping("/tree")
	@ApiOperation(value = "查询标签树列表")
	public HttpResponseResult<List<MusicTagVo>> tree() {
		List<MusicTagVo> treeList = musicTagService.selectMusicTagTree();
		return succeed(treeList);
	}

    /**
	 * 新增
	 */
	@PostMapping(value = "/save",  consumes="application/json", produces="application/json")
	@ApiOperation(value = "新增", httpMethod="POST", consumes="application/json", produces="application/json")
	public HttpResponseResult<Boolean> save(@Validated(MusicTagSaveDto.Create.class) @RequestBody MusicTagSaveDto musicTagSaveDto) {
		if (musicTagSaveDto.getParentTagId() != null && musicTagSaveDto.getParentTagId() != 0) {
			MusicTag musicTag = musicTagService.getById(musicTagSaveDto.getParentTagId());
			if (musicTag.getParentTagId() != 0) {
				return failed("标签只能有两级");
			}
		}
		return succeed(musicTagService.createMusicTag(musicTagSaveDto));
	}

    /**
	 * 修改
	 */
	@PostMapping(value =  "/update",  consumes="application/json", produces="application/json")
	@ApiOperation(value = "修改", httpMethod="POST", consumes="application/json", produces="application/json")
	public HttpResponseResult<Boolean> update(@Validated(MusicTagSaveDto.Update.class) @RequestBody MusicTagSaveDto musicTagSaveDto) {
		if (musicTagSaveDto.getParentTagId() != null && musicTagSaveDto.getParentTagId() != 0) {
			MusicTag musicTag = musicTagService.getById(musicTagSaveDto.getParentTagId());
			if (musicTag.getParentTagId() != 0) {
				return failed("标签只能有两级");
			}
		}
		return succeed(musicTagService.updateMusicTag(musicTagSaveDto));
	}

 	/**
	 * 删除
	 */
	@PostMapping("/remove/{id}")
	@ApiOperation(value = "逻辑删除", notes = "传入id")
	public HttpResponseResult<Boolean> remove(@ApiParam(value = "标签编号", required = true) @PathVariable Long id) {
        if (StringUtil.isEmpty(id)) {
			return failed("参数不能为空");
		}
		return status(musicTagService.del(id));
	}

	/**
	 * 启用/停用
	 */
	@PostMapping("/state/{id}")
	@ApiOperation(value = "启用/停用", notes = "传入id")
	public HttpResponseResult<Boolean> state(@ApiParam(value = "标签编号", required = true)  @PathVariable Long id) {
		if (StringUtil.isEmpty(id)) {
			return failed("参数不能为空");
		}
		return status(musicTagService.state(id));
	}
}