package com.ebts.generator.service.impl;

import java.util.List;

import com.ebts.common.constant.ReturnConstants;
import com.ebts.common.core.entity.Result;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 校验规则Service业务层处理
 *
 * @author binlin
 * @date 2021-01-18
 */
@Service
@Slf4j
public class RegularServiceImpl implements RegularService {

    @Resource
    private RegularDao regularDao;

    /**
     * 查询校验规则
     *
     * @param id 校验规则ID
     * @return 校验规则
     */
    @Override
    public Result<Regular> selectRegularById(Long id) {
        Regular regular = regularDao.selectRegularById(id);
        if (regular != null) {
            return Result.ok(regular);
        } else {
            return Result.info(ReturnConstants.RESULT_EMPTY);
        }
    }

    /**
     * 查询校验规则列表
     *
     * @param regular 校验规则
     * @return 校验规则
     */
    @Override
    public Result<List<Regular>> selectRegularList(Regular regular) {
        List<Regular> regulars = regularDao.selectRegularList(regular);
        if (regulars.size() > 0) {
            return Result.ok(regulars);
        } else {
            return Result.info(ReturnConstants.RESULT_EMPTY);
        }
    }

    /**
     * 查询校验规则下拉框列表
     *
     * @return 校验规则
     */
    @Override
    public Result<List<Regular>> selectRegularOption() {
        List<Regular> regulars = regularDao.selectRegularOption();
        if (regulars.size() > 0) {
            return Result.ok(regulars);
        } else {
            return Result.info(ReturnConstants.RESULT_EMPTY);
        }
    }

    /**
     * 新增校验规则
     *
     * @param regular 校验规则
     * @return 结果
     */
    @Override
    public Result<Integer> insertRegular(Regular regular) {
        regular.setCreateTime(DateUtils.getNowDate());
        regular.setCreateBy(SecurityUtils.getUserId());
        regularDao.insertRegular(regular);
        return Result.ok();
    }

    /**
     * 修改校验规则
     *
     * @param regular 校验规则
     * @return 结果
     */
    @Override
    public Result<Integer> updateRegular(Regular regular) {
        regular.setUpdateTime(DateUtils.getNowDate());
        regular.setUpdateBy(SecurityUtils.getUserId());
        regularDao.updateRegular(regular);
        return Result.ok();
    }

    /**
     * 批量删除校验规则
     *
     * @param ids 需要删除的校验规则ID
     * @return 结果
     */
    @Override
    public Result<Integer> deleteRegularByIds(Long[] ids) {
        regularDao.deleteRegularByIds(ids);
        return Result.ok();
    }

    /**
     * 删除校验规则信息
     *
     * @param id 校验规则ID
     * @return 结果
     */
    @Override
    public Result<Integer> deleteRegularById(Long id) {
        regularDao.deleteRegularById(id);
        return Result.ok();
    }
}
