package com.yc.ship.module.product.controller.admin.voyagestockdistribute;

import com.yc.ship.framework.common.util.collection.CollectionUtils;
import com.yc.ship.module.ota.api.OtaDistributorApi;
import com.yc.ship.module.product.controller.admin.voyagestockdistribute.vo.QueryCanSelectRoomListReqVO;
import com.yc.ship.module.resource.api.room.dto.RoomRespDTO;
import com.yc.ship.module.resource.dal.dataobject.roommodel.ResourceRoomModelDO;
import com.yc.ship.module.resource.service.roommodel.ResourceRoomModelService;
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.*;
import java.util.*;

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.module.product.controller.admin.voyagestockdistribute.vo.*;
import com.yc.ship.module.product.dal.dataobject.voyagestockdistribute.VoyageStockDistributeDO;
import com.yc.ship.module.product.service.voyagestockdistribute.VoyageStockDistributeService;

@Tag(name = "管理后台 - 航次库存分配")
@RestController
@RequestMapping("/product/voyage-stock-distribute")
@Validated
public class VoyageStockDistributeController {

    @Resource
    private VoyageStockDistributeService voyageStockDistributeService;

    @Resource
    private OtaDistributorApi distributorApi;

    @Resource
    private ResourceRoomModelService resourceRoomModelService;

    @PostMapping("/create")
    @Operation(summary = "创建航次库存分配")
    @PreAuthorize("@ss.hasPermission('product:voyage-stock-distribute:create')")
    public CommonResult<Boolean> createVoyageStockDistribute(@Valid @RequestBody List<VoyageStockDistributeSaveReqVO> createReqVO) {
        voyageStockDistributeService.createVoyageStockDistribute(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新航次库存分配")
    @PreAuthorize("@ss.hasPermission('product:voyage-stock-distribute:update')")
    public CommonResult<Boolean> updateVoyageStockDistribute(@Valid @RequestBody VoyageStockDistributeSaveReqVO updateReqVO) {
        voyageStockDistributeService.updateVoyageStockDistribute(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除航次库存分配")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:voyage-stock-distribute:delete')")
    public CommonResult<Boolean> deleteVoyageStockDistribute(@RequestParam("id") Long id) {
        voyageStockDistributeService.deleteVoyageStockDistribute(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得航次库存分配")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:voyage-stock-distribute:query')")
    public CommonResult<VoyageStockDistributeRespVO> getVoyageStockDistribute(@RequestParam("id") Long id) {
        VoyageStockDistributeDO voyageStockDistribute = voyageStockDistributeService.getVoyageStockDistribute(id);
        return success(BeanUtils.toBean(voyageStockDistribute, VoyageStockDistributeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得航次库存分配分页")
    @PreAuthorize("@ss.hasPermission('product:voyage-stock-distribute:query')")
    public CommonResult<PageResult<VoyageStockDistributeRespPageVO>> getVoyageStockDistributePage(@Valid VoyageStockDistributePageReqVO pageReqVO) {
        PageResult<VoyageStockDistributeRespPageVO> pageResult = voyageStockDistributeService.getVoyageStockDistributePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VoyageStockDistributeRespPageVO.class));
    }

    @GetMapping("/detail")
    @Operation(summary = "获取航次库存分配详情")
    public CommonResult<List<VoyageStockDistributeRespVO>> getVoyageStockDistributeDetail(@Valid VoyageStockDistributeReqVO reqVO) {
        List<VoyageStockDistributeDO> voyageStockDistribute = voyageStockDistributeService.getVoyageStockDistributeDetail(reqVO);
        List<VoyageStockDistributeRespVO> respVO = BeanUtils.toBean(voyageStockDistribute, VoyageStockDistributeRespVO.class);
        List<Long> roomModelIds = CollectionUtils.convertList(respVO, VoyageStockDistributeRespVO::getRoomModelId);
        List<ResourceRoomModelDO> roomModelList = resourceRoomModelService.getList(roomModelIds);
        Map<Long, ResourceRoomModelDO> roomModelDOMap = CollectionUtils.convertMap(roomModelList, ResourceRoomModelDO::getId);
        respVO.stream().forEach(item -> {
            if(roomModelDOMap.get(item.getRoomModelId())!=null){
                item.setRoomModelName(roomModelDOMap.get(item.getRoomModelId()).getName());
            }
        });
        return success(respVO);
    }

    @GetMapping("/list-by-voyage_id")
    @Operation(summary = "根据航次ID获取给旅行社分配航次库存列表")
    public CommonResult<List<VoyageStockDistributeRespVO>> getVoyageStockDistributeListByVoyageId(@RequestParam("voyageId") Long voyageId) {
        List<VoyageStockDistributeDO> list = voyageStockDistributeService.getListByVoyageId(voyageId);
        List<VoyageStockDistributeRespVO> respVOS = BeanUtils.toBean(list, VoyageStockDistributeRespVO.class);
        return success(respVOS);
    }

    @GetMapping("/total-by-voyage-id")
    @Operation(summary = "获取航次库存分配总数")
    public CommonResult<VoyageStockDistributeTotalRespVO> getVoyageStockDistributeTotal(@RequestParam("voyageId") Long voyageId) {
        VoyageStockDistributeTotalRespVO total = voyageStockDistributeService.getVoyageStockDistributeTotal(voyageId);
        return success(total);
    }

    @GetMapping("/getRoomList")
    @Operation(summary = "获取航次库存分配房间列表")
    public CommonResult<List<RoomRespDTO>> getRoomList(@RequestParam("voyageId") Long voyageId) {
        List<RoomRespDTO> list = voyageStockDistributeService.getRoomList(voyageId);
        return success(list);
    }

    @PostMapping("/getCanSelectRoomList")
    @Operation(summary = "获得可以选择的房间号")
    public CommonResult<List<RoomRespDTO>> getCanSelectRoomList(@RequestBody QueryCanSelectRoomListReqVO reqVO) {
        List<RoomRespDTO> list = voyageStockDistributeService.getCanSelectRoomList(reqVO);
        return success(list);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出航次库存分配 Excel")
//    @PreAuthorize("@ss.hasPermission('product:voyage-stock-distribute:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportVoyageStockDistributeExcel(@Valid VoyageStockDistributePageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<VoyageStockDistributeDO> list = voyageStockDistributeService.getVoyageStockDistributePage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "航次库存分配.xls", "数据", VoyageStockDistributeRespVO.class,
//                        BeanUtils.toBean(list, VoyageStockDistributeRespVO.class));
//    }

}