package com.fufan.mtsaas.modules.base.log.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fufan.mtsaas.modules.base.log.bo.request.LoginLogQueryRequest;
import com.fufan.mtsaas.modules.base.log.dao.TLoginLogDao;
import com.fufan.mtsaas.modules.base.log.model.TLoginLog;
import com.fufan.mtsaas.modules.base.log.service.TSysLoginLogSvc;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.tools.MapTools;
import com.fufan.mtsaas.util.ParamUtil;
import com.github.pagehelper.ISelect;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 系统登录日志表 服务实现类
 * </p>
 *
 * @author Liby
 * @since 2022-03-16
 */
@Service("TSysLoginLogSvcImpl")
@Slf4j
@Primary
public class TSysLoginLogSvcImpl extends ServiceImpl<TLoginLogDao, TLoginLog> implements TSysLoginLogSvc {
    @Autowired
    private TLoginLogDao tLoginLogDao;

    @Override
    public Result doQueryPage(LoginLogQueryRequest loginLogQueryRequest) {
        RequestPojo requestPojo = ParamUtil.doChange();
        Map<String, String> map = MapTools.objectToMap(loginLogQueryRequest);
        Result result = new Result();

        Page page = new Page();
        if (!StringUtils.isEmpty(map.get("pageSize")) && !StringUtils.isEmpty(map.get("currentPage"))) {
            page.setSize(Integer.parseInt(map.get("pageSize")));
            page.setCurrent(Integer.parseInt(map.get("currentPage")));
        }

        int total = (int) com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                tLoginLogDao.selectList(page, map);
            }
        });

        List<TLoginLog> logResponseList = tLoginLogDao.selectList(page, map);

        page.setTotal((int) total);
        page.setRecords(logResponseList);
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        result.setResData(page);

        return result;
    }
}
