package com.lovenav.service.serviceImpl;

import com.lovenav.dao.UrlAccessDao;
import com.lovenav.entity.UrlAccess;
import com.lovenav.service.UrlAccessService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@Service
public class UrlAccessServiceImpl implements UrlAccessService {
    @Autowired
    UrlAccessDao urlAccessDao;
    @Override
    public String inreaseUrlViews(UrlAccess urlAccess){
        if (urlAccess.getUrlId()==null){
            return "url的Id不能为空！";
        }
        Date date=new Date();
        urlAccess.setTime(date);
        urlAccess.setUrlId(2);
        UrlAccess urlAccess1=urlAccessDao.selectAlreadyExist(urlAccess);
        if (urlAccess1==null){
            urlAccess.setViews(1L);
            urlAccessDao.insertSelective(urlAccess);
        }else {
            urlAccess.setViews(urlAccess1.getViews()+1);
            urlAccessDao.updateByUrlId(urlAccess);
        }
        return "更新成功！";
    }

    @Override
    public HashMap<Date, Object> getUrlAccess(UrlAccess urlAccess) {
        HashMap<Date, Object> map=new HashMap<>();
        map=urlAccessDao.selectUrlAccess(urlAccess);
        return map;
    }
}
