package com.huiju.estateagents.redpack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiju.estateagents.redpack.entity.TaMpPerson;
import com.huiju.estateagents.redpack.mapper.TaMpPersonMapper;
import com.huiju.estateagents.redpack.service.ITaMpMiniappService;
import com.huiju.estateagents.redpack.service.ITaMpPersonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 公众号用户表  服务实现类
 * </p>
 *
 * @author fxf
 * @since 2020-06-02
 */
@Service
public class TaMpPersonServiceImpl extends ServiceImpl<TaMpPersonMapper, TaMpPerson> implements ITaMpPersonService {

    @Autowired
    ITaMpMiniappService iTaMpMiniappService;

    @Autowired
    TaMpPersonMapper taMpPersonMapper;

    @Override
    public TaMpPerson newPerson(TaMpPerson taMpPerson) {
        TaMpPerson origin = getByOpenId(taMpPerson.getOrgId(), taMpPerson.getAppid(), taMpPerson.getMpOpenid());
        if (null != origin) {
            return origin;
        }

        save(taMpPerson);
        return getByOpenId(taMpPerson.getOrgId(), taMpPerson.getAppid(), taMpPerson.getMpOpenid());
    }

    @Override
    public TaMpPerson getByOpenId(Integer orgId, String appid, String openid) {
        QueryWrapper<TaMpPerson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", orgId);
        queryWrapper.eq("appid", appid);
        queryWrapper.eq("mp_openid", openid);

        return getOne(queryWrapper);
    }

    @Override
    public TaMpPerson getByMiniAppPerson(Integer orgId, String miniAppPersonId) {
        return taMpPersonMapper.getByMiniAppPerson(orgId, miniAppPersonId);
    }
}
