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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.controller.BaseController;
import com.tofly.pojo.plan.dto.GpsdeviceDto;
import com.tofly.pojo.plan.vo.GpsdeviceVo;

import com.tofly.pojo.plan.Gpsdevice;

import com.tofly.gpsboot.service.process.GpsdeviceService;

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 javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 车载GPS设备信息
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@RestController
@RequestMapping("/gpsdevice")
@Api(tags="车载GPS设备信息")
public class GpsdeviceController extends BaseController<Gpsdevice, GpsdeviceVo> {

    @Autowired
    public GpsdeviceController(GpsdeviceService service) {
        init(service);
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone<Page<GpsdeviceVo>> getGpsdevicePage(Page page, @ModelAttribute GpsdeviceDto parDto) {
        return super.getInfoPage(page, JSON.parseObject(JSON.toJSONString(parDto), Gpsdevice.class));
    }

    public boolean checkIsExit(String deviceCode,Long id){
        Page page=new Page();
        page.setCurrent(1);
        page.setSize(10);
        Gpsdevice gpsdevice=new Gpsdevice();
        gpsdevice.setDeviceCode(deviceCode);
        ResultRespone<Page<GpsdeviceVo>> pageList=super.getInfoPage(page, gpsdevice);
        List<GpsdeviceVo> list=pageList.getResult().getRecords();
       Long count=list.stream().filter(a->!a.getId().equals(id)).count();
        if(count>0){
            return false;
        }
       return true;
    }

    @ApiOperation(value = "新增基本信息对象")
    @PostMapping("/save")
    public ResultRespone<Object> saveGpsdevice(@Valid @RequestBody GpsdeviceDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
         if(!checkIsExit(parDto.getDeviceCode(),0L)){
            // rest.setMessage("标识编码:"+parDto.getDeviceCode()+"已存在，不能重复添加");
             return ResultRespone.failed("标识编码:"+parDto.getDeviceCode()+"已存在，不能重复添加");

         }else {
             return Optional.ofNullable(super.validRest(rest, result)).orElseGet(() -> super.save(JSON.parseObject(JSON.toJSONString(parDto), Gpsdevice.class)));
         }
    }

    @ApiOperation(value = "修改基本信息")
    @PutMapping("/updata")
    public ResultRespone<Object> updateByIdGpsdevice(@Valid @RequestBody GpsdeviceDto parDto, BindingResult result) {
        ResultRespone<Object> rest = new ResultRespone<>();
        // 数据校验 判空返回
        if(!checkIsExit(parDto.getDeviceCode(),parDto.getId())){
            return ResultRespone.failed("标识编码:"+parDto.getDeviceCode()+"已存在，不能修改");

        }else{
        return Optional.ofNullable(super.validRest(rest, result)).orElseGet(() -> super.updateById(JSON.parseObject(JSON.toJSONString(parDto), Gpsdevice.class)));
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone<Gpsdevice> getByIdGpsdevice(@PathVariable Long id) {
        return super.getById(id);
    }



    @ApiOperation(value = "通过ID批量删除基本信息对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone<Object> removeByIdListGpsdevice(String ids) {
        return super.removeByIdList(ids);
    }
}
