package cn.iocoder.yudao.module.mes.controller.admin.moldreturn;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mes.controller.admin.moldreturn.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldreturn.MoldReturnDO;
import cn.iocoder.yudao.module.mes.service.moldreturn.MoldReturnService;

@Tag(name = "管理后台 - 模具管理-模具入库")
@RestController
@RequestMapping("/mes/mold-return")
@Validated
public class MoldReturnController {

    @Resource
    private MoldReturnService moldReturnService;

    @PostMapping("/create")
    @Operation(summary = "创建模具管理-模具入库")
    @PreAuthorize("@ss.hasPermission('mes:mold-return:create')")
    public CommonResult<Long> createMoldReturn(@Valid @RequestBody MoldReturnSaveReqVO createReqVO) {
        return success(moldReturnService.createMoldReturn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具管理-模具入库")
    @PreAuthorize("@ss.hasPermission('mes:mold-return:update')")
    public CommonResult<Boolean> updateMoldReturn(@Valid @RequestBody MoldReturnSaveReqVO updateReqVO) {
        moldReturnService.updateMoldReturn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具管理-模具入库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:mold-return:delete')")
    public CommonResult<Boolean> deleteMoldReturn(@RequestParam("id") Long id) {
        moldReturnService.deleteMoldReturn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具管理-模具入库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mold-return:query')")
    public CommonResult<MoldReturnRespVO> getMoldReturn(@RequestParam("id") Long id) {
        MoldReturnDO moldReturn = moldReturnService.getMoldReturn(id);
        return success(BeanUtils.toBean(moldReturn, MoldReturnRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具管理-模具入库分页")
    @PreAuthorize("@ss.hasPermission('mes:mold-return:query')")
    public CommonResult<PageResult<MoldReturnRespVO>> getMoldReturnPage(@Valid MoldReturnPageReqVO pageReqVO) {
        PageResult<MoldReturnDO> pageResult = moldReturnService.getMoldReturnPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MoldReturnRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具管理-模具入库 Excel")
    @PreAuthorize("@ss.hasPermission('mes:mold-return:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoldReturnExcel(@Valid MoldReturnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoldReturnDO> list = moldReturnService.getMoldReturnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具管理-模具入库.xls", "数据", MoldReturnRespVO.class,
                        BeanUtils.toBean(list, MoldReturnRespVO.class));
    }

}