package com.ylx.area.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.core.domain.R;
import com.ylx.massage.domain.Area;
import com.ylx.massage.domain.dto.CoordinateDTO;
import com.ylx.massage.domain.vo.AreaTreeNode;
import com.ylx.massage.domain.vo.CityInfoVo;
import com.ylx.area.service.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 区域数据(Area)表控制层
 *
 * @author makejava
 * @since 2024-07-01 09:22:27
 */
@RestController
@Api(tags = {"区域数据表控制层"})
@RequestMapping("area")
public class AreaController {
    /**
     * 服务对象
     */
    @Resource
    private AreaService areaService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param area 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<Area> page, Area area) {
        return R.ok(this.areaService.page(page, new QueryWrapper<>(area)));
    }

    /**
     * 按层级查询
     * @param area
     * @return R
     */
    @GetMapping("/select")
    @ApiOperation("按层级查询")
    public R select(Area area) {
        try {
            LambdaQueryWrapper<Area> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            objectLambdaQueryWrapper.eq(null != area.getLevel(),Area::getLevel, area.getLevel())
                    .eq(StringUtils.isNotBlank(area.getParentCode()),Area::getParentCode, area.getParentCode());
            return R.ok(this.areaService.list(objectLambdaQueryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.areaService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param area 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody Area area) {
        return R.ok(this.areaService.save(area));
    }

    /**
     * 修改数据
     *
     * @param area 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Area area) {
        return R.ok(this.areaService.updateById(area));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.areaService.removeByIds(idList));
    }

    /**
     * 根据城市名称查询编码
     * @param name
     * @return R
     */
    @GetMapping("/code")
    @ApiOperation("name获取code")
    public R getCodeByName(@RequestParam String name) {
        LambdaQueryWrapper<Area> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Area::getName, name);
        List<Area> list = this.areaService.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return R.fail("未找到此数据");
        }
        return R.ok(CollectionUtil.getFirst(list).getCode());
    }

    /**
     * 获取省、市、区三级树形结构
     *
     * @return R<List<AreaTreeNode>>
     */
    @GetMapping("/tree")
    @ApiOperation("获取省、市、区三级树形结构")
    public R<List<AreaTreeNode>> getAreaTree() {
        try {
            List<AreaTreeNode> tree = this.areaService.getAreaTree();
            return R.ok(tree);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

     /**
     * 获取省、市两级区域树形结构
     *
     * @return R<List<AreaTreeNode>>
     */
    @GetMapping("/provinceCityTree")
    @ApiOperation("获取省市两级区域树形结构")
    public R<List<AreaTreeNode>> getProvinceCityTree() {
        try {
            List<AreaTreeNode> tree = this.areaService.getProvinceCityTree();
            return R.ok(tree);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/city")
    @ApiOperation("根据经纬度获取城市信息")
    public R<CityInfoVo> getCityInfoByCoordinates(@RequestBody CoordinateDTO dto) {
        CityInfoVo cityInfoVo = this.areaService.getCityInfoByCoordinates(dto);
        return R.ok(cityInfoVo);
    }

}

