/*
 * Author: Zev
 * 生成controller
 */
package com.tofly.gpsboot.controller.process;

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.common.Constant;
import com.tofly.gpsboot.controller.BaseController;
import com.tofly.pojo.plan.dto.PiperecordDto;
import com.tofly.pojo.plan.vo.PiperecordVo;

import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.pojo.plan.Piperecord;
import com.tofly.gpsboot.service.process.PiperecordService;

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.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 管线巡查记录上报
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/piperecord")
@Api(tags="管线巡查记录上报")
public class PiperecordController extends BaseController<Piperecord, PiperecordVo> {

    @Autowired
    public PiperecordService service;

    @Autowired
    public PiperecordController(PiperecordService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ToFlyAppLog(title = "分页查询管线巡查记录上报")
    public ResultRespone<Page<PiperecordVo>> getPiperecordPage(Page page, @ModelAttribute PiperecordDto parDto) {
        return super.getInfoPage(page, JSON.parseObject(JSON.toJSONString(parDto), Piperecord.class));
    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    @ToFlyAppLog(title = "新增管线巡查记录上报")
    public ResultRespone<Object> savePiperecord(@Valid PiperecordDto parDto, BindingResult result,@RequestParam(value = "fileList", required = false) List<MultipartFile> fileList) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if (!Optional.ofNullable(super.validRest(rest, result)).isPresent()) {
            service.savaPipeRecordInfo(fileList, parDto);
        }
        return rest;
    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    @ToFlyAppLog(title = "修改管线巡查记录上报")
    public ResultRespone<Object> updateByIdPiperecord(@Valid PiperecordDto 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.updatePipeRecordInfo(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<Piperecord> getByIdPiperecord(@PathVariable Long id) {
        return super.getById(id);
    }


    @ApiOperation(value = "通过ID批量删除基本信息对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    @ToFlyAppLog(title = "通过ID批量删除管线巡查记录上报")
    public ResultRespone<Object> removeByIdListPiperecord(String ids) {
        ResultRespone<Object> rest = new ResultRespone<>();
        List<Long> removeId = Arrays.stream(ids.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                .collect(Collectors.toList());
        try {

            service.deletePipeRecordInfo(removeId);
        } catch (BusinessException e) {
            rest.setCode(e.getRetCd());
            rest.setMessage(e.getMsgDes());
        }
        return rest;
    }

}
