package org.dromara.zhishu.service.impl;

import lombok.extern.log4j.Log4j2;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.zhishu.domain.NewUserInfo;
import org.dromara.zhishu.domain.bo.NewUserBo;
import org.dromara.zhishu.mapper.NewUserBoMapper;
import org.dromara.zhishu.service.NewUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Log4j2
public class NewUserServiceImpl implements NewUserService {
    @Autowired
    private NewUserBoMapper newUserMapper;


    @Transactional
    @Override
    public boolean insertByBo(NewUserBo newUserBo) {
        Long userId = LoginHelper.getUserId();
        NewUserInfo add = MapstructUtils.convert(newUserBo, NewUserInfo.class);
        if(add.getLicenseTime().equals("")){
            add.setLicenseTime(null);
        }
        if(add.getBusinessLicenseTime().equals("")){
            add.setBusinessLicenseTime(null);
        }
        Integer message = newUserMapper.selectAudit(userId);
        //当message不等于0时返回false，否则执行下面代码
//         Integer message2 = newUserMapper.selectByPhone(add.getContactPhone());
        if (message != 0) {
            throw new RuntimeException("您已经提交过申请，请耐心等待审核");
        }
        try {
            newUserMapper.insertAudit(userId);
            Long id =  newUserMapper.selectId(userId);
            add.setAuditId(id);
            Integer message1 =   newUserMapper.insertAuditInfo(add);
            System.out.println(message1);
            newUserMapper.updatePhone(newUserBo.getEmail(),newUserBo.getContactPhone(),userId);
        } catch (Exception e) {
            log.error("插入数据失败",e);
            return false;
        }
        return true;
    }
}
