package com.sptg.gateway.controller.estate;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.estate.api.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.CarDto;
import com.sptg.estate.api.entity.Car;
import com.sptg.estate.api.feigns.CarFeign;
import com.sptg.permissions.api.feigns.ConfFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/estate/car")
@RequiredArgsConstructor
public class CarController {

    private final ConfFeign confFeign;
    private final CarFeign carFeign;


    @GetMapping(value = "/index")
    @Security(token = EstateSecurityToken.ESTATE_CAR_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("estate/car-manage");
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    @GetMapping(value = "/addView")
    @Security(token = EstateSecurityToken.ESTATE_CAR_ADD,format = DataFormatEnum.VIEW)
    public ModelAndView addVieW(CarDto dto){
        ModelAndView modelAndView = new ModelAndView("estate/car-add");
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    @GetMapping(value = "/editView/{id}")
    @Security(token = EstateSecurityToken.ESTATE_CAR_EDIT,format = DataFormatEnum.VIEW)
    public ModelAndView editView(@PathVariable Long id){
        Car car = this.carFeign.getById(id);
        Assert.notNull(car,"选择的车辆不存在");
        ModelAndView modelAndView = new ModelAndView("estate/car-edit");
        modelAndView.addObject("car",car);
        modelAndView.addObject("fastDfsServer",this.confFeign.getFileServerHost());
        return modelAndView;
    }

    @GetMapping(value = "/page")
    @Security(token = EstateSecurityToken.ESTATE_CAR_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<Car>> page(CarDto dto,Page<Car> page){
        dto.setPage(page);
        return this.carFeign.page(dto);
    }

    @PostMapping(value = "/add")
    @Security(token = EstateSecurityToken.ESTATE_CAR_ADD,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> add(CarDto dto){
        return this.carFeign.add(dto);
    }

    @PostMapping(value = "/edit")
    @Security(token = EstateSecurityToken.ESTATE_CAR_EDIT,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> edit(CarDto dto){
        return this.carFeign.edit(dto);
    }

    @PostMapping(value = "/delete")
    @Security(token = EstateSecurityToken.ESTATE_CAR_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> delete(CarDto dto){
        return this.carFeign.delete(dto);
    }
}
