package com.ozs.account.service.impl;

import javax.servlet.http.HttpServletResponse;

import com.ozs.account.service.AccountInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ozs.account.dto.UserDTO;
import com.ozs.account.mapper.AccountInfoMapper;
import com.ozs.account.util.R;
import com.ozs.account.util.S;
import com.ozs.account.util.TokenUtil;

@Service
public class AccountInfoServiceImpl implements AccountInfoService {

	private final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private AccountInfoMapper accountInfoMapper;

	@Autowired
	private TokenUtil tokenUtil;

	@Override
	public R<UserDTO> login(UserDTO user, HttpServletResponse response) throws Exception {
		UserDTO userResult = accountInfoMapper.login(user);
		if (userResult == null) {
			return new R<>(S.USER_PASSWD_ENTNO_ERROR);
		}
		// 生成token
		String token = tokenUtil.createToken(userResult.getId(), null, 30);
		// 保存token
		tokenUtil.saveToken(userResult.getId(), token, 30);
		response.setHeader("token", token);
		// 获取用户权限菜单列表
		// 数据返回前台前把不必要数据置空，保证安全性
		// userResult.setPlatformCode(null);
		return new R<>(userResult);
	}

	/*
	 * *
	 * 
	 * @Author jade
	 * 
	 * @Description 修改页面主题风格
	 * 
	 * @Date 2019/3/26
	 * 
	 * @Param [id, theme]
	 * 
	 * @return com.datau.account.util.R<java.lang.String>
	 **/
	@Override
	public R<String> changeTheme(String id, String theme) throws Exception {
		accountInfoMapper.changeTheme(id, theme);
		return new R<>(S.OK, S.getMsg(S.OK));
	}

	@Override
	public Integer getUserType(String userId) throws Exception {
		UserDTO user = accountInfoMapper.getUserType(userId);
		return user.getUserType();
	}
}
