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

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.routepoint.vo.RoutePointPageReqVO;
import com.yc.ship.module.resource.controller.admin.routepoint.vo.RoutePointRespVO;
import com.yc.ship.module.resource.controller.admin.routepoint.vo.RoutePointSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.routepoint.RoutePointDO;
import com.yc.ship.module.resource.service.routepoint.RoutePointService;
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("/resource/routePoint")
@Validated
public class RoutePointController {

    @Resource
    private RoutePointService routePointService;

    @PostMapping("/create")
    @Operation(summary = "创建产品路线点位")
    @PreAuthorize("@ss.hasPermission('resource:route-point:create')")
    public CommonResult<Long> createRoutePoint(@Valid @RequestBody RoutePointSaveReqVO createReqVO) {
        return success(routePointService.createRoutePoint(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品路线点位")
    @PreAuthorize("@ss.hasPermission('resource:route-point:update')")
    public CommonResult<Boolean> updateRoutePoint(@Valid @RequestBody RoutePointSaveReqVO updateReqVO) {
        routePointService.updateRoutePoint(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品路线点位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('resource:route-point:delete')")
    public CommonResult<Boolean> deleteRoutePoint(@RequestParam("id") Long id) {
        routePointService.deleteRoutePoint(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品路线点位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:route-point:query')")
    public CommonResult<RoutePointRespVO> getRoutePoint(@RequestParam("id") Long id) {
        RoutePointDO routePoint = routePointService.getRoutePoint(id);
        return success(BeanUtils.toBean(routePoint, RoutePointRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品路线点位分页")
    @PreAuthorize("@ss.hasPermission('resource:route-point:query')")
    public CommonResult<PageResult<RoutePointRespVO>> getRoutePointPage(@Valid RoutePointPageReqVO pageReqVO) {
        PageResult<RoutePointDO> pageResult = routePointService.getRoutePointPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RoutePointRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品路线点位 Excel")
    @PreAuthorize("@ss.hasPermission('resource:route-point:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportRoutePointExcel(@Valid RoutePointPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RoutePointDO> list = routePointService.getRoutePointPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品路线点位.xls", "数据", RoutePointRespVO.class,
                        BeanUtils.toBean(list, RoutePointRespVO.class));
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获得产品路线点位")
    public CommonResult<List<RoutePointRespVO>> getRoutePointList() {
        List<RoutePointDO> pageResult = routePointService.selectAll();
        return success(BeanUtils.toBean(pageResult, RoutePointRespVO.class));
    }

}