package com.cm.common.plugin.service.systemlogger.impl;

import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.exception.UpdateException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import com.cm.common.plugin.dao.systemlogger.ISystemLoggerDao;
import com.cm.common.plugin.pojo.dtos.systemlogger.SystemLoggerDTO;
import com.cm.common.plugin.service.systemlogger.ISystemLoggerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SystemLoggerServiceImpl
 * @Description: 系统日志
 * @Author: admin
 * @Date: 2019-05-28 17:42:45
 * @Version: 1.0
 **/
@Service
public class SystemLoggerServiceImpl extends AbstractService implements ISystemLoggerService {

    @Autowired
    private ISystemLoggerDao systemLoggerDao;

    @Override
    public SuccessResult saveSystemLogger(Map<String, Object> params) throws SaveException {
        systemLoggerDao.saveSystemLogger(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult removeSystemLogger(Map<String, Object> params) throws RemoveException {
        params.put("systemLoggerIds", Arrays.asList(params.get("systemLoggerIds").toString().split("_")));
        setUpdateInfo(params);
        systemLoggerDao.removeSystemLogger(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateSystemLogger(Map<String, Object> params) throws UpdateException {
        setUpdateInfo(params);
        systemLoggerDao.updateSystemLogger(params);
        return new SuccessResult();
    }

    @Override
    public List<SystemLoggerDTO> listSystemLoggerAll(Map<String, Object> params) throws SearchException {
        List<SystemLoggerDTO> systemLoggerDTOs = systemLoggerDao.listSystemLogger(params);
        return systemLoggerDTOs;
    }

    @Override
    public SystemLoggerDTO getSystemLogger(Map<String, Object> params) throws SearchException {
        return systemLoggerDao.getSystemLogger(params);
    }

    @Override
    public SuccessResultList<List<SystemLoggerDTO>> listPageSystemLogger(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<SystemLoggerDTO> systemLoggerDTOs = systemLoggerDao.listSystemLogger(page.getParams());
        PageInfo<SystemLoggerDTO> pageInfo = new PageInfo<>(systemLoggerDTOs);
        return new SuccessResultList<>(systemLoggerDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public List<SystemLoggerDTO> listSystemLogger(Map<String, Object> params) throws SearchException {
        return systemLoggerDao.listSystemLogger(params);
    }

    @Override
    public int countSystemLogger(Map<String, Object> params) throws SearchException {
        Integer count = systemLoggerDao.countSystemLogger(params);
        return count == null ? 0 : count;
    }

}