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

import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.framework.common.util.collection.MapUtils;
import com.yc.ship.module.resource.controller.admin.roommodelservice.vo.ResourceRoomModelServiceRespVO;
import com.yc.ship.module.resource.dal.dataobject.roommodelrooms.ResourceRoomModelRoomsDO;
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.roomModelRooms.ResourceRoomModelRoomsService;
import com.yc.ship.module.resource.service.roommodelservice.ResourceRoomModelServiceService;
import com.yc.ship.module.resource.service.ship.ResourceShipService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

import com.yc.ship.framework.excel.core.util.ExcelUtils;

import com.yc.ship.framework.apilog.core.annotation.ApiAccessLog;
import static com.yc.ship.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yc.ship.module.resource.controller.admin.roommodel.vo.*;
import com.yc.ship.module.resource.dal.dataobject.roommodel.ResourceRoomModelDO;
import com.yc.ship.module.resource.service.roommodel.ResourceRoomModelService;

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

    @Resource
    private ResourceRoomModelService roomModelService;

    @Resource
    private ResourceShipService shipService;

    @Resource
    private ResourceRoomModelRoomsService roomModelRoomsService;

    @Resource
    private ResourceRoomModelServiceService resourceRoomModelServiceService;


    @PostMapping("/create")
    @Operation(summary = "创建资源管理-房型管理")
    @PreAuthorize("@ss.hasPermission('resource:room-model:create')")
    public CommonResult<Long> createRoomModel(@Valid @RequestBody ResourceRoomModelSaveReqVO createReqVO) {
        return success(roomModelService.createRoomModel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源管理-房型管理")
    @PreAuthorize("@ss.hasPermission('resource:room-model:update')")
    public CommonResult<Boolean> updateRoomModel(@Valid @RequestBody ResourceRoomModelSaveReqVO updateReqVO) {
        roomModelService.updateRoomModel(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得资源管理-房型管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:room-model:query')")
    public CommonResult<ResourceRoomModelRespVO> getRoomModel(@RequestParam("id") Long id) {
        ResourceRoomModelDO roomModel = roomModelService.getRoomModel(id);
        ResourceRoomModelRespVO vo = BeanUtils.toBean(roomModel, ResourceRoomModelRespVO.class);
        List<ResourceRoomModelRoomsDO> list = roomModelRoomsService.getList(id);
        if(!(list.isEmpty())) {
            List<Long> roomIds = CollectionUtils.convertList(list, ResourceRoomModelRoomsDO::getRoomId);
            vo.setRoomNums(roomIds.stream().map(String::valueOf).collect(Collectors.joining("，")));
        }
        List<ResourceRoomModelServiceDO> services = resourceRoomModelServiceService.getByRoomModelId(id);
        vo.setServices(BeanUtils.toBean(services, ResourceRoomModelServiceRespVO.class));
        return success(vo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源管理-房型管理分页")
    @PreAuthorize("@ss.hasPermission('resource:room-model:query')")
    public CommonResult<PageResult<ResourceRoomModelRespVO>> getRoomModelPage(@Valid ResourceRoomModelPageReqVO pageReqVO) {
        PageResult<ResourceRoomModelDO> pageResult = roomModelService.getRoomModelPage(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.stream().forEach(item -> {
            MapUtils.findAndThen(longResourceShipDOMap, item.getShipId(), ship -> item.setShipName(ship.getName()));
            item.setFloorsNumStr(item.getFloorsNum());
        });
        return success(page);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源管理-房型管理 Excel")
    @PreAuthorize("@ss.hasPermission('resource:room-model:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRoomModelExcel(@Valid ResourceRoomModelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResourceRoomModelDO> list = roomModelService.getRoomModelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源管理-房型管理.xls", "数据", ResourceRoomModelRespVO.class,
                        BeanUtils.toBean(list, ResourceRoomModelRespVO.class));
    }
    @GetMapping("/getRoomModelListByShipId")
    @Operation(summary = "根据游轮ID获取游轮房间管理")
    public CommonResult<List<ResourceRoomModelRespVO>> getRoomModelListByShipId(@RequestParam("shipId") String shipId) {
        List<ResourceRoomModelDO> roomModelList = roomModelService.getRoomModelListByShipId(shipId);
        List<ResourceRoomModelRespVO> list = BeanUtils.toBean(roomModelList, ResourceRoomModelRespVO.class);
        return success(list);
    }

    @GetMapping("/getRoomModelCanSaleListByShipId")
    @Operation(summary = "根据游轮ID获取可以销售的游轮房间管理")
    public CommonResult<List<ResourceRoomModelRespVO>> getRoomModelCanSaleListByShipId(@RequestParam("shipId") String shipId) {
        List<ResourceRoomModelDO> roomModelList = roomModelService.getRoomModelCanSaleListByShipId(shipId);
        List<ResourceRoomModelRespVO> list = BeanUtils.toBean(roomModelList, ResourceRoomModelRespVO.class);
        return success(list);
    }

}