package com.tax.token.service.impl;


import com.tax.token.mapper.SmsSampleMapper;
import com.tax.token.model.SmsSample;
import com.tax.token.model.SmsSampleExample;
import com.tax.token.model.TaxSmsModel;
import com.tax.token.service.api.SmsSampleService;
import com.tax.token.service.util.SmsUtil;
import com.yxcheng.token.common.service.AbstractService;
import com.yxcheng.token.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Transactional
@Component
@ComponentScan(basePackages = {"com.tax.token.dao", "com.tax.token.integration"})
public class SmsSampleServiceImpl extends AbstractService<SmsSampleMapper,SmsSampleExample,SmsSample> implements SmsSampleService {

    private static final Logger logger = LoggerFactory.getLogger(SmsSampleServiceImpl.class);

    public TaxSmsModel parseZoneCode(String timeFlag, String smsCode) {
        TaxSmsModel taxSmsModel = new TaxSmsModel();
        //根据关键字获取区域编码
        List<SmsSample> smsSampleList = baseMapper.selectAll();
        String zoneCode = null;


        for(SmsSample smsSample:smsSampleList) {
            String zoneSample = smsSample.getZoneSample();
            String[] ruleArr = zoneSample.split("@*@");

            boolean vaildFlag = true;
            for(String rule:ruleArr) {
                String[] detailArr = rule.split("=");
                //都满足退出

                for(String detail:detailArr) {
                    if(!smsCode.contains(detail)) {
                        vaildFlag = false;
                        break;
                    }
                }
                if(vaildFlag) {
                    zoneCode = smsSample.getZoneCode();
                }
            }

            if(StringUtil.isNotEmpty(zoneCode)) {
                break;
            }
        }
        //去掉电话号码
        smsCode = smsCode.replace("1236600", "");
        //获取校验码
        String validCode = SmsUtil.getYzmFromSms(smsCode);
        taxSmsModel.setZoneCode(zoneCode);
        taxSmsModel.setValidCode(validCode);

        logger.info(timeFlag + " old smsCode " + smsCode + "-->" + taxSmsModel.toString());

        return taxSmsModel;
    }

}
