package com.lovenav.service.serviceImpl;

import com.alibaba.fastjson2.JSON;
import com.lovenav.dao.NavDao;
import com.lovenav.entity.Nav;
import com.lovenav.service.NavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NavServiceImpl implements NavService {
    @Autowired
    NavDao navDao;
    @Override
    public String deleteNav(Nav nav) {
        if (navDao.selectAlreadyExist(nav)==null) {
            return "没有该记录";
        }
        int nav1=navDao.deleteByNavName(nav.getNavName());
        if (nav1==1){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }

    @Override
    public String updateNav(Nav nav) {
        if (navDao.selectAlreadyExist(nav)==null) {
            return "没有该记录";
        }
        int result=navDao.updateByName(nav);
        if (result==1){
            return "更新成功";
        }else {
            return "更新失败";
        }
    }

    @Override
    public String selectNav(Nav nav) {
        return null;
    }

    @Override
    public String addNav(Nav nav) {
        int result;
        if(navDao.selectAlreadyExist(nav)!=null){
            return "已经有相同名称的导航!";
        }
        result=navDao.insertSelective(nav);
        if (result==1){
            return "添加成功";
        }else {
            return "添加失败";
        }
    }

    @Override
    public String selectAllNav() {
        return (String) JSON.toJSON(navDao.selectAllNav());
    }
}
