package com.ebts.generator.service.impl;

import com.ebts.common.constant.ReturnConstants;
import com.ebts.common.core.entity.Result;
import com.ebts.common.utils.SecurityUtils;
import com.ebts.generator.dao.ApiClassDao;
import com.ebts.generator.entity.ApiClass;
import com.ebts.generator.service.ApiClassService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 接口类名Service业务层处理
 *
 * @author binlin
 * @date 2021-01-24
 */
@Service
public class ApiClassServiceImpl implements ApiClassService {

    @Resource
    private ApiClassDao apiclassDao;

    /**
     * 查询接口类名
     *
     * @param id 接口类名ID
     * @return 接口类名
     */
    @Override
    public Result<ApiClass> selectApiclassById(Long id) {
        ApiClass apiclass = apiclassDao.selectApiclassById(id);
        if (apiclass != null) {
            return Result.ok(apiclass);
        } else {
            return Result.info(ReturnConstants.RESULT_EMPTY);
        }
    }

    /**
     * 查询接口类名列表
     *
     * @param apiclass 接口类名
     * @return 接口类名
     */
    @Override
    public Result<List<ApiClass>> selectApiclassList(ApiClass apiclass) {
        List<ApiClass> apiClassList = apiclassDao.selectApiclassList(apiclass);
        if (apiClassList.size() > 0) {
            return Result.ok(apiClassList);
        } else {
            return Result.info(ReturnConstants.RESULT_EMPTY);
        }
    }

    /**
     * 新增接口类名
     *
     * @param apiclass 接口类名
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result<Integer> insertApiclass(ApiClass apiclass) {
        apiclass.setCreateBy(SecurityUtils.getUserId());
        apiclassDao.insertApiclass(apiclass);
        return Result.ok();
    }

    /**
     * 修改接口类名
     *
     * @param apiclass 接口类名
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result<Integer> updateApiclass(ApiClass apiclass) {
        apiclass.setUpdateBy(SecurityUtils.getUserId());
        int renewal = apiclassDao.updateApiclass(apiclass);
        if (renewal > 0) {
            return Result.ok(renewal);
        } else {
            return Result.info(ReturnConstants.OP_ERROR);
        }
    }

    /**
     * 批量删除接口类名
     *
     * @param ids 需要删除的接口类名ID
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result<Integer> deleteApiclassByIds(Long[] ids) {
        int renewal = apiclassDao.deleteApiclassByIds(ids);
        if (renewal > 0) {
            return Result.ok(renewal);
        } else {
            return Result.info(ReturnConstants.OP_ERROR);
        }
    }

    /**
     * 删除接口类名信息
     *
     * @param id 接口类名ID
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Result<Integer> deleteApiclassById(Long id) {
        int renewal = apiclassDao.deleteApiclassById(id);
        if (renewal > 0) {
            return Result.ok(renewal);
        } else {
            return Result.info(ReturnConstants.OP_ERROR);
        }
    }
}
