package com.ebts.generator.service.impl;

import java.util.List;

import com.ebts.common.constant.ReturnConstants;
import com.ebts.common.exception.EbtsException;
import com.ebts.common.utils.SecurityUtils;
import com.ebts.common.utils.ServerResult;
import com.ebts.generator.dao.ApiClassDao;
import com.ebts.generator.entity.ApiClass;
import com.ebts.generator.service.ApiClassService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 接口类名Service业务层处理
 *
 * @author binlin
 * @date 2021-01-24
 */
@Service
public class ApiClassServiceImpl implements ApiClassService {
    private Logger logger = LoggerFactory.getLogger(ApiClassServiceImpl.class);

    @Autowired
    private ApiClassDao apiclassDao;

    /**
     * 查询接口类名
     *
     * @param id 接口类名ID
     * @return 接口类名
     */
    @Override
    public ServerResult<ApiClass> selectApiclassById(Long id) {
        try {
            ApiClass apiclass = apiclassDao.selectApiclassById(id);
            if (apiclass != null) {
                return new ServerResult<>(true, apiclass);
            } else {
                return new ServerResult<>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 查询接口类名列表
     *
     * @param apiclass 接口类名
     * @return 接口类名
     */
    @Override
    public ServerResult<List<ApiClass>> selectApiclassList(ApiClass apiclass) {
        try {
            List<ApiClass> apiClassList = apiclassDao.selectApiclassList(apiclass);
            if (apiClassList.size() > 0) {
                return new ServerResult<>(true, apiClassList);
            } else {
                return new ServerResult<>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 新增接口类名
     *
     * @param apiclass 接口类名
     * @return 结果
     */
    @Override
    public ServerResult<Integer> insertApiclass(ApiClass apiclass) {
        try {
            apiclass.setCreateBy(SecurityUtils.getUserId());
            Integer renewal = apiclassDao.insertApiclass(apiclass);
            if (renewal > 0) {
                return new ServerResult<Integer>(true, renewal);
            } else {
                return new ServerResult<Integer>(false, ReturnConstants.OP_ERROR);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 修改接口类名
     *
     * @param apiclass 接口类名
     * @return 结果
     */
    @Override
    public ServerResult<Integer> updateApiclass(ApiClass apiclass) {
        try {
            apiclass.setUpdateBy(SecurityUtils.getUserId());
            Integer renewal = apiclassDao.updateApiclass(apiclass);
            if (renewal > 0) {
                return new ServerResult<>(true, renewal);
            } else {
                return new ServerResult<>(false, ReturnConstants.OP_ERROR);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 批量删除接口类名
     *
     * @param ids 需要删除的接口类名ID
     * @return 结果
     */
    @Override
    public ServerResult<Integer> deleteApiclassByIds(Long[] ids) {
        try {
            Integer renewal = apiclassDao.deleteApiclassByIds(ids);
            if (renewal > 0) {
                return new ServerResult<>(true, renewal);
            } else {
                return new ServerResult<>(false, ReturnConstants.OP_ERROR);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 删除接口类名信息
     *
     * @param id 接口类名ID
     * @return 结果
     */
    @Override
    public ServerResult<Integer> deleteApiclassById(Long id) {
        try {
            Integer renewal = apiclassDao.deleteApiclassById(id);
            if (renewal > 0) {
                return new ServerResult<>(true, renewal);
            } else {
                return new ServerResult<>(false, ReturnConstants.OP_ERROR);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }
}
