package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.mapper.AdminMapper;
import com.lightinit.hsdataportal.entity.Admin;
import com.lightinit.hsdataportal.entity.AdminExample;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.service.IAdminService;
import com.lightinit.hsdataportal.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/12.
 */
@Service
public class AdminServiceImpl implements IAdminService{
    @Autowired
    private AdminMapper adminMapper;

    public Admin SelectOne(String username) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andLockEqualTo(0);
        List<Admin> list=adminMapper.selectByExample(example);
        if(list!=null && list.size()>0)
            return list.get(0);
        return null;
    }

    public Admin QueryOne(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return adminMapper.selectByPrimaryKey(id);
    }

    public List<Admin> QueryList(Dictionary<String, String> condition, String order, int page, int rows) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        //AdminExample.Criteria criteria=example.createCriteria();
        BaseExample.Page _page=new BaseExample.Page();
        _page.setPageNo(page);
        _page.setPageSize(rows);
        example.setPage(_page);
        return adminMapper.selectByExample(example);
    }

    public long QueryCount(Dictionary<String, String> condition, String order) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        //AdminExample.Criteria criteria=example.createCriteria();
        return adminMapper.countByExample(example);
    }

    public boolean AddAdmin(Admin entity) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return adminMapper.insert(entity)>0;
    }

    public boolean UpdateAdmin(Admin entity) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(entity.getId());
        return adminMapper.updateByExample(entity,example)>0;
    }

    public boolean IsExits(String username) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andUsernameEqualTo(username);
        return adminMapper.countByExample(example)>0;
    }

    public boolean Delete(Long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(id);
        return adminMapper.deleteByExample(example)>0;
    }
}
