
package com.tmerclub.cloud.delivery.controller.admin;

import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.order.vo.AreaVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.order.dto.AreaDTO;
import com.tmerclub.cloud.delivery.service.AreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 省市区地区信息
 *
 * @author YXF
 * @date 2020-11-25 14:48:52
 */
@RestController("adminAreaController")
@RequestMapping("/mp/area")
@Tag(name = "店铺-地区信息")
public class AreaController {

    @Autowired
    private AreaService areaService;


    @GetMapping("/list")
    @Operation(summary = "获取省市区地区信息列表", description = "分页获取省市区地区信息列表")
    public ServerResponseEntity<List<AreaVO>> list(AreaDTO areaDTO) {
        List<AreaVO> list = areaService.list(areaDTO);
        return ServerResponseEntity.success(list);
    }

    @GetMapping
    @Operation(summary = "获取省市区地区信息", description = "根据areaId获取省市区地区信息")
    public ServerResponseEntity<AreaVO> getByAreaId(@RequestParam Long areaId) {
        return ServerResponseEntity.success(areaService.getByAreaId(areaId));
    }

    @GetMapping("/parentByPid")
    @Operation(summary = "通过父级id获取区域列表", description = "通过父级id获取区域列表")
    public ServerResponseEntity<List<AreaVO>> parentByPid(Long pid) {
        List<AreaVO> list = areaService.listByPid(pid);
        return ServerResponseEntity.success(list);
    }

    @GetMapping("/getAreaListInfo")
    @Operation(summary = "获取可用的区域省市区信息（三级地址列表，省市区结构）", description = "获取可用的区域省市区信息")
    public ServerResponseEntity<List<AreaVO>> getAreaListInfo() {
        return ServerResponseEntity.success(areaService.getAreaListInfo());
    }

    @GetMapping("/AreaOfEnableList")
    @Operation(summary = "获取可用的省市区列表（三级地址列表）", description = "获取可用的省市区列表")
    public ServerResponseEntity<List<AreaVO>> AreaOfEnableList() {
        List<AreaVO> list = areaService.listAreaOfEnable();
        List<AreaVO> res = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (AreaVO pArea : list) {
            res.add(pArea);
            for (AreaVO cArea : pArea.getAreas()) {
                res.add(cArea);
                res.addAll(cArea.getAreas());
                cArea.setAreas(null);
            }
            pArea.setAreas(null);
        }
        return ServerResponseEntity.success(res);
    }
}
