package com.xinelu.manage.controller.sysarea;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.common.custominterface.Update;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.sysarea.SysArea;
import com.xinelu.manage.service.sysarea.ISysAreaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 区域Controller
 *
 * @author jihan
 * @date 2022-09-02
 */
@RestController
@RequestMapping("/system/area")
public class SysAreaController extends BaseController {
    @Resource
    private ISysAreaService sysAreaService;

    /**
     * 查询区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:area:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysArea sysArea) {
        startPage();
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        return getDataTable(list);
    }

    /**
     * 导出区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:area:export')")
    @Log(title = "区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysArea sysArea) {
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        ExcelUtil<SysArea> util = new ExcelUtil<>(SysArea.class);
        util.exportExcel(response, list, "区域数据");
    }

    /**
     * 获取区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:area:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysAreaService.selectSysAreaById(id));
    }

    /**
     * 新增区域
     */
    @PreAuthorize("@ss.hasPermi('system:area:add')")
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Validated(Insert.class) SysArea sysArea) {
        return sysAreaService.insertSysArea(sysArea);
    }

    /**
     * 修改区域
     */
    @PreAuthorize("@ss.hasPermi('system:area:edit')")
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody @Validated(Update.class) SysArea sysArea) {
        return sysAreaService.updateSysArea(sysArea);
    }

    /**
     * 删除区域
     */
    @PreAuthorize("@ss.hasPermi('system:area:remove')")
    @Log(title = "区域", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysAreaService.deleteSysAreaByIds(ids));
    }


    /**
     * 获取区域详细信息
     */
    @GetMapping("/getSubordinateRegions")
    public AjaxResult getSubordinateRegionsFindSuperiorRegions(@RequestParam("areaCode") String areaCode) {
        if (StringUtils.isBlank(areaCode)) {
            return AjaxResult.error("区域编码不能为空！");
        }
        return AjaxResult.success(sysAreaService.getSubordinateRegionsFindSuperiorRegions(areaCode));
    }

    /**
     * 查询区域信息(不分页）
     *
     * @param sysArea 区域信息
     * @return AjaxResult
     */
    @GetMapping("/selectAreaList")
    public AjaxResult selectAreaList(SysArea sysArea) {
        return sysAreaService.selectAreaList(sysArea);
    }

    /**
     * 是否是偏远地区状态修改
     *
     * @param sysArea 区域信息
     * @return AjaxResult
     */
    @PostMapping("/updateRemoteSigns")
    public AjaxResult updateRemoteSigns(SysArea sysArea) {
        return sysAreaService.updateRemoteSigns(sysArea);
    }
}