package org.dromara.web.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
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.web.domain.bo.SettlementBo;
import org.dromara.web.domain.vo.SettlementVo;
import org.dromara.web.service.ISettlementService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 结算管理
 *
 * @author Lion Li
 * @date 2025-07-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/settlement")
public class SettlementController extends BaseController {

    private final ISettlementService settlementService;

    /**
     * 查询结算管理列表
     */
    @SaCheckPermission("system:settlement:list")
    @GetMapping("/list")
    public TableDataInfo<SettlementVo> list(SettlementBo bo, PageQuery pageQuery) {
        return settlementService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出结算管理列表
     */
    @SaCheckPermission("system:settlement:export")
    @Log(title = "结算管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SettlementBo bo, HttpServletResponse response) {
        List<SettlementVo> list = settlementService.queryList(bo);
        ExcelUtil.exportExcel(list, "结算管理", SettlementVo.class, response);
    }

    /**
     * 获取结算管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:settlement:query")
    @GetMapping("/{id}")
    public R<SettlementVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(settlementService.queryById(id));
    }

    /**
     * 新增结算管理
     */
    @SaCheckPermission("system:settlement:add")
    @Log(title = "结算管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SettlementBo bo) {
        return toAjax(settlementService.insertByBo(bo));
    }

    /**
     * 修改结算管理
     */
    @SaCheckPermission("system:settlement:edit")
    @Log(title = "结算管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SettlementBo bo) {
        return toAjax(settlementService.updateByBo(bo));
    }

    /**
     * 删除结算管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:settlement:remove")
    @Log(title = "结算管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(settlementService.deleteWithValidByIds(List.of(ids), true));
    }
}
