package com.cpop.oam.business.controller.backstage;

import cn.dev33.satoken.stp.StpUtil;
import com.cpop.core.base.entity.R;
import com.cpop.core.utils.SpringUtils;
import com.cpop.oam.business.bo.*;
import com.cpop.oam.business.service.DeptService;
import com.cpop.oam.business.service.RoleService;
import com.cpop.oam.business.service.StaffService;
import com.cpop.oam.business.vo.*;
import com.cpop.oam.framework.enums.InitRoleEnum;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.cpop.oam.business.entity.table.RoleTableDef.ROLE;


/**
 * 员工表 控制层。
 *
 * @author DB
 * @since 2023-09-07
 */
@RestController
@Tag(name = "员工账号管理模块")
@RequestMapping("/backstage/staff")
public class BackstageStaffController {

    @Autowired
    private StaffService staffService;

    @Autowired
    private DeptService deptService;

    /**
     * 查询员工分页列表
     * @author DB
     * @since 2023/09/07 18:07
     * @param bo 请求参数
     * @return R<Page<SysStaffPageVo>>
     */
    @Operation(summary = "查询员工分页列表")
    @GetMapping("/getStaffPageList")
    public R<Page<StaffPageVo>> getStaffPageList(StaffPageBo bo) {
        Page<StaffPageVo> pageVo = staffService.getStaffPageList(bo);
        return R.ok(pageVo);
    }

    /**
     * 新增员工
     * @author DB
     * @since 2023/09/08 14:04
     * @param bo 请求参数
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @Operation(summary = "新增员工")
    @PostMapping("/insertStaff")
    public R<Void> insertStaff(@RequestBody @Validated StaffBo bo) {
        staffService.insertStaff(bo);
        return R.ok();
    }

    /**
     * 修改员工
     * @author DB
     * @since 2023/09/08 15:20
     * @param bo 请求参数
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @Operation(summary = "修改员工")
    @PutMapping("/updateStaff")
    public R<Void> updateStaff(@RequestBody @Validated StaffBo bo) {
        staffService.updateStaff(bo);
        return R.ok();
    }

    /**
     * 删除员工
     * @author DB
     * @since 2023/09/12 16:57
     * @param id 主键
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @Operation(summary = "删除员工")
    @DeleteMapping("/removeStaffById/{id}")
    public R<Void> removeStaffById(@PathVariable String id) {
        staffService.removeStaffById(id);
        return R.ok();
    }

    /**
     * 查询部门树列表
     * @author DB
     * @since 2023/09/08 18:19
     * @param bo 请求参数
     * @return com.jambox.core.base.R<List<DeptVo>>
     */
    @Operation(summary = "查询部门树列表")
    @GetMapping("/getDeptTreeList")
    public R<List<DeptVo>> getDeptTreeList(DeptListBo bo) {
        List<DeptVo> list = deptService.getDeptTreeList(bo);
        return R.ok(list);
    }
    /**
     * 获取所有角色信息
     */
    @Operation(summary = "获取所有角色信息")
    @GetMapping("/getAllRoleList")
    public R<List<RoleVo>> getAllSysRoleList() {
        List<RoleVo> list = SpringUtils.getBean(RoleService.class).listAs(QueryWrapper.create()
                        .where(ROLE.STATUS.eq(true))
                        .and(ROLE.ID.ne(InitRoleEnum.SUPER_OAM_ROLE.getId()))
                        .and(ROLE.USER_TYPE.eq(StpUtil.getSession().get("userType")))
                        .orderBy(ROLE.ORDER_NO.asc())
                , RoleVo.class);
        return R.ok(list);
    }

    /**
     * 用户名是否存在
     * @param username 用户名
     * @param id 主键
     * @return R<Void>
     * @author DB
     * @since 2023/5/11 10:09
     **/
    @Operation(summary = "用户名是否存在")
    @GetMapping("/isAccountExist")
    public R<Void> isAccountExist(String username, String id) {
        staffService.isAccountExist(username, id);
        return R.ok();
    }

    /**
     * 获取员工信息
     * @param id 主键
     * @return R<StaffVo>
     * @author DB
     * @since 2023/5/17 10:33
     **/
    @Operation(summary = "获取员工信息")
    @GetMapping("/getStaffInfo/{id}")
    public R<StaffInfoVo> getStaffInfo(@PathVariable String id) {
        StaffInfoVo staffInfo = staffService.getStaffInfo(id);
        return R.ok(staffInfo);
    }

    /**
     * 获取用户操作日志分页
     * @param bo 请求参数
     * @return R<PageVo<SysOperationLogVo>>
     * @author DB
     * @since 2023/5/12 14:30
     **/
    @Operation(summary = "获取用户操作日志分页")
    @GetMapping("/getUserLogList")
    public R<Page<OperationLogVo>> getUserLogList(@Validated UserLogBo bo) {
        Page<OperationLogVo> list = staffService.getUserLogList(bo);
        return R.ok(list);
    }

    /**
     * 修改系统用户密码
     * @param bo 请求参数
     * @return AjaxResult<Void>
     * @author DB
     * @since 2023/5/12 16:00
     **/
    @Operation(summary = "修改系统用户密码")
    @PutMapping("/modifyUserPassword")
    public R<Void> modifyUserPassword(@Validated @RequestBody ModifyUserPasswordBo bo) {
        staffService.modifyUserPassword(bo);
        return R.ok();
    }

    /**
     * 修改运维状态
     * @author DB
     * @since 2023/12/07
     * @param bo 请求参数
     * @return R<Void>
     */
    @Operation(summary = "修改运维状态")
    @PutMapping("/changeOperationStatus")
    public R<Void> changeOperationStatus(@Validated @RequestBody OperationStatusBo bo) {
        staffService.changeOperationStatus(bo);
        return R.ok();
    }

}
