package com.ruoyi.ems.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.ems.domain.Area;
import com.ruoyi.ems.service.IAreaService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 服务区Controller
 *
 * @author ruoyi
 * @date 2024-07-09
 */
@RestController
@RequestMapping("/basecfg/area")
@Api(value = "AreaController", description = "地块管理")
public class AreaController extends BaseController {
    @Autowired
    private IAreaService areaService;

    /**
     * 查询服务区列表
     */
    @RequiresPermissions("basecfg:area:list")
    @GetMapping("/list")
    public TableDataInfo list(Area area) {
        startPage();
        List<Area> list = areaService.selectAreaList(area);
        return getDataTable(list);
    }

    /**
     * 查询建筑基本信息列表
     */
    @RequiresPermissions("basecfg:area:list")
    @GetMapping("/listTotal")
    public AjaxResult listTotal() {
        List<Area> list = areaService.selectAreaList(new Area());
        return success(list);
    }

    /**
     * 导出服务区列表
     */
    @RequiresPermissions("basecfg:area:export")
    @Log(title = "区块划分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Area area) {
        List<Area> list = areaService.selectAreaList(area);
        ExcelUtil<Area> util = new ExcelUtil<Area>(Area.class);
        util.exportExcel(response, list, "服务区数据");
    }

    /**
     * 获取服务区详细信息
     */
    @RequiresPermissions("basecfg:area:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(areaService.selectAreaById(id));
    }

    /**
     * 获取服务区树
     *
     * @param tier 层级 Area、Building、Zoning
     * @return 区域位置树
     */
    @GetMapping(value = "/areaTree")
    public AjaxResult getAreaTree(@RequestParam(name = "tier") String tier) {
        return success(areaService.selectAreaTreeList(tier));
    }

    /**
     * 获取服务区树
     *
     * @param tier    层级 Area、Building、Zoning
     * @param tagCode 标签
     * @return 区域位置树
     */
    @GetMapping(value = "/areaTreeByTag")
    public AjaxResult getCoTree(@RequestParam(name = "tier") String tier,
        @RequestParam(name = "tagCode") String tagCode) {
        return success(areaService.selectAreaTreeByTag(tier, tagCode));
    }

    /**
     * 获取服务区树
     *
     * @param tier 层级 Area、Building、Zoning
     * @param code 代码
     * @return 区域位置树
     */
    @GetMapping(value = "/areaTreeByCode")
    public AjaxResult getAreaTreeByCode(@RequestParam(name = "tier") String tier,
        @RequestParam(name = "code") String code) {
        return success(areaService.selectAreaTreeByCode(tier, code));
    }

    /**
     * 新增服务区
     */
    @RequiresPermissions("basecfg:area:add")
    @Log(title = "区块划分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Area area) {
        return toAjax(areaService.insertArea(area));
    }

    /**
     * 修改服务区
     */
    @RequiresPermissions("basecfg:area:edit")
    @Log(title = "区块划分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Area area) {
        return toAjax(areaService.updateArea(area));
    }

    /**
     * 删除服务区
     */
    @RequiresPermissions("basecfg:area:remove")
    @Log(title = "区块划分", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(areaService.deleteAreaByIds(ids));
    }
}
