package cn.iocoder.yudao.module.pms.controller.admin.iotcarzhbd;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotcarzhbd.vo.IotCarZhbdPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotcarzhbd.vo.IotCarZhbdRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotcarzhbd.vo.IotCarZhbdSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotcarzhbd.IotCarZhbdDO;
import cn.iocoder.yudao.module.pms.service.iotcarzhbd.IotCarZhbdService;
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 cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 中航北斗车辆表")
@RestController
@RequestMapping("/pms/iot-car-zhbd")
@Validated
public class IotCarZhbdController {

    @Resource
    private IotCarZhbdService iotCarZhbdService;

    @PostMapping("/create")
    @Operation(summary = "创建中航北斗车辆表")
    @PreAuthorize("@ss.hasPermission('pms:iot-car-zhbd:create')")
    public CommonResult<Long> createIotCarZhbd(@Valid @RequestBody IotCarZhbdSaveReqVO createReqVO) {
        return success(iotCarZhbdService.createIotCarZhbd(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新中航北斗车辆表")
    @PreAuthorize("@ss.hasPermission('pms:iot-car-zhbd:update')")
    public CommonResult<Boolean> updateIotCarZhbd(@Valid @RequestBody IotCarZhbdSaveReqVO updateReqVO) {
        iotCarZhbdService.updateIotCarZhbd(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除中航北斗车辆表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-car-zhbd:delete')")
    public CommonResult<Boolean> deleteIotCarZhbd(@RequestParam("id") Long id) {
        iotCarZhbdService.deleteIotCarZhbd(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得中航北斗车辆表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-car-zhbd:query')")
    public CommonResult<IotCarZhbdRespVO> getIotCarZhbd(@RequestParam("id") Long id) {
        IotCarZhbdDO iotCarZhbd = iotCarZhbdService.getIotCarZhbd(id);
        return success(BeanUtils.toBean(iotCarZhbd, IotCarZhbdRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得中航北斗车辆表分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-car-zhbd:query')")
    public CommonResult<PageResult<IotCarZhbdRespVO>> getIotCarZhbdPage(@Valid IotCarZhbdPageReqVO pageReqVO) {
        PageResult<IotCarZhbdDO> pageResult = iotCarZhbdService.getIotCarZhbdPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotCarZhbdRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出中航北斗车辆表 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-car-zhbd:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotCarZhbdExcel(@Valid IotCarZhbdPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotCarZhbdDO> list = iotCarZhbdService.getIotCarZhbdPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "中航北斗车辆表.xls", "数据", IotCarZhbdRespVO.class,
                        BeanUtils.toBean(list, IotCarZhbdRespVO.class));
    }

}