package com.hchyun.generator.service.impl;

import java.util.List;

import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.utils.SecurityUtils;
import com.hchyun.common.utils.ServerResult;
import com.hchyun.generator.dao.ApiclassDao;
import com.hchyun.generator.entity.Apiclass;
import com.hchyun.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 hchyun
 * @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<Apiclass>(true,apiclass);
            }else {
                return new ServerResult<Apiclass>(false, ReturnConstants.RESULT_EMPTY);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return new ServerResult<Apiclass>(false,ReturnConstants.DB_EX);
        }
    }

    /**
     * 查询接口类名列表
     * 
     * @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<List<Apiclass>>(true,apiclassList);
            }else {
                return new ServerResult<List<Apiclass>>(false,ReturnConstants.RESULT_EMPTY);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return new ServerResult<List<Apiclass>>(false,ReturnConstants.DB_EX);
        }
    }

    /**
     * 新增接口类名
     * 
     * @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.SYS_FAILL);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return new ServerResult<Integer>(false,ReturnConstants.DB_EX);
        }
    }

    /**
     * 修改接口类名
     * 
     * @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<Integer>(true,renewal);
            }else {
                return new ServerResult<Integer>(false,ReturnConstants.SYS_FAILL);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return new ServerResult<Integer>(false,ReturnConstants.DB_EX);
        }
    }

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

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