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

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

import com.ruoyi.bst.credit.domain.Credit;
import com.ruoyi.bst.credit.domain.CreditVO;
import com.ruoyi.bst.credit.service.CreditConverter;
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 CreditConverterImpl implements CreditConverter {

    @Autowired
    private UserService userService;

    @Override
    public Credit toPoByCreate(CreditVO data) {
        if (data == null) {
            return null;
        }
        Credit po = new Credit();
        this.setAdminId(po, data);
        po.setName(data.getName());
        po.setStatus(data.getStatus());
        po.setLimitAmount(data.getLimitAmount());
        po.setCreateId(data.getCreateId());
        po.setAreaId(data.getAreaId());
        po.setLogo(data.getLogo());
        return po;
    }

    // 设置管理员ID
    private void setAdminId(Credit po, CreditVO data) {
        if (StringUtils.hasText(data.getAdminMobile())) {
            UserVO admin = userService.getOrCreateByPhone(data.getAdminMobile());
            ServiceUtil.assertion(admin == null, "手机号为%s的管理员不存在", data.getAdminMobile());
            po.setAdminId(admin.getUserId());
        }
    }

    @Override
    public Credit toPoByUpdate(CreditVO data) {
        if (data == null) {
            return null;
        }
        Credit po = new Credit();
        this.setAdminId(po, data);
        po.setId(data.getId());
        po.setName(data.getName());
        po.setStatus(data.getStatus());
        po.setLimitAmount(data.getLimitAmount());
        po.setAreaId(data.getAreaId());
        po.setLogo(data.getLogo());
        return po;
        
    }
}
