package com.yida.data.system.controller;

import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.system.EduArea;
import com.yida.data.system.service.EduAreaService;
import com.yida.data.system.vo.CascadeAreaVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "区域管理")
@RestController
@RequestMapping("/eduArea")
public class EduAreaController {

    @Resource
    private EduAreaService eduAreaService;

    @ApiOperation("保存、编辑区域节点")
    @PostMapping("/saveArea")
    public ResultBean saveArea(@RequestBody EduArea area) {
        if (area.getParentId() != null && eduAreaService.getInfo(area.getParentId()) == null) {
            return ResultBean.buildError("父节点不存在");
        }
        if (eduAreaService.getInfoByName(area.getName(), area.getParentId()) != null) {
            return ResultBean.buildError("该区域节点已存在");
        }
        eduAreaService.saveArea(area);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除区域节点")
    @PostMapping("/deleteArea")
    public ResultBean deleteArea(@ApiParam("区域id") @RequestBody Long areaId) {
        if (areaId == null || eduAreaService.getInfo(areaId) == null) {
            return ResultBean.buildError("节点不存在");
        }
        eduAreaService.deleteArea(areaId);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询区域信息")
    @GetMapping("/findAreaTreeOrList")
    public ResultBean<List<EduArea>> findAreaTreeOrList(@ApiParam("父节点id，默认从省级查询") @RequestParam(required =
            false) Long parentId,
                                                        @ApiParam("是否子查询直接儿子节点，0-是，1-否，默认为是") @RequestParam(required = false,
                                                                defaultValue = "0") Integer isOnlyNext,
                                                        @ApiParam("返回数据是否为树形结构,0-是，1-否，默认为树形") @RequestParam(required = false,
                                                                defaultValue = "0") Integer isTree) {
        return ResultBean.buildSuccess(eduAreaService.findAreaTreeOrList(parentId, isOnlyNext, isTree));
    }


    @ApiOperation("查询父区域信息")
    @GetMapping("/findAreaParentId")
    public ResultBean<List<Long>> findAreaParentId(@ApiParam("子节点Id") @RequestParam Long areaId) {
        return ResultBean.buildSuccess(eduAreaService.findAreaParentId(areaId));
    }

    @ApiOperation("根据街道id查询所有区域信息")
    @GetMapping("/findAllAreaInfo")
    public ResultBean<CascadeAreaVO> findAllAreaInfo(@ApiParam("街道子节点Id") @RequestParam Long areaId) {
        return ResultBean.buildSuccess(eduAreaService.findAllAreaInfo(areaId));
    }
}
