package com.njzscloud.supervisory.sys.district.controller;

import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.config.AppProperties;
import com.njzscloud.supervisory.sys.district.pojo.entity.DistrictEntity;
import com.njzscloud.supervisory.sys.district.pojo.result.DistrictTreeResult;
import com.njzscloud.supervisory.sys.district.service.DistrictService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 省市区表
 */
@Slf4j
@RestController
@RequestMapping("/district")
@RequiredArgsConstructor
public class DistrictController {
    private final DistrictService districtService;
    private final AppProperties appProperties;

    /**
     * 新增
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody DistrictEntity districtEntity) {
        districtService.add(districtEntity);
        return R.success();
    }

    /**
     * 修改
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody DistrictEntity districtEntity) {
        districtService.modify(districtEntity);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        districtService.del(ids);
        return R.success();
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<DistrictEntity> detail(@RequestParam Long id) {
        return R.success(districtService.detail(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/paging")
    public R<PageResult<DistrictEntity>> paging(PageParam pageParam, DistrictEntity districtEntity) {
        return R.success(districtService.paging(pageParam, districtEntity));
    }

    /**
     * 获取默认区域
     */
    @GetMapping("/default_place")
    public R<AppProperties.DefaultPlace> defaultPlace() {
        AppProperties.DefaultPlace defaultPlace = appProperties.getDefaultPlace();
        return R.success(defaultPlace);
    }

    /**
     * 列表查询
     *
     * @param pid  父级pid
     * @param name 地区名称
     */
    @GetMapping("/list")
    public R<List<DistrictEntity>> listDistrict(
            @RequestParam(value = "pid", required = false, defaultValue = "0") String pid,
            @RequestParam(value = "name", required = false) String name
    ) {
        return R.success(districtService.listDistrict(pid, name));
    }

    /**
     * 树查询
     */
    @GetMapping("/tree")
    public R<List<DistrictTreeResult>> tree() {
        return R.success(districtService.tree());
    }


    /**
     * 区查询
     */
    @GetMapping("/areaList")
    public R<List<DistrictEntity>> areaList() {
        return R.success(districtService.areaList());
    }
}
