package org.dromara.controller.residentInfo;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.domain.residentInfo.bo.ResidentInfoBo;
import org.dromara.domain.residentInfo.vo.ResidentInfoVo;
import org.dromara.service.IResidentInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 居住人员信息，存储居住人员的详细信息
 *
 * @author boman
 * @date 2025-04-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wuYe/residentInfo")
public class ResidentInfoController extends BaseController {

    private final IResidentInfoService residentInfoService;

    /**
     * 查询居住人员信息，存储居住人员的详细信息列表
     */
    @SaCheckPermission("wuYe:residentInfo:list")
    @GetMapping("/list")
    public TableDataInfo<ResidentInfoVo> list(ResidentInfoBo bo, PageQuery pageQuery) {
        return residentInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询居住人员需要认证的列表 给居住人员在未认证通过的情况下调用
     */
    //@SaCheckPermission("wuYe:staffManage:listAuthentication")
    @GetMapping("/listAuthentication")
    public TableDataInfo<ResidentInfoVo> listAuthentication(ResidentInfoBo bo, PageQuery pageQuery) {
        return residentInfoService.listAuthentication(bo, pageQuery);
    }

    /**
     * 查询居住人员党员数量
     */
    @SaCheckPermission("wuYe:residentInfo:count")
    @GetMapping("/count")
    public R<Long> count(ResidentInfoBo residentInfo) {
        return residentInfoService.selectCount(residentInfo);

    }

    /**
     * 导出居住人员信息，存储居住人员的详细信息列表
     */
    @SaCheckPermission("wuYe:residentInfo:export")
    @Log(title = "居住人员信息，存储居住人员的详细信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ResidentInfoBo bo, HttpServletResponse response) {
        List<ResidentInfoVo> list = residentInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "居住人员信息，存储居住人员的详细信息", ResidentInfoVo.class, response);
    }

    /**
     * 获取居住人员信息，存储居住人员的详细信息详细信息
     *
     * @param residentId 主键
     */
    @SaCheckPermission("wuYe:residentInfo:query")
    @GetMapping("/{residentId}")
    public R<ResidentInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long residentId) {
        return R.ok(residentInfoService.queryById(residentId));
    }

    /**
     *  获取居住人员信息 过滤租户
     *
     * @param residentId 主键
     */
    //@SaCheckPermission("wuYe:staffManage:query")
    @GetMapping("/authentication/{residentId}")
    public R<ResidentInfoVo> getAuthenticationInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable Long residentId) {
        return R.ok(residentInfoService.getAuthenticationInfo(residentId));
    }

    /**
     * 管理员新增居住人员信息，默认就审核过
     */
    @SaCheckPermission("wuYe:residentInfo:addResidentInfo")
    @Log(title = "居住人员信息，存储居住人员的详细信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addResidentInfo")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ResidentInfoBo bo) {
        return toAjax(residentInfoService.addResidentInfo(bo));
    }

    /**
     * 新增居住人员提交认证信息
     */
    //@SaCheckPermission("wuYe:residentInfo:add")
    @Log(title = "新增居住人员提交认证信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> addResidentInfo(@Validated(AddGroup.class) @RequestBody ResidentInfoBo bo) {
        return toAjax(residentInfoService.insertByBo(bo));
    }

    /**
     * 修改居住人员信息，存储居住人员的详细信息
     */
    @SaCheckPermission("wuYe:residentInfo:edit")
    @Log(title = "居住人员信息，存储居住人员的详细信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ResidentInfoBo bo) {
        return toAjax(residentInfoService.updateByBo(bo));
    }


    /**
     * 管理员修改住户管理
     */
    @SaCheckPermission("wuYe:residentInfo:editAuthentication")
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put/authentication")
    public R<Void> editAuthentication(@Validated(EditGroup.class) @RequestBody ResidentInfoBo bo) {
        return toAjax(residentInfoService.editAuthentication(bo));
    }

    /**
     * 删除居住人员信息，存储居住人员的详细信息
     *
     * @param residentIds 主键串
     */
    @SaCheckPermission("wuYe:residentInfo:remove")
    @Log(title = "居住人员信息，存储居住人员的详细信息", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{residentIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] residentIds) {
        return toAjax(residentInfoService.deleteWithValidByIds(List.of(residentIds), true));
    }

    /**
     * 删除居住人员的认证信息
     *
     * @param bo 主键串
     */
    @SaCheckPermission("wuYe:residentInfo:deleteResident")
    @Log(title = "居住人员信息", businessType = BusinessType.DELETE)
    @PostMapping("/deleteResident")
    public R<Void> deleteStaffManage(@RequestBody ResidentInfoBo bo) {
        return toAjax(residentInfoService.deleteResident(bo));
    }
    /**
     * 居住用户认证审核
     *
     * @param bo 员工信息
     */
    @PostMapping("/examineResident")
    @SaCheckPermission("wuYe:residentInfo:examineStaff")
    public R<Void> examineResident(@RequestBody ResidentInfoBo bo) {
        return toAjax(residentInfoService.examineResident(bo));
    }
}
