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.TpIntegralDetailBo;
import org.dromara.work.domain.vo.TpIntegralDetailVo;
import org.dromara.work.service.ITpIntegralDetailService;
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/integralDetail")
public class TpIntegralDetailController extends BaseController {

    private final ITpIntegralDetailService tpIntegralDetailService;

    /**
     * 查询积分明细列表
     */
    @SaCheckPermission("work:integralDetail:list")
    @GetMapping("/list")
    public TableDataInfo<TpIntegralDetailVo> list(TpIntegralDetailBo bo, PageQuery pageQuery) {
        return tpIntegralDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出积分明细列表
     */
    @SaCheckPermission("work:integralDetail:export")
    @Log(title = "积分明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpIntegralDetailBo bo, HttpServletResponse response) {
        List<TpIntegralDetailVo> list = tpIntegralDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "积分明细", TpIntegralDetailVo.class, response);
    }

    /**
     * 获取积分明细详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:integralDetail:query")
    @GetMapping("/{id}")
    public R<TpIntegralDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpIntegralDetailService.queryById(id));
    }

    /**
     * 新增积分明细
     */
    @SaCheckPermission("work:integralDetail:add")
    @Log(title = "积分明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpIntegralDetailBo bo) {
        return toAjax(tpIntegralDetailService.insertByBo(bo));
    }

    /**
     * 修改积分明细
     */
    @SaCheckPermission("work:integralDetail:edit")
    @Log(title = "积分明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpIntegralDetailBo bo) {
        return toAjax(tpIntegralDetailService.updateByBo(bo));
    }

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