/*
 * 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.Enterprise;
import com.codvision.basic.entity.vo.EnterpriseVO;
import com.codvision.basic.service.EnterpriseService;
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("/enterprise")
@Api(tags = "企业单位(经济组织)")
public class EnterpriseController {

    private final EnterpriseService service;

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

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

    @PostMapping("/update")
    @ApiOperation("编辑")
    public ResponseEntity<String> update(@RequestBody Enterprise enterprise) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (enterprise.getId() == null) {
           throw new BusinessException(30002);
        }
        service.updateEnterprise(enterprise);
        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();
    }
}
