package com.iformall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.po.MallRole;
import com.iformall.mapper.MallRoleMapper;
import com.iformall.service.MallRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MallRoleServiceImpl implements MallRoleService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    MallRoleMapper mallRoleMapper;


    @Override
    public PageInfo<MallRole> listAsPage(MallRole record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> mallRoleMapper.findList(record));
    }

    @Override
    public List<MallRole> getList(MallRole record) {
        return mallRoleMapper.findList(record);
    }

    @Override
    public int countList(MallRole record) {
        return mallRoleMapper.selectCount(new QueryWrapper(record));
    }

    @Override
    public MallRole getById(Long id) {
        return mallRoleMapper.selectById(id);
    }

    @Override
    public void saveOrUpdate(MallRole record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            mallRoleMapper.insert(record);
        } else {
            mallRoleMapper.updateById(record);
        }
    }

    @Override
    public void deleteById(Long id) {
        mallRoleMapper.deleteById(id);
    }
    
    
    @Override
    public List<MallRole> findRoleByUserId(Long userId) {
        return mallRoleMapper.findRoleByUserId(userId);
    }

	@Override
	public int countByName(String name,Long id) {
		Map<String,Object> map = new HashMap<>(2);
		map.put("name", name);
		if(id!=null) {
			map.put("id", id);
		}
		return mallRoleMapper.countByName(map);
	}
    
    
    
    
    
    
}
