package com.lovenav.service.serviceImpl;

import com.lovenav.dao.UrlCateListDao;
import com.lovenav.dao.UrlListDao;
import com.lovenav.dao.UserDao;
import com.lovenav.entity.UrlCateList;
import com.lovenav.entity.UrlList;
import com.lovenav.entity.User;
import com.lovenav.service.UrlListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service("urlListService")
public class UrlLiserServiceImpl implements UrlListService {

    @Autowired
    UrlCateListDao urlCateListDao;

    @Autowired
    UrlListDao urlListDao;
    @Autowired
    UserDao userDao;
    public int selectCateAndInsertUrl(String parent,String name , String icon ,String url  ,String email)
    {
        //找用户ID
        User user = userDao.selectByEmail(email);
        int userId = user.getId();

        UrlCateList urlCateList = urlCateListDao.selectCateByNameAnduserId(parent,userId);
        UrlList urlList = new UrlList();
        //设置属性
        urlList.setCateId(urlCateList.getId());
        urlList.setUrl(url);
        urlList.setIcon(icon);
        urlList.setName(name);
        Date date = new Date();
        urlList.setCreatetime(date.getTime());
        urlList.setViews(Long.valueOf("0"));
        Byte b = 0;
        urlList.setNeedLogin(b);
        int flag = urlListDao.insert(urlList);
        return flag;
    }

    public List<UrlList> selectUrList(){
        return urlListDao.selectUrList();
    }


    public List<UrlList> selectUrListByNeedLogin(){
        return urlListDao.selectUrListByNeedLogin();
    }

    public int insertUrlByUser(UrlList urlList){return urlListDao.insertSelective(urlList);}

    public UrlList selectUrListByInput(String input)
    {
        return urlListDao.selectUrListByInput(input);
    }
    public UrlList selectUrlListByUrlId(Long urlId)
    {
        UrlList urlList = urlListDao.selectUrlListByUrlId(urlId);
        urlList.setViews(urlList.getViews()+1);
        urlListDao.updateByPrimaryKeySelective(urlList);
        return urlList;
    }

    @Override
    public int updateUrlStatusListById(UrlList urlList) {
        if (urlListDao.updateByPrimaryKey(urlList)==1){
            return 1;
        }else {
            return 0;
        }

    }
    public int deleteByPrimaryKey(Long id){
        return urlListDao.deleteByPrimaryKey(id);
    }

    public int deleteByCateId(Long id)
    {
        return urlListDao.deleteByCateId(id);
    }
}
