package com.yingpaipay.setting.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import com.yingpaipay.setting.domain.bo.AgreementSettingBo;
import com.yingpaipay.setting.domain.vo.AgreementSettingPrivacyVo;
import com.yingpaipay.setting.domain.vo.AgreementSettingUserVo;
import com.yingpaipay.setting.domain.vo.AgreementSettingVo;
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.service.IAgreementSettingService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 协议设置
 *
 * @author Huanyi
 * @date 2025-11-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/setting/agreement")
public class AggrementSettingController extends BaseController {

    private final IAgreementSettingService appletSettingService;

    /**
     * 查询协议设置列表
     */
    @SaCheckPermission("setting:agreement:list")
    @GetMapping("/list")
    public TableDataInfo<AgreementSettingVo> list(AgreementSettingBo bo, PageQuery pageQuery) {
        return appletSettingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出协议设置列表
     */
    @SaCheckPermission("setting:agreement:export")
    @Log(title = "协议设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgreementSettingBo bo, HttpServletResponse response) {
        List<AgreementSettingVo> list = appletSettingService.queryList(bo);
        ExcelUtil.exportExcel(list, "协议设置", AgreementSettingVo.class, response);
    }

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

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

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

    /**
     * 删除协议设置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("setting:agreement: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<AgreementSettingUserVo> getUser() {
        return R.ok(new AgreementSettingUserVo(appletSettingService.queryById(1L).getUserAgreement()));
    }

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

}
