/*
 * Copyright (c) 2024 codvision.com All Rights Reserved.
 */

package com.codvision.basic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.basic.entity.Address;
import com.codvision.basic.entity.vo.AddressVO;
import com.codvision.basic.entity.vo.BatchAddressVO;
import com.codvision.basic.service.AddressService;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author KX
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/address")
@Api(tags = "地址/房屋信息")
public class AddressController {

    private final AddressService service;

    @PostMapping("/getPageResult")
    @ApiOperation("分页列表")
    public ResponseEntity<IPage<AddressVO>> getPageResult(@RequestBody QueryPage<Map<String, Object>> queryPage) {
        return new ResponseEntity<IPage<AddressVO>>().data(service.getPageResult(queryPage));
    }

    @PostMapping("/add")
    @ApiOperation("添加")
    public ResponseEntity<String> add(@Validated @RequestBody Address address) {
        service.add(address);
        return new ResponseEntity<String>().ok();
    }

    @PostMapping("/addList")
    @ApiOperation("小区下批量建楼幢")
    public ResponseEntity<String> addList(@RequestBody BatchAddressVO batchAddressVO) {
        service.addList(batchAddressVO);
        return new ResponseEntity<String>().ok();
    }

    @PostMapping("/update")
    @ApiOperation("编辑")
    public ResponseEntity<String> update(@RequestBody Address address) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (address.getId() == null) {
            throw new BusinessException(30002);
        }
        service.updateAddress(address);
        return responseEntity.ok();
    }

    @PostMapping("/details")
    @ApiOperation("详情")
    public ResponseEntity<AddressVO> details(@ApiParam(value = "地址id", required = true) @RequestParam(value = "id") Integer id) {
        return new ResponseEntity<AddressVO>().data(service.details(id));
    }

    @PostMapping("/getAddressHouseListByCode")
    @ApiOperation("根据楼幢地址编码查询楼幢下房屋信息")
    public ResponseEntity<IPage<AddressVO>> getAddressHouseListByCode(@RequestBody QueryPage<Map<String, Object>> queryPage) {
        return new ResponseEntity<IPage<AddressVO>>().data(service.getAddressHouseListByCode(queryPage));
    }

    @PostMapping("/getAddressBuildingListByCode")
    @ApiOperation("根据小区地址编码查询小区下楼幢信息")
    public ResponseEntity<IPage<AddressVO>> getAddressBuildingListByCode(@RequestBody QueryPage<Map<String, Object>> queryPage) {
        return new ResponseEntity<IPage<AddressVO>>().data(service.getAddressBuildingListByCode(queryPage));
    }

    @PostMapping("/delete")
    @ApiOperation("删除地址信息")
    public ResponseEntity<String> delete(@RequestBody List<Integer> addressIds) {
        if (CollectionUtils.isEmpty(addressIds)) {
            throw new BusinessException(30002);
        }
        service.batchLogicDel(addressIds);
        return new ResponseEntity<String>().ok();
    }
}
