package com.yonge.cooleshow.portal.service.impl;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.cooleshow.mbg.mapper.UmsMemberLevelMapper;
import com.yonge.cooleshow.mbg.mapper.UmsMemberMapper;
import com.yonge.cooleshow.mbg.model.UmsMember;
import com.yonge.cooleshow.mbg.model.UmsMemberExample;
import com.yonge.cooleshow.mbg.model.UmsMemberLevel;
import com.yonge.cooleshow.mbg.model.UmsMemberLevelExample;
import com.yonge.cooleshow.portal.domain.MemberDetails;
import com.yonge.cooleshow.portal.service.UmsMemberCacheService;
import com.yonge.cooleshow.portal.service.UmsMemberService;
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.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 会员管理Service实现类
 * Created by macro on 2018/8/3.
 */
@Service
public class UmsMemberServiceImpl implements UmsMemberService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UmsMemberServiceImpl.class);

    @Autowired
    private UmsMemberMapper memberMapper;
    @Autowired
    private UmsMemberLevelMapper memberLevelMapper;
    @Autowired
    private UmsMemberCacheService memberCacheService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Override
    public UmsMember getByUsername(String username) {
        UmsMember member = memberCacheService.getMember(username);
        if(member!=null) return member;
        UmsMemberExample example = new UmsMemberExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<UmsMember> memberList = memberMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(memberList)) {
            member = memberList.get(0);
            memberCacheService.setMember(member);
            return member;
        }
        return null;
    }

    @Override
    public UmsMember getById(Long id) {
        return memberMapper.selectByPrimaryKey(id);
    }

    @Override
    public UmsMember getCurrentMember() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            throw new BizException("用户信息获取失败");
        }
        UmsMember umsMember = getById(sysUser.getId());
        // 商城库用户不存在-> 创建
        if (umsMember == null || umsMember.getId() == null) {
            umsMember = new UmsMember();
            umsMember.setId(sysUser.getId());
            umsMember.setCreateTime(sysUser.getCreateTime());
            umsMember.setUsername(sysUser.getUsername());
            umsMember.setPassword(sysUser.getPassword());
            umsMember.setPhone(sysUser.getPhone());
            umsMember.setStatus(sysUser.getLockFlag());
            umsMember.setIcon(sysUser.getAvatar());
            umsMember.setGender(sysUser.getGender());
            umsMember.setBirthday(sysUser.getBirthdate());
            //获取默认会员等级并设置
            UmsMemberLevelExample levelExample = new UmsMemberLevelExample();
            levelExample.createCriteria().andDefaultStatusEqualTo(1);
            List<UmsMemberLevel> memberLevelList = memberLevelMapper.selectByExample(levelExample);
            if (!CollectionUtils.isEmpty(memberLevelList)) {
                umsMember.setMemberLevelId(memberLevelList.get(0).getId());
            }
            memberMapper.insert(umsMember);
        } else {
            boolean flag = false;
            if (sysUser.getUsername() != null && !sysUser.getUsername().equals(umsMember.getUsername())) {
                umsMember.setUsername(sysUser.getUsername());
                flag = true;
            }
            if (sysUser.getPhone() != null && !sysUser.getPhone().equals(umsMember.getUsername())) {
                umsMember.setPhone(sysUser.getPhone());
                flag = true;
            }
            if (sysUser.getAvatar() != null && !sysUser.getAvatar().equals(umsMember.getIcon())) {
                umsMember.setIcon(sysUser.getAvatar());
                flag = true;
            }
            if (flag) {
                memberMapper.updateByPrimaryKeySelective(umsMember);
            }
        }
        return umsMember;
    }

    @Override
    public void updateIntegration(Long id, Integer integration) {
        UmsMember record=new UmsMember();
        record.setId(id);
        record.setIntegration(integration);
        memberMapper.updateByPrimaryKeySelective(record);
        memberCacheService.delMember(id);
    }

    @Override
    public UserDetails loadUserByUsername(String username) {
        UmsMember member = getByUsername(username);
        if(member!=null){
            return new MemberDetails(member);
        }
        throw new UsernameNotFoundException("用户名或密码错误");
    }
}
