package org.dromara.work.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.work.domain.bo.TpRemittanceBo;
import org.dromara.work.domain.vo.TpRemittanceVo;
import org.dromara.work.service.ITpRemittanceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 汇款
 *
 * @author Maosw
 * @date 2024-08-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/remittance")
public class TpRemittanceController extends BaseController {

    private final ITpRemittanceService tpRemittanceService;

    /**
     * 查询汇款列表
     */
    @SaCheckPermission("work:remittance:list")
    @GetMapping("/list")
    public TableDataInfo<TpRemittanceVo> list(TpRemittanceBo bo, PageQuery pageQuery) {
        return tpRemittanceService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出汇款列表
     */
    @SaCheckPermission("work:remittance:export")
    @Log(title = "汇款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpRemittanceBo bo, HttpServletResponse response) {
        List<TpRemittanceVo> list = tpRemittanceService.queryList(bo);
        ExcelUtil.exportExcel(list, "汇款", TpRemittanceVo.class, response);
    }

    /**
     * 获取汇款详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:remittance:query")
    @GetMapping("/{id}")
    public R<TpRemittanceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpRemittanceService.queryById(id));
    }

    /**
     * 新增汇款
     */
    @SaCheckPermission("work:remittance:add")
    @Log(title = "汇款", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpRemittanceBo bo) {
        return toAjax(tpRemittanceService.insertByBo(bo));
    }

    /**
     * 修改汇款
     */
    @SaCheckPermission("work:remittance:edit")
    @Log(title = "汇款", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpRemittanceBo bo) {
        return toAjax(tpRemittanceService.updateByBo(bo));
    }

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