/*
 * 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.SocialOrg;
import com.codvision.basic.entity.vo.SocialOrgVO;
import com.codvision.basic.service.SocialOrgService;
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 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 codvision
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/socialOrg")
@Api(tags = "社会组织")
public class SocialOrgController {

    private final SocialOrgService service;

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

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

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

    @PostMapping("/delete")
    @ApiOperation("删除社会组织信息")
    public ResponseEntity<String> delete(@RequestBody List<Integer> ids) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (CollectionUtils.isEmpty(ids)) {
            throw new BusinessException(30002);
        }
        service.removeBatchByIds(ids);
        return responseEntity.ok();
    }
}
