package io.hmit.modules.fhjw.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.fhjw.dao.FhjwRoleFunctionDao;
import io.hmit.modules.fhjw.dto.FhjwRoleFunctionDTO;
import io.hmit.modules.fhjw.entity.FhjwRoleFunctionEntity;
import io.hmit.modules.fhjw.service.FhjwRoleFunctionService;
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-06-22
 */
@Service
public class FhjwRoleFunctionServiceImpl extends CrudServiceImpl<FhjwRoleFunctionDao, FhjwRoleFunctionEntity, FhjwRoleFunctionDTO> implements FhjwRoleFunctionService {

    @Override
    public QueryWrapper<FhjwRoleFunctionEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String roleId = params.get("roleId")==null?"":params.get("roleId").toString();

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

        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<Long> funcIdList) {

        //先删除角色评价模块关系
        deleteByRoleIds(roleId);

        //保存角色菜单关系
        for (Long funcId : funcIdList) {
            FhjwRoleFunctionEntity roleFunctionEntity = new FhjwRoleFunctionEntity();
            roleFunctionEntity.setFunctionId(funcId);
            roleFunctionEntity.setRoleId(roleId);

            //保存
            insert(roleFunctionEntity);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByRoleIds(Long roleId) {

        baseDao.delete(new QueryWrapper<FhjwRoleFunctionEntity>().eq("role_id",roleId));
    }

}
