package com.ruoyi.system.service.impl;

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

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ExamineConfig;
import com.ruoyi.system.mapper.ExamineConfigMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.IExamineConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;


/**
 * 审核角色配置Service业务层处理
 *
 * @author boman
 * @date 2022-01-24
 */
@Service
public class ExamineConfigServiceImpl implements IExamineConfigService {
    @Autowired
    private ExamineConfigMapper examineConfigMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 查询审核角色配置
     *
     * @param id 审核角色配置主键
     * @return 审核角色配置
     */
    @Override
    public ExamineConfig selectExamineConfigById(Long id) {
        ExamineConfig examineConfig = examineConfigMapper.selectExamineConfigById(id);
        String roleId = examineConfig.getRoleId();
        if (StringUtils.isNotBlank(roleId)) {
            List<Integer> arr = new ArrayList<>();
            String[] split = roleId.split(",");
            StringBuilder sb = new StringBuilder();
            if (split.length > 0){

                for (String roleIds : split) {
                    SysRole sysRole = sysRoleMapper.selectRoleById(Long.valueOf(roleIds));
                    String roleName = sysRole.getRoleName();
                    sb.append(roleName).append("|");
                    arr.add(Integer.valueOf(roleIds));
                }
            }
            examineConfig.setRoleName(sb.toString());
            examineConfig.setRoleIds(arr.stream().mapToInt(Integer::valueOf).toArray());
        }
        return examineConfig;
    }

    /**
     * 查询审核角色配置列表
     *
     * @param examineConfig 审核角色配置
     * @return 审核角色配置
     */
    @Override
    public List<ExamineConfig> selectExamineConfigList(ExamineConfig examineConfig) {

        List<ExamineConfig> examineConfigs = examineConfigMapper.selectExamineConfigList(examineConfig);
        if (examineConfigs.size() > 0) {
            for (ExamineConfig config : examineConfigs) {
                String roleId = config.getRoleId();

                if (StringUtils.isNotBlank(roleId)) {
                    String[] split = roleId.split(",");
                    List<Integer> arr = new ArrayList<>();
                    StringBuilder sb = new StringBuilder();
                    if (split.length > 0){
                        for (String id : split) {
                            SysRole sysRole = sysRoleMapper.selectRoleById(Long.valueOf(id));
                            String roleName = sysRole.getRoleName();
                            sb.append(roleName).append("|");
                            arr.add(Integer.valueOf(id));
                        }
                    }
                    config.setRoleName(sb.toString());
                    config.setRoleIds(arr.stream().mapToInt(Integer::valueOf).toArray());
                }
            }
        }
        return examineConfigs;
    }

    /**
     * 新增审核角色配置
     *
     * @param examineConfig 审核角色配置
     * @return 结果
     */
    @Override
    public int insertExamineConfig(ExamineConfig examineConfig) {
        int[] roleIds = examineConfig.getRoleIds();
        if (roleIds.length > 0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < roleIds.length; i++) {
                if (i == roleIds.length - 1) {
                    sb.append(roleIds[i]);
                } else {
                    sb.append(roleIds[i]).append(",");
                }
            }
            examineConfig.setRoleId(sb.toString());
        }
        examineConfig.setCreateTime(DateUtils.getNowDate());
        return examineConfigMapper.insertExamineConfig(examineConfig);
    }

    /**
     * 修改审核角色配置
     *
     * @param examineConfig 审核角色配置
     * @return 结果
     */
    @Override
    public int updateExamineConfig(ExamineConfig examineConfig) {
        int[] roleIds = examineConfig.getRoleIds();
        if (roleIds.length > 0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < roleIds.length; i++) {
                if (i == roleIds.length - 1) {
                    sb.append(roleIds[i]);
                } else {
                    sb.append(roleIds[i]).append(",");
                }
            }
            examineConfig.setRoleId(sb.toString());
        }
        examineConfig.setUpdateTime(DateUtils.getNowDate());
        return examineConfigMapper.updateExamineConfig(examineConfig);
    }

    /**
     * 批量删除审核角色配置
     *
     * @param ids 需要删除的审核角色配置主键
     * @return 结果
     */
    @Override
    public int deleteExamineConfigByIds(Long[] ids) {
        return examineConfigMapper.deleteExamineConfigByIds(ids);
    }

    /**
     * 删除审核角色配置信息
     *
     * @param id 审核角色配置主键
     * @return 结果
     */
    @Override
    public int deleteExamineConfigById(Long id) {
        return examineConfigMapper.deleteExamineConfigById(id);
    }

    /**
     * 根据审批等级查看角色列表
     *
     * @param level
     * @return
     */
    @Override
    public List<ExamineConfig> selectExamineConfigByLevel(String level) {
        return examineConfigMapper.selectExamineConfigByLevel(level);
    }
}
