package com.hchyun.system.service.impl;

import java.util.List;

import com.hchyun.common.utils.DateUtils;

import com.hchyun.common.constant.ReturnConstants;
import com.hchyun.common.utils.SecurityUtils;
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.system.dao.RegularDao;
import com.hchyun.system.entity.Regular;
import com.hchyun.system.service.RegularService;

/**
 * 校验规则Service业务层处理
 *
 * @author hchyun
 * @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<Regular>(true, regular);
            } else {
                return new ServerResult<Regular>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return new ServerResult<Regular>(false, ReturnConstants.DB_EX);
        }
    }

    /**
     * 查询校验规则列表
     *
     * @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<List<Regular>>(true, regulars);
            } else {
                return new ServerResult<List<Regular>>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return new ServerResult<List<Regular>>(false, ReturnConstants.DB_EX);
        }
    }

    /**
     * 新增校验规则
     *
     * @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<Integer>(true, renewal);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return new ServerResult<Integer>(false, ReturnConstants.DB_EX);
        }
    }

    /**
     * 修改校验规则
     *
     * @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<Integer>(true, renewal);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return new ServerResult<Integer>(false, ReturnConstants.DB_EX);
        }
    }

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

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