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

import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.module.mes.controller.admin.paigongrecord.vo.PaigongRecordRespVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.organization.OrganizationDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.paigongrecord.PaigongRecordDO;
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 static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

import cn.iocoder.yudao.module.mes.controller.admin.zjproduct.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.zjproduct.ZjProductDO;
import cn.iocoder.yudao.module.mes.service.zjproduct.ZjProductService;

@Tag(name = "管理后台 - 质量管理-质检参数")
@RestController
@RequestMapping("/mes/zj-product")
@Validated
public class ZjProductController {

    @Resource
    private ZjProductService zjProductService;

    @PostMapping("/create")
    @Operation(summary = "创建质量管理-质检参数")
    @PreAuthorize("@ss.hasPermission('erp:product:create')")
    public CommonResult<Long> createZjProduct(@Valid @RequestBody ZjProductSaveReqVO createReqVO) {
        return success(zjProductService.createZjProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新质量管理-质检参数")
    @PreAuthorize("@ss.hasPermission('erp:product:update')")
    public CommonResult<Boolean> updateZjProduct(@Valid @RequestBody ZjProductSaveReqVO updateReqVO) {
        zjProductService.updateZjProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除质量管理-质检参数")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:product:delete')")
    public CommonResult<Boolean> deleteZjProduct(@RequestParam("id") Long id) {
        zjProductService.deleteZjProduct(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得质量管理-质检参数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:product:query')")
    public CommonResult<ZjProductRespVO> getZjProduct(@RequestParam("id") Long id) {
        ZjProductDO zjProduct = zjProductService.getZjProduct(id);
        return success(BeanUtils.toBean(zjProduct, ZjProductRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得质量管理-质检参数分页")
    @PreAuthorize("@ss.hasPermission('erp:product:query')")
    public CommonResult<PageResult<ZjProductRespVO>> getZjProductPage(@Valid ZjProductPageReqVO pageReqVO) {
        PageResult<ZjProductDO> pageResult = zjProductService.getZjProductPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ZjProductRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出质量管理-质检参数 Excel")
    @PreAuthorize("@ss.hasPermission('erp:product:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportZjProductExcel(@Valid ZjProductPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ZjProductDO> list = zjProductService.getZjProductPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "质量管理-质检参数.xls", "数据", ZjProductRespVO.class,
                        BeanUtils.toBean(list, ZjProductRespVO.class));
    }

    @GetMapping("/list-by-item-product-id")
    @Operation(summary = "获得产品质检明细列表")
    @Parameter(name = "productId", description = "产品ID")
    @PreAuthorize("@ss.hasPermission('erp:product:query')")
    public CommonResult<List<ZjProductRespVO>> getZjProductByPlanId(@RequestParam("productId") Long productId) {
        List<ZjProductDO> pageResult = zjProductService.getZjProductByProductId(productId);
        return success(BeanUtils.toBean(pageResult, ZjProductRespVO.class));
    }
}