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

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.dvcheck.vo.DvCheckPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dvcheck.vo.DvCheckRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.dvcheck.vo.DvCheckSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvcheck.DvCheckDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvcheckdevice.DvCheckDeviceDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dvchecksubject.DvCheckSubjectDO;
import cn.iocoder.yudao.module.mes.service.dvcheck.DvCheckService;
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/dv-check")
@Validated
public class DvCheckController {

    @Resource
    private DvCheckService dvCheckService;

    @PostMapping("/create")
    @Operation(summary = "创建维保计划")
    @PreAuthorize("@ss.hasPermission('mes:dv-check:create')")
    public CommonResult<Long> createDvCheck(@Valid @RequestBody DvCheckSaveReqVO createReqVO) {
        return success(dvCheckService.createDvCheck(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新维保计划")
    @PreAuthorize("@ss.hasPermission('mes:dv-check:update')")
    public CommonResult<Boolean> updateDvCheck(@Valid @RequestBody DvCheckSaveReqVO updateReqVO) {
        dvCheckService.updateDvCheck(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除维保计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:dv-check:delete')")
    public CommonResult<Boolean> deleteDvCheck(@RequestParam("id") Long id) {
        dvCheckService.deleteDvCheck(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得维保计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:dv-check:query')")
    public CommonResult<DvCheckRespVO> getDvCheck(@RequestParam("id") Long id) {
        DvCheckDO dvCheck = dvCheckService.getDvCheck(id);
        return success(BeanUtils.toBean(dvCheck, DvCheckRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得维保计划分页")
    @PreAuthorize("@ss.hasPermission('mes:dv-check:query')")
    public CommonResult<PageResult<DvCheckRespVO>> getDvCheckPage(@Valid DvCheckPageReqVO pageReqVO) {
        PageResult<DvCheckDO> pageResult = dvCheckService.getDvCheckPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DvCheckRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出维保计划 Excel")
    @PreAuthorize("@ss.hasPermission('mes:dv-check:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDvCheckExcel(@Valid DvCheckPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DvCheckDO> list = dvCheckService.getDvCheckPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "维保计划.xls", "数据", DvCheckRespVO.class,
                        BeanUtils.toBean(list, DvCheckRespVO.class));
    }

    // ==================== 子表（维保计划设备） ====================

    @GetMapping("/dv-check-device/list-by-plan-id")
    @Operation(summary = "获得维保计划设备列表")
    @Parameter(name = "planId", description = "计划ID")
    @PreAuthorize("@ss.hasPermission('mes:dv-check:query')")
    public CommonResult<List<DvCheckDeviceDO>> getDvCheckDeviceListByPlanId(@RequestParam("planId") Long planId) {
        return success(dvCheckService.getDvCheckDeviceListByPlanId(planId));
    }

    // ==================== 子表（维保计划项目） ====================

    @GetMapping("/dv-check-subject/list-by-plan-id")
    @Operation(summary = "获得维保计划项目列表")
    @Parameter(name = "planId", description = "计划ID")
    @PreAuthorize("@ss.hasPermission('mes:dv-check:query')")
    public CommonResult<List<DvCheckSubjectDO>> getDvCheckSubjectListByPlanId(@RequestParam("planId") Long planId) {
        return success(dvCheckService.getDvCheckSubjectListByPlanId(planId));
    }

}