/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.gpsboot.controller.maintain;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;

import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.gpsboot.controller.BaseController;
import com.tofly.pojo.maintain.dto.RepairrecordDto;
import com.tofly.pojo.maintain.vo.RepairrecordVo;

import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.service.maintain.RepairrecordService;
import com.tofly.pojo.maintain.Repairrecord;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 抢维修处理记录表
 *
 * @author admin
 * @date Tue Apr 06 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/repairrecord")
@Api(tags="抢维修处理记录表接口")
public class RepairrecordController extends BaseController<Repairrecord, RepairrecordVo> {

    @Autowired
    private RepairrecordService service;

    @Autowired
    public RepairrecordController(RepairrecordService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询抢维修处理记录")
    public ResultRespone<Page<RepairrecordVo>> getRepairrecordPage(Page page, @ModelAttribute RepairrecordDto parDto) {
        return super.getInfoPage(page, JSON.parseObject(JSON.toJSONString(parDto), Repairrecord.class));
    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增抢维修处理记录")
    public ResultRespone<Object> saveRepairrecord(@Valid RepairrecordDto parDto, BindingResult result,@RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {

            try {
                service.savaRepairrecordInfo(fileList, parDto);
            } catch (BusinessException e) {
                rest.setCode(e.getRetCd());
                rest.setMessage(e.getMsgDes());
            }
        }
        return rest;

    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @ToFlyAppLog(title = "通过ID查询抢维修处理记录")
    public ResultRespone<Repairrecord> getByIdRepairrecord(@PathVariable Long id) {
        return super.getById(id);
    }

}
