package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.MusicSheetAccompanimentDto;
import com.yonge.cooleshow.biz.dal.entity.MusicSheetAccompaniment;
import com.yonge.cooleshow.biz.dal.service.MusicSheetAccompanimentService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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 javax.validation.Valid;

/**
 * 曲谱伴奏表 web 控制层
 * @author yzp
 * @date 2022-03-26 00:21:46
 * @version v1.0
 **/
@RestController
@RequestMapping("${app-config.url.admin:}/music/sheet/accompaniment")
@Api(tags = "曲谱伴奏表 API接口")
public class MusicSheetAccompanimentController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;

	@Autowired
	private MusicSheetAccompanimentService musicSheetAccompanimentService;

	@ApiOperation(value = "新增", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/create", consumes="application/json", produces="application/json")
    public HttpResponseResult<Object> create(@Valid @RequestBody MusicSheetAccompaniment musicSheetAccompaniment) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }

        //musicSheetAccompaniment.setCreateBy(sysUser.getId());
        //musicSheetAccompaniment.setCreateTime(new Date());
        musicSheetAccompanimentService.save(musicSheetAccompaniment);
        return succeed();
    }

    @ApiOperation(value = "删除", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping("/delete/{id}")
    public Object delete(@PathVariable Long id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }

        musicSheetAccompanimentService.removeById(id);
        return succeed();
    }

    @ApiOperation(value = "修改", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/update", consumes="application/json", produces="application/json")
    public HttpResponseResult<Object> update(@Valid @RequestBody MusicSheetAccompaniment musicSheetAccompaniment) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if (musicSheetAccompaniment.getId()==null){
            return failed("缺少ID");
        }

        //musicSheetAccompaniment.setUpdateBy(sysUser.getId());
        //musicSheetAccompaniment.setUpdateTime(new Date());
        musicSheetAccompanimentService.updateById(musicSheetAccompaniment);
        return succeed();
    }

    @ApiOperation(value = "分页查询", httpMethod="POST", consumes="application/json", produces="application/json")
    @PostMapping(value="/list", consumes="application/json", produces="application/json")
    public HttpResponseResult<Object> list(@RequestBody MusicSheetAccompanimentDto musicSheetAccompanimentDto) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }

        int pageNo = musicSheetAccompanimentDto.getPageNo();
        int pageSize = musicSheetAccompanimentDto.getPageSize();

        try {
            if (pageNo==0) {
                pageNo = 1;
            }
            if (pageSize==0) {
                pageSize = 10;
            }

            LambdaQueryWrapper<MusicSheetAccompaniment> lambdaQueryWrapper = Wrappers.lambdaQuery();
            //lambdaQueryWrapper.like(MusicSheetAccompaniment::getName , "k");

            Page<MusicSheetAccompaniment> page = new Page<>(pageNo,pageSize);
            return succeed(musicSheetAccompanimentService.page(page, lambdaQueryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            return HttpResponseResult.failed(e.getMessage());
        }
    }
}
