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

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.subjectmoldplan.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.subjectmoldplan.SubjectMoldPlanDO;
import cn.iocoder.yudao.module.mes.service.subjectmoldplan.SubjectMoldPlanService;

@Tag(name = "管理后台 - 项目方案关联")
@RestController
@RequestMapping("/mes/subject-mold-plan")
@Validated
public class SubjectMoldPlanController {

    @Resource
    private SubjectMoldPlanService subjectPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建项目方案关联")
    @PreAuthorize("@ss.hasPermission('mes:subject-mold-plan:create')")
    public CommonResult<Long> createSubjectMoldPlan(@Valid @RequestBody SubjectMoldPlanSaveReqVO createReqVO) {
        return success(subjectPlanService.createSubjectMoldPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目方案关联")
    @PreAuthorize("@ss.hasPermission('mes:subject-mold-plan:update')")
    public CommonResult<Boolean> updateSubjectMoldPlan(@Valid @RequestBody SubjectMoldPlanSaveReqVO updateReqVO) {
        subjectPlanService.updateSubjectMoldPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目方案关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:subject-mold-plan:delete')")
    public CommonResult<Boolean> deleteSubjectMoldPlan(@RequestParam("id") Long id) {
        subjectPlanService.deleteSubjectMoldPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目方案关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:subject-mold-plan:query')")
    public CommonResult<SubjectMoldPlanRespVO> getSubjectMoldPlan(@RequestParam("id") Long id) {
        SubjectMoldPlanDO subjectPlan = subjectPlanService.getSubjectMoldPlan(id);
        return success(BeanUtils.toBean(subjectPlan, SubjectMoldPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目方案关联分页")
    @PreAuthorize("@ss.hasPermission('mes:subject-mold-plan:query')")
    public CommonResult<PageResult<SubjectMoldPlanRespVO>> getSubjectMoldPlanPage(@Valid SubjectMoldPlanPageReqVO pageReqVO) {
        PageResult<SubjectMoldPlanDO> pageResult = subjectPlanService.getSubjectMoldPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SubjectMoldPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目方案关联 Excel")
    @PreAuthorize("@ss.hasPermission('mes:subject-mold-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSubjectMoldPlanExcel(@Valid SubjectMoldPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SubjectMoldPlanDO> list = subjectPlanService.getSubjectMoldPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目方案关联.xls", "数据", SubjectMoldPlanRespVO.class,
                        BeanUtils.toBean(list, SubjectMoldPlanRespVO.class));
    }

}