package com.fastbee.data.controller.gis;

import com.fastbee.common.core.controller.BaseController;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.core.page.TableDataInfo;
import com.fastbee.common.utils.poi.ExcelUtil;
import com.fastbee.ggroup.domain.GGroups;
import com.fastbee.ggroup.domain.vo.GGroupSiteVo;
import com.fastbee.ggroup.enums.GroupTagEnum;
import com.fastbee.ggroup.service.IGGroupsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * 组Controller
 *
 * @author kerwincui
 * &#064;date  2024-09-27
 */
@RestController
@RequestMapping("/gis/groups")
@Api(tags = "组")
//@PreAuthorize("@ss.hasPermi('ggroup:groups')")
public class GGroupsController extends BaseController
{
    private final IGGroupsService gGroupsService;
    public GGroupsController(IGGroupsService gGroupsService) {
        this.gGroupsService = gGroupsService;
    }

    /**
    * 查询组树状结构列表
    */
    @GetMapping("/list")
    @ApiOperation("查询组列表")
    public AjaxResult list(GGroups gGroups)
    {
        return success(gGroupsService.selectGGroupsList(gGroups));
    }

    /**
     * 查询组带叶子节点树状列表
     */
    @PostMapping("/list-with-leaf")
    @ApiOperation("查询组带叶子节点树状列表")
    public AjaxResult listWithLeaf(@RequestBody GGroups gGroups){
        return success(gGroupsService.selectGGroupsAndSitesList(gGroups));
    }

    /**
     * 查询组下面的直属站点
     */
    @GetMapping("/list-site")
    @ApiOperation("查询组下面的直属站点")
    public TableDataInfo listSite(GGroups gGroups)
    {
        super.startPage();
        List<GGroupSiteVo> list = gGroupsService.selectGGroupsListSites(gGroups);
        return super.getDataTable(list);
    }

    /**
     * 获取组的标签(类别)
     */
    @GetMapping("/list-label")
    @ApiOperation("获取组的标签(类别)")
    public AjaxResult listLabel() {
        List<Map<String, Object>> labelList = new ArrayList<>();
        for (GroupTagEnum tag : GroupTagEnum.values()) {
            Map<String, Object> map = new HashMap<>();
            map.put("code", tag.getCode());
            map.put("value", tag.getDescription());
            labelList.add(map);
        }
        return AjaxResult.success(labelList);
    }

    /**
     * 导出组列表
     */
    @ApiOperation("导出组列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, GGroups gGroups)
    {
        List<GGroups> list = gGroupsService.selectGGroupsList(gGroups);
        ExcelUtil<GGroups> util = new ExcelUtil<>(GGroups.class);
        util.exportExcel(response, list, "组数据");
    }

    /**
     * 获取组详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取组详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(gGroupsService.selectGGroupsById(id));
    }

    /**
     * 新增组
     */
    @PostMapping
    @ApiOperation("新增组")
    public AjaxResult add(@RequestBody GGroups gGroups)
    {
        return toAjax(gGroupsService.insertGGroups(gGroups));
    }

    /**
     * 修改组
     */
    @PutMapping
    @ApiOperation("修改组")
    public AjaxResult edit(@RequestBody GGroups gGroups)
    {
        return toAjax(gGroupsService.updateGGroups(gGroups));
    }

    /**
     * 删除组
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除组")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(gGroupsService.deleteGGroupsById(id));
    }
}
