package cn.iocoder.yudao.module.smartfactory.controller.admin.factoryforklift;

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.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factoryforklift.vo.FactoryForkliftPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factoryforklift.vo.FactoryForkliftRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factoryforklift.vo.FactoryForkliftSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factoryforklift.FactoryForkliftDO;
import cn.iocoder.yudao.module.smartfactory.service.factoryforklift.FactoryForkliftService;
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.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 叉车管理")
@RestController
@RequestMapping("/smartfactory/factory-forklift")
@Validated
public class FactoryForkliftController {

    @Resource
    private FactoryForkliftService factoryForkliftService;

    @PostMapping("/create")
    @Operation(summary = "创建叉车管理")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-forklift:create')")
    public CommonResult<Long> createFactoryForklift(@Valid @RequestBody FactoryForkliftSaveReqVO createReqVO) {
        return success(factoryForkliftService.createFactoryForklift(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新叉车管理")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-forklift:update')")
    public CommonResult<Boolean> updateFactoryForklift(@Valid @RequestBody FactoryForkliftSaveReqVO updateReqVO) {
        factoryForkliftService.updateFactoryForklift(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除叉车管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-forklift:delete')")
    public CommonResult<Boolean> deleteFactoryForklift(@RequestParam("id") Long id) {
        factoryForkliftService.deleteFactoryForklift(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得叉车管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-forklift:query')")
    public CommonResult<FactoryForkliftRespVO> getFactoryForklift(@RequestParam("id") Long id) {
        FactoryForkliftDO factoryForklift = factoryForkliftService.getFactoryForklift(id);
        return success(BeanUtils.toBean(factoryForklift, FactoryForkliftRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得叉车管理分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-forklift:query')")
    public CommonResult<PageResult<FactoryForkliftRespVO>> getFactoryForkliftPage(@Valid FactoryForkliftPageReqVO pageReqVO) {
        PageResult<FactoryForkliftRespVO> pageResult = factoryForkliftService.getFactoryForkliftPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出叉车管理 Excel")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-forklift:export')")
    @OperateLog(type = EXPORT)
    public void exportFactoryForkliftExcel(@Valid FactoryForkliftPageReqVO pageReqVO,
                                           HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FactoryForkliftRespVO> list = factoryForkliftService.getFactoryForkliftPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "叉车管理.xls", "数据", FactoryForkliftRespVO.class,
                list);
    }

}
