package com.jeequan.jeepay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.model.QRCodeParams;
import com.jeequan.jeepay.db.entity.MemberInfo;
import com.jeequan.jeepay.service.mapper.MemberInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 会员信息表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2023-04-12
 */
@Service
public class MemberInfoService extends ServiceImpl<MemberInfoMapper, MemberInfo> {

    @Autowired private MemberInfoMapper memberInfoMapper;

    /**
     * 根据  商户号 + 会员ID  查询会员
     * */
    public MemberInfo selectOne(String mchNo, String mbrId) {
        return selectOne(mchNo, mbrId, null, null, null);
    }

    /**
     * 根据商户号 + （会员ID || 手机号 || 渠道用户号）查询会员
     * */
    public MemberInfo selectOne(String mchNo, String mbrId, String mbrTel, String channelUesrId, String pageType) {
        if (StringUtils.isBlank(mchNo)) {
            return null;
        }

        LambdaQueryWrapper<MemberInfo> wrapper = MemberInfo.gw()
                .select(MemberInfo::getMbrId, MemberInfo::getMbrName, MemberInfo::getMbrTel, MemberInfo::getWxMpOpenId, MemberInfo::getWxLiteOpenId, MemberInfo::getAliUserId,
                        MemberInfo::getYsfUserId, MemberInfo::getMchNo, MemberInfo::getBalance, MemberInfo::getState, MemberInfo::getAvatarUrl, MemberInfo::getRemark, MemberInfo::getCreatedAt)
                .eq(MemberInfo::getMchNo, mchNo);

        if (StringUtils.isNotBlank(mbrId)) {
            return this.getOne(wrapper.eq(MemberInfo::getMbrId, mbrId));

        }else if (StringUtils.isNotBlank(mbrTel)) {
            return this.getOne(wrapper.eq(MemberInfo::getMbrTel, mbrTel));

        }else if (StringUtils.isNoneBlank(channelUesrId, pageType)) {
            if (StringUtils.equalsAny(pageType, QRCodeParams.PAGE_TYPE_ALIPAY_H5, QRCodeParams.PAGE_TYPE_ALIPAY_LITE)) {
                return this.getOne(wrapper.eq(MemberInfo::getAliUserId, channelUesrId));

            }else if (StringUtils.equals(pageType, QRCodeParams.PAGE_TYPE_WECHAT_H5)) {
                return this.getOne(wrapper.eq(MemberInfo::getWxMpOpenId, channelUesrId));

            }else if (StringUtils.equals(pageType, QRCodeParams.PAGE_TYPE_WECHAT_LITE)) {
                return this.getOne(wrapper.eq(MemberInfo::getWxLiteOpenId, channelUesrId));

            }else if (StringUtils.equals(pageType, QRCodeParams.PAGE_TYPE_YSFPAY_H5)) {
                return this.getOne(wrapper.eq(MemberInfo::getYsfUserId, channelUesrId));
            }
        }

        return null;
    }

    public Map count(LambdaQueryWrapper<MemberInfo> wrapper) {
        return memberInfoMapper.count(wrapper);
    }

    /** 查询全部余额 */
    public Long selectTotalBalance(LambdaQueryWrapper<MemberInfo> wrapper) {
        return memberInfoMapper.selectTotalBalance(wrapper);
    }

    public void memberParams(MemberInfo memberInfo, LambdaQueryWrapper<MemberInfo> wrapper) {
        // 商户号
        wrapper.eq(StringUtils.isNotEmpty(memberInfo.getMchNo()), MemberInfo::getMchNo, memberInfo.getMchNo());
        // 会员手机号
        wrapper.like(StringUtils.isNotEmpty(memberInfo.getMbrTel()), MemberInfo::getMbrTel, memberInfo.getMbrTel());
        // 会员ID
        wrapper.eq(StringUtils.isNotEmpty(memberInfo.getMbrId()), MemberInfo::getMbrId, memberInfo.getMbrId());
        // 会员名称
        wrapper.like(StringUtils.isNotEmpty(memberInfo.getMbrName()), MemberInfo::getMbrName, memberInfo.getMbrName());
        // 会员状态
        wrapper.eq(memberInfo.getState() != null, MemberInfo::getState, memberInfo.getState());

        // 时间范围条件
        Date[] dateRange = memberInfo.buildQueryDateRange();
        if (dateRange[0] != null) {
            wrapper.ge(MemberInfo::getCreatedAt, dateRange[0]);
        }
        if (dateRange[1] != null) {
            wrapper.le(MemberInfo::getCreatedAt, dateRange[1]);
        }


        // 查询的字段
        wrapper.select(MemberInfo::getMbrId, MemberInfo::getMbrName, MemberInfo::getMbrTel, MemberInfo::getState,
                MemberInfo::getBalance, MemberInfo::getMchNo, MemberInfo::getAvatarUrl, MemberInfo::getCreatedAt);

    }
}
