package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.bo.WxRuleBo;
import com.propertyManagement.business.domain.vo.WxRuleVo;
import com.propertyManagement.business.service.IWxRuleService;
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.helper.LoginHelper;
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-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/rule")
public class WxRuleController extends BaseController {

    private final IWxRuleService iWxRuleService;

    /**
     * 查询规则说明列表
     */
    @SaCheckPermission("business:rule:list")
    @GetMapping("/list")
    public TableDataInfo<WxRuleVo> list(WxRuleBo bo, PageQuery pageQuery) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return iWxRuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出规则说明列表
     */
    @SaCheckPermission("business:rule:export")
    @Log(title = "规则说明", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxRuleBo bo, HttpServletResponse response) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        List<WxRuleVo> list = iWxRuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "规则说明", WxRuleVo.class, response);
    }

    /**
     * 获取规则说明详细信息
     *
     * @param ruleId 主键
     */
    @SaCheckPermission("business:rule:query")
    @GetMapping("/{ruleId}")
    public R<WxRuleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long ruleId) {
        return R.ok(iWxRuleService.queryById(ruleId));
    }

    /**
     * 新增规则说明
     */
    @SaCheckPermission("business:rule:add")
    @Log(title = "规则说明", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxRuleBo bo) {
        return toAjax(iWxRuleService.insertByBo(bo));
    }

    /**
     * 修改规则说明
     */
    @SaCheckPermission("business:rule:edit")
    @Log(title = "规则说明", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxRuleBo bo) {
        return toAjax(iWxRuleService.updateByBo(bo));
    }

    /**
     * 删除规则说明
     *
     * @param ruleIds 主键串
     */
    @SaCheckPermission("business:rule:remove")
    @Log(title = "规则说明", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ruleIds) {
        return toAjax(iWxRuleService.deleteWithValidByIds(Arrays.asList(ruleIds), true));
    }
}
