package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.entity.MusicMargin;
import com.ym.mec.biz.dal.page.MusicMarginQueryInfo;
import com.ym.mec.biz.service.MusicMarginService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;
import io.swagger.annotations.Api;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Api(tags = "乐器保证金")
@RequestMapping("musicMargin")
@RestController
public class MusicMarginController extends BaseController {

    @Autowired
    private MusicMarginService musicMarginService;

    @ApiOperation(value = "退款")
    @PostMapping("/refund")
    @PreAuthorize("@pcs.hasPermissions('musicMargin/refund')")
    public Object refund(@RequestBody MusicMargin musicMargin){
        MusicMargin margin = musicMarginService.get(musicMargin.getId());
        if(Objects.isNull(margin)){
            throw new BizException("乐器保证金不存在");
        }
        margin.setRefundFlag(1);
        margin.setRefund(musicMargin.getRefund());
        margin.setMemo(musicMargin.getMemo());
        musicMarginService.update(margin);
        return succeed();
    }

    @ApiOperation(value = "拒绝退款")
    @PostMapping("/refusedRefund")
    @PreAuthorize("@pcs.hasPermissions('musicMargin/refusedRefund')")
    public Object refusedRefund(@RequestBody MusicMargin musicMargin){
        MusicMargin margin = musicMarginService.get(musicMargin.getId());
        if(Objects.isNull(margin)){
            throw new BizException("乐器保证金不存在");
        }
        margin.setRefundFlag(2);
        margin.setMemo(musicMargin.getMemo());
        musicMarginService.update(margin);
        return succeed();
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('musicMargin/queryPage')")
    public Object queryPage(@RequestBody MusicMarginQueryInfo queryInfo){
        return succeed(musicMarginService.queryPage(queryInfo));
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    @PreAuthorize("@pcs.hasPermissions('musicMargin/delete')")
    public Object delete(Integer id){
        musicMarginService.delete(id);
        return succeed();
    }
}
