package cn.iocoder.yudao.module.system.controller.admin.notice;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.group.NoticeGroupCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.group.NoticeGroupPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.group.NoticeGroupRespVO;
import cn.iocoder.yudao.module.system.controller.admin.notice.vo.group.NoticeGroupUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserRespVO;
import cn.iocoder.yudao.module.system.convert.notice.NoticeGroupConvert;
import cn.iocoder.yudao.module.system.convert.user.UserConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.notice.NoticeGroupDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import cn.iocoder.yudao.module.system.service.notice.NoticeGroupService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 用户组")
@RestController
@RequestMapping("/system/notice/user-group")
@Validated
public class NoticeGroupController {

    @Resource
    private NoticeGroupService userGroupService;

    @Resource
    private AdminUserService userService;
    @Resource
    private DeptService deptService;

    @PostMapping("/create")
    @Operation(summary = "创建用户组")
    @PreAuthorize("@ss.hasPermission('system:notice-gropu:create')")
    public CommonResult<Long> createUserGroup(@Valid @RequestBody NoticeGroupCreateReqVO createReqVO) {
        return success(userGroupService.createUserGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户组")
    @PreAuthorize("@ss.hasPermission('system:notice-gropu:update')")
    public CommonResult<Boolean> updateUserGroup(@Valid @RequestBody NoticeGroupUpdateReqVO updateReqVO) {
        userGroupService.updateUserGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:notice-gropu:delete')")
    public CommonResult<Boolean> deleteUserGroup(@RequestParam("id") Long id) {
        userGroupService.deleteUserGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:notice-gropu:query')")
    public CommonResult<NoticeGroupRespVO> getUserGroup(@RequestParam("id") Long id) {

        NoticeGroupDO userGroup = userGroupService.getUserGroup(id);

        //获得用户信息
        List<AdminUserDO> userDOS = userService.getUserList(userGroup.getMemberUserIds());
        //获得部门信息 map
        Map<Long, DeptDO> deptMap = deptService.getDeptMap(convertList(userDOS, AdminUserDO::getDeptId));

        List<UserRespVO> userRespVOS = convertList(userDOS, user -> UserConvert.INSTANCE.convert(user, deptMap.get(user.getDeptId())));

        NoticeGroupRespVO noticeGroupRespVO = NoticeGroupConvert.INSTANCE.convert(userGroup);
        noticeGroupRespVO.setUserInfo(userRespVOS);
        noticeGroupRespVO.setMemberDeptIds(deptMap.keySet());

        return success(noticeGroupRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户组分页")
    @PreAuthorize("@ss.hasPermission('system:notice-gropu:query')")
    public CommonResult<PageResult<NoticeGroupRespVO>> getUserGroupPage(@Valid NoticeGroupPageReqVO pageVO) {
        PageResult<NoticeGroupDO> pageResult = userGroupService.getUserGroupPage(pageVO);
        return success(NoticeGroupConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取用户组精简信息列表", description = "只包含被开启的用户组，主要用于前端的下拉选项")
    public CommonResult<List<NoticeGroupRespVO>> getSimpleUserGroups() {
        // 获用户门列表，只要开启状态的
        List<NoticeGroupDO> list = userGroupService.getUserGroupListByStatus(CommonStatusEnum.ENABLE.getStatus());
        // 排序后，返回给前端
        return success(NoticeGroupConvert.INSTANCE.convertList2(list));
    }
}
