package com.qxgmat.service.inline;

import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.Tools;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.nuliji.tools.mybatis.Example;
import com.qxgmat.data.dao.ManagerLogMapper;
import com.qxgmat.data.dao.entity.Manager;
import com.qxgmat.data.dao.entity.ManagerLog;
import com.qxgmat.help.ShiroHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

@Service
public class ManagerLogService extends AbstractService {
    @Resource
    private ManagerLogMapper managerLogMapper;

    @Resource
    private ShiroHelp shiroHelp;

    public boolean log(HttpServletRequest request){
        Manager manager = shiroHelp.getLoginManager();
        ManagerLog managerLog = ManagerLog.builder()
                .content(request.getRequestURI())
                .managerId(manager.getId())
                .username(manager.getUsername())
                .ip(Tools.getClientIp(request))
                .build();
        int result = insert(managerLogMapper, managerLog);
        return result > 0;
    }

    public ManagerLog add(ManagerLog managerLog){
        int result = insert(managerLogMapper, managerLog);
        managerLog = one(managerLogMapper, managerLog.getId());
        if(managerLog == null){
            throw new SystemException("日志添加失败");
        }
        return managerLog;
    }

    public ManagerLog get(Long id){
        ManagerLog in = one(managerLogMapper, id);

        if(in == null){
            throw new ParameterException("日志不存在");
        }
        return in;
    }

    public Page<ManagerLog> select(int page, int pageSize){return select(managerLogMapper, page, pageSize);
    }

    public Page<ManagerLog> select(int page, int pageSize, boolean sort){
        Example example = new Example(ManagerLog.class);

        example.setOrderByClause("id desc");
        return select(managerLogMapper, example, page, pageSize);
    }

    public List<ManagerLog> select(Collection ids){
        return select(managerLogMapper, ids);
    }


}
