package com.platform.yijia.service.impl;

import com.platform.yijia.dao.AppUserInfoMapper;
import com.platform.yijia.pojo.AppUserInfo;
import com.platform.yijia.pojo.AppUserInfoExample;
import com.platform.yijia.service.AppUserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

@Service("appUserInfoService")
public class AppUserInfoServiceImpl implements AppUserInfoService {
    @Resource
    private AppUserInfoMapper appUserInfoMapper;
    /**
     * 用户信息
     */
    @Override
    public List<AppUserInfo> Authentication(AppUserInfo appUserInfo) {
        AppUserInfoExample example =new AppUserInfoExample();
        if(appUserInfo.getUserType().equals("1")){
            example.or().andUserTypeEqualTo(appUserInfo.getUserType()).andBlogOpenidEqualTo(appUserInfo.getBlogOpenid());
        }else if(appUserInfo.getUserType().equals("2")){
            example.or().andUserTypeEqualTo(appUserInfo.getUserType()).andMinaOpenidEqualTo(appUserInfo.getMinaOpenid());
        }
        List<AppUserInfo> list= appUserInfoMapper.selectByExample(example);
        return list;
    }

    /**
     * 添加用户
     * @param appUserInfo
     */
    @Override
    public void AddAppUserInfo(AppUserInfo appUserInfo) {
        appUserInfoMapper.insert(appUserInfo);
    }

    /****
     * 根据用户token和userType 判断该用户是否存在
     * @param params
     * @return
     */
    @Override
    public int isExistAppUser(Map params) {
        return  appUserInfoMapper.isExistAppUser(params);
    }

    @Override
    public boolean selectAppUserInfo(String openId, String userType) {
        Boolean flag=true;
        if(StringUtils.isNotBlank(openId)&&StringUtils.isNotBlank(userType)){
            //AppUserInfo appUserInfo =new AppUserInfo();
            AppUserInfoExample example =new AppUserInfoExample();
            if(userType.equals("1")){
                example.or().andUserTypeEqualTo(userType).andBlogOpenidEqualTo(openId);
            }else if(userType.equals("2")){
                example.or().andUserTypeEqualTo(userType).andMinaOpenidEqualTo(openId);
            }
            List<AppUserInfo> list= appUserInfoMapper.selectByExample(example);
            if(list!=null && list.size()>0){
                flag=true;
            }else {
                flag=false;
            }
        }else{
            flag=false;
        }
        return flag;
    }

    //更新unionID
    @Override
    public void updateAppUserInfo(AppUserInfo appUserInfo) {
        appUserInfoMapper.updateAppUserInfo(appUserInfo);
    }

    //获取用户信息
    @Override
    public AppUserInfo getUserInfo(AppUserInfo appUserInfo) {
        return appUserInfoMapper.getUserInfo(appUserInfo);
    }


    /**
     * 判断使用小程序支付的用户是否关注公众号
     * @param map
     * @return
     */
    @Override
    public String whetherFollowGzh(Map map) {
        return appUserInfoMapper.whetherFollowGzh(map);
    }
}
