package com.ningdatech.carapi.sys.controller;


import com.ningdatech.carapi.sys.entity.req.RegionDeleteReq;
import com.ningdatech.carapi.sys.entity.req.RegionTreeReq;
import com.ningdatech.carapi.sys.entity.vo.CompanyCarInfoVO;
import com.ningdatech.carapi.sys.entity.vo.RegionCompanyCarTreeVo;
import com.ningdatech.carapi.sys.entity.vo.RegionTreeVO;
import com.ningdatech.carapi.sys.manage.RegionManage;
import com.ningdatech.log.annotation.WebLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuXinXin
 * @since 2022-10-08
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/region")
@Api(tags = "系统管理-地域管理")
public class RegionController {

    private final RegionManage regionManage;

    @PostMapping("/tree")
    @ApiOperation("获取区域编码的树状结构")
    public List<RegionTreeVO> getRegionTree(@RequestBody(required = false) RegionTreeReq req) {
        return regionManage.getRegionTree(req);
    }

    @GetMapping("/tree/getCompanyCarInfo")
    @ApiOperation("获取区域下的企业车辆树状结构")
    public CompanyCarInfoVO getCompanyCarInfo(RegionTreeReq req) {
        return regionManage.getCompanyCarInfo(req);
    }

    @PostMapping("/regionCompanyCarNoTree")
    @ApiOperation("获取区域编码的树状结构（子节点信息同名）")
    public List<RegionCompanyCarTreeVo> getRegionCompanyCarNoTree(@RequestBody(required = false) RegionTreeReq req) {
        return regionManage.getRegionCompanyCarNoTree(req);
    }

    @PostMapping("/regionCompanyTree")
    @ApiOperation("获取区域企业的树状结构")
    public List<RegionCompanyCarTreeVo> getRegionCompanyTree(@RequestBody(required = false) RegionTreeReq req) {
        return regionManage.getRegionCompanyTree(req);
    }

    @PostMapping("/save")
    @ApiOperation("保存或修改地域信息")
    @WebLog(value = "保存/修改地域信息", modular = "地域管理")
    public void saveRegion(@RequestBody @Valid RegionTreeVO region) {
        regionManage.saveOrUpdate(region);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除地域信息")
    @WebLog(value = "删除地域信息", modular = "地域管理")
    public void deleteRegion(@RequestBody @Valid RegionDeleteReq req) {
        regionManage.remove(req.getId(), req.getLevel());
    }

}
