package com.yc.ship.module.resource.controller.app.roommodel;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.collection.MapUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.controller.admin.roommodel.vo.ResourceRoomModelRespVO;
import com.yc.ship.module.resource.controller.admin.roommodelservice.vo.ResourceRoomModelServiceRespVO;
import com.yc.ship.module.resource.controller.app.roommodel.vo.AppResourceRoomModelPageReqVO;
import com.yc.ship.module.resource.dal.dataobject.roommodel.ResourceRoomModelDO;
import com.yc.ship.module.resource.dal.dataobject.roommodelservice.ResourceRoomModelServiceDO;
import com.yc.ship.module.resource.dal.dataobject.ship.ResourceShipDO;
import com.yc.ship.module.resource.service.roommodel.ResourceRoomModelService;
import com.yc.ship.module.resource.service.roommodelservice.ResourceRoomModelServiceService;
import com.yc.ship.module.resource.service.ship.ResourceShipService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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 com.yc.ship.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 资源管理-房型管理")
@RestController
@RequestMapping("/resource/room-model")
@Validated
public class AppResourceRoomModelController {

    @Resource
    private ResourceRoomModelService roomModelService;

    @Resource
    private ResourceShipService shipService;

    @Resource
    private ResourceRoomModelServiceService resourceRoomModelServiceService;




    @GetMapping("/page")
    @Operation(summary = "获得资源管理-房型管理分页")
    public CommonResult<PageResult<ResourceRoomModelRespVO>> getRoomModelPage(@Valid AppResourceRoomModelPageReqVO pageReqVO) {
        PageResult<ResourceRoomModelDO> pageResult = roomModelService.getAppRoomModelPage(pageReqVO);
        PageResult<ResourceRoomModelRespVO> page = BeanUtils.toBean(pageResult, ResourceRoomModelRespVO.class);
        List<ResourceRoomModelRespVO> list = page.getList();
        List<Long> shipIds = CollectionUtils.convertList(list, ResourceRoomModelRespVO::getShipId);
        List<ResourceShipDO> shipList = shipService.getList(shipIds);
        Map<Long, ResourceShipDO> longResourceShipDOMap = CollectionUtils.convertMap(shipList, ResourceShipDO::getId);

        List<Long> roomModelIds = CollectionUtils.convertList(list, ResourceRoomModelRespVO::getId);
        List<ResourceRoomModelServiceDO> services = resourceRoomModelServiceService.getByRoomModelIds(roomModelIds);
        List<ResourceRoomModelServiceRespVO> serviceResps = BeanUtils.toBean(services, ResourceRoomModelServiceRespVO.class);
        Map<Long, List<ResourceRoomModelServiceRespVO>> servicesMap = CollectionUtils.convertMultiMap(serviceResps, ResourceRoomModelServiceRespVO::getRoomModelId);
        list.stream().forEach(item -> {
            MapUtils.findAndThen(longResourceShipDOMap, item.getShipId(), ship -> item.setShipName(ship.getName()));
            item.setFloorsNumStr(item.getFloorsNum());
            MapUtils.findAndThen(servicesMap, item.getId(), services1 -> item.setServices(services1));
        });
        return success(page);
    }

}