package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.bo.WxUserCommunityLedgerBo;
import com.propertyManagement.business.domain.vo.WxUserCommunityLedgerVo;
import com.propertyManagement.business.service.IWxUserCommunityLedgerService;
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-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/userCommunityLedger")
public class WxUserCommunityLedgerController extends BaseController {

    private final IWxUserCommunityLedgerService iWxUserCommunityLedgerService;

    /**
     * 查询用户、小区、台账关联报列表
     */
    @SaCheckPermission("business:userCommunityLedger:list")
    @GetMapping("/list")
    public TableDataInfo<WxUserCommunityLedgerVo> list(WxUserCommunityLedgerBo bo, PageQuery pageQuery) {
        return iWxUserCommunityLedgerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户、小区、台账关联报列表
     */
    @SaCheckPermission("business:userCommunityLedger:export")
    @Log(title = "用户、小区、台账关联报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxUserCommunityLedgerBo bo, HttpServletResponse response) {
        List<WxUserCommunityLedgerVo> list = iWxUserCommunityLedgerService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户、小区、台账关联报", WxUserCommunityLedgerVo.class, response);
    }

    /**
     * 获取用户、小区、台账关联报详细信息
     *
     * @param userId 主键
     */
    @SaCheckPermission("business:userCommunityLedger:query")
    @GetMapping("/{userId}")
    public R<WxUserCommunityLedgerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userId) {
        return R.ok(iWxUserCommunityLedgerService.queryById(userId));
    }

    /**
     * 新增用户、小区、台账关联报
     */
    @SaCheckPermission("business:userCommunityLedger:add")
    @Log(title = "用户、小区、台账关联报", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxUserCommunityLedgerBo bo) {
        return toAjax(iWxUserCommunityLedgerService.insertByBo(bo));
    }

    /**
     * 修改用户、小区、台账关联报
     */
    @SaCheckPermission("business:userCommunityLedger:edit")
    @Log(title = "用户、小区、台账关联报", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxUserCommunityLedgerBo bo) {
        return toAjax(iWxUserCommunityLedgerService.updateByBo(bo));
    }

    /**
     * 删除用户、小区、台账关联报
     *
     * @param userIds 主键串
     */
    @SaCheckPermission("business:userCommunityLedger:remove")
    @Log(title = "用户、小区、台账关联报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userIds) {
        return toAjax(iWxUserCommunityLedgerService.deleteWithValidByIds(Arrays.asList(userIds), true));
    }
}
