package com.bizzan.bitrade.service;

import com.bizzan.bitrade.dao.CoinExtDao;
import com.bizzan.bitrade.entity.CoinExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class CoinExtService {

    @Autowired
    private CoinExtDao coinExtDao;

    public CoinExt findBySymbol(String symbol) {
        return coinExtDao.findBySymbol(symbol);
    }

    @Transactional(rollbackFor = Exception.class)
    public CoinExt saveOrUpdate(CoinExt ext) {
        CoinExt existing = coinExtDao.findBySymbol(ext.getSymbol());
        if (existing == null) {
            ext.setCreatedTime(new Date());
            ext.setUpdatedTime(new Date());
            return coinExtDao.save(ext);
        } else {
            existing.setNameCn(ext.getNameCn());
            existing.setNameEn(ext.getNameEn());
            existing.setIcon(ext.getIcon());
            existing.setRank(ext.getRank());
            existing.setMarketCap(ext.getMarketCap());
            existing.setCirculatingSupply(ext.getCirculatingSupply());
            existing.setIssuePrice(ext.getIssuePrice());
            existing.setAthPrice(ext.getAthPrice());
            existing.setAthDate(ext.getAthDate());
            existing.setWhitepaper(ext.getWhitepaper());
            existing.setUpdatedTime(new Date());
            return coinExtDao.save(existing);
        }
    }
}
