package org.dromara.controller.staffManage;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
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.staffManage.bo.StaffManageBo;
import org.dromara.domain.staffManage.vo.StaffManageVo;
import org.dromara.service.IStaffManageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工管理
 *
 * @author boman
 * @date 2025-04-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wuYe/staffManage")
public class StaffManageController extends BaseController {

    private final IStaffManageService staffManageService;

    /**
     * 查询员工管理列表
     */
    @SaCheckPermission("wuYe:staffManage:list")
    @GetMapping("/list")
    public TableDataInfo<StaffManageVo> list(StaffManageBo bo, PageQuery pageQuery) {
        return staffManageService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询员工管理列表不分页
     */
    @SaCheckPermission("wuYe:staffManage:list")
    @GetMapping("/listNoPage")
    public TableDataInfo<StaffManageVo> listNoPage(StaffManageBo staffManage) {
        List<StaffManageVo> list = staffManageService.queryList(staffManage);
        return TableDataInfo.build(list);
    }

    /**
     * 查询员工管理需要认证的列表
     */
    //@SaCheckPermission("wuYe:staffManage:listAuthentication")
    @GetMapping("/listAuthentication")
    public TableDataInfo<StaffManageVo> listAuthentication(StaffManageBo bo, PageQuery pageQuery) {
        return staffManageService.listAuthentication(bo, pageQuery);
    }

    /**
     * 导出员工管理列表
     */
    @SaCheckPermission("wuYe:staffManage:export")
    @Log(title = "员工管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StaffManageBo bo, HttpServletResponse response) {
        List<StaffManageVo> list = staffManageService.queryList(bo);
        ExcelUtil.exportExcel(list, "员工管理", StaffManageVo.class, response);
    }

    /**
     * 获取员工管理详细信息
     *
     * @param staffId 主键
     */
    @SaCheckPermission("wuYe:staffManage:query")
    @GetMapping("/{staffId}")
    public R<StaffManageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long staffId) {
        return R.ok(staffManageService.queryById(staffId));
    }


    /**
     * 获取员工管理详细信息 过滤租户
     *
     * @param staffId 主键
     */
    //@SaCheckPermission("wuYe:staffManage:query")
    @GetMapping("/authentication/{staffId}")
    public R<StaffManageVo> getAuthenticationInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long staffId) {
        return R.ok(staffManageService.getAuthenticationInfo(staffId));
    }

    /**
     * 获取员工管理详细信息
     *
     * @param userId 用户id
     */
    @SaIgnore
    @GetMapping("/getInfoByUserId/{userId}")
    public R<StaffManageVo> getInfoByUserId(@NotNull(message = "主键不能为空")
                                    @PathVariable Long userId) {
        return R.ok(staffManageService.getInfoByUserId(userId));
    }

    /**
     * 新增员工管理
     */
    //@SaCheckPermission("wuYe:staffManage:add")
    @SaIgnore
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StaffManageBo bo) {
        return toAjax(staffManageService.insertByBo(bo));
    }


    /**
     * 管理员新增员工信息默认认证
     */
    @SaCheckPermission("wuYe:staffManage:authentication")
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/authentication")
    public R<Void> authentication(@Validated(AddGroup.class) @RequestBody StaffManageBo bo) {
        return toAjax(staffManageService.insertAuthenticationByBo(bo));
    }

    /**
     * 修改员工管理
     */
    //@SaCheckPermission("wuYe:staffManage:edit")
    @SaIgnore
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StaffManageBo bo) {
        return toAjax(staffManageService.updateByBo(bo));
    }

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

    /**
     * 删除员工管理
     *
     * @param staffIds 主键串
     */
    @SaCheckPermission("wuYe:staffManage:remove")
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{staffIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] staffIds) {
        return toAjax(staffManageService.deleteWithValidByIds(List.of(staffIds), true));
    }


    /**
     * 删除员工管理
     *
     * @param bo 主键串
     */
    @SaCheckPermission("wuYe:staffManage:deleteStaffManage")
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
    @PostMapping("/deleteStaffManage")
    public R<Void> deleteStaffManage(@RequestBody StaffManageBo bo) {
        return toAjax(staffManageService.deleteStaffManage(bo));
    }

    /**
     * 员工认证审核
     *
     * @param bo 员工信息
     */
    @PostMapping("/examineStaff")
    @SaCheckPermission("wuYe:staffManage:examineStaff")
    public R<Void> examineStaff(@RequestBody StaffManageBo bo) {
        return toAjax(staffManageService.examineStaff(bo));
    }
}
