package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.StringUtils;
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.system.domain.SysDeptJl;
import com.ruoyi.system.service.ISysDeptJlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 吉林地区管理Controller
 *
 * @author CH
 * @date 2022-03-25
 */
@RestController
@RequestMapping("/jlDept")
public class SysDeptJlController extends BaseController
{
    @Autowired
    private ISysDeptJlService sysDeptJlService;

    /**
     * 查询吉林地区管理列表
     */
    @RequiresPermissions("system:jlDept:list")
    @GetMapping("/list")
    public TableDataInfo list(SysDeptJl sysDeptJl)
    {
        startPage();
        List<SysDeptJl> list = sysDeptJlService.selectSysDeptJlList(sysDeptJl);
        return getDataTable(list);
    }


    @GetMapping("/getList")
    public AjaxResult getList(SysDeptJl sysDeptJl)
    {
        if(StringUtils.isEmpty(sysDeptJl.getParentId())){
            sysDeptJl.setParentId("479a446fce894740b373f9c4b356daac");
        }
        List<SysDeptJl> list = sysDeptJlService.selectSysDeptJlList(sysDeptJl);
        return AjaxResult.success(list);
    }

    /**
     *
     * 同步行政区 到部门表
     */
    @RequiresPermissions("system:jlDept:syncToDept")
    @GetMapping("/syncToDept")
    public AjaxResult syncToDept()
    {
        return toAjax(sysDeptJlService.syncToDept());
    }

    /**
     * 导出吉林地区管理列表
     */
    @RequiresPermissions("system:jlDept:export")
    @Log(title = "吉林地区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDeptJl sysDeptJl)
    {
        List<SysDeptJl> list = sysDeptJlService.selectSysDeptJlList(sysDeptJl);
        ExcelUtil<SysDeptJl> util = new ExcelUtil<SysDeptJl>(SysDeptJl.class);
        util.exportExcel(response, list, "吉林地区管理数据");
    }

    /**
     * 获取吉林地区管理详细信息
     */
    @RequiresPermissions("system:jlDept:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(sysDeptJlService.selectSysDeptJlById(id));
    }

    /**
     * 新增吉林地区管理
     */
    @RequiresPermissions("system:jlDept:add")
    @Log(title = "吉林地区管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDeptJl sysDeptJl)
    {
        sysDeptJlService.checkParentId(sysDeptJl.getParentId());
        return AjaxResult.success(sysDeptJlService.insertSysDeptJl(sysDeptJl));
    }

    /**
     * 修改吉林地区管理
     */
    @RequiresPermissions("system:jlDept:edit")
    @Log(title = "吉林地区管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDeptJl sysDeptJl)
    {
        return toAjax(sysDeptJlService.updateSysDeptJl(sysDeptJl));
    }

    /**
     * 删除吉林地区管理
     */
    @RequiresPermissions("system:jlDept:remove")
    @Log(title = "吉林地区管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        for (String id : ids) {
            sysDeptJlService.checkChildrenLevel(id);
        }
        return toAjax(sysDeptJlService.deleteSysDeptJlByIds(ids));
    }
}
