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

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.iotmaterialrequisition.vo.IotMaterialRequisitionPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialrequisition.vo.IotMaterialRequisitionRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialrequisition.vo.IotMaterialRequisitionSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialrequisition.vo.IotMaterialRequisitionSaveVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaterialrequisition.IotMaterialRequisitionDO;
import cn.iocoder.yudao.module.pms.service.iotmaterialrequisition.IotMaterialRequisitionService;
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-material-requisition")
@Validated
public class IotMaterialRequisitionController {

    @Resource
    private IotMaterialRequisitionService iotMaterialRequisitionService;

    @PostMapping("/create")
    @Operation(summary = "创建物料领用")
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition:create')")
    public CommonResult<Long> createIotMaterialRequisition(@Valid @RequestBody IotMaterialRequisitionSaveReqVO createReqVO) {
        return success(iotMaterialRequisitionService.createIotMaterialRequisition(createReqVO));
    }

    @PutMapping("/addMaterialRequisition")
    @Operation(summary = "新增物料领用")
    @PreAuthorize("@ss.hasPermission('pms:iot-main-work-order:create')")
    public CommonResult<Boolean> addMaterialRequisition(@Valid @RequestBody IotMaterialRequisitionSaveVO saveVO) {
        iotMaterialRequisitionService.addMaterialRequisition(saveVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料领用")
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition:update')")
    public CommonResult<Boolean> updateIotMaterialRequisition(@Valid @RequestBody IotMaterialRequisitionSaveReqVO updateReqVO) {
        iotMaterialRequisitionService.updateIotMaterialRequisition(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料领用")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition:delete')")
    public CommonResult<Boolean> deleteIotMaterialRequisition(@RequestParam("id") Long id) {
        iotMaterialRequisitionService.deleteIotMaterialRequisition(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料领用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition:query')")
    public CommonResult<IotMaterialRequisitionRespVO> getIotMaterialRequisition(@RequestParam("id") Long id) {
        IotMaterialRequisitionDO iotMaterialRequisition = iotMaterialRequisitionService.getIotMaterialRequisition(id);
        return success(BeanUtils.toBean(iotMaterialRequisition, IotMaterialRequisitionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料领用分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition:query')")
    public CommonResult<PageResult<IotMaterialRequisitionRespVO>> getIotMaterialRequisitionPage(@Valid IotMaterialRequisitionPageReqVO pageReqVO) {
        PageResult<IotMaterialRequisitionDO> pageResult = iotMaterialRequisitionService.getIotMaterialRequisitionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotMaterialRequisitionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料领用 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-material-requisition:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotMaterialRequisitionExcel(@Valid IotMaterialRequisitionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotMaterialRequisitionDO> list = iotMaterialRequisitionService.getIotMaterialRequisitionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料领用.xls", "数据", IotMaterialRequisitionRespVO.class,
                        BeanUtils.toBean(list, IotMaterialRequisitionRespVO.class));
    }

}