package com.hotwj.platform.resourceManagement.companySafetyManager.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hotwj.platform.resourceManagement.companySafetyManager.domain.bo.HotCompanySafetyManagerBo;
import com.hotwj.platform.resourceManagement.companySafetyManager.domain.bo.HotCompanySafetyManagerChangeReasonBo;
import com.hotwj.platform.resourceManagement.companySafetyManager.domain.vo.HotCompanySafetyManagerVo;
import com.hotwj.platform.resourceManagement.companySafetyManager.service.IHotCompanySafetyManagerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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.core.validate.QueryGroup;
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.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 安全管理人员
 *
 * @author shihongwei
 * @date 2025-12-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/resourceManagement/companySafetyManager")
@Tag(name = "安全管理人员", description = "安全管理人员增删改查与同步")
public class HotCompanySafetyManagerController extends BaseController {

    private final IHotCompanySafetyManagerService hotCompanySafetyManagerService;

    /**
     * 查询安全管理人员列表
     */
    //@SaCheckPermission("resourceManagement:companySafetyManager:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询安全管理人员列表")
    public TableDataInfo<HotCompanySafetyManagerVo> list(HotCompanySafetyManagerBo bo, PageQuery pageQuery) {
        return hotCompanySafetyManagerService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询部门负责人列表
     */
    //@SaCheckPermission("resourceManagement:companySafetyManager:list")
    @GetMapping("/headList")
    @Operation(summary = "分页查询该企业下所有人员列表")
    public TableDataInfo<HotCompanySafetyManagerVo> headList(@Validated(QueryGroup.class) HotCompanySafetyManagerBo bo, PageQuery pageQuery) {
        return hotCompanySafetyManagerService.queryCompanyUserList(bo, pageQuery);
    }

    /**
     * 获取上级管理人员候选人列表
     */
    //@SaCheckPermission("resourceManagement:companySafetyManager:list")
    @GetMapping("/superiorCandidates")
    @Operation(summary = "获取上级管理人员候选人列表")
    public R<List<HotCompanySafetyManagerVo>> getSuperiorCandidates(@NotNull(message = "公司ID不能为空") @RequestParam Long companyId,
                                                                    @RequestParam(required = false) Long deptId,
                                                                    @RequestParam(required = false) Long currentId) {
        return R.ok(hotCompanySafetyManagerService.getSuperiorCandidates(companyId, deptId, currentId));
    }

    /**
     * 导出安全管理人员列表
     */
    //@SaCheckPermission("resourceManagement:companySafetyManager:export")
    @Log(title = "安全管理人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出安全管理人员列表")
    public void export(HotCompanySafetyManagerBo bo, HttpServletResponse response) {
        List<HotCompanySafetyManagerVo> list = hotCompanySafetyManagerService.queryList(bo);
        ExcelUtil.exportExcel(list, "安全管理人员", HotCompanySafetyManagerVo.class, response);
    }

    /**
     * 获取安全管理人员详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("resourceManagement:companySafetyManager:query")
    @GetMapping("/{id}")
    @Operation(summary = "查询安全管理人员详情")
    public R<HotCompanySafetyManagerVo> getInfo(@NotNull(message = "主键不能为空")
                                                @PathVariable Long id) {
        return R.ok(hotCompanySafetyManagerService.queryById(id));
    }

    /**
     * 新增安全管理人员
     */
    //@SaCheckPermission("resourceManagement:companySafetyManager:add")
    @Log(title = "安全管理人员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Operation(summary = "新增安全管理人员并同步系统用户")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HotCompanySafetyManagerBo bo) {
        return toAjax(hotCompanySafetyManagerService.insertByBo(bo));
    }

    /**
     * 修改安全管理人员
     */
    //@SaCheckPermission("resourceManagement:companySafetyManager:edit")
    @Log(title = "安全管理人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Operation(summary = "修改安全管理人员并同步系统用户")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HotCompanySafetyManagerChangeReasonBo bo) {
        return toAjax(hotCompanySafetyManagerService.updateByBo(bo));
    }

    //@SaCheckPermission("resourceManagement:companySafetyManager:resetPwd")
    @Log(title = "安全管理人员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/{id}/resetPwd")
    @Operation(summary = "重置安全管理人员登录密码")
    public R<Void> resetPwd(@PathVariable Long id) {
        return toAjax(hotCompanySafetyManagerService.resetPassword(id));
    }

    /**
     * 删除安全管理人员
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("resourceManagement:companySafetyManager:remove")
    @Log(title = "安全管理人员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除安全管理人员并禁用系统用户")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(hotCompanySafetyManagerService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 查询所有安全管理人员列表(不分页)
     *
     * @param companyId 企业ID
     */
    @SaCheckPermission("resourceManagement:companySafetyManager:list")
    @GetMapping("/allList")
    @Operation(summary = "查询所有安全管理人员列表(不分页)")
    public R<List<HotCompanySafetyManagerVo>> allList(@NotNull(message = "企业ID不能为空") @RequestParam Long companyId) {
        return R.ok(hotCompanySafetyManagerService.queryAllList(companyId));
    }

    /**
     * 查询有效安全管理人员列表(不分页，未离职、已启用、已通过审核)
     *
     * @param companyId 企业ID
     */
    @SaCheckPermission("resourceManagement:companySafetyManager:list")
    @GetMapping("/validList")
    @Operation(summary = "查询有效安全管理人员列表(不分页，未离职、已启用、已通过审核)")
    public R<List<HotCompanySafetyManagerVo>> validList(@NotNull(message = "企业ID不能为空") @RequestParam Long companyId) {
        return R.ok(hotCompanySafetyManagerService.queryValidList(companyId));
    }
}
