package org.dromara.main.controller;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.main.domain.bo.MainBackClauseBo;
import org.dromara.main.domain.vo.MainBackClauseVo;
import org.dromara.main.service.IMainBackClauseService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/backClause")
public class MainBackClauseController extends BaseController {

    private final IMainBackClauseService mainBackClauseService;

    /**
     * 查询背调条款分页列表
     */
    // @SaCheckPermission("system:backClause:list") // 如果需要权限控制请取消注释
    @GetMapping("/list")
    public TableDataInfo<MainBackClauseVo> list(MainBackClauseBo bo, PageQuery pageQuery) {
        return mainBackClauseService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取背调条款详细信息
     */
    // @SaCheckPermission("system:backClause:query")
    @GetMapping("/{id}")
    public R<MainBackClauseVo> getInfo(@PathVariable Long id) {
        return R.ok(mainBackClauseService.queryById(id));
    }

    /**
     * 新增背调条款
     */
    // @SaCheckPermission("system:backClause:add")
    @PostMapping()
    public R<Void> add(@Validated @RequestBody MainBackClauseBo bo) {
        return toAjax(mainBackClauseService.insertByBo(bo));
    }

    /**
     * 修改背调条款
     */
    // @SaCheckPermission("system:backClause:edit")
    @PutMapping()
    public R<Void> edit(@Validated @RequestBody MainBackClauseBo bo) {
        return toAjax(mainBackClauseService.updateByBo(bo));
    }

    /**
     * 删除背调条款
     */
    // @SaCheckPermission("system:backClause:remove")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(mainBackClauseService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 修改条款状态
     */
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody MainBackClauseBo bo) {
        // 这里不需要 @Validated，因为我们只更新状态
        MainBackClauseBo updateBo = new MainBackClauseBo();
        updateBo.setId(bo.getId());
        updateBo.setStatus(bo.getStatus());
        return toAjax(mainBackClauseService.updateByBo(updateBo));
    }
}
