package com.cool.bang.modules.system.controller;

import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.web.request.PageDomain;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.common.web.response.module.ResultTable;
import com.cool.bang.modules.system.domain.Label;
import com.cool.bang.modules.system.service.ILabelService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 标签
 *
 * @author Sy
 * @version 1.0.0
 * 2024-11-13 10:39
 */
@RestController
@RequestMapping(ControllerConstant.SYSTEM + "label/v1")
public class LabelController extends BaseController {

    private ILabelService labelService;

    @Autowired
    public LabelController(ILabelService labelService) {
        this.labelService = labelService;
    }

    @GetMapping
    @PreAuthorize("hasPermission('','system:label:main')")
    public ResultTable main(PageDomain pageDomain, Label param) {
        PageInfo<Label> labelPage = labelService.page(pageDomain, param);
        return pageTable(labelPage.getList(), labelPage.getTotal());
    }

    @GetMapping("{id}")
    @PreAuthorize("hasPermission('','system:label:main')")
    public Result get(@PathVariable Long id) {
        return success(labelService.queryById(id));
    }

    @GetMapping("select")
    public Result getSelect() {
        return success(labelService.list(new Label()));
    }

    @PostMapping
    @PreAuthorize("hasPermission('','system:label:add')")
    public Result add(@RequestBody Label label) {
        label.insertConfigure();
        return decide(labelService.save(label));
    }

    @PostMapping("edit")
    @PreAuthorize("hasPermission('','system:label:add')")
    public Result edit(@RequestBody Label label) {
        return decide(labelService.update(label));
    }

    @DeleteMapping("{id}")
    @PreAuthorize("hasPermission('','system:label:add')")
    public Result remove(@PathVariable Integer id) {
        return decide(labelService.removeById(id));
    }

}
