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

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.iotsappickinglist.vo.IotSapPickingListPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotsappickinglist.vo.IotSapPickingListRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotsappickinglist.vo.IotSapPickingListSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotsappickinglist.IotSapPickingListDO;
import cn.iocoder.yudao.module.pms.service.iotsappickinglist.IotSapPickingListService;
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 = "管理后台 - PMS SAP 领料单")
@RestController
@RequestMapping("/pms/iot-sap-picking-list")
@Validated
public class IotSapPickingListController {

    @Resource
    private IotSapPickingListService iotSapPickingListService;

    @PostMapping("/create")
    @Operation(summary = "创建PMS SAP 领料单")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-picking-list:create')")
    public CommonResult<Long> createIotSapPickingList(@Valid @RequestBody IotSapPickingListSaveReqVO createReqVO) {
        return success(iotSapPickingListService.createIotSapPickingList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新PMS SAP 领料单")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-picking-list:update')")
    public CommonResult<Boolean> updateIotSapPickingList(@Valid @RequestBody IotSapPickingListSaveReqVO updateReqVO) {
        iotSapPickingListService.updateIotSapPickingList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除PMS SAP 领料单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-picking-list:delete')")
    public CommonResult<Boolean> deleteIotSapPickingList(@RequestParam("id") Long id) {
        iotSapPickingListService.deleteIotSapPickingList(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得PMS SAP 领料单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-picking-list:query')")
    public CommonResult<IotSapPickingListRespVO> getIotSapPickingList(@RequestParam("id") Long id) {
        IotSapPickingListDO iotSapPickingList = iotSapPickingListService.getIotSapPickingList(id);
        return success(BeanUtils.toBean(iotSapPickingList, IotSapPickingListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得PMS SAP 领料单分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-picking-list:query')")
    public CommonResult<PageResult<IotSapPickingListRespVO>> getIotSapPickingListPage(@Valid IotSapPickingListPageReqVO pageReqVO) {
        PageResult<IotSapPickingListDO> pageResult = iotSapPickingListService.getIotSapPickingListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotSapPickingListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出PMS SAP 领料单 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-sap-picking-list:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotSapPickingListExcel(@Valid IotSapPickingListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotSapPickingListDO> list = iotSapPickingListService.getIotSapPickingListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "PMS SAP 领料单.xls", "数据", IotSapPickingListRespVO.class,
                        BeanUtils.toBean(list, IotSapPickingListRespVO.class));
    }

}