package com.propertyManagement.web.controller.business;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.propertyManagement.common.annotation.RepeatSubmit;
import com.propertyManagement.common.annotation.Log;
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.validate.AddGroup;
import com.propertyManagement.common.core.validate.EditGroup;
import com.propertyManagement.common.enums.BusinessType;
import com.propertyManagement.common.utils.poi.ExcelUtil;
import com.propertyManagement.business.domain.vo.WxUserVo;
import com.propertyManagement.business.domain.bo.WxUserBo;
import com.propertyManagement.business.service.IWxUserService;
import com.propertyManagement.common.core.page.TableDataInfo;

/**
 * 微信用户
 *
 * @author liushuai
 * @date 2025-05-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/user")
public class WxUserController extends BaseController {

    private final IWxUserService iWxUserService;

    /**
     * 查询微信用户列表
     */
    @SaCheckPermission("business:user:list")
    @GetMapping("/list")
    public TableDataInfo<WxUserVo> list(WxUserBo bo, PageQuery pageQuery) {
        return iWxUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出微信用户列表
     */
    @SaCheckPermission("business:user:export")
    @Log(title = "微信用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxUserBo bo, HttpServletResponse response) {
        List<WxUserVo> list = iWxUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "微信用户", WxUserVo.class, response);
    }

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

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

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

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