package org.dromara.system.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.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.system.domain.vo.TblMiniprogramVo;
import org.dromara.system.domain.bo.TblMiniprogramBo;
import org.dromara.system.service.ITblMiniprogramService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 小程序信息
 *
 * @author Lion Li
 * @date 2024-07-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/miniprogram")
public class TblMiniprogramController extends BaseController {

    private final ITblMiniprogramService tblMiniprogramService;

    /**
     * 查询小程序信息列表
     */
//    @SaCheckPermission("system:miniprogram:list")
    @GetMapping("/list")
    public TableDataInfo<TblMiniprogramVo> list(TblMiniprogramBo bo, PageQuery pageQuery) {
        return tblMiniprogramService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小程序信息列表
     */
//    @SaCheckPermission("system:miniprogram:export")
    @Log(title = "小程序信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TblMiniprogramBo bo, HttpServletResponse response) {
        List<TblMiniprogramVo> list = tblMiniprogramService.queryList(bo);
        ExcelUtil.exportExcel(list, "小程序信息", TblMiniprogramVo.class, response);
    }

    /**
     * 获取小程序信息详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("system:miniprogram:query")
    @GetMapping("/{id}")
    public R<TblMiniprogramVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tblMiniprogramService.queryById(id));
    }

    /**
     * 新增小程序信息
     */
//    @SaCheckPermission("system:miniprogram:add")
    @Log(title = "小程序信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TblMiniprogramBo bo) {
        return toAjax(tblMiniprogramService.insertByBo(bo));
    }

    /**
     * 修改小程序信息
     */
//    @SaCheckPermission("system:miniprogram:edit")
    @Log(title = "小程序信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TblMiniprogramBo bo) {
        return toAjax(tblMiniprogramService.updateByBo(bo));
    }

    /**
     * 删除小程序信息
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("system:miniprogram:remove")
    @Log(title = "小程序信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tblMiniprogramService.deleteWithValidByIds(List.of(ids), true));
    }
}
