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

import com.cpop.core.base.entity.LoginUser;
import com.cpop.core.base.enums.InitRoleEnum;
import com.cpop.core.utils.SecurityUtils;
import com.cpop.system.business.service.RoleService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.cpop.common.constant.Constants;
import com.cpop.core.annontation.OperationLog;
import com.cpop.core.base.R;
import com.cpop.core.base.enums.OperationLogEnum;
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.StaffService;
import com.cpop.oam.business.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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


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

    @Autowired
    private StaffService staffService;

    @Autowired
    private DeptService deptService;

    /**
     * @descriptions 查询员工分页列表
     * @author DB
     * @date 2023/09/07 18:07
     * @param bo 请求参数
     * @return R<Page<SysStaffPageVo>>
     */
    @PreAuthorize("@aps.hasPermission('system:account:list')")
    @ApiOperation("查询员工分页列表")
    @GetMapping("/getStaffPageList")
    public R<Page<StaffPageVo>> getStaffPageList(StaffPageBo bo) {
        Page<StaffPageVo> pageVo = staffService.getStaffPageList(bo);
        return R.ok(pageVo);
    }

    /**
     * @descriptions 新增员工
     * @author DB
     * @date 2023/09/08 14:04
     * @param bo 请求参数
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @PreAuthorize("@aps.hasPermission('system:account:insert')")
    @OperationLog(operationLogEnumType= OperationLogEnum.INSERT_OAM_STAFF)
    @ApiOperation("新增员工")
    @PostMapping("/insertStaff")
    public R<Void> insertStaff(@RequestBody @Validated StaffBo bo) {
        staffService.insertStaff(bo);
        return R.ok();
    }

    /**
     * @descriptions 修改员工
     * @author DB
     * @date 2023/09/08 15:20
     * @param bo 请求参数
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @PreAuthorize("@aps.hasPermission('system:account:update')")
    @OperationLog(operationLogEnumType= OperationLogEnum.UPDATE_OAM_STAFF)
    @ApiOperation("修改员工")
    @PutMapping("/updateStaff")
    public R<Void> updateStaff(@RequestBody @Validated StaffBo bo) {
        staffService.updateStaff(bo);
        return R.ok();
    }

    /**
     * @descriptions 删除员工
     * @author DB
     * @date 2023/09/12 16:57
     * @param id 主键
     * @return com.jambox.core.base.R<java.lang.Void>
     */
    @PreAuthorize("@aps.hasPermission('system:account:remove')")
    @OperationLog(operationLogEnumType= OperationLogEnum.REMOVE_OAM_STAFF)
    @ApiOperation("删除员工")
    @DeleteMapping("/removeStaffById/{id}")
    public R<Void> removeStaffById(@PathVariable String id) {
        staffService.removeStaffById(id);
        return R.ok();
    }

    /**
     * @descriptions 查询部门树列表
     * @author DB
     * @date 2023/09/08 18:19
     * @param bo 请求参数
     * @return com.jambox.core.base.R<List<DeptVo>>
     */
    @ApiOperation("查询部门树列表")
    @GetMapping("/getDeptTreeList")
    public R<List<DeptVo>> getDeptTreeList(DeptListBo bo) {
        List<DeptVo> list = deptService.getDeptTreeList(bo);
        return R.ok(list);
    }
    /**
     * 获取所有角色信息
     */
    @ApiOperation("获取所有角色信息")
    @GetMapping("/getAllRoleList")
    public R<List<RoleVo>> getAllSysRoleList() {
        LoginUser loginUser = SecurityUtils.getInstance().getLoginUser();
        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(loginUser.getUserType()))
                        .orderBy(ROLE.ORDER_NO.asc())
                , RoleVo.class);
        return R.ok(list);
    }

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

    /**
     * @Description: 获取员工信息
     * @param id
     * @return: R<StaffVo>
     * @Author: DB
     * @Date: 2023/5/17 10:33
     **/
    @PreAuthorize("@aps.hasPermission('system:account:info')")
    @ApiOperation("获取员工信息")
    @GetMapping("/getStaffInfo/{id}")
    public R<StaffInfoVo> getStaffInfo(@PathVariable String id) {
        StaffInfoVo staffInfo = staffService.getStaffInfo(id);
        return R.ok(staffInfo);
    }

    /**
     * @Description: 获取用户操作日志分页
     * @param bo 请求参数
     * @return: R<PageVo<SysOperationLogVo>>
     * @Author: DB
     * @Date: 2023/5/12 14:30
     **/
    @PreAuthorize("@aps.hasPermission('system:account:list')")
    @ApiOperation("获取用户操作日志分页")
    @GetMapping("/getUserLogList")
    public R<Page<SysOperationLogVo>> getUserLogList(@Validated SysUserLogBo bo) {
        Page<SysOperationLogVo> list = staffService.getUserLogList(bo);
        return R.ok(list);
    }

    /**
     * @Description: 修改系统用户密码
     * @param bo 请求参数
     * @return: AjaxResult<Void>
     * @Author: DB
     * @Date: 2023/5/12 16:00
     **/
    @PreAuthorize("@aps.hasPermission('system:account:update')")
    @OperationLog(operationLogEnumType= OperationLogEnum.MODIFY_OAM_STAFF_PASSWORD)
    @ApiOperation("修改系统用户密码")
    @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>
     */
    @ApiOperation("修改运维状态")
    @PutMapping("/changeOperationStatus")
    public R<Void> changeOperationStatus(@Validated @RequestBody OperationStatusBo bo) {
        staffService.changeOperationStatus(bo);
        return R.ok();
    }

}
