package com.ruoyi.project.business.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.annotation.DataUpdateCheck;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.TbArea;
import com.ruoyi.project.business.domain.vo.selectParam.AreaParam;
import com.ruoyi.project.business.service.ITbAreaService;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
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.RestController;

/**
 * 区域Controller
 * 
 * @author ruoyi
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/user/area")
public class TbAreaController extends BaseController
{
    @Autowired
    private ITbAreaService tbAreaService;

    /**
     * 查询区域列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AreaParam tbArea)
    {
        startPage();
        List<TbArea> list = tbAreaService.selectTbAreaList(tbArea);
        return getDataTable(list);
    }

    /**
     * 获取所有区域列表
     */
    @GetMapping("/getAllAreaList")
    public AjaxResult getAllAreaList()
    {
        return success(tbAreaService.getAllAreaList());
    }

    /**
     * 获取所有区域列表
     */
    @GetMapping("/getAreaListByLevel/{level}")
    public AjaxResult getAreaListByLevel(@PathVariable String level)
    {
        return success(tbAreaService.getAreaListByLevel(level));
    }

    /**
     * 获取所有区域列表 tree
     */
    @GetMapping("/getAllAreaTree")
    public AjaxResult getAllAreaTree()
    {
        return success(tbAreaService.getAllAreaTree());
    }

    /**
     * 根据登录用户获取区域树
     */
    @GetMapping("/getAreaTreeByUser")
    public AjaxResult getAreaTreeByUser() {
        return success(tbAreaService.getAreaTreeByUser());
    }

    @GetMapping("/getAreaByUser")
    public AjaxResult getAreaByUser() {
        return success(tbAreaService.getAreaByUser());
    }

    /**
     * 导出区域列表
     */
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AreaParam tbArea) {
        List<TbArea> list = tbAreaService.selectTbAreaListExport(tbArea);
        ExcelUtil<TbArea> util = new ExcelUtil<TbArea>(TbArea.class);
        util.exportExcel(response, list, "区域数据");
    }

    /**
     * 获取区域详细信息
     */
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable("areaId") Long areaId)
    {
        return success(tbAreaService.selectTbAreaByAreaId(areaId));
    }

    /**
     * 新增区域
     */
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbArea tbArea)
    {
        return toAjax(tbAreaService.insertTbArea(tbArea));
    }

    /**
     * 修改区域
     */
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PutMapping
    @DataUpdateCheck(key = "area_id", tableName = "tb_area")
    public AjaxResult edit(@RequestBody TbArea tbArea)
    {
        return toAjax(tbAreaService.updateTbArea(tbArea));
    }

    /**
     * 删除区域
     */
    @Log(title = "区域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{areaIds}")
    public AjaxResult remove(@PathVariable Long[] areaIds)
    {
        return toAjax(tbAreaService.deleteTbAreaByAreaIds(areaIds));
    }

    // /**
    //  * 获取指定层级及后代区域列表
    //  *
    //  * @param areaId        区域主键
    //  * @param isJustSubs    是否只查询子区域（不包含孙子区域）   不传默认为false
    //  * @param isIncludeSelf 是否包含自己  不传默认为false
    //  */
    // @GetMapping("/getChildAreaList")
    // public AjaxResult getChildAreaList(Long areaId, Boolean isJustSubs, Boolean isIncludeSelf) {
    //     return success(tbAreaService.selectChildAreaList(areaId, Boolean.TRUE.equals(isJustSubs), Boolean.TRUE.equals(isIncludeSelf)));
    // }
}
