package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.bo.WxCommunityBo;
import com.propertyManagement.business.domain.vo.WxCommunityVo;
import com.propertyManagement.business.service.IWxCommunityService;
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/community")
public class WxCommunityController extends BaseController {

    private final IWxCommunityService iWxCommunityService;

    /**
     * 查询小区列表
     */
    @SaCheckPermission("business:community:list")
    @GetMapping("/list")
    public TableDataInfo<WxCommunityVo> list(WxCommunityBo bo, PageQuery pageQuery) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return iWxCommunityService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询小区列表
     */
    @GetMapping("/listAll")
    public R<List<WxCommunityVo>> listAll(WxCommunityBo bo) {
        return R.ok(iWxCommunityService.queryList(bo));
    }

    /**
     * 导出小区列表
     */
    @SaCheckPermission("business:community:export")
    @Log(title = "小区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxCommunityBo bo, HttpServletResponse response) {
        List<WxCommunityVo> list = iWxCommunityService.queryList(bo);
        ExcelUtil.exportExcel(list, "小区", WxCommunityVo.class, response);
    }

    /**
     * 获取小区详细信息
     *
     * @param communityId 主键
     */
    @SaCheckPermission("business:community:query")
    @GetMapping("/{communityId}")
    public R<WxCommunityVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long communityId) {
        return R.ok(iWxCommunityService.queryById(communityId));
    }

    /**
     * 新增小区
     */
    @SaCheckPermission("business:community:add")
    @Log(title = "小区", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxCommunityBo bo) {
        return toAjax(iWxCommunityService.insertByBo(bo));
    }

    /**
     * 修改小区
     */
    @SaCheckPermission("business:community:edit")
    @Log(title = "小区", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxCommunityBo bo) {
        return toAjax(iWxCommunityService.updateByBo(bo));
    }

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