package org.dromara.system.controller.system;

import cn.hutool.crypto.digest.BCrypt;
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.EditGroup;
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.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysEmployeeBo;
import org.dromara.system.domain.vo.SysEmployeeVo;
import org.dromara.system.service.ISysEmployeeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 员工管理
 *
 * @author Lion Li
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/employee")
public class SysEmployeeController extends BaseController {

    private final ISysEmployeeService sysEmployeeService;

    /**
     * 获取当前登录员工信息
     */
    @GetMapping("/getInfo")
    public R<SysEmployeeVo> getMyInfo() {
        Long employeeId = LoginHelper.getUserId();
        return R.ok(sysEmployeeService.queryDetail(employeeId));
    }

    /**
     * 更新当前登录员工信息
     */
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateProfile")
    public R<Void> updateMyInfo(@RequestBody SysEmployeeBo bo) {
        Long employeeId = LoginHelper.getUserId();
        bo.setId(employeeId);
        return toAjax(sysEmployeeService.updateByBo(bo));
    }

    /**
     * 查询员工列表
     */
    @GetMapping("/list")
    public TableDataInfo<SysEmployeeVo> list(SysEmployeeBo bo, PageQuery pageQuery) {
        return sysEmployeeService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取员工详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<SysEmployeeVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(sysEmployeeService.queryById(id));
    }

    /**
     * 获取员工详细信息（含授权客户详情列表）
     */
    @GetMapping("/detail/{id}")
    public R<SysEmployeeVo> getDetail(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(sysEmployeeService.queryDetail(id));
    }

    /**
     * 新增员工
     */
    @Log(title = "员工管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody SysEmployeeBo bo) {
        bo.setPassword(BCrypt.hashpw(bo.getPassword()));
        return toAjax(sysEmployeeService.insertByBo(bo));
    }

    /**
     * 修改员工
     */
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysEmployeeBo bo) {
        return toAjax(sysEmployeeService.updateByBo(bo));
    }

    /**
     * 删除员工
     *
     * @param ids 主键串
     */
    @Log(title = "员工管理", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable List<Long> ids) {
        return toAjax(sysEmployeeService.deleteWithValidByIds(ids, true));
    }

    /**
     * 授权客户（支持多个，逗号分隔）
     *
     * @param id                 员工ID
     * @param authClientFRowIDs  授权客户 RowID 列表
     */
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/auth")
    public R<Void> auth(@NotNull(message = "员工ID不能为空") Long id,
                        @NotEmpty(message = "授权客户 RowID 不能为空") String authClientFRowIDs) {
        sysEmployeeService.checkAuthClientConflict(id, authClientFRowIDs);
        SysEmployeeBo bo = new SysEmployeeBo();
        bo.setId(id);
        bo.setAuthClientFRowIDs(authClientFRowIDs);
        return toAjax(sysEmployeeService.updateByBo(bo));
    }

    /**
     * 修改员工状态（启用/禁用）
     */
    @Log(title = "员工管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@NotNull(message = "员工ID不能为空") Long id,
                                @NotNull(message = "状态不能为空") String status) {
        return toAjax(sysEmployeeService.changeStatus(id, status));
    }
}
