package com.ruoyi.bst.creditUser.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.bst.creditUser.domain.CreditUser;
import com.ruoyi.bst.creditUser.domain.CreditUserVO;
import com.ruoyi.bst.creditUser.service.CreditUserConverter;
import com.ruoyi.common.core.domain.vo.UserVO;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.user.service.UserService;

@Service
public class CreditUserConverterImpl implements CreditUserConverter {
    
    @Autowired
    private UserService userService;

    @Override
    public CreditUser toPoByCreate(CreditUserVO data) {
        if (data == null) {
            return null;
        }
        CreditUser po = new CreditUser();
        po.setCreditId(data.getCreditId());
        po.setLimitAmount(data.getLimitAmount());
        po.setRemark(data.getRemark());
        if (StringUtils.hasText(data.getUserMobile())) {
            UserVO user = userService.selectUserByPhone(data.getUserMobile());
            ServiceUtil.assertion(user == null, "手机号为【%s】的用户不存在", data.getUserMobile());
            po.setUserId(user.getUserId());
        }
        return po;
    }

    @Override
    public CreditUser toPoByUpdate(CreditUserVO data) {
        if (data == null) {
            return null;
        }
        CreditUser po = new CreditUser();
        po.setCreditId(data.getCreditId());
        po.setLimitAmount(data.getLimitAmount());
        po.setRemark(data.getRemark());
        if (StringUtils.hasText(data.getUserMobile())) {
            UserVO user = userService.selectUserByPhone(data.getUserMobile());
            ServiceUtil.assertion(user == null, "手机号为【%s】的用户不存在", data.getUserMobile());
            po.setUserId(user.getUserId());
        }
        return po;
    }
}
