package com.propertyManagement.web.controller.business;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.propertyManagement.business.domain.bo.WxGuestBo;
import com.propertyManagement.business.domain.vo.WxGuestVo;
import com.propertyManagement.business.service.IWxGuestService;
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/guest")
public class WxGuestController extends BaseController {

    private final IWxGuestService iWxGuestService;

    /**
     * 查询访客列表
     */
    @SaCheckPermission("business:guest:list")
    @GetMapping("/list")
    public TableDataInfo<WxGuestVo> list(WxGuestBo bo, PageQuery pageQuery) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        return iWxGuestService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出访客列表
     */
    @SaCheckPermission("business:guest:export")
    @Log(title = "访客", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxGuestBo bo, HttpServletResponse response) {
        if (!LoginHelper.isAdmin()) {
            bo.setCommunityId(LoginHelper.getLoginUser().getCommunityId());
        }
        List<WxGuestVo> list = iWxGuestService.queryList(bo);
        ExcelUtil.exportExcel(list, "访客", WxGuestVo.class, response);
    }

    /**
     * 获取访客详细信息
     *
     * @param guestId 主键
     */
    @SaCheckPermission("business:guest:query")
    @GetMapping("/{guestId}")
    public R<WxGuestVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long guestId) {
        return R.ok(iWxGuestService.queryById(guestId));
    }

    /**
     * 新增访客
     */
    @SaCheckPermission("business:guest:add")
    @Log(title = "访客", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxGuestBo bo) {
        return toAjax(iWxGuestService.insertByBo(bo));
    }

    /**
     * 修改访客
     */
    @SaCheckPermission("business:guest:edit")
    @Log(title = "访客", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxGuestBo bo) {
        return toAjax(iWxGuestService.updateByBo(bo));
    }

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