package io.hmit.modules.serviceOrder.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.modules.serviceOrder.dao.RoleServiceDao;
import io.hmit.modules.serviceOrder.dto.RoleServiceDTO;
import io.hmit.modules.serviceOrder.entity.RoleServiceEntity;
import io.hmit.modules.serviceOrder.service.RoleServiceService;
import io.hmit.modules.sys.entity.SysRoleMenuEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 角色菜单关系
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-01-26
 */
@Service
public class RoleServiceServiceImpl extends CrudServiceImpl<RoleServiceDao, RoleServiceEntity, RoleServiceDTO> implements RoleServiceService {

    @Override
    public QueryWrapper<RoleServiceEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<RoleServiceEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateRoleService(Long roleId, List<Long> serviceIdList) {
        //先删除角色服务关系
        baseDao.deleteByRoleIds(new Long[]{roleId});

        if (CollUtil.isEmpty(serviceIdList)) {
            return;
        }
        //保存角色服务关系
        for (Long serviceId : serviceIdList) {
            RoleServiceEntity roleServiceEntity = new RoleServiceEntity();
            roleServiceEntity.setServiceTypeId(serviceId);
            roleServiceEntity.setRoleId(roleId);
            //保存
            insert(roleServiceEntity);
        }

    }

    @Override
    public List<Long> getServiceIdList(Long roleId) {
        return baseDao.getServiceIdList(roleId);
    }

}
