package com.fs.framework.service;



import com.fs.common.exception.ServiceException;
import com.fs.common.utils.StringUtils;
import com.fs.his.domain.FsStore;
import com.fs.his.domain.StoreLoginUser;
import com.fs.his.mapper.FsStoreMapper;
import com.fs.hisStore.domain.FsStoreScrm;
import com.fs.hisStore.domain.StoreLoginUserScrm;
import com.fs.hisStore.mapper.FsStoreScrmMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 用户验证处理
 *

 */
@Service
public class UserDetailsScrmServiceImpl implements UserDetailsService
{
    private static final Logger log = LoggerFactory.getLogger(UserDetailsScrmServiceImpl.class);


    @Autowired
    private FsStoreScrmMapper fsStoreMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
    {
         FsStoreScrm user = fsStoreMapper.selectFsStoreByAccount(username);
        if (StringUtils.isNull(user))
        {
            log.info("登录用户：{} 不存在.", username);
            throw new ServiceException("登录用户：" + username + " 不存在");
        }

        else if (user.getStatus()==0)
        {
            log.info("登录用户：{} 已被停用.", username);
            throw new ServiceException("对不起，您的店铺：" + username + " 已停用");
        }

        return createLoginUser(user);
    }

    public UserDetails createLoginUser(FsStoreScrm user)
    {
        StoreLoginUserScrm storeLoginUser = new StoreLoginUserScrm();
        storeLoginUser.setFsStore(user);
        return storeLoginUser;
    }
}
