package cn.sh.stc.sict.cloud.upms.service.impl;

import cn.sh.stc.sict.cloud.common.core.constant.Constant;
import cn.sh.stc.sict.cloud.common.core.util.NumberUtil;
import cn.sh.stc.sict.cloud.upms.dao.SysUserBaseMapper;
import cn.sh.stc.sict.cloud.upms.dto.UserDTO;
import cn.sh.stc.sict.cloud.upms.dto.UserInfo;
import cn.sh.stc.sict.cloud.upms.model.HpUserRole;
import cn.sh.stc.sict.cloud.upms.model.SysUserBase;
import cn.sh.stc.sict.cloud.upms.service.HpUserRoleService;
import cn.sh.stc.sict.cloud.upms.service.SysUserBaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.stereotype.Service;

@Slf4j
@Service("sysUserBaseService")
@AllArgsConstructor
public class SysUserBaseServiceImpl extends ServiceImpl<SysUserBaseMapper, SysUserBase> implements SysUserBaseService {

    private final SysUserBaseMapper sysUserBaseMapper;
    private WxMpService wxMpService;
    private HpUserRoleService hpUserRoleService;

    @Override
    public UserInfo getUserInfo(String appId, String username) {

        LambdaQueryWrapper<SysUserBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserBase::getPhone, username).or().eq(SysUserBase::getUserName, username);
        SysUserBase user = this.getOne(wrapper);
        if (user != null) {
            UserInfo info = new UserInfo();
            info.setSysUserBase(user);
            info.setOpenId(user.getOpenId());
            return info;
        }
        return null;
    }

    @Override
    public UserInfo getUserInfoBySocial(String appId, String inStr) throws WxErrorException {
        log.error("=================> appId = {}, inStr = {}", appId, inStr);
        // code = 011Eg6Ga1UOXeA0R5wHa16dkab2Eg6G7
        String[] split = inStr.split(StringPool.AT);

        WxOAuth2AccessToken token = wxMpService.getOAuth2Service().getAccessToken(split[2]);

        return this.getByOpenId(token.getOpenId());
    }

    @Override
    public boolean saveDto(UserDTO dto) {
        if(NumberUtil.isNotNullOrZero(dto.getUser().getId())){
            hpUserRoleService.saveBatch(dto.getUser().getId(), dto.getRoleList());
        }

        return true;
    }


    private UserInfo getByOpenId(String openId) {
        LambdaQueryWrapper<SysUserBase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserBase::getOpenId, openId);
        SysUserBase user = this.getOne(wrapper);
        // 未注册用户默认注册
        if (user == null) {
            user = new SysUserBase();
            user.setOpenId(openId);
            user.setStatus(Constant.BYTE_NO);
            user.setPasswd(Constant.DEFAULT_PASSWORD);
            this.save(user);
        }

        UserInfo info = new UserInfo();
        info.setSysUserBase(user);
        info.setOpenId(openId);
        return info;
    }
}
