package com.willalp.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.StringUtils;
import com.willalp.files.domain.HsOrgPersonnelFiles;
import com.willalp.files.service.IHsOrgPersonnelFilesService;
import com.willalp.group.domain.HsGroupConfig;
import com.willalp.group.mapper.HsGroupConfigMapper;
import com.willalp.group.service.IHsGroupConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 群组配置Service业务层处理
 *
 * @author willalp
 * @date 2022-07-22
 */
@Service
public class HsGroupConfigServiceImpl extends ServiceImpl<HsGroupConfigMapper, HsGroupConfig> implements IHsGroupConfigService {
    @Resource
    private HsGroupConfigMapper hsGroupConfigMapper;
    @Resource
    private IHsOrgPersonnelFilesService filesService;

    /**
     * 查询群组配置列表
     *
     * @param hsGroupConfig 群组配置
     * @return 群组配置
     */
    @Override
    public List<HsGroupConfig> selectHsGroupConfigList(HsGroupConfig hsGroupConfig) {
        List<HsGroupConfig> hsGroupConfigs = hsGroupConfigMapper.selectHsGroupConfigList(hsGroupConfig);
        List<HsOrgPersonnelFiles> hsOrgPersonnelFiles = filesService.selectHsOrgPersonnelFilesList(null);
        if (0 != hsGroupConfigs.size()) {
            hsGroupConfigs.forEach(i -> {
                String[] groupMembers = i.getGroupMembers();
                if (null != groupMembers) {
                    for (int i1 = 0; i1 < groupMembers.length; i1++) {
                        int finalI = i1;
                        List<HsOrgPersonnelFiles> collect = hsOrgPersonnelFiles.stream().filter(f ->
                                StringUtils.equals(f.getUserCode(), groupMembers[finalI]))
                                .collect(Collectors.toList());
                        if (0 != collect.size()) {
                            i.getGroupMemberText().add(collect.get(0).getUserName());
                        }
                    }
                }
            });
        }
        return hsGroupConfigs;
    }

    @Override
    public boolean groupAddUser(HsGroupConfig hsGroupConfig) {
        String id = hsGroupConfig.getId();
        HsGroupConfig gc = hsGroupConfigMapper.selectById(id);
        List<String> groupMemberText = hsGroupConfig.getGroupMemberText();

        StringBuilder groupMember = new StringBuilder();
        if (0 != groupMemberText.size()) {
            groupMemberText.forEach(codeItem -> {
                if (null != gc.getGroupMember()) {
                    if (gc.getGroupMember().contains(codeItem)) {
                        throw new BaseException("用户编号：" + codeItem + "重复");
                    }
                    groupMember.append(",").append(codeItem);
                }
            });
        } else {
            throw new BaseException("未添加USER_CODE，请检测");
        }
        if (StringUtils.isBlank(gc.getGroupMember()) && StringUtils.isNotBlank(groupMember)) {
            gc.setGroupMember(groupMember.substring(1));
        } else {
            gc.setGroupMember(gc.getGroupMember() + groupMember);
        }
        return this.updateById(gc);
    }

    @Override
    public List<HsGroupConfig> appGroupList(String un) {
        if (StringUtils.isBlank(un)) {
            throw new BaseException("用户编号验证失败数据为空");
        }
        return this.baseMapper.selectList(new QueryWrapper<HsGroupConfig>()
                .like("group_member", un));
    }
}
