/*
 * 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.PersonAddressRelation;
import com.codvision.basic.entity.dto.PersonAddressRelationDTO;
import com.codvision.basic.entity.vo.PersonAddressRelationVO;
import com.codvision.basic.service.PersonAddressRelationService;
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 org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author KX
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/personAddressRelation")
@Api(tags = "人房关系")
public class PersonAddressRelationController {

    private final PersonAddressRelationService service;


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

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

    @PostMapping("/update")
    @ApiOperation("编辑")
    public ResponseEntity<String> update(@RequestBody PersonAddressRelation personAddressRelation) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (personAddressRelation.getId() == null) {
            throw new BusinessException(3002);
        }

        service.updateById(personAddressRelation);
        return responseEntity.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@ApiParam(value = "关联id", required = true) @RequestBody List<Integer> ids) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (CollectionUtils.isEmpty(ids)) {
            throw new BusinessException(3002);
        }
        service.removeBatchByIds(ids);
        return responseEntity.ok();
    }

    @PostMapping("/moveOut")
    @ApiOperation("迁出")
    public ResponseEntity<String> moveOut(@ApiParam(value = "关联id", required = true) @RequestBody List<Integer> ids) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (CollectionUtils.isEmpty(ids)) {
            throw new BusinessException(3002);
        }

        service.moveOut(ids);
        return responseEntity.ok();
    }

}
