package jnpf.attendance.controller;

import jnpf.attendance.service.AttendanceApprovalSettingService;
import jnpf.attendance.service.AttendanceSuperAdminService;
import jnpf.base.ActionResult;
import jnpf.model.attendance.dto.*;
import jnpf.model.attendance.vo.AttendanceManagerDetailVo;
import jnpf.model.attendance.vo.AttendanceUserVo;
import jnpf.model.attendance.vo.CurUserPermissionVo;
import jnpf.model.attendance.vo.permission.ActionPermissionVo;
import jnpf.model.attendance.vo.permission.ApprovalSettingVo;
import jnpf.model.attendance.vo.permission.AttendanceTeamSetVo;
import jnpf.model.attendance.vo.permission.app.ManagerPermissionVo;
import jnpf.model.authority.vo.role.FtbPermissionPositionMenuVO;
import jnpf.util.ParamUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * APP全局设置
 * @author yier
 * @Time 2023-11-23
 */
@RestController
@RequestMapping("/permission")
public class AttendanceSuperAdminController {

    @Resource
    private AttendanceSuperAdminService attendanceSuperAdminService;
    @Resource
    private AttendanceApprovalSettingService attendanceApprovalSettingService;


    /**
     * 添加考勤组超级管理员
     * @param saveSuperAdminDto 保存信息
     * @return Void
     * @throws Exception 抛出异常
     */
    @PostMapping
    public ActionResult<Void> add(@RequestBody SaveSuperAdminDto saveSuperAdminDto) throws Exception {
        ParamUtil.checkParam(saveSuperAdminDto);
        attendanceSuperAdminService.add(saveSuperAdminDto);
        return ActionResult.success();
    }

    /**
     * 删除超级管理员
     * @param userIds 用户id
     * @return ActionResult<Void>
     */
    @DeleteMapping
    public ActionResult<Void> delete(@RequestParam List<String> userIds) {
        attendanceSuperAdminService.delete(userIds);
        return ActionResult.success();
    }

    /**
     * 获取考勤超级管理员
     * @return ActionResult<List<UserEntity>>
     */
    @GetMapping
    public ActionResult<List<AttendanceUserVo>> getSuperAdmin(String name) {
        return ActionResult.success(attendanceSuperAdminService.querySuperAdmin(name));
    }

    /**
     * 添加考勤组管理员
     * @param saveGroupAdmin
     * @return ActionResult<Void>
     */
    @PostMapping("/addGroupAdmin")
    public ActionResult<Void> addGroupAdmin(@RequestBody SaveGroupAdmin saveGroupAdmin) throws Exception {
        ParamUtil.checkParam(saveGroupAdmin);
        attendanceSuperAdminService.addGroupAdmin(saveGroupAdmin);
        return ActionResult.success();
    }

    /**
     * 批量添加考勤组管理员
     * @param groupAdminList
     * @return ActionResult<Void>
     */
    @PostMapping("/batchAddGroupAdmin")
    public ActionResult<Void> batchAddGroupAdmin(@RequestBody BatchSaveGroupAdmin groupAdminList) throws Exception {
        ParamUtil.checkParam(groupAdminList);
        attendanceSuperAdminService.batchAddGroupAdmin(groupAdminList);
        return ActionResult.success();
    }


    /**
     * 修改考勤组管理员权限
     * @param saveGroupAdmin
     * @return
     * @throws Exception
     */
    @PutMapping("/updateGroupAdmin")
    public ActionResult<Void> updateGroupAdmin(@RequestBody SaveGroupAdmin saveGroupAdmin) throws Exception {
        ParamUtil.checkParam(saveGroupAdmin);
        attendanceSuperAdminService.updateGroupAdmin(saveGroupAdmin);
        return ActionResult.success();
    }

    /**
     * 删除考勤组管理员
     * @param saveGroupAdmin
     * @return ActionResult<Void>
     */
    @DeleteMapping("/deleteGroupAdmin")
    public ActionResult<Void> deleteGroupAdmin(@RequestBody SaveGroupAdmin saveGroupAdmin) {
        attendanceSuperAdminService.deleteGroupAdmin(saveGroupAdmin);
        return ActionResult.success();
    }

    /**
     * 查询考勤组管理员列表
     * @param groupId 考勤组id
     * @return ActionResult<List<AttendanceGroupAdminVo>>
     */
    @GetMapping("/listGroupAdmin")
    public ActionResult<Map<String, Object>> listGroupAdmin(String groupId) {
        Map<String, Object> result = attendanceSuperAdminService.listGroupAdmin(groupId);
        return ActionResult.success(result);
    }

