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.DateUtils;
import com.ebts.common.utils.SecurityUtils;
import com.ebts.common.utils.ServerResult;
import com.ebts.generator.dao.RegularDao;
import com.ebts.generator.entity.Regular;
import com.ebts.generator.service.RegularService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 校验规则Service业务层处理
 *
 * @author binlin
 * @date 2021-01-18
 */
@Service
public class RegularServiceImpl implements RegularService {
    private Logger logger = LoggerFactory.getLogger(RegularServiceImpl.class);

    @Autowired
    private RegularDao regularDao;

    /**
     * 查询校验规则
     *
     * @param id 校验规则ID
     * @return 校验规则
     */
    @Override
    public ServerResult<Regular> selectRegularById(Long id) {
        try {
            Regular regular = regularDao.selectRegularById(id);
            if (regular != null) {
                return new ServerResult<>(true, regular);
            } else {
                return new ServerResult<>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 查询校验规则列表
     *
     * @param regular 校验规则
     * @return 校验规则
     */
    @Override
    public ServerResult<List<Regular>> selectRegularList(Regular regular) {
        try {
            List<Regular> regulars = regularDao.selectRegularList(regular);
            if (regulars.size() > 0) {
                return new ServerResult<>(true, regulars);
            } else {
                return new ServerResult<>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }
    /**
     * 查询校验规则下拉框列表
     *
     * @return 校验规则
     */
    @Override
    public ServerResult<List<Regular>> selectRegularOption() {
        try {
            List<Regular> regulars = regularDao.selectRegularOption();
            if (regulars.size() > 0) {
                return new ServerResult<>(true, regulars);
            } else {
                return new ServerResult<>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 新增校验规则
     *
     * @param regular 校验规则
     * @return 结果
     */
    @Override
    public ServerResult<Integer> insertRegular(Regular regular) {
        try {
            regular.setCreateTime(DateUtils.getNowDate());
            regular.setCreateBy(SecurityUtils.getUserId());
            int renewal = regularDao.insertRegular(regular);
            return new ServerResult<>(true, renewal);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 修改校验规则
     *
     * @param regular 校验规则
     * @return 结果
     */
    @Override
    public ServerResult<Integer> updateRegular(Regular regular) {
        try {
            regular.setUpdateTime(DateUtils.getNowDate());
            regular.setUpdateBy(SecurityUtils.getUserId());
            int renewal = regularDao.updateRegular(regular);
            return new ServerResult<>(true, renewal);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 批量删除校验规则
     *
     * @param ids 需要删除的校验规则ID
     * @return 结果
     */
    @Override
    public ServerResult<Integer> deleteRegularByIds(Long[] ids) {
        try {
            Integer renewals = regularDao.deleteRegularByIds(ids);
            return new ServerResult<>(true, renewals);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 删除校验规则信息
     *
     * @param id 校验规则ID
     * @return 结果
     */
    @Override
    public ServerResult<Integer> deleteRegularById(Long id) {
        try {
            Integer renewal = regularDao.deleteRegularById(id);
            return new ServerResult<>(true, renewal);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }
}
