package io.renren.service.impl;

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

import io.renren.common.entity.RdSmsTestEntity;
import io.renren.dao.RdSmsTestDao;
import io.renren.dto.RdSmsTestDTO;
import io.renren.entity.CompaniesEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.renren.common.constant.Constant;
import io.renren.common.exception.ErrorCode;
import io.renren.common.redis.RedisKeys;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.Result;
import io.renren.common.utils.security.SM4Utils;
import io.renren.dao.CustomersDao;
import io.renren.dto.CustomersDTO;
import io.renren.entity.CustomersEntity;
import io.renren.service.CustomersService;
import io.renren.util.CacheUtils;
import lombok.extern.log4j.Log4j2;

/**
 * 客户表
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2022-05-29
 */
@Log4j2
@Service
public class CustomersServiceImpl extends CrudServiceImpl<CustomersDao, CustomersEntity, CustomersDTO> implements CustomersService {
	
	/**
	 * 加解密秘钥
	 */
	@Value("${encrypt.token}")
	private String TRANSMIT_KEY;
	
	@Autowired
	private CacheUtils cacheUtils;

	@Autowired
	private RdSmsTestDao rdSmsTestDao;


	@Override
	public QueryWrapper<CustomersEntity> getWrapper(Map<String, Object> params) {
		return null;
	}
	
	@Override
	public Result<String> inserCustomer(CustomersDTO dto) throws Exception{
		log.info("新增客户表请求参数"+JSONObject.toJSONString(dto));
		//1、验证码验证

		if (StringUtils.isEmpty(dto.getVerifyCode())) {
			return new Result<String>().error(ErrorCode.VERIFICATION_CODE_ERROR,"验证码不正确");
		}
		String code = cacheUtils.get(RedisKeys.getVerificationCodeKey(dto.getMobile()))!=null?cacheUtils.get(RedisKeys.getVerificationCodeKey(dto.getMobile())).toString():null;

		if (StringUtils.isNotEmpty(code)){
			if( ! dto.getVerifyCode().equals(code)){
				return new Result<String>().error(ErrorCode.VERIFICATION_CODE_ERROR,"验证码不正确");
			}else {
				cacheUtils.remove(RedisKeys.getVerificationCodeKey(dto.getMobile()));
			}
		}else{
			RdSmsTestEntity rdSmsTestEntity = rdSmsTestDao.selectOne(new QueryWrapper<RdSmsTestEntity>()
					.eq("mobile", dto.getMobile()).eq("status", Constant.Status.ENABLE.getValue()).last("limit 1"));
			if(null == rdSmsTestEntity || ! rdSmsTestEntity.getCode().equalsIgnoreCase(code)) {
				return new Result<String>().error(ErrorCode.VERIFICATION_CODE_ERROR,"验证码不正确");
			}
		}

		CustomersEntity customers = new CustomersEntity();
		BeanUtils.copyProperties(dto, customers);
		customers.setMobile(SM4Utils.encryptData_ECB(customers.getMobile(), TRANSMIT_KEY));//手机号加密
		customers.setCompanyId(0L);
		customers.setCreateDate(new Date());
		customers.setIsPush(Constant.PushStatus.UN_PUSH.getValue());
		baseDao.insert(customers);

		return new Result<String>().pageOk(ErrorCode.SUCCESS_CODE,"提交成功");
		
		
	}
	

}