package com.sunxung.factoring.service.sys.impl;

import com.github.pagehelper.PageHelper;
import com.sunxung.factoring.component.util.DateUtil;
import com.sunxung.factoring.component.util.StringUtil;
import com.sunxung.factoring.component.util.ThreadLocalUtil;
import com.sunxung.factoring.component.util.ValidatorUtil;
import com.sunxung.factoring.dict.impl.LogTypeDict;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ValidatorEntity.ValidatorType;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.entity.sys.LoginLog;
import com.sunxung.factoring.entity.sys.User;
import com.sunxung.factoring.entity.sys.vo.SearchLoginLogVo;
import com.sunxung.factoring.mapper.sys.LoginLogMapper;
import com.sunxung.factoring.service.sys.DictionaryService;
import com.sunxung.factoring.service.sys.LoginLogService;
import com.sunxung.factoring.service.sys.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @className:LoginLogServiceImpl
 * @description:登录日志实体类
 * @author:杜小姣
 * @e-mail:1261237207@qq.com
 * @date:2018年1月23日 下午2:09:47
 */
@Service
public class LoginLogServiceImpl implements LoginLogService {

    @Autowired
    private LoginLogMapper loginLogMapper;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private RoleService roleService;

    @Override
    public GridPage<LoginLog> findBySearch(SearchLoginLogVo search) {
        PageHelper.startPage(search.getPage(), search.getRows()).size();
        List<LoginLog> list = loginLogMapper.findBySearch(search);
        Integer count = loginLogMapper.countBySearch(search);
        return new GridPage<LoginLog>(search.getRows(), search.getPage(), count, list);
    }

    @Override
    public LoginLog add(LoginLog log) {
        ValidatorUtil.validator(ValidatorType.ADD, log);
        if (null != log.getLogType() && null != log.getLogType().getId()) {
            Long logTypeId = log.getLogType().getId();
            String remark = "";
            Dictionary dictionary = dictionaryService.get(logTypeId);
            if (null != dictionary) {
                String logTypeName = dictionary.getName();
                String logTypeCode = dictionary.getCode();
                // 用户被管理员注销的日志
                if (logTypeCode.equals(LogTypeDict.ChildEnum.USER_OFF_BY_ADMIN.getCode())) {
                    User loginUser = ThreadLocalUtil.getUser();
                    // 判断是否是超级管理员角色
                    boolean b = roleService.judgmentAdmin(loginUser.getId());
                    String loginUserName = "";
                    if (null != loginUser && StringUtil.isNotEmpty(loginUser.getName())) {
                        loginUserName = loginUser.getName();
                    }
                    if (b) {
                        remark = DateUtil.formateYMDHMS(new Date()) + " 用户被超级管理员" + loginUserName + "注销";
                    } else {
                        remark = DateUtil.formateYMDHMS(new Date()) + " 用户被子管理员" + loginUserName + "注销";
                    }
                } else {
                    if (StringUtil.isNotEmpty(log.getAccessIp())) {
                        remark = DateUtil.formateYMDHMS(new Date()) + " " + logTypeName + "，访问IP：" + log.getAccessIp();
                    } else {
                        remark = DateUtil.formateYMDHMS(new Date()) + " " + logTypeName;
                    }
                }
            }
            log.setRemark(remark);
        }
        loginLogMapper.add(log);
        log = get(log.getId());
        return log;
    }

    @Override
    public LoginLog get(Long id) {
        return loginLogMapper.get(id);
    }

    @Override
    public LoginLog findLastLogInByUserAndIp(String loginName, String clientIP) {
        return loginLogMapper.findLastLogInByUserAndIp(loginName,clientIP);
    }
}
