package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.bo.WxProtocolBo;
import com.propertyManagement.business.domain.vo.WxProtocolVo;
import com.propertyManagement.business.service.IWxProtocolService;
import com.propertyManagement.common.annotation.Log;
import com.propertyManagement.common.annotation.RepeatSubmit;
import com.propertyManagement.common.core.controller.BaseController;
import com.propertyManagement.common.core.domain.PageQuery;
import com.propertyManagement.common.core.domain.R;
import com.propertyManagement.common.core.page.TableDataInfo;
import com.propertyManagement.common.core.validate.AddGroup;
import com.propertyManagement.common.core.validate.EditGroup;
import com.propertyManagement.common.enums.BusinessType;
import com.propertyManagement.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户协议
 *
 * @author liushuai
 * @date 2025-05-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/protocol")
public class WxProtocolController extends BaseController {

    private final IWxProtocolService iWxProtocolService;

    /**
     * 查询用户协议列表
     */
    @SaCheckPermission("business:protocol:list")
    @GetMapping("/list")
    public TableDataInfo<WxProtocolVo> list(WxProtocolBo bo, PageQuery pageQuery) {
        return iWxProtocolService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户协议列表
     */
    @SaCheckPermission("business:protocol:export")
    @Log(title = "用户协议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxProtocolBo bo, HttpServletResponse response) {
        List<WxProtocolVo> list = iWxProtocolService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户协议", WxProtocolVo.class, response);
    }

    /**
     * 获取用户协议详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:protocol:query")
    @GetMapping("/{id}")
    public R<WxProtocolVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iWxProtocolService.queryById(id));
    }

    /**
     * 新增用户协议
     */
    @SaCheckPermission("business:protocol:add")
    @Log(title = "用户协议", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxProtocolBo bo) {
        return toAjax(iWxProtocolService.insertByBo(bo));
    }

    /**
     * 修改用户协议
     */
    @SaCheckPermission("business:protocol:edit")
    @Log(title = "用户协议", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxProtocolBo bo) {
        return toAjax(iWxProtocolService.updateByBo(bo));
    }

    /**
     * 删除用户协议
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:protocol:remove")
    @Log(title = "用户协议", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iWxProtocolService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
