package lingtao.net.service;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lingtao.net.bean.Msg;
import lingtao.net.bean.SysDictSearchPro;
import lingtao.net.bean.SysUser;
import lingtao.net.dao.SysDictSearchProMapper;

@Service
public class SysDictSearchProService {

	@Autowired
	SysDictSearchProMapper searchProMapper;

	// 查询
	public List<SysDictSearchPro> keyWordsList(SysDictSearchPro searchPro) {
		return searchProMapper.keyWordsList(searchPro);
	}

	// 新增
	public Msg addSearchPro(SysDictSearchPro searchPro, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		searchPro.setCreator(user.getRealname());
		try {
			searchProMapper.insertSelective(searchPro);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail();
		}
	}

	// 修改关键字
	public Msg updateKeyWordById(HttpSession session, int id, String value, String field) {
		SysUser user = (SysUser) session.getAttribute("USER_SESSION");
		SysDictSearchPro searchPro = new SysDictSearchPro();
		searchPro.setId(id);
		if ("url".equals(field)) {
			searchPro.setUrl(value);
		} else {
			searchPro.setLikeProTypeLabel(value);
		}
		searchPro.setUpdater(user.getRealname());
		try {
			searchProMapper.updateKeyWordById(searchPro);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail();
		}
	}

	// 改变状态
	public Msg changeKeyWordStatus(Integer id) {
		try {
			searchProMapper.changeKeyWordStatus(id);
			return Msg.success();
		} catch (Exception e) {
			return Msg.fail();
		}
	}
}
