package com.sptg.gateway.controller.common;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.security.annotation.Security;
import com.sptg.permissions.api.constant.PermissionsSecurityToken;
import com.sptg.permissions.api.dto.AreaDto;
import com.sptg.permissions.api.entity.Area;
import com.sptg.permissions.api.feigns.AreaFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * title: AreaController
 * package: com.sptg.gateway.controller.common
 * description: 区域-控制器
 *
 *                  
 *                
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/common/area")
public class AreaController {

    private final AreaFeign areaFeign;

    /**
     * 功能描述: 根据父级编号获取子节点列表
     * @param parentCode 父级编号
     * @return List<Area>
     *                  
     *                      
     */
    @GetMapping(value = "/getSubList/{parentCode}")
    public ResultBean<Area> getSubList(@PathVariable(value = "parentCode") Long parentCode){
        List<Area> areaList = this.areaFeign.getSubList(parentCode);
        return new ResultBean<>(ResultEnum.SUCCESS,areaList);
    }

    @GetMapping(value = "/index")
    @Security(token = PermissionsSecurityToken.AREA_MANAGE)
    public ModelAndView index(){
        return new ModelAndView("permissions/area-manage");
    }

    @GetMapping(value = "/add")
    @Security(token = PermissionsSecurityToken.AREA_ADD)
    public ModelAndView add(){
        return new ModelAndView("permissions/area-add");
    }
    @GetMapping(value = "/pagingQuery")
    public ResultBean<Page<Area>> pagingQuery( AreaDto areaDto,Page<Area> page){
        areaDto.setPage(page);
        return this.areaFeign.pagingQuery(areaDto);
    }
    @PostMapping(value = "/addArea")
    public ResultBean<Boolean> addArea(AreaDto areaDto){
        return this.areaFeign.addArea(areaDto);
    }
    @GetMapping(value = "/delete/{id}")
    public ResultBean<Boolean> delete(@PathVariable(value = "id") Long id){
        Area area = this.areaFeign.getById(id);
        Assert.notNull(area,"该地区不存在");
        return this.areaFeign.delete(area);
    }
}
