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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ebts.generator.dao.UniQueryDao;
import com.ebts.generator.entity.UniQuery;
import com.ebts.generator.service.UniQueryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 万能查询Service业务层处理
 *
 * @author binlin
 * @date 2021-01-30
 */
@Service
public class UniQueryServiceImpl implements UniQueryService {
    private Logger logger = LoggerFactory.getLogger(UniQueryServiceImpl.class);

    @Autowired
    private UniQueryDao uniQueryDao;

    /**
     * 查询万能查询
     *
     * @param id 万能查询ID
     * @return 万能查询
     */
    @Override
    public ServerResult<UniQuery> selectUniQueryById(Long id) {
        try {
            UniQuery uniQuery = uniQueryDao.selectUniQueryById(id);
            if (uniQuery != null) {
                return new ServerResult<UniQuery>(true, uniQuery);
            } else {
                return new ServerResult<UniQuery>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 查询万能查询列表
     *
     * @param uniQuery 万能查询
     * @return 万能查询
     */
    @Override
    public ServerResult<List<UniQuery>> selectUniQueryList(UniQuery uniQuery) {
        try {
            List<UniQuery> uniQueryList = uniQueryDao.selectUniQueryList(uniQuery);
            if (uniQueryList.size() > 0) {
                return new ServerResult<List<UniQuery>>(true, uniQueryList);
            } else {
                return new ServerResult<List<UniQuery>>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 新增万能查询
     *
     * @param uniQuery 万能查询
     * @return 结果
     */
    @Override
    public ServerResult<Integer> insertUniQuery(UniQuery uniQuery) {
        try {
            uniQuery.setCreateBy(SecurityUtils.getUserId());
            Integer renewal = uniQueryDao.insertUniQuery(uniQuery);
            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());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 修改万能查询
     *
     * @param uniQuery 万能查询
     * @return 结果
     */
    @Override
    public ServerResult<Integer> updateUniQuery(UniQuery uniQuery) {
        try {
            uniQuery.setUpdateBy(SecurityUtils.getUserId());
            Integer renewal = uniQueryDao.updateUniQuery(uniQuery);
            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());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 批量删除万能查询
     *
     * @param ids 需要删除的万能查询ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServerResult<Integer> deleteUniQueryByIds(Long[] ids) {
        try {
            Integer renewal = uniQueryDao.deleteUniQueryByIds(ids);
            for (Long id : ids) {
                uniQueryDao.deleteUniQueryByUqId(id);
            }
            if (renewal > 0) {
                return new ServerResult<>(true, renewal);
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ServerResult<>(false, ReturnConstants.SYS_FAILL);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 删除万能查询信息
     *
     * @param id 万能查询ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ServerResult<Integer> deleteUniQueryById(Long id) {
        try {
            Integer renewal = uniQueryDao.deleteUniQueryById(id);
            uniQueryDao.deleteUniQueryByUqId(id);
            if (renewal > 0) {
                return new ServerResult<>(true, renewal);
            } else {
                return new ServerResult<>(false, ReturnConstants.SYS_FAILL);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }
}