package com.hchyun.test.service.impl;

import java.util.List;

import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.utils.ServerResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hchyun.test.dao.TestTreeDao;
import com.hchyun.test.entity.TestTree;
import com.hchyun.test.service.TestTreeService;

/**
 * 树测试Service业务层处理
 * 
 * @author hchyun
 * @date 2021-01-23
 */
@Service
public class TestTreeServiceImpl implements TestTreeService {
    private Logger logger = LoggerFactory.getLogger(TestTreeServiceImpl.class);

    @Autowired
    private TestTreeDao testTreeDao;

    /**
     * 查询树测试
     * 
     * @param id 树测试ID
     * @return 树测试
     */
    @Override
    public ServerResult<TestTree> selectTestTreeById(Long id) {
        try {
            TestTree testTree = testTreeDao.selectTestTreeById(id);
            if (testTree != null){
                return new ServerResult<TestTree>(true,testTree);
            }else {
                return new ServerResult<TestTree>(false, ReturnConstants.RESULT_EMPTY);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return new ServerResult<TestTree>(false,ReturnConstants.DB_EX);
        }
    }

    /**
     * 查询树测试列表
     * 
     * @param testTree 树测试
     * @return 树测试
     */
    @Override
    public ServerResult<List<TestTree>> selectTestTreeList(TestTree testTree) {
        try {
            List<TestTree> testTreeList = testTreeDao.selectTestTreeList(testTree);
            if (testTreeList.size()>0){
                return new ServerResult<List<TestTree>>(true,testTreeList);
            }else {
                return new ServerResult<List<TestTree>>(false,ReturnConstants.RESULT_EMPTY);
            }
        }catch (RuntimeException e){
            logger.error(e.getMessage());
            return new ServerResult<List<TestTree>>(false,ReturnConstants.DB_EX);
        }
    }

    /**
     * 新增树测试
     * 
     * @param testTree 树测试
     * @return 结果
     */
    @Override
    public ServerResult<Integer> insertTestTree(TestTree testTree) {
        try {
            Integer renewal = testTreeDao.insertTestTree(testTree);
            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 testTree 树测试
     * @return 结果
     */
    @Override
    public ServerResult<Integer> updateTestTree(TestTree testTree) {
        try {
            Integer renewal = testTreeDao.updateTestTree(testTree);
            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> deleteTestTreeByIds(Long[] ids) {
        try {
            Integer renewal = testTreeDao.deleteTestTreeByIds(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> deleteTestTreeById(Long id) {
        try {
            Integer renewal = testTreeDao.deleteTestTreeById(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);
        }
    }
}
