package org.dromara.web.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.web.domain.bo.ChargeRecordBo;
import org.dromara.web.domain.vo.ChargeRecordTotalizeVo;
import org.dromara.web.domain.vo.ChargeRecordVo;
import org.dromara.web.service.IChargeRecordService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;

import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 收费记录
 *
 * @author Lion Li
 * @date 2025-08-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/settlement/chargeRecord")
public class ChargeRecordController extends BaseController {

    private final IChargeRecordService chargeRecordService;

    /**
     * 查询收费记录列表
     */
    @SaCheckPermission("settlement:chargeRecord:list")
    @GetMapping("/list")
    public TableDataInfo<ChargeRecordVo> list(ChargeRecordBo bo, PageQuery pageQuery) {
        return chargeRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出收费记录列表
     */
    @SaCheckPermission("settlement:chargeRecord:export")
    @Log(title = "收费记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ChargeRecordBo bo, HttpServletResponse response) {
        List<ChargeRecordVo> list = chargeRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "收费记录", ChargeRecordVo.class, response);
    }

    /**
     * 获取收费记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("settlement:chargeRecord:query")
    @GetMapping("/{id}")
    public R<ChargeRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(chargeRecordService.queryById(id));
    }

    /**
     * 新增收费记录
     */
    @SaCheckPermission("settlement:chargeRecord:add")
    @Log(title = "收费记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ChargeRecordBo bo) {
        return toAjax(chargeRecordService.insertByBo(bo));
    }

    /**
     * 修改收费记录
     */
    @SaCheckPermission("settlement:chargeRecord:edit")
    @Log(title = "收费记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ChargeRecordBo bo) {
        return toAjax(chargeRecordService.updateByBo(bo));
    }

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

    @GetMapping("/listInvoice")
    public TableDataInfo<ChargeRecordVo> listInvoice(PageQuery pageQuery) {
        return chargeRecordService.queryInvoicePageList(pageQuery);
    }

    @GetMapping("/totalize")
    public R<ChargeRecordTotalizeVo> totalize() {
        return chargeRecordService.totalize();
    }
}
