package com.xinyilu.web.controller.systemarea;

import com.xinyilu.common.annotation.Log;
import com.xinyilu.common.core.controller.BaseController;
import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.custominterface.Insert;
import com.xinyilu.common.custominterface.Update;
import com.xinyilu.common.enums.BusinessType;
import com.xinyilu.common.utils.poi.ExcelUtil;
import com.xinyilu.system.domain.systemarea.SysArea;
import com.xinyilu.system.service.systemarea.ISysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 区域管理Controller
 *
 * @author jihan
 * @date 2022-07-27
 */
@RestController
@RequestMapping("/system/area")
public class SysAreaController extends BaseController {
    @Autowired
    private ISysAreaService sysAreaService;

    /**
     * 查询区域管理列表
     *
     * @param sysArea 区域对象
     * @return AjaxResult
     */
//    @PreAuthorize("@ss.hasPermi('system:area:list')")
    @GetMapping("/list")
    public AjaxResult list(SysArea sysArea) {
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        return AjaxResult.success(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>(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));
    }

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

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

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

    /**
     * 查询省市区域管理列表
     *
     * @param sysArea 区域对象
     * @return AjaxResult
     */
    @GetMapping("/listCityInfo")
    public AjaxResult listCityInfo(SysArea sysArea) {
        List<SysArea> list = sysAreaService.getCityAreaInfoList(sysArea);
        return AjaxResult.success(list);
    }

    /**
     * 查询省级区域信息信息
     *
     * @return 省级区域信息集合
     */
    @GetMapping("/getProvinceInfo")
    public AjaxResult getProvinceAreaInfo() {
        return sysAreaService.getProvinceAreaInfo();
    }

    /**
     * 根据父级id
     *
         * @param id 区域id
     * @return 所属下级区域信息集合
     */
    @GetMapping("/getSubordinateInfo")
    public AjaxResult getSubordinateAreaInfo(Long id) {
        if (Objects.isNull(id)) {
            return AjaxResult.error("上级区域id不能为空");
        }
        return sysAreaService.getSubordinateAreaInfo(id);
    }
}
