package cn.iocoder.yudao.module.bpm.controller.admin.meeting.room;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.meeting.room.vo.OaMeetingRoomPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.meeting.room.vo.OaMeetingRoomRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.meeting.room.vo.OaMeetingRoomSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.meeting.room.OaMeetingRoomDO;
import cn.iocoder.yudao.module.bpm.service.meeting.room.OaMeetingRoomService;
import cn.iocoder.yudao.module.employee.api.EmployeeApi;
import cn.iocoder.yudao.module.employee.api.dto.EmployeeRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import liquibase.pro.packaged.R;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 会议室信息管理")
@RestController
@RequestMapping("/bpm/oa-meeting-room")
@Validated
public class OaMeetingRoomController {

    @Resource
    private OaMeetingRoomService oaMeetingRoomService;

    @Resource
    private EmployeeApi employeeApi;

    @PostMapping("/create")
    @Operation(summary = "创建会议室信息管理")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-meeting-room:create')")
    public CommonResult<Long> createOaMeetingRoom(@Valid @RequestBody OaMeetingRoomSaveReqVO createReqVO) {
        return success(oaMeetingRoomService.createOaMeetingRoom(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会议室信息管理")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-meeting-room:update')")
    public CommonResult<Boolean> updateOaMeetingRoom(@Valid @RequestBody OaMeetingRoomSaveReqVO updateReqVO) {
        oaMeetingRoomService.updateOaMeetingRoom(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会议室信息管理")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-meeting-room:delete')")
    public CommonResult<Boolean> deleteOaMeetingRoom(@RequestParam("id") Long id) {
        oaMeetingRoomService.deleteOaMeetingRoom(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会议室信息管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-meeting-room:query')")
    public CommonResult<OaMeetingRoomRespVO> getOaMeetingRoom(@RequestParam("id") Long id) {
        OaMeetingRoomDO oaMeetingRoom = oaMeetingRoomService.getOaMeetingRoom(id);
        OaMeetingRoomRespVO oaMeetingRoomRespVO = BeanUtils.toBean(oaMeetingRoom, OaMeetingRoomRespVO.class);

        // 设置管理员名称
        if (oaMeetingRoomRespVO != null) {
            EmployeeRespDTO employee = employeeApi.getEmployeeById(oaMeetingRoomRespVO.getManagerId());
            if (employee != null) {
                oaMeetingRoomRespVO.setManagerName(employee.getName());
            }
        }
        return success(oaMeetingRoomRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得会议室信息管理分页")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-meeting-room:query')")
    public CommonResult<PageResult<OaMeetingRoomRespVO>> getOaMeetingRoomPage(@Valid @ParameterObject OaMeetingRoomPageReqVO pageReqVO) {
        PageResult<OaMeetingRoomDO> pageResult = oaMeetingRoomService.getOaMeetingRoomPage(pageReqVO);
        PageResult<OaMeetingRoomRespVO> voPageResult = BeanUtils.toBean(pageResult, OaMeetingRoomRespVO.class);

        // 设置管理员名称
        List<OaMeetingRoomRespVO> list = voPageResult.getList();
        if (CollUtil.isNotEmpty(list)) {
            List<Long> managerIdList = list.stream().map(OaMeetingRoomRespVO::getManagerId).collect(Collectors.toList());
            List<EmployeeRespDTO> employeeList = employeeApi.getEmployeeListByIds(managerIdList);
            Map<Long, EmployeeRespDTO> employeeManagerMap = employeeList.stream().collect(Collectors.toMap(EmployeeRespDTO::getId, Function.identity()));

            for (OaMeetingRoomRespVO oaMeetingRoomRespVO : list) {
                EmployeeRespDTO employeeRespDTO = employeeManagerMap.get(oaMeetingRoomRespVO.getManagerId());
                if (employeeRespDTO != null) {
                    oaMeetingRoomRespVO.setManagerName(employeeRespDTO.getName());
                }
            }

        }
        return success(voPageResult);
    }

    @GetMapping("/list")
    @Operation(summary = "获得会议室信息管理列表")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-meeting-room:list')")
    public CommonResult<List<OaMeetingRoomRespVO>> getOaMeetingRoomList() {
        List<OaMeetingRoomDO> listResult = oaMeetingRoomService.getOaMeetingRoomList();
        return success(BeanUtils.toBean(listResult, OaMeetingRoomRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会议室信息管理 Excel")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-meeting-room:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOaMeetingRoomExcel(@ParameterObject OaMeetingRoomPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OaMeetingRoomDO> list = oaMeetingRoomService.getOaMeetingRoomPage(pageReqVO).getList();
        List<OaMeetingRoomRespVO> respVOList = BeanUtils.toBean(list, OaMeetingRoomRespVO.class);
        if (CollUtil.isNotEmpty(respVOList)) {
            List<Long> managerIdList = respVOList.stream().map(OaMeetingRoomRespVO::getManagerId).collect(Collectors.toList());
            Map<Long, EmployeeRespDTO> employeeManagerMap = employeeApi.getEmployeeListByIds(managerIdList)
                    .stream()
                    .collect(Collectors.toMap(EmployeeRespDTO::getId, Function.identity()));
            for (OaMeetingRoomRespVO oaMeetingRoomRespVO : respVOList) {
                EmployeeRespDTO employeeRespDTO = employeeManagerMap.get(oaMeetingRoomRespVO.getManagerId());
                if (employeeRespDTO != null) {
                    oaMeetingRoomRespVO.setManagerName(employeeRespDTO.getName());
                }
            }

        }
        // 导出 Excel
        ExcelUtils.write(response, "会议室信息管理.xls", "数据", OaMeetingRoomRespVO.class,
                        respVOList);
    }

}