package com.yingpaipay.setting.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import com.yingpaipay.setting.domain.vo.AppletSettingPrivacyVo;
import com.yingpaipay.setting.domain.vo.AppletSettingUserVo;
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 com.yingpaipay.setting.domain.vo.AppletSettingVo;
import com.yingpaipay.setting.domain.bo.AppletSettingBo;
import com.yingpaipay.setting.service.IAppletSettingService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 小程序设置
 *
 * @author Huanyi
 * @date 2025-11-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/setting/applet")
public class AppletSettingController extends BaseController {

    private final IAppletSettingService appletSettingService;

    /**
     * 查询小程序设置列表
     */
    @SaCheckPermission("setting:applet:list")
    @GetMapping("/list")
    public TableDataInfo<AppletSettingVo> list(AppletSettingBo bo, PageQuery pageQuery) {
        return appletSettingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小程序设置列表
     */
    @SaCheckPermission("setting:applet:export")
    @Log(title = "小程序设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppletSettingBo bo, HttpServletResponse response) {
        List<AppletSettingVo> list = appletSettingService.queryList(bo);
        ExcelUtil.exportExcel(list, "小程序设置", AppletSettingVo.class, response);
    }

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

    /**
     * 新增小程序设置
     */
    @SaCheckPermission("setting:applet:add")
    @Log(title = "小程序设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppletSettingBo bo) {
        return toAjax(appletSettingService.insertByBo(bo));
    }

    /**
     * 修改小程序设置
     */
    @SaCheckPermission("setting:applet:edit")
    @Log(title = "小程序设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppletSettingBo bo) {
        return toAjax(appletSettingService.updateByBo(bo));
    }

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

    @SaIgnore
    @GetMapping("/user")
    public R<AppletSettingUserVo> getUser() {
        return R.ok(new AppletSettingUserVo(appletSettingService.queryById(1L).getUserAgreement()));
    }

    @SaIgnore
    @GetMapping("/privacy")
    public R<AppletSettingPrivacyVo> getPrivacy() {
        return R.ok(new AppletSettingPrivacyVo(appletSettingService.queryById(1L).getPrivacyAgreement()));
    }

}
