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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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("/obtain_area")
    public R<List<DistrictEntity>> obtainArea() {
        AppProperties.DefaultPlace defaultPlace = appProperties.getDefaultPlace();
        return R.success(districtService.list(Wrappers.<DistrictEntity>lambdaQuery()
                .eq(DistrictEntity::getPid, defaultPlace.getCity())
                .orderByAsc(DistrictEntity::getSort, DistrictEntity::getId)));
    }

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

}
