package com.hzs.system.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.system.sys.SysRoleMenuColumn;
import com.hzs.system.sys.mapper.SysRoleMenuColumnMapper;
import com.hzs.system.sys.param.RoleMenuColumnDetailItemParam;
import com.hzs.system.sys.param.RoleMenuColumnDetailParam;
import com.hzs.system.sys.param.RoleMenuColumnParam;
import com.hzs.system.sys.service.ISysRoleMenuColumnService;
import com.hzs.system.sys.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色菜单字段表 服务实现类
 *
 * @author hzs
 * @since 2023-06-07
 */
@Service
public class SysRoleMenuColumnServiceImpl extends ServiceImpl<SysRoleMenuColumnMapper, SysRoleMenuColumn> implements ISysRoleMenuColumnService {

    @Autowired
    private ISysRoleService iSysRoleService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveData(RoleMenuColumnParam param, Long userId) {
        // 遍历角色ID列表
        for (Long roleId : param.getRoleIdList()) {
            if (CollectionUtil.isNotEmpty(param.getDetailList())) {
                // 遍历明细列表
                for (RoleMenuColumnDetailParam detailParam : param.getDetailList()) {
                    if (CollectionUtil.isNotEmpty(detailParam.getMenuColumnList())) {
                        // 入库数据
                        List<SysRoleMenuColumn> saveList = new ArrayList<>();

                        // 遍历菜单列列表
                        for (RoleMenuColumnDetailItemParam menuColumn : detailParam.getMenuColumnList()) {
                            if (StringUtils.isNotEmpty(menuColumn.getMenuColumn())) {
                                SysRoleMenuColumn roleMenuColumn = new SysRoleMenuColumn();
                                roleMenuColumn.setPkRole(roleId);
                                roleMenuColumn.setPkMenu(detailParam.getMenuId());
                                roleMenuColumn.setMenuColumn(menuColumn.getMenuColumn());
                                roleMenuColumn.setShowFlag(null != menuColumn.getShowFlag() ? menuColumn.getShowFlag() : EYesNo.NO.getIntValue());
                                saveList.add(roleMenuColumn);
                            }
                        }

                        if (CollectionUtil.isNotEmpty(saveList)) {
                            // 插入并更新数据
                            baseMapper.insertBatch(saveList, userId);
                        }
                    }
                }
            }
        }
    }

    @Override
    public List<SysRoleMenuColumn> queryMenuColumn(String perms, Long userId) {
        return baseMapper.queryMenuColumn(perms, iSysRoleService.queryRoleIdListByUser(userId));
    }

}
