package com.ruoyi.zongzhi.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zongzhi.domain.TcTown;
import com.ruoyi.zongzhi.service.ITcTownService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 区域Controller
 *
 * @author ruoyi
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/zongzhi/town")
@Api(tags = "区域")
public class TcTownController extends BaseController {
    @Autowired
    private ITcTownService tcTownService;

    /**
     * 查询区域列表
     */

    @GetMapping("/list")
    public TableDataInfo list(TcTown tcTown) {
        startPage();
        List<TcTown> list = tcTownService.selectTcTownList(tcTown);
        return getDataTable(list);
    }

    /**
     * 导出区域列表
     */


    @PostMapping("/export")
    public void export(HttpServletResponse response, TcTown tcTown) {
        List<TcTown> list = tcTownService.selectTcTownList(tcTown);
        ExcelUtil<TcTown> util = new ExcelUtil<TcTown>(TcTown.class);
        util.exportExcel(response, list, "区域数据");
    }


    /**
     * 获取区域详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tcTownService.selectTcTownById(id));
    }

    /**
     * 新增区域
     */

    @PostMapping
    public AjaxResult add(@RequestBody TcTown tcTown) {
        tcTown.setIsStatus(true);
        return toAjax(tcTownService.insertTcTown(tcTown));
    }

    /**
     * 修改区域
     */

    @PutMapping
    public AjaxResult edit(@RequestBody TcTown tcTown) {
        return toAjax(tcTownService.updateTcTown(tcTown));
    }

    /**
     * 删除区域
     */

    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tcTownService.deleteTcTownByIds(ids));
    }


    /**
     * 导入excel
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<TcTown> util = new ExcelUtil<TcTown>(TcTown.class);
        List<TcTown> tcTownList = util.importExcel(file.getInputStream());
        String message = tcTownService.importUser(tcTownList);
        return AjaxResult.success(message);
    }

    /**
     * 树形结构列表
     *
     * @return
     */
    @GetMapping("/new/tree")
    @ApiOperation(value = "树形结构列表")
    public AjaxResult treeselect(TcTown tcTown) {
        List<TcTown> tcTowns = tcTownService.selectTcTownList(tcTown);
        return AjaxResult.success(tcTownService.buildDeptTreeSelect(tcTowns));
    }

}
