package com.ruoyi.mall.controller;

import java.util.List;

import com.ruoyi.common.core.domain.RegionTreeSelect;
import com.ruoyi.mall.domain.MallRegion;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.service.IMallRegionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 省市区Controller
 *
 * @author ruoyi
 * @date 2022-07-15
 */
@RestController
@RequestMapping("/mall/mallRegion")
public class MallRegionController extends BaseController
{
    @Autowired
    private IMallRegionService mallRegionService;

    /**
     * 查询省市区列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallRegion:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallRegion mallRegion)
    {
        startPage();
        List<MallRegion> list = mallRegionService.selectMallRegionList(mallRegion);
        return getDataTable(list);
    }

    /**
     * 查询省市区列表
     */
    //@PreAuthorize("@ss.hasPermi('mall:mallRegion:list')")
    @GetMapping("/selectList")
    public List<RegionTreeSelect> selectList()
    {
        return mallRegionService.buildRegionTreeList();
    }

    /**
     * 导出省市区列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallRegion:export')")
    @Log(title = "省市区", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallRegion mallRegion)
    {
        List<MallRegion> list = mallRegionService.selectMallRegionList(mallRegion);
        ExcelUtil<MallRegion> util = new ExcelUtil<MallRegion>(MallRegion.class);
        return util.exportExcel(list, "省市区数据");
    }

    /**
     * 获取省市区详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallRegion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(mallRegionService.selectMallRegionById(id));
    }

    /**
     * 新增省市区
     */
    @PreAuthorize("@ss.hasPermi('mall:mallRegion:add')")
    @Log(title = "省市区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallRegion mallRegion)
    {
        return toAjax(mallRegionService.insertMallRegion(mallRegion));
    }

    /**
     * 修改省市区
     */
    @PreAuthorize("@ss.hasPermi('mall:mallRegion:edit')")
    @Log(title = "省市区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallRegion mallRegion)
    {
        return toAjax(mallRegionService.updateMallRegion(mallRegion));
    }

    /**
     * 删除省市区
     */
    @PreAuthorize("@ss.hasPermi('mall:mallRegion:remove')")
    @Log(title = "省市区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mallRegionService.deleteMallRegionByIds(ids));
    }

    /**
     * 根据上级id获取省市区列表（0-省级）
     * @return
     */
    @GetMapping("/getRegionByPid/{pid}")
    public AjaxResult getRegionByPid(@PathVariable("pid") Integer pid) {
        return AjaxResult.success(mallRegionService.getRegionByPid(pid));
    }
}
