package com.sqx.modules.address.controller.app;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.Result;
import com.sqx.modules.address.entity.InsideAddress;
import com.sqx.modules.address.service.InsideAddressService;
import com.sqx.modules.address.vo.InsideAddressShortVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(value = "用户端-内部地址", tags = {"用户端-内部地址"})
@RequestMapping(value = "app/inside-address")
@RequiredArgsConstructor
public class InsideAddressController {

    private final InsideAddressService insideAddressService;

    @ApiOperation("根据站点id查询内部地址列表")
    @GetMapping("list/{stationId}")
    public Result listByStationId(@PathVariable("stationId") Long stationId) {
        List<InsideAddress> list = insideAddressService.listByStationId(stationId);
        return Result.success().put("data", list);
    }

    @ApiOperation("根据id查询内部地址")
    @GetMapping("{id}")
    public Result info(@PathVariable("id") Long id) {
        InsideAddress address = insideAddressService.getById(id);
        if(ObjectUtil.isNull(address)){
            throw new SqxException("无效的内部地址id");
        }

        InsideAddressShortVO insideAddressShortVO = new InsideAddressShortVO();
        BeanUtil.copyProperties(address, insideAddressShortVO);
        return Result.success().put("data", insideAddressShortVO);
    }
}
