package com.hs.api.service.Impl;

import com.hs.api.common.exceptions.DBConfigurationError;
import com.hs.api.common.utils.TokenUtil;
import com.hs.api.mapper.DicOrgMapper;
import com.hs.api.mapper.SysRoleMapper;
import com.hs.api.mapper.SysUserMapper;
import com.hs.api.mapper.SysVersionMapper;
import com.hs.api.model.*;
import com.hs.api.model.respmodel.LoginInfo;
import com.hs.api.service.LoginService;
import com.hs.api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UserService userServiceImpl;

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private DicOrgMapper dicOrgMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysVersionMapper sysVersionMapper;

    @Override
    public LoginInfo login(String logName, String password, String macAddrss, String ip) {

        LoginInfo loginInfo = new LoginInfo();

        SysUser user = userServiceImpl.findByLoginName(logName);
        if (user != null && user.getPassword().equals(password) && (user.getMacAddress() == null || user.getMacAddress().equals(macAddrss))) {
            String token = TokenUtil.getToken(password, user);
            //修改ip以及最后登录时间、MAC地址
            user.setLastIp(ip);
            user.setLastTime(new Date());
            user.setMacAddress(macAddrss);
            sysUserMapper.updateByPrimaryKey(user);

            user.setPassword(null);
            loginInfo.setToken(token);
            loginInfo.setUser(user);

            //查询用户角色
            SysRole role = sysRoleMapper.selectByUser(user.getId());
            loginInfo.setRole(role);
            //查询用户机构
            List<DicOrg> orgList = dicOrgMapper.selectByUser(user.getId());
            if (orgList != null && orgList.size() > 0) {
                List<SerDepartment> deptList = dicOrgMapper.selectDeptByUser(user.getId(), orgList.get(0).getId());
                loginInfo.setDept(deptList != null && deptList.size() > 0 ? deptList.get(0) : null);
            }
            loginInfo.setOrg(orgList);
            //查询病案信息
            SysVersion version = sysVersionMapper.selectLastDate();
            loginInfo.setVersion(version);
        } else {
            throw new DBConfigurationError("账号、密码或者MAC地址错误！");
        }
        return loginInfo;
    }

    public boolean updatePass(String logName, String oldPassword, String newPassword) {
        int result = 0;
        SysUser user = userServiceImpl.findByLoginName(logName);
        if (user != null && user.getPassword().equals(oldPassword)) {
            user.setPassword(newPassword);
            result = sysUserMapper.updateByPrimaryKey(user);
        } else {
            throw new DBConfigurationError("旧密码错误！");
        }
        return result > 0;
    }


    public String refreshToken(String oldToken) {
        return TokenUtil.refresh(oldToken);
    }
}
