package com.fdkankan.ucenter.service.impl;

import com.fdkankan.common.constant.ConstantRegex;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.ucenter.constant.LoginConstant;
import com.fdkankan.ucenter.entity.AgentAudit;
import com.fdkankan.ucenter.mapper.IAgentAuditMapper;
import com.fdkankan.ucenter.service.IAgentAuditService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 代理商申请表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-07-29
 */
@Service
public class AgentAuditServiceImpl extends ServiceImpl<IAgentAuditMapper, AgentAudit> implements IAgentAuditService {

    @Override
    public void add(AgentAudit param) {
        if(param == null || StringUtils.isEmpty(param.getName()) || StringUtils.isEmpty(param.getAddress()) ||
                StringUtils.isEmpty(param.getCountry()) || StringUtils.isEmpty(param.getRegion()) ||
                StringUtils.isEmpty(param.getSurName()) || StringUtils.isEmpty(param.getUserName()) ||
                StringUtils.isEmpty(param.getPost()) || StringUtils.isEmpty(param.getAreaCode()) ||
                StringUtils.isEmpty(param.getPhone()) || StringUtils.isEmpty(param.getEmail())){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
        }

        if(!param.getEmail().matches(ConstantRegex.EMAIL_REGEX)){
            throw new BusinessException(LoginConstant.FAILURE_CODE_3019, LoginConstant.FAILURE_MSG_3019);
        }
        if(param.getName().length() >200 || param.getAddress().length() >200
                || param.getSurName().length() >50 || param.getUserName().length() >50){
            if(StringUtils.isNotBlank(param.getStoreAddress()) && param.getStoreAddress().length() >200){
                throw new BusinessException(LoginConstant.FAILURE_CODE_3036, LoginConstant.FAILURE_MSG_3036);
            }
            throw new BusinessException(LoginConstant.FAILURE_CODE_3036, LoginConstant.FAILURE_MSG_3036);
        }

        AgentAudit agentAuditEntity = new AgentAudit();
        BeanUtils.copyProperties(param, agentAuditEntity);
        agentAuditEntity.setCreateTime(new Date());
        agentAuditEntity.setUpdateTime(new Date());
        agentAuditEntity.setRecStatus("A");
        agentAuditEntity.setNoteType(1);
        this.save(agentAuditEntity);
    }
}
