package com.lovenav.service.serviceImpl;

import com.alibaba.fastjson2.JSON;
import com.lovenav.dao.BannersDao;
import com.lovenav.entity.Banners;
import com.lovenav.service.BannersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.HashMap;

@Service
public class BannersServiceImpl implements BannersService {
    @Autowired
    private BannersDao bannersDao;

//  跳转链接
    @Override
    public String Jump_url(int id){
        Banners banners = bannersDao.selectByPrimaryKey(id);
        HashMap<String, Object> hashMap = new HashMap<>();
        if(banners != null){
            hashMap.put("url",banners.getUrl());
        }else{
            hashMap.put("code",500);
            hashMap.put("msg","找不到指定链接");
        }
        return JSON.toJSONString(hashMap);
    }

//  添加banner
    @Override
    public String Add_banner(Banners banners) {
        int result = bannersDao.insert(banners);
        HashMap<String, Object> hashMap = new HashMap<>();
        if(result>0){
            return JSON.toJSONString(banners);
        }else{
            hashMap.put("code",500);
            hashMap.put("msg","添加banner失败");
            return JSON.toJSONString(hashMap);
        }
    }

//    删除banner
    @Override
    public String Delete_banner(int id){
        int result = bannersDao.deleteByPrimaryKey(id);
        HashMap<String, Object> hashMap = new HashMap<>();
        if(result>0){
            hashMap.put("code",200);
            hashMap.put("msg","删除成功");
            return JSON.toJSONString(hashMap);
        }else{
            hashMap.put("code",500);
            hashMap.put("msg","删除失败");
            return JSON.toJSONString(hashMap);
        }
    }

//    展示banner
    public String View_banner(){
        List<Banners> list = bannersDao.selectAllBanners();
        return JSON.toJSONString(list);
    }
}
