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

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.moldget.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldget.MoldGetDO;
import cn.iocoder.yudao.module.mes.service.moldget.MoldGetService;

@Tag(name = "管理后台 - 模具管理-领模申请")
@RestController
@RequestMapping("/mes/mold-get")
@Validated
public class MoldGetController {

    @Resource
    private MoldGetService moldGetService;

    @PostMapping("/create")
    @Operation(summary = "创建模具管理-领模申请")
    @PreAuthorize("@ss.hasPermission('mes:mold-get:create')")
    public CommonResult<Long> createMoldGet(@Valid @RequestBody MoldGetSaveReqVO createReqVO) {
        return success(moldGetService.createMoldGet(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具管理-领模申请")
    @PreAuthorize("@ss.hasPermission('mes:mold-get:update')")
    public CommonResult<Boolean> updateMoldGet(@Valid @RequestBody MoldGetSaveReqVO updateReqVO) {
        moldGetService.updateMoldGet(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得模具管理-领模申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mold-get:query')")
    public CommonResult<MoldGetRespVO> getMoldGet(@RequestParam("id") Long id) {
        MoldGetDO moldGet = moldGetService.getMoldGet(id);
        return success(BeanUtils.toBean(moldGet, MoldGetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具管理-领模申请分页")
    @PreAuthorize("@ss.hasPermission('mes:mold-get:query')")
    public CommonResult<PageResult<MoldGetRespVO>> getMoldGetPage(@Valid MoldGetPageReqVO pageReqVO) {
        PageResult<MoldGetDO> pageResult = moldGetService.getMoldGetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MoldGetRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具管理-领模申请 Excel")
    @PreAuthorize("@ss.hasPermission('mes:mold-get:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoldGetExcel(@Valid MoldGetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoldGetDO> list = moldGetService.getMoldGetPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具管理-领模申请.xls", "数据", MoldGetRespVO.class,
                        BeanUtils.toBean(list, MoldGetRespVO.class));
    }

}