package com.iformall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.po.MallRolePermission;
import com.iformall.mapper.MallRolePermissionMapper;
import com.iformall.service.MallRolePermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MallRolePermissionServiceImpl implements MallRolePermissionService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    MallRolePermissionMapper mallUserRolePermissionMapper;
	 

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

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

    @Override
    public MallRolePermission getById(Long id) {
        return mallUserRolePermissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(MallRolePermission record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            mallUserRolePermissionMapper.insertSelective(record);
        } else {
            mallUserRolePermissionMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public void deleteById(Long id) {
        mallUserRolePermissionMapper.deleteByPrimaryKey(id);
    }
    
    
    
	@Override
	public void savePermissions(String tenantId,Long roleId, Long[] perId) {
//		Map<String,Object> map = new HashMap<>();
//		map.put("roleId", roleId);
		mallUserRolePermissionMapper.deleteByRoleId(roleId);
		 for(Long pId:perId){
			   MallRolePermission permission =new MallRolePermission();
			   final IdWorker idWorker = IdWorker.get();
			   permission.setId(idWorker.nextId());
			   permission.setPermissionId(pId);
			   permission.setTenantId(tenantId);
			   permission.setRoleId(roleId);
			   mallUserRolePermissionMapper.insert(permission);
		   }
		
	}
	
	public void deleteByRoleId(Long roleId) {
		mallUserRolePermissionMapper.deleteByRoleId(roleId);
	}
    
    
    
    
    
}
