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

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.collection.MapUtils;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.module.product.controller.admin.pricetemplate.vo.*;
import com.yc.ship.module.product.controller.admin.pricevoyage.vo.PriceVoyagePageReqVO;
import com.yc.ship.module.product.controller.admin.pricevoyage.vo.PriceVoyageRespVO;
import com.yc.ship.module.product.controller.admin.pricevoyage.vo.PriceVoyageSaveReqVO;
import com.yc.ship.module.product.dal.dataobject.basicpricearea.BasicPriceAreaDO;
import com.yc.ship.module.product.dal.dataobject.pricearea.PriceAreaDO;
import com.yc.ship.module.product.dal.dataobject.pricefloor.PriceFloorDO;
import com.yc.ship.module.product.dal.dataobject.priceroommodel.PriceRoomModelDO;
import com.yc.ship.module.product.dal.dataobject.pricesinglesetting.PriceSingleSettingDO;
import com.yc.ship.module.product.dal.dataobject.pricevoyage.PriceVoyageDO;
import com.yc.ship.module.product.dal.dataobject.voyage.VoyageDO;
import com.yc.ship.module.product.enums.ErrorCodeConstants;
import com.yc.ship.module.product.service.basicpricearea.BasicPriceAreaService;
import com.yc.ship.module.product.service.pricearea.PriceAreaService;
import com.yc.ship.module.product.service.pricefloor.PriceFloorService;
import com.yc.ship.module.product.service.priceroommodel.PriceRoomModelService;
import com.yc.ship.module.product.service.priceroommodeltype.PriceRoomModelTypeService;
import com.yc.ship.module.product.service.pricesinglesetting.PriceSingleSettingService;
import com.yc.ship.module.product.service.pricespu.PriceSpuService;
import com.yc.ship.module.product.service.pricevoyage.PriceVoyageService;
import com.yc.ship.module.product.service.voyage.VoyageService;
import com.yc.ship.module.resource.api.route.RouteApi;
import com.yc.ship.module.resource.api.route.dto.RouteRespDTO;
import com.yc.ship.module.resource.api.ship.ShipApi;
import com.yc.ship.module.resource.api.ship.dto.ShipRespDTO;
import com.yc.ship.module.resource.controller.admin.roommodel.vo.ResourceRoomModelRespVO;
import com.yc.ship.module.resource.dal.dataobject.roommodel.ResourceRoomModelDO;
import com.yc.ship.module.resource.service.roommodel.ResourceRoomModelService;
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.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 航次价格")
@RestController
@RequestMapping("/product/price-voyage")
@Validated
public class PriceVoyageController {

    @Resource
    private PriceVoyageService priceVoyageService;


    @Resource
    private VoyageService voyageService;

    @Resource
    private ShipApi shipApi;

    @Resource
    private RouteApi routeApi;

    @PostMapping("/create")
    @Operation(summary = "创建航次价格")
    @PreAuthorize("@ss.hasPermission('product:price-voyage:create')")
    public CommonResult<Long> createPriceVoyage(@Valid @RequestBody PriceVoyageSaveReqVO createReqVO) {
        return success(priceVoyageService.createPriceVoyage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新航次价格")
    @PreAuthorize("@ss.hasPermission('product:price-voyage:update')")
    public CommonResult<Boolean> updatePriceVoyage(@Valid @RequestBody PriceVoyageSaveReqVO updateReqVO) {
        priceVoyageService.updatePriceVoyage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除航次价格")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:price-voyage:delete')")
    public CommonResult<Boolean> deletePriceVoyage(@RequestParam("id") Long id) {
        priceVoyageService.deletePriceVoyage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得航次价格")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:price-voyage:query')")
    public CommonResult<PriceVoyageRespVO> getPriceVoyage(@RequestParam("id") Long id) {
        PriceVoyageRespVO respVO = priceVoyageService.getPriceVoyage(id);
        return success(respVO);
    }

    @GetMapping("/get-by-voyage-id")
    @Operation(summary = "通过航次ID获得航次价格")
    public CommonResult<PriceVoyageRespVO> getByVoyageId(@RequestParam("voyageId") Long voyageId) {
        PriceVoyageRespVO respVO = priceVoyageService.getByVoyageId(voyageId);
        return success(respVO);
    }


    @GetMapping("/page")
    @Operation(summary = "获得航次价格分页")
    @PreAuthorize("@ss.hasPermission('product:price-voyage:query')")
    public CommonResult<PageResult<PriceVoyageRespVO>> getPriceVoyagePage(@Valid PriceVoyagePageReqVO pageReqVO) {
        PageResult<PriceVoyageDO> pageResult = priceVoyageService.getPriceVoyagePage(pageReqVO);
        PageResult<PriceVoyageRespVO> page = BeanUtils.toBean(pageResult, PriceVoyageRespVO.class);
        List<PriceVoyageRespVO> list = page.getList();
        List<Long> shipIds = CollectionUtils.convertList(list, PriceVoyageRespVO::getShipId);
        Map<Long, ShipRespDTO> shipMap = shipApi.queryShip(shipIds);

        List<Long> voyageIds = CollectionUtils.convertList(list, PriceVoyageRespVO::getVoyageId);
        Map<Long, VoyageDO> voyageMap = voyageService.getListMap(voyageIds);

        List<Long> routeIds = CollectionUtils.convertList(list, PriceVoyageRespVO::getRouteId);
        Map<Long, RouteRespDTO> routeMap = routeApi.queryRoute(routeIds);

        list.stream().forEach(item -> {
            MapUtils.findAndThen(shipMap, item.getShipId(), shipRespDTO -> item.setShipName(shipRespDTO.getName()));
            MapUtils.findAndThen(voyageMap, item.getVoyageId(), VoyageDO -> item.setVoyageName(VoyageDO.getName()));
            MapUtils.findAndThen(routeMap, item.getRouteId(), RouteRespDTO -> item.setRouteName(RouteRespDTO.getName()));
        });
        return success(page);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出航次价格 Excel")
    @PreAuthorize("@ss.hasPermission('product:price-voyage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPriceVoyageExcel(@Valid PriceVoyagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PriceVoyageDO> list = priceVoyageService.getPriceVoyagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "航次价格.xls", "数据", PriceVoyageRespVO.class,
                        BeanUtils.toBean(list, PriceVoyageRespVO.class));
    }

}