package com.willalp.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.system.domain.HsPostConfig;
import com.willalp.system.domain.vo.PostTreeVo;
import com.willalp.system.mapper.HsPostConfigMapper;
import com.willalp.system.service.IHsPostConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 岗位配置Service业务层处理
 *
 * @author willalp
 * @date 2022-09-21
 */
@Service
public class HsPostConfigServiceImpl extends ServiceImpl<HsPostConfigMapper, HsPostConfig> implements IHsPostConfigService {
    @Resource
    private HsPostConfigMapper hsPostConfigMapper;

    /**
     * 查询岗位配置列表
     *
     * @param hsPostConfig 岗位配置
     * @return 岗位配置
     */
    @Override
    public List<HsPostConfig> selectHsPostConfigList(HsPostConfig hsPostConfig) {
        return hsPostConfigMapper.selectHsPostConfigList(hsPostConfig);
    }

    @Override
    public List<PostTreeVo> getTreePost(List<HsPostConfig> hsPostConfigs) {
        if (hsPostConfigs != null && 0 != hsPostConfigs.size()) {
            List<PostTreeVo> list = new ArrayList<>();
            hsPostConfigs.forEach(item -> {
                PostTreeVo postTreeVo = new PostTreeVo();
                postTreeVo.setText(item.getPostName());
                postTreeVo.setValue(item.getId());
                String postUserG = item.getPostUserG();
                List<PostTreeVo> postTreeCcVos = new ArrayList<>(setUsers(item));
                if (postTreeCcVos.size() != 0) {
                    postTreeVo.setChildren(postTreeCcVos);
                } else {
                    postTreeVo.setChildren(null);
                }
                list.add(postTreeVo);
            });
            return list;
        } else {
            return getTreePost(selectHsPostConfigList(null));
        }
    }

    @Override
    public List<PostTreeVo> setUsers(HsPostConfig postConfig) {
        List<PostTreeVo> postTreeVos = new ArrayList<>();
        String userBS = "AA";
        if (postConfig.getPostUserG() != null) {
            String[] split = postConfig.getPostUserG().split(",");
            for (int i = 0; i < split.length; i++) {
                PostTreeVo postTreeCcVo = new PostTreeVo();
                String nick = null;
                String userId = null;
                int of = split[i].indexOf(userBS);
                if (of != -1) {
                    nick = split[i].substring(0, of);
                    userId = split[i].substring(of);
                } else {
                    if (split[i].contains("admin")) {
                        nick = "WILLALP";
                        userId = "admin";
                    }
                }
                postTreeCcVo.setText(nick);
                postTreeCcVo.setValue(userId);
                postTreeVos.add(postTreeCcVo);
            }
        }
        return postTreeVos;
    }
}
