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

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.subjectplan.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.subjectplan.SubjectPlanDO;
import cn.iocoder.yudao.module.mes.service.subjectplan.SubjectPlanService;

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

    @Resource
    private SubjectPlanService subjectPlanService;

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

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

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

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

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

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

}