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

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.moldrecord.vo.MoldRecordPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.moldrecord.vo.MoldRecordRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.moldrecord.vo.MoldRecordSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldrecord.MoldRecordDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldrecord.MoldRecordSubjectDetailDO;
import cn.iocoder.yudao.module.mes.service.moldrecord.MoldRecordService;
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")
@Validated
public class MoldRecordController {

    @Resource
    private MoldRecordService moldRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建模具维保")
    @PreAuthorize("@ss.hasPermission('mes:mold-record:create')")
    public CommonResult<Long> createMoldRecord(@Valid @RequestBody MoldRecordSaveReqVO createReqVO) {
        return success(moldRecordService.createMoldRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具维保")
    @PreAuthorize("@ss.hasPermission('mes:mold-record:update')")
    public CommonResult<Boolean> updateMoldRecord(@Valid @RequestBody MoldRecordSaveReqVO updateReqVO) {
        moldRecordService.updateMoldRecord(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得模具维保")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mold-record:query')")
    public CommonResult<MoldRecordRespVO> getMoldRecord(@RequestParam("id") Long id) {
        MoldRecordDO moldRecord = moldRecordService.getMoldRecord(id);
        return success(BeanUtils.toBean(moldRecord, MoldRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具维保分页")
    @PreAuthorize("@ss.hasPermission('mes:mold-record:query')")
    public CommonResult<PageResult<MoldRecordRespVO>> getMoldRecordPage(@Valid MoldRecordPageReqVO pageReqVO) {
        PageResult<MoldRecordDO> pageResult = moldRecordService.getMoldRecordPage(pageReqVO);
        List<MoldRecordRespVO> list = moldRecordService.buildVOList(pageResult.getList());
        return success(new PageResult<>(list,pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具维保 Excel")
    @PreAuthorize("@ss.hasPermission('mes:mold-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoldRecordExcel(@Valid MoldRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoldRecordDO> list = moldRecordService.getMoldRecordPage(pageReqVO).getList();

        // 导出 Excel
        ExcelUtils.write(response, "模具维保.xls", "数据", MoldRecordRespVO.class,
                       moldRecordService.buildVOList(list));
    }

    // ==================== 子表（模具维保明细） ====================

    @GetMapping("/mold-record-subject-detail/list-by-record-id")
    @Operation(summary = "获得模具维保明细列表")
    @Parameter(name = "recordId", description = "维保记录id")
    @PreAuthorize("@ss.hasPermission('mes:mold-record:query')")
    public CommonResult<List<MoldRecordSubjectDetailDO>> getMoldRecordSubjectDetailListByRecordId(@RequestParam("recordId") Long recordId) {
        return success(moldRecordService.getMoldRecordSubjectDetailListByRecordId(recordId));
    }

    @GetMapping("/updateStatus")
    @Operation(summary = "更新模具维保状态")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mold-record:update')")
    public CommonResult<MoldRecordRespVO> updateStatus(@RequestParam("id") Long id, @RequestParam("status")String status) {
        MoldRecordDO moldRecord = moldRecordService.getMoldRecord(id);
        return success(BeanUtils.toBean(moldRecord, MoldRecordRespVO.class));
    }
}