package com.propertyManagement.web.controller.business.api;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.propertyManagement.business.domain.WxRule;
import com.propertyManagement.business.domain.bo.WxRuleBo;
import com.propertyManagement.business.domain.vo.LoginWxUser;
import com.propertyManagement.business.domain.vo.WxRuleVo;
import com.propertyManagement.business.mapper.WxRuleMapper;
import com.propertyManagement.business.service.IWxRuleService;
import com.propertyManagement.business.support.util.AuthUtil;
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.enums.IsEnableType;
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-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/rule")
public class ApiWxRuleController extends BaseController {

    private final IWxRuleService iWxRuleService;
    private final WxRuleMapper wxRuleMapper;

    /**
     * 获取规则说明详细信息
     *
     * @param type 规则类型(1报修规则 2服务规则)
     */
    @GetMapping("/{type}")
    public R<WxRuleVo> getInfo(@PathVariable Integer type) {
        LoginWxUser wxUser = AuthUtil.getWxUser();
        if (wxUser.getCommunityId() == null) {
            return R.fail("查询失败，未选择小区");
        }
        LambdaQueryWrapper<WxRule> ruleLambdaQueryWrapper = Wrappers.lambdaQuery();
        ruleLambdaQueryWrapper.eq(WxRule::getCommunityId, wxUser.getCommunityId());
        ruleLambdaQueryWrapper.eq(WxRule::getRuleType, type);
        ruleLambdaQueryWrapper.eq(WxRule::getIsEnable, IsEnableType.YES.getCode());
        ruleLambdaQueryWrapper.orderByDesc(WxRule::getUpdateTime);
        List<WxRuleVo> ruleList = wxRuleMapper.selectVoList(ruleLambdaQueryWrapper);
        if (ruleList.isEmpty()) {
            return R.ok();
        }
        return R.ok(ruleList.get(0));
    }

}
