package com.lightinit.hsdataplatform.impl;

import com.lightinit.hsdataplatform.mapper.MenuMapper;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.entity.Menu;
import com.lightinit.hsdataplatform.entity.MenuExample;
import com.lightinit.hsdataplatform.service.IMenuService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Dictionary;
import java.util.List;

/**
 * Created by Mr.Yao on 2017/4/13.
 */
@Service
public class MenuServiceImpl implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;

    public List<Menu> QueryList(int page, int rows, Dictionary<String, String> conditions) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        MenuExample example=new MenuExample();
        MenuExample.Criteria criteria=example.createCriteria();
        criteria.andMenutypeEqualTo(0);
        if(rows>0) {
            BaseExample.Page _page = new BaseExample.Page();
            _page.setPageNo(page);
            _page.setPageSize(rows);
            example.setPage(_page);
        }
        return menuMapper.selectByExample(example);
    }

    @Override
    public List<Menu> QueryList(String username) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return menuMapper.selectByUserMenu(username);
    }

    public long QueryCount(Dictionary<String, String> conditions) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        MenuExample example=new MenuExample();
        //MenuExample.Criteria criteria=example.createCriteria();
        return menuMapper.countByExample(example);
    }

    public boolean InsertMenu(Menu menu) {

        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return menuMapper.insert(menu)>0;
    }

    public boolean DeleteMenu(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return menuMapper.deleteByPrimaryKey(id)>0;
    }

    public boolean UpdateMenu(Menu menu) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        MenuExample example=new MenuExample();
        MenuExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(menu.getId());
        return menuMapper.updateByExampleSelective(menu,example)>0;
    }

}
