package com.seer.teach.pay.admin.sys.controller;

import com.seer.teach.common.ResultBean;
import com.seer.teach.pay.admin.sys.convert.AdminCoinConfigConvert;
import com.seer.teach.pay.admin.sys.controller.req.AdminCoinConfigReq;
import com.seer.teach.pay.admin.sys.controller.resp.AdminCoinConfigResp;
import com.seer.teach.pay.admin.sys.service.IAdminCoinConfigService;
import com.seer.teach.pay.entity.CoinConfigEntity;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import cn.dev33.satoken.annotation.SaCheckPermission;

@RestController
@RequestMapping("/pay/coin-config")
@Tag(name = "管理端 - 学豆配置")
@Validated
@RequiredArgsConstructor
public class AdminCoinConfigController {

    private final IAdminCoinConfigService coinConfigService;

    @Operation(summary = "学豆配置列表查询")
    @GetMapping("/list")
    @SaCheckPermission("admin:coin:config:list")
    public ResultBean<List<AdminCoinConfigResp>> list(){
        List<CoinConfigEntity> coinConfigEntities = coinConfigService.list();
        return ResultBean.success(AdminCoinConfigConvert.INSTANCE.list(coinConfigEntities));
    }

    @Operation(summary = "学豆配置详情")
    @GetMapping("/{id}")
    @SaCheckPermission("admin:coin:config:detail")
    public ResultBean<AdminCoinConfigResp> getById(@PathVariable("id") Integer id){
        CoinConfigEntity config = coinConfigService.getById(id);
        return ResultBean.success(AdminCoinConfigConvert.INSTANCE.convertOne(config));
    }

    @Operation(summary = "新增学豆配置")
    @PostMapping("/save")
    @SaCheckPermission("admin:coin:config:create")
    public ResultBean<Boolean> create(@RequestBody @Validated AdminCoinConfigReq coinConfigReq){
        CoinConfigEntity coinConfigEntity = AdminCoinConfigConvert.INSTANCE.req2entity(coinConfigReq);
        return ResultBean.success(coinConfigService.create(coinConfigEntity));
    }

    @Operation(summary = "修改学豆配置")
    @PutMapping("/update")
    @SaCheckPermission("admin:coin:config:update")
    public ResultBean<Boolean> update(@RequestBody @Validated AdminCoinConfigReq coinConfigReq){
        CoinConfigEntity coinConfigEntity = AdminCoinConfigConvert.INSTANCE.req2entity(coinConfigReq);
        return ResultBean.success(coinConfigService.update(coinConfigEntity));
    }

    @Operation(summary = "删除学豆配置")
    @DeleteMapping("/delete/{id}")
    @SaCheckPermission("admin:coin:config:delete")
    public ResultBean<Boolean> delete(@PathVariable("id") Integer id){
        return ResultBean.success(coinConfigService.delete(id));
    }
}