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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.mes.controller.admin.moldrecordsubject.vo.MoldRecordSubjectPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.moldrecordsubject.vo.MoldRecordSubjectRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.moldrecordsubject.vo.MoldRecordSubjectSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldrecordsubject.MoldRecordSubjectDO;
import cn.iocoder.yudao.module.mes.service.moldrecordsubject.MoldRecordSubjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 维保项目")
@RestController
@RequestMapping("/mes/mold-record-subject")
@Validated
public class MoldRecordSubjectController {

    @Resource
    private MoldRecordSubjectService moldRecordSubjectService;

    @PostMapping("/create")
    @Operation(summary = "创建维保项目")
    @PreAuthorize("@ss.hasPermission('mes:mold-record-subject:create')")
    public CommonResult<Long> createMoldRecordSubject(@Valid @RequestBody MoldRecordSubjectSaveReqVO createReqVO) {
        return success(moldRecordSubjectService.createMoldRecordSubject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新维保项目")
    @PreAuthorize("@ss.hasPermission('mes:mold-record-subject:update')")
    public CommonResult<Boolean> updateMoldRecordSubject(@Valid @RequestBody MoldRecordSubjectSaveReqVO updateReqVO) {
        moldRecordSubjectService.updateMoldRecordSubject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除维保项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:mold-record-subject:delete')")
    public CommonResult<Boolean> deleteMoldRecordSubject(@RequestParam("id") Long id) {
        moldRecordSubjectService.deleteMoldRecordSubject(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得维保项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mold-record-subject:query')")
    public CommonResult<MoldRecordSubjectRespVO> getMoldRecordSubject(@RequestParam("id") Long id) {
        MoldRecordSubjectDO moldRecordSubject = moldRecordSubjectService.getMoldRecordSubject(id);
        return success(BeanUtils.toBean(moldRecordSubject, MoldRecordSubjectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得维保项目分页")
    @PreAuthorize("@ss.hasPermission('mes:mold-record-subject:query')")
    public CommonResult<PageResult<MoldRecordSubjectRespVO>> getMoldRecordSubjectPage(@Valid MoldRecordSubjectPageReqVO pageReqVO) {
        PageResult<MoldRecordSubjectDO> pageResult = moldRecordSubjectService.getMoldRecordSubjectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MoldRecordSubjectRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出维保项目 Excel")
    @PreAuthorize("@ss.hasPermission('mes:mold-record-subject:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoldRecordSubjectExcel(@Valid MoldRecordSubjectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoldRecordSubjectDO> list = moldRecordSubjectService.getMoldRecordSubjectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "维保项目.xls", "数据", MoldRecordSubjectRespVO.class,
                        BeanUtils.toBean(list, MoldRecordSubjectRespVO.class));
    }
    @GetMapping("/getList")
    @Operation(summary = "获得维保项目列表")
    public CommonResult<List<MoldRecordSubjectDO>> getList(MoldRecordSubjectPageReqVO pageReqVO) {
        List<MoldRecordSubjectDO> list = moldRecordSubjectService.selectBy(pageReqVO);
        return success(list);
    }
}