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.ResultsDao;
import com.hchyun.test.entity.Results;
import com.hchyun.test.service.ResultsService;

/**
 * 成绩Service业务层处理
 *
 * @author hchyun
 * @date 2021-01-24
 */
@Service
public class ResultsServiceImpl implements ResultsService {
    private Logger logger = LoggerFactory.getLogger(ResultsServiceImpl.class);

    @Autowired
    private ResultsDao resultsDao;

    /**
     * 查询成绩
     *
     * @param id 成绩ID
     * @return 成绩
     */
    @Override
    public ServerResult<Results> selectResultsById(Long id) {
        try {
            Results results = resultsDao.selectResultsById(id);
            if (results != null) {
                return new ServerResult<Results>(true, results);
            } else {
                return new ServerResult<Results>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return new ServerResult<Results>(false, ReturnConstants.DB_EX);
        }
    }

    /**
     * 查询成绩列表
     *
     * @param results 成绩
     * @return 成绩
     */
    @Override
    public ServerResult<List<Results>> selectResultsList(Results results) {
        try {
            List<Results> resultsList = resultsDao.selectResultsList(results);
            if (resultsList.size() > 0) {
                return new ServerResult<List<Results>>(true, resultsList);
            } else {
                return new ServerResult<List<Results>>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return new ServerResult<List<Results>>(false, ReturnConstants.DB_EX);
        }
    }

    /**
     * 新增成绩
     *
     * @param results 成绩
     * @return 结果
     */
    @Override
    public ServerResult<Integer> insertResults(Results results) {
        try {
            Integer renewal = resultsDao.insertResults(results);
            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 results 成绩
     * @return 结果
     */
    @Override
    public ServerResult<Integer> updateResults(Results results) {
        try {
            Integer renewal = resultsDao.updateResults(results);
            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> deleteResultsByIds(Long[] ids) {
        try {
            Integer renewal = resultsDao.deleteResultsByIds(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> deleteResultsById(Long id) {
        try {
            Integer renewal = resultsDao.deleteResultsById(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);
        }
    }
}
