package com.tofly.gpsboot.custom.controller;

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.common.oauth.exception.ToflyDeniedException;
import com.tofly.gpsboot.custom.service.CustomCarGpstocarService;
import com.tofly.api.es.SnwsGpsClient;
import com.tofly.gpsboot.service.car.CarGpstocarService;
import com.tofly.pojo.car.CarGpstocar;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@RestController
@AllArgsConstructor
@RequestMapping("/cargpstocar")
@Api(tags="车辆GPS绑定信息接口")
public class CustomCarGpstocarController {

    private final CustomCarGpstocarService customCarGpstocarService;
    private final CarGpstocarService carGpstocarService;
    private final SnwsGpsClient dyrqGpsService;

    @ApiOperation(tags = "车辆GPS绑定信息接口", value = "根据车牌号模糊分页查询")
    @GetMapping("/numberpage")
    public ResultRespone pageByCarNumber(Page page, String number) {
        return ResultRespone.success(customCarGpstocarService.pageByCarNumber(page, number));
    }

    /**
     * 新增车辆GPS绑定信息
     *
     * @param carGpstocar 车辆GPS绑定信息
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "新增车辆GPS绑定信息")
    @ApiOperation(value = "新增车辆GPS绑定信息")
    @PostMapping
    public ResultRespone save(@RequestBody CarGpstocar carGpstocar) {
        List<CarGpstocar> gpsList = carGpstocarService.list();
        gpsList.forEach(f -> {
            if (carGpstocar.getGpsId().equals(f.getGpsId())) {
                throw new ToflyDeniedException("此GPS已被绑定,不能重复绑定");
            }
        });
        boolean save = carGpstocarService.save(carGpstocar);
        updateCarInfo();
        return ResultRespone.success(save);
    }

    /**
     * 修改车辆GPS绑定信息
     *
     * @param carGpstocar 车辆GPS绑定信息
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "修改车辆GPS绑定信息")
    @ApiOperation(value = "修改车辆GPS绑定信息")
    @PutMapping
    public ResultRespone updateById(@RequestBody CarGpstocar carGpstocar) {
        List<CarGpstocar> gpsList = carGpstocarService.list();
        gpsList.forEach(f -> {
            if (carGpstocar.getGpsId().equals(f.getGpsId())) {
                throw new ToflyDeniedException("此GPS已被绑定,不能重复绑定");
            }
        });
        boolean b = carGpstocarService.updateById(carGpstocar);
        updateCarInfo();
        return ResultRespone.success(b);
    }

    /**
     * 通过id删除车辆GPS绑定信息
     *
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过id删除车辆GPS绑定信息")
    @ApiOperation(value = "通过ID删除车辆GPS绑定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable Long id) {
        boolean b = carGpstocarService.removeById(id);
        updateCarInfo();
        return ResultRespone.success();
    }

    /**
     * 通过id批量删除车辆GPS绑定信息
     *
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title = "通过id批量删除车辆GPS绑定信息")
    @ApiOperation(value = "通过ID批量删除车辆GPS绑定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeById(String ids) {
        boolean b = carGpstocarService.removeByIds(Arrays.asList(ids.split(",")));
        updateCarInfo();
        return ResultRespone.success(b);
    }

    /**
     * 增删查改时修改cargps里缓存的车辆绑定信息
     */
    private void updateCarInfo() {
        try {
            dyrqGpsService.updateCarInfo();
        } catch (Exception e) {

        }
    }
}
