/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yifu4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.yifu.cloud.plus.v1.check.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.check.entity.TCheckMobile;
import com.yifu.cloud.plus.v1.check.mapper.TCheckMobileMapper;
import com.yifu.cloud.plus.v1.check.service.TCheckMobileService;
import com.yifu.cloud.plus.v1.check.utils.ChecksUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.exception.ErrorCodes;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.MsgUtils;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 手机号码校验
 *
 * @author fxj
 * @date 2022-06-20 17:29:03
 */
@Service
public class TCheckMobileServiceImpl extends ServiceImpl<TCheckMobileMapper, TCheckMobile> implements TCheckMobileService {

	/**
	 * @Author fxj
	 * @Description 号码状态校验，单次请求上限100  多一个号码请以逗号分割
	 * @Date 19:18 2022/6/20
	**/
	@Override
	public R<Map<String,TCheckMobile>> checkMobiles(String mobiles){
		R<Map<String,TCheckMobile>> mobileMapR;
		Map<String,TCheckMobile> checkMobileMap;
		if (Common.isNotNull(mobiles)){
			List<String>  mobileList = Common.initStrToList(mobiles,CommonConstants.COMMA_STRING);
			if (Common.isNotEmpty(mobileList) && mobileList.size() > CommonConstants.INTEGER_HUNDRED){
				return R.failed(MsgUtils.getMessage(ErrorCodes.CHECKS_MOBILE_REQUEST_LIMIT_HUNDRED));
			}
			mobileMapR = ChecksUtil.checkMobile(mobiles);
			if (Common.isEmpty(mobileMapR) && Common.isEmpty(mobileMapR.getData())){
				return R.failed(MsgUtils.getMessage(ErrorCodes.CHECKS_MOBILE_REQUEST_ERROR));
			}
			checkMobileMap = mobileMapR.getData();
			if (Common.isNotNull(mobileList) && Common.isNotNull(checkMobileMap)){
				TCheckMobile checkMobile = null;
				for (String mobile:mobileList){
					if (Common.isEmpty(checkMobileMap.get(mobile))){
						checkMobile = new TCheckMobile();
						checkMobile.setType(CommonConstants.ONE_STRING);
						checkMobile.setMobile(mobile);
						checkMobile.setStatus(CommonConstants.TEN_STRING);
						checkMobile.setMessage(Common.isEmpty(mobileMapR)?MsgUtils.getMessage(ErrorCodes.CHECKS_MOBILE_REQUEST_ERROR):mobileMapR.getMsg());
						checkMobile.setChargesstatus(CommonConstants.ZERO_STRING);
						checkMobile.setNumbertype(CommonConstants.EMPTY_STRING);
						checkMobile.setArea(CommonConstants.EMPTY_STRING);
						checkMobileMap.put(mobile,checkMobile);
					}
				}
				mobileMapR.setData(checkMobileMap);
			}
		}else {
			return R.failed(MsgUtils.getMessage(ErrorCodes.PARAM_NOT_EMPTY));
		}
		if (Common.isNotNull(checkMobileMap)){
			this.saveBatch(checkMobileMap.values());
		}
		return mobileMapR;
	}

}
