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

import cn.iocoder.yudao.module.mes.dal.dataobject.zjproduct.ZjProductDO;
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.zjproductrecord.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.zjproductrecord.ZjProductRecordDO;
import cn.iocoder.yudao.module.mes.service.zjproductrecord.ZjProductRecordService;

@Tag(name = "管理后台 - 试产质检记录")
@RestController
@RequestMapping("/mes/zj-product-record")
@Validated
public class ZjProductRecordController {

    @Resource
    private ZjProductRecordService zjProductRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建试产质检记录")
//    @PreAuthorize("@ss.hasPermission('mes:zj-product-record:create')")
    public CommonResult<Long> createZjProductRecord(@Valid @RequestBody ZjProductRecordSaveReqVO createReqVO) {
        return success(zjProductRecordService.createZjProductRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新试产质检记录")
//    @PreAuthorize("@ss.hasPermission('mes:zj-product-record:update')")
    public CommonResult<Boolean> updateZjProductRecord(@Valid @RequestBody ZjProductRecordSaveReqVO updateReqVO) {
        zjProductRecordService.updateZjProductRecord(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateBatch")
    @Operation(summary = "批量更新试产质检记录")
    @PreAuthorize("@ss.hasPermission('erp:product:update')")
    public CommonResult<Boolean> updateZjProductRecordBatch(@Valid @RequestBody List<ZjProductRecordSaveReqVO> updateReqVOList) {
        for (ZjProductRecordSaveReqVO source : updateReqVOList) {
            zjProductRecordService.updateZjProductRecord(source);
        }
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除试产质检记录")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('mes:zj-product-record:delete')")
    public CommonResult<Boolean> deleteZjProductRecord(@RequestParam("id") Long id) {
        zjProductRecordService.deleteZjProductRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得试产质检记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:product:query')")
    public CommonResult<ZjProductRecordRespVO> getZjProductRecord(@RequestParam("id") Long id) {
        ZjProductRecordDO zjProductRecord = zjProductRecordService.getZjProductRecord(id);
        return success(BeanUtils.toBean(zjProductRecord, ZjProductRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得试产质检记录分页")
    @PreAuthorize("@ss.hasPermission('erp:product:query')")
    public CommonResult<PageResult<ZjProductRecordRespVO>> getZjProductRecordPage(@Valid ZjProductRecordPageReqVO pageReqVO) {
        PageResult<ZjProductRecordDO> pageResult = zjProductRecordService.getZjProductRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ZjProductRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出试产质检记录 Excel")
//    @PreAuthorize("@ss.hasPermission('mes:zj-product-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportZjProductRecordExcel(@Valid ZjProductRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ZjProductRecordDO> list = zjProductRecordService.getZjProductRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "试产质检记录.xls", "数据", ZjProductRecordRespVO.class,
                        BeanUtils.toBean(list, ZjProductRecordRespVO.class));
    }

}