package com.yonge.cooleshow.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.microsvc.toolkit.common.response.template.R;
import com.yonge.cooleshow.biz.dal.service.InstrumentService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.wrapper.InstrumentWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
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.annotation.Resource;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/musicalInstrument")
@Api(tags = "曲目乐器")
public class MusicalInstrumentController {

    @Resource
    private InstrumentService instrumentService;
    @Resource
    private SysUserService sysUserService;


    @ApiOperation(value = "修改乐器")
    @PreAuthorize("@pcs.hasPermissions('musicalInstrument/update', {'BACKEND'})")
    @PostMapping("/update")
    public R<JSONObject> update(@RequestBody @Validated InstrumentWrapper.Update update) {
        update.setOperator(sysUserService.getUserId());
        instrumentService.modify(update);
        return R.defaultR();

    }

    @ApiOperation(value = "乐器列表")
    @PreAuthorize("@pcs.hasPermissions('musicalInstrument/list', {'BACKEND'})")
    @PostMapping("/list")
    public R<List<InstrumentWrapper.Instrument>> list(@RequestBody InstrumentWrapper.InstrumentQuery query) {
        List<InstrumentWrapper.Instrument> instruments = instrumentService.getList(query);
        return R.from(instruments);
	}

}
