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 com.ebts.generator.dao.ModuleDao;
import com.ebts.generator.entity.Module;
import com.ebts.generator.service.ModuleService;
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-24
 */
@Service
public class ModuleServiceImpl implements ModuleService {
    private Logger logger = LoggerFactory.getLogger(ModuleServiceImpl.class);

    @Autowired
    private ModuleDao moduleDao;

    /**
     * 查询模块管理
     *
     * @param id 模块管理ID
     * @return 模块管理
     */
    @Override
    public ServerResult<Module> selectModuleById(Long id) {
        try {
            Module module = moduleDao.selectModuleById(id);
            if (module != null) {
                return new ServerResult<Module>(true, module);
            } else {
                return new ServerResult<Module>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 查询模块管理列表
     *
     * @param module 模块管理
     * @return 模块管理
     */
    @Override
    public ServerResult<List<Module>> selectModuleList(Module module) {
        try {
            List<Module> moduleList = moduleDao.selectModuleList(module);
            if (moduleList.size() > 0) {
                return new ServerResult<List<Module>>(true, moduleList);
            } else {
                return new ServerResult<List<Module>>(false, ReturnConstants.RESULT_EMPTY);
            }
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 新增模块管理
     *
     * @param module 模块管理
     * @return 结果
     */
    @Override
    public ServerResult<Integer> insertModule(Module module) {
        try {
            module.setCreateBy(SecurityUtils.getUserId());
            Integer renewal = moduleDao.insertModule(module);
            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 module 模块管理
     * @return 结果
     */
    @Override
    public ServerResult<Integer> updateModule(Module module) {
        try {
            module.setUpdateBy(SecurityUtils.getUserId());
            Integer renewal = moduleDao.updateModule(module);
            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
    public ServerResult<Integer> deleteModuleByIds(Long[] ids) {
        try {
            Integer renewal = moduleDao.deleteModuleByIds(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());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }

    /**
     * 删除模块管理信息
     *
     * @param id 模块管理ID
     * @return 结果
     */
    @Override
    public ServerResult<Integer> deleteModuleById(Long id) {
        try {
            Integer renewal = moduleDao.deleteModuleById(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());
            throw new EbtsException(ReturnConstants.OP_ERROR,e);
        }
    }
}
