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

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.PriceAreaRespVO;
import com.yc.ship.module.product.controller.admin.pricetemplate.vo.*;
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.pricetemplate.PriceTemplateDO;
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.pricesinglesetting.PriceSingleSettingService;
import com.yc.ship.module.product.service.pricespu.PriceSpuService;
import com.yc.ship.module.product.service.pricetemplate.PriceTemplateService;
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 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("/product/price-template")
@Validated
public class PriceTemplateController {

    @Resource
    private PriceTemplateService priceTemplateService;

    @Resource
    private ShipApi shipApi;

    @Resource
    private RouteApi routeApi;

    @PostMapping("/create")
    @Operation(summary = "创建价格模板")
    @PreAuthorize("@ss.hasPermission('product:price-template:create')")
    public CommonResult<Long> createPriceTemplate(@Valid @RequestBody PriceTemplateSaveReqVO createReqVO) {
        return success(priceTemplateService.createPriceTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新价格模板")
    @PreAuthorize("@ss.hasPermission('product:price-template:update')")
    public CommonResult<Boolean> updatePriceTemplate(@Valid @RequestBody PriceTemplateSaveReqVO updateReqVO) {
        priceTemplateService.updatePriceTemplate(updateReqVO);
        return success(true);
    }

    @PutMapping("/copy")
    @Operation(summary = "复制价格模板")
    @PreAuthorize("@ss.hasPermission('product:price-template:copy')")
    public CommonResult<Boolean> copyPriceTemplate(@Valid @RequestBody PriceTemplateSaveReqVO updateReqVO) {
        priceTemplateService.copyPriceTemplate(updateReqVO);
        return success(true);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除价格模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:price-template:delete')")
    public CommonResult<Boolean> deletePriceTemplate(@RequestParam("id") Long id) {
        priceTemplateService.deletePriceTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得价格模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:price-template:query')")
    public CommonResult<PriceTemplateRespVO> getPriceTemplate(@RequestParam("id") Long id) {
        PriceTemplateRespVO respVO = priceTemplateService.getPriceTemplate(id);
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得价格模板分页")
    @PreAuthorize("@ss.hasPermission('product:price-template:query')")
    public CommonResult<PageResult<PriceTemplateRespVO>> getPriceTemplatePage(@Valid PriceTemplatePageReqVO pageReqVO) {
        PageResult<PriceTemplateDO> pageResult = priceTemplateService.getPriceTemplatePage(pageReqVO);
        PageResult<PriceTemplateRespVO> page = BeanUtils.toBean(pageResult, PriceTemplateRespVO.class);
        List<PriceTemplateRespVO> list = page.getList();
        List<Long> shipIds = CollectionUtils.convertList(list, PriceTemplateRespVO::getShipId);
        Map<Long, ShipRespDTO> shipMap = shipApi.queryShip(shipIds);

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

        list.stream().forEach(item -> {
            MapUtils.findAndThen(shipMap, item.getShipId(), ship -> item.setShipName(ship.getName()));
            MapUtils.findAndThen(routeMap, item.getRouteId(), route -> item.setRouteName(route.getName()));
        });
        return success(page);
    }

    @GetMapping("/list-simple")
    @Operation(summary = "获得资源管理-价格模板列表")
    public CommonResult<List<PriceTemplateRespVO>> getPriceTemplateListSimple(PriceTemplateReqVO reqVO) {
        List<PriceTemplateDO> list = priceTemplateService.getListSimple(reqVO);
        return success(BeanUtils.toBean(list, PriceTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出价格模板 Excel")
    @PreAuthorize("@ss.hasPermission('product:price-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPriceTemplateExcel(@Valid PriceTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PriceTemplateDO> list = priceTemplateService.getPriceTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "价格模板.xls", "数据", PriceTemplateRespVO.class,
                        BeanUtils.toBean(list, PriceTemplateRespVO.class));
    }

}