    /**
     * 获取当前登录用户权限
     */
    @GetMapping("/getCurPermission")
    public ActionResult<CurUserPermissionVo> getCurPermission(String groupId) {
        CurUserPermissionVo curUserPermissionVo = attendanceSuperAdminService.getByUserId(groupId);
        return ActionResult.success(curUserPermissionVo);
    }

    /**
     * 修改审批设置
     * @param attendanceApprovalSettingDto
     * @return ActionResult<Void>
     */
    @PostMapping("/updateApprovalSetting")
    public ActionResult<Void> updateApprovalSetting(@RequestBody AttendanceApprovalSettingDto attendanceApprovalSettingDto) {
        attendanceApprovalSettingService.update(attendanceApprovalSettingDto);
        return ActionResult.success();
    }

    /**
     * 管理员权限详情
     * @param groupId
     * @param userId
     * @return ActionResult<List<PermissionDictVo>>
     */
    @GetMapping("/adminDetail")
    public ActionResult<AttendanceManagerDetailVo> adminDetail(String groupId, String userId) {
        AttendanceManagerDetailVo dictVos = attendanceSuperAdminService.adminDetail(groupId, userId);
        return ActionResult.success(dictVos);
    }

    /**
     * 是否有查看权限
     * @return ActionResult
     */
    @GetMapping("/isView")
    public ActionResult<Boolean> isView(String groupId) {
        Boolean viewPermission = attendanceSuperAdminService.isViewPermission(groupId);
        return ActionResult.success(viewPermission);
    }

    /**
     * 获取操作权限
     * @return ActionResult<ActionPermissionVo>
     */
    @GetMapping("/actionPermission")
    public ActionResult<ActionPermissionVo> actionPermission(String groupId) {
        ActionPermissionVo actionPermission = attendanceSuperAdminService.getActionPermission(groupId);
        return ActionResult.success(actionPermission);
    }

    /**
     * 是否有全局设置权限
     * @return ActionResult<Boolean>
     */
    @GetMapping("/isGlobal")
    public ActionResult<Boolean> isGlobal() {
        Boolean globalSetting = attendanceSuperAdminService.isGlobalSetting();
        return ActionResult.success(globalSetting);
    }

    /**
     * 是否是考勤组管理员
     * @return ActionResult<Boolean>
     */
    @GetMapping("/isManager")
    public ActionResult<ManagerPermissionVo> isManager() {
        ManagerPermissionVo manager = attendanceSuperAdminService.isManager();
        return ActionResult.success(manager);
    }

    /**
     * 获取考勤组审批设置
     * @return ActionResult<ApprovalSettingVo>
     */
    @GetMapping("/getGroupApprovalPermission")
    public ActionResult<ApprovalSettingVo> getGroupApprovalPermission(@RequestParam("groupId") String groupId,
                                                                      @RequestParam("type") Integer type) {
        ApprovalSettingVo approvalSettingInfo = attendanceSuperAdminService.getApprovalSettingInfo(groupId, type);
        return ActionResult.success(approvalSettingInfo);
    }

    /**
     * 根据用户id获取所属权限集合
     * @param userIds 用户id
     * @return FtbPermissionPositionMenuInnerVO
     */
    @PostMapping("/queryPermissionListByUserIds")
    public ActionResult<List<FtbPermissionPositionMenuVO.FtbPermissionPositionMenuInnerVO>> queryPermissionListByUserIds(@RequestBody List<String> userIds) {
        List<FtbPermissionPositionMenuVO.FtbPermissionPositionMenuInnerVO> voList = attendanceSuperAdminService.queryPermissionListByUserIds(userIds);
        return ActionResult.success(voList);
    }

    /**
     * 获取考勤组团队设置
     * @return ActionResult<AttendanceTeamSetVo>
     */
    @GetMapping("/getTeamSet")
    public ActionResult<AttendanceTeamSetVo> getTeamSet(@Valid GroupFilterDto dto) {
        return ActionResult.success(attendanceSuperAdminService.getTeamSet(dto));
    }

    /**
     * 考勤组月报通知开启/关闭
     * @return ActionResult<Boolean>
     */
    @PutMapping("/setMonthNotice")
    public ActionResult<Boolean> setMonthNotice(@Valid @RequestBody GroupFilterDto dto) {
        return ActionResult.success(attendanceSuperAdminService.setMonthNotice(dto));
    }
}
