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

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.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.resource.controller.admin.productRoute.vo.ProductRoutePageReqVO;
import com.yc.ship.module.resource.controller.admin.productRoute.vo.ProductRouteRespVO;
import com.yc.ship.module.resource.controller.admin.productRoute.vo.ProductRouteSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.productRoute.ProductRouteDO;
import com.yc.ship.module.resource.service.productRoute.ProductRouteService;
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 static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 基础产品路线")
@RestController
@RequestMapping("/product/route")
@Validated
public class ProductRouteController {

    @Resource
    private ProductRouteService routeService;

    @PostMapping("/create")
    @Operation(summary = "创建基础产品路线")
    @PreAuthorize("@ss.hasPermission('product:route:create')")
    public CommonResult<Long> createRoute(@Valid @RequestBody ProductRouteSaveReqVO createReqVO) {
        return success(routeService.createRoute(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新基础产品路线")
    @PreAuthorize("@ss.hasPermission('product:route:update')")
    public CommonResult<Boolean> updateRoute(@Valid @RequestBody ProductRouteSaveReqVO updateReqVO) {
        routeService.updateRoute(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除基础产品路线")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:route:delete')")
    public CommonResult<Boolean> deleteRoute(@RequestParam("id") Long id) {
        routeService.deleteRoute(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得基础产品路线")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:route:query')")
    public CommonResult<ProductRouteRespVO> getRoute(@RequestParam("id") Long id) {
        ProductRouteDO route = routeService.getRoute(id);
        return success(BeanUtils.toBean(route, ProductRouteRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得基础产品路线分页")
//    @PreAuthorize("@ss.hasPermission('product:route:query')")
    public CommonResult<PageResult<ProductRouteRespVO>> getRoutePage(@Valid ProductRoutePageReqVO pageReqVO) {
        if (pageReqVO.getPageSizeNone() == 0) {
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }
        PageResult<ProductRouteDO> pageResult = routeService.getRoutePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductRouteRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出基础产品路线 Excel")
    @PreAuthorize("@ss.hasPermission('product:route:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportRouteExcel(@Valid ProductRoutePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductRouteDO> list = routeService.getRoutePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "基础产品路线.xls", "数据", ProductRouteRespVO.class,
                        BeanUtils.toBean(list, ProductRouteRespVO.class));
    }

}