package com.yonge.cooleshow.cms.controller;

import com.yonge.cooleshow.cms.dal.entity.SysNewsType;
import com.yonge.cooleshow.cms.dto.SysNewsTypeDto;
import com.yonge.cooleshow.cms.dto.search.MusicTheorySearch;
import com.yonge.cooleshow.cms.service.SysNewsTypeService;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.base.page.QueryInfo;
import com.yonge.toolset.base.util.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.web.bind.annotation.*;

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

import static com.yonge.cooleshow.common.entity.HttpResponseResult.failed;
import static com.yonge.cooleshow.common.entity.HttpResponseResult.succeed;

/**
 * Description
 *
 * @author liujunchi
 * @date 2022-04-26
 */
@RestController
@RequestMapping("music/theory")
@Api(tags = "乐理管理")
public class MusicTheoryController {

    @Autowired
    private SysNewsTypeService newsTypeService;


    /**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "乐理章节详情", notes = "传入id")
    public HttpResponseResult<SysNewsType> detail(@ApiParam(value = "编号", required = true) @PathVariable("id") Long id) {
        return succeed(newsTypeService.get(id));
    }

    @PostMapping(value = "/page", consumes="application/json", produces="application/json")
    @ApiOperation(value = "乐理章节详情查询分页", httpMethod="POST", consumes="application/json", produces="application/json")
    public HttpResponseResult<PageInfo<SysNewsType>> page(@Valid @RequestBody MusicTheorySearch query) {
        query.setParentId(6L);
        PageInfo<SysNewsType> pages = newsTypeService.queryPage( query);
        return succeed(pages);
    }


    /**
     * 新增
     */
    @PostMapping(value = "/save",  consumes="application/json", produces="application/json")
    @ApiOperation(value = "乐理章节详情新增", httpMethod="POST", consumes="application/json", produces="application/json")
    public HttpResponseResult<Boolean> save( @RequestBody SysNewsType newsType) {
        if (newsType.getOrder() == null) {
            return failed("排序不能为空");
        }
        if (StringUtil.isEmpty(newsType.getName())) {
            return failed("章节名称不能为空");
        }
        newsType.setDelFlag(false);
        newsType.setParentId(6L);
        newsType.setCreateTime(new Date());
        newsType.setUpdateTime(new Date());

        return succeed(newsTypeService.insert(newsType) > 0);
    }

    /**
     * 修改
     */
    @PostMapping(value =  "/update",  consumes="application/json", produces="application/json")
    @ApiOperation(value = "乐理章节详情修改", httpMethod="POST", consumes="application/json", produces="application/json")
    public HttpResponseResult<Boolean> update(  @RequestBody SysNewsType newsType) {
        if (newsType.getOrder() == null) {
            return failed("排序不能为空");
        }
        if (StringUtil.isEmpty(newsType.getName())) {
            return failed("章节名称不能为空");
        }
        if (newsType.getId() == null) {
            return failed("章节id不能为空");
        }
        newsType.setDelFlag(false);
        newsType.setParentId(6L);
        newsType.setUpdateTime(new Date());
        return succeed(newsTypeService.update(newsType)>0);
    }

    /**
     * 删除
     */
    @PostMapping("/remove/{id}")
    @ApiOperation(value = "乐理章节详情删除", notes = "传入id")
    public HttpResponseResult<Boolean> remove(@ApiParam(value = "编号", required = true) @PathVariable Long id) {
        if (StringUtil.isEmpty(id)) {
            return failed("参数不能为空");
        }
        SysNewsType sysNewsType = newsTypeService.get(id);
        if (sysNewsType == null || sysNewsType.getParentId() != 6) {
            return failed("未找到乐理章节");
        }

        return succeed(newsTypeService.delete(id) > 0);
    }


    /**
     * 删除
     */
    @PostMapping("/app/page")
    @ApiOperation(value = "app乐理知识")
    public HttpResponseResult<PageInfo<SysNewsTypeDto>> appPage(@RequestBody QueryInfo query) {

        return succeed(newsTypeService.appPage(query));
    }


}
