package com.yc.ship.module.resource.controller.admin.room;

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.module.resource.controller.admin.room.vo.ResourceRoomBatchSaveReqVO;
import com.yc.ship.module.resource.controller.admin.room.vo.ResourceRoomPageReqVO;
import com.yc.ship.module.resource.controller.admin.room.vo.ResourceRoomRespVO;
import com.yc.ship.module.resource.controller.admin.room.vo.ResourceRoomSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.room.ResourceRoomDO;
import com.yc.ship.module.resource.dal.dataobject.roommodel.ResourceRoomModelDO;
import com.yc.ship.module.resource.dal.dataobject.ship.ResourceShipDO;
import com.yc.ship.module.resource.service.room.ResourceRoomService;
import com.yc.ship.module.resource.service.roommodel.ResourceRoomModelService;
import com.yc.ship.module.resource.service.ship.ResourceShipService;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.yc.ship.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 资源管理-游轮房间管理")
@RestController
@RequestMapping("/resource/room")
@Validated
public class ResourceRoomController {

    @Resource
    private ResourceRoomService roomService;

    @Resource
    private ResourceShipService shipService;

    @Resource
    private ResourceRoomModelService modelService;

    @PostMapping("/create")
    @Operation(summary = "创建资源管理-游轮房间管理")
    @PreAuthorize("@ss.hasPermission('resource:room:create')")
    public CommonResult<Long> createRoom(@Valid @RequestBody ResourceRoomSaveReqVO createReqVO) {
        return success(roomService.createRoom(createReqVO));
    }

    @PostMapping("/createBatch")
    @Operation(summary = "批量创建资源管理-游轮房间管理")
    @PreAuthorize("@ss.hasPermission('resource:room:createBatch')")
    public CommonResult<Boolean> createBatchRoom(@Valid @RequestBody ResourceRoomBatchSaveReqVO createReqVO) {
        return success(roomService.createBatchRoom(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源管理-游轮房间管理")
    @PreAuthorize("@ss.hasPermission('resource:room:update')")
    public CommonResult<Boolean> updateRoom(@Valid @RequestBody ResourceRoomSaveReqVO updateReqVO) {
        roomService.updateRoom(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源管理-游轮房间管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('resource:room:delete')")
    public CommonResult<Boolean> deleteRoom(@RequestParam("id") Long id) {
        roomService.deleteRoom(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源管理-游轮房间管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:room:query')")
    public CommonResult<ResourceRoomRespVO> getRoom(@RequestParam("id") Long id) {
        ResourceRoomDO room = roomService.getRoom(id);
        return success(BeanUtils.toBean(room, ResourceRoomRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源管理-游轮房间管理分页")
    @PreAuthorize("@ss.hasPermission('resource:room:query')")
    public CommonResult<PageResult<ResourceRoomRespVO>> getRoomPage(@Valid ResourceRoomPageReqVO pageReqVO) {
        PageResult<ResourceRoomDO> pageResult = roomService.getRoomPage(pageReqVO);
        PageResult<ResourceRoomRespVO> page = BeanUtils.toBean(pageResult, ResourceRoomRespVO.class);
        List<ResourceRoomRespVO> list = page.getList();
        //处理游轮名称
        List<Long> shipIds = CollectionUtils.convertList(list, ResourceRoomRespVO::getShipId);
        List<ResourceShipDO> shipList = shipService.getList(shipIds);
        Map<Long, ResourceShipDO> longResourceShipDOMap = CollectionUtils.convertMap(shipList, ResourceShipDO::getId);

        //处理房型名称
        List<Long> modelIds = CollectionUtils.convertList(list, ResourceRoomRespVO::getRoomModelId);
        List<ResourceRoomModelDO> modelList = modelService.getList(modelIds);
        Map<Long, ResourceRoomModelDO> longResourceRoomModelDOMap = CollectionUtils.convertMap(modelList, ResourceRoomModelDO::getId);
        list.stream().forEach(item -> {
            ResourceShipDO resourceShipDO = longResourceShipDOMap.get(item.getShipId());
            if(resourceShipDO != null) {
                item.setShipName(resourceShipDO.getName());
            }else {
                item.setShipName("已删除");
            }
            ResourceRoomModelDO resourceRoomModelDO = longResourceRoomModelDOMap.get(item.getRoomModelId());
            if(resourceRoomModelDO != null) {
                item.setRoomModelName(resourceRoomModelDO.getName());
            }else {
                item.setRoomModelName("已删除");
            }
        });
        return success(page);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源管理-游轮房间管理 Excel")
    @PreAuthorize("@ss.hasPermission('resource:room:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRoomExcel(@Valid ResourceRoomPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceRoomDO> list = roomService.getRoomPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源管理-游轮房间管理.xls", "数据", ResourceRoomRespVO.class,
                        BeanUtils.toBean(list, ResourceRoomRespVO.class));
    }

    @GetMapping("/getRoomListByShipId")
    @Operation(summary = "根据游轮ID获取游轮房间列表")
    public CommonResult<List<ResourceRoomRespVO>> getRoomListByShipId(@RequestParam("shipId") String shipId) {
        List<ResourceRoomDO> list = roomService.getRoomListByShipId(shipId);
        return success(BeanUtils.toBean(list, ResourceRoomRespVO.class));
    }

    @GetMapping("/getRoomListByShipIdAndFloors")
    @Operation(summary = "根据游轮ID获取游轮房间管理")
    public CommonResult<List<ResourceRoomRespVO>> getRoomListByShipIdAndFloors(@RequestParam("shipId") String shipId, @RequestParam("floors") String floors) {
        List<ResourceRoomDO> list = roomService.getRoomListByShipIdAndFloors(shipId, floors);
        return success(BeanUtils.toBean(list, ResourceRoomRespVO.class));
    }


    @GetMapping("/getRoomListByShipIdAndFloorsForArrange")
    @Operation(summary = "根据游轮ID获取游轮房间管理-用于分房")
    public CommonResult<List<ResourceRoomRespVO>> getRoomListByShipIdAndFloorsForArrange(@RequestParam("shipId") String shipId,@RequestParam("voyageId") String voyageId, @RequestParam("floors") String floors) {
        return success(roomService.getRoomListByShipIdAndFloorsForArrange(shipId,voyageId, floors));
    }
}