18756
2024-08-22 33dbf637c8527e9f8d66a454ee711b7f7aee7102
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.product.administration.service;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.product.admin.config.CmnConst;
import com.product.admin.service.PublicService;
import com.product.administration.service.ide.ITaskGoalService;
import com.product.common.lang.StringUtils;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.util.BaseUtil;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * @Author cheng
 * @Description 任务指标
 * @Date 2021/6/29 14:22
 * @Version 1.0
 */
@Service
public class TaskGoalService extends PublicService implements ITaskGoalService {
 
    /**
     * 根据用户uuid & 单位组uuid查询员工所在机构
     *
     * @param user_ids    用户uuids
     * @param group_uuids 单位组uuids
     * @return
     */
    public List<JSONObject> getStaffsLevelAll(String user_ids, String group_uuids) {
        if (StringUtils.isEmpty(user_ids) && StringUtils.isEmpty(group_uuids)) {
            return null;
        }
        if (!StringUtils.isEmpty(group_uuids)) {
            //查询单位组下的所有人员id
            String[] group_uuid = group_uuids.split(CmnConst.COMMA);
            StringBuilder sql = new StringBuilder();
            sql.append(" SELECT ");
            sql.append(" CONCAT(user_ids) user_ids ");
            sql.append(" FROM  ");
            sql.append("product_sys_company_user_group ");
            sql.append(" WHERE ");
            sql.append(BaseUtil.buildQuestionMarkFilter(CmnConst.UUID, group_uuid.length, true));
            FieldSetEntity fs = getBaseDao().getFieldSetEntityBySQL(sql.toString(), group_uuid, false);
            if (fs != null) {
                if (StringUtils.isEmpty(user_ids)) {
                    user_ids = "";
                } else {
                    user_ids += CmnConst.COMMA;
                }
                user_ids += fs.getString("user_ids");
            }
        }
        //结果集合
        List<JSONObject> result = Lists.newArrayList();
        if (!StringUtils.isEmpty(user_ids)) {
            String[] user_id = user_ids.split(CmnConst.COMMA);
            //放入set集合去重
            Set<String> u = Sets.newHashSet(user_id);
            //转为String数组 作为查询动态参数
            user_id = u.toArray(new String[]{});
            //获取需要展示的员工信息
            DataTableEntity dt = getBaseDao().listTable(CmnConst.PRODUCT_SYS_STAFFS, BaseUtil.buildQuestionMarkFilter(CmnConst.USER_ID, user_id.length, true), user_id
                    , new Object[]{"uuid", "user_id", "show_name", "dept_uuid", "org_level_uuid"});
            //查询当前用户所在客户所有的组织机构
            DataTableEntity org_level = getBaseDao().listTable(CmnConst.PRODUCT_SYS_ORG_LEVELS, "client_uuid=?",
                    new Object[]{SpringMVCContextHolder.getCurrentUser().getClientUuid()},
                    new Object[]{"uuid", "org_level_code", "org_level_code_parent", "org_level_name", "org_level_type"});
            // 所有机构信息 key为org_level_code
            Map<String, JSONObject> orgLevel = getOrgLevel(org_level);
            List<String> includeStaffOrg = Lists.newArrayList();
            if (!BaseUtil.dataTableIsEmpty(org_level) && !BaseUtil.dataTableIsEmpty(dt) && orgLevel.size() > 0) {
                for (int i = 0; i < dt.getRows(); i++) {
                    //员工所属部门uuid
                    String org_level_uuid = dt.getString(i, CmnConst.DEPT_UUID);
                    if (!StringUtils.isEmpty(org_level_uuid)) {
                        List<FieldSetEntity> fieldSets = org_level.getFieldSetEntity(org_level_uuid);
                        if (fieldSets != null && fieldSets.size() > 0) {
                            FieldSetEntity fs = fieldSets.get(0);
                            String org_level_code = fs.getString(CmnConst.ORG_LEVEL_CODE);
                            //根据部门code获取部门信息
                            JSONObject currentDept = orgLevel.get(org_level_code);
                            if (currentDept != null) {
                                //获取部门下级
                                JSONArray children = currentDept.getJSONArray(CmnConst.CHILDREN);
                                if (children == null) {
                                    children = new JSONArray();
                                    currentDept.put(CmnConst.CHILDREN, children);
                                }
                                Map<Object, Object> values = dt.getFieldSetEntity(i).getValues();
                                values.put("~type~", 2);
                                values.put("name", values.get("show_name"));
                                //在部门下级中增加当前遍历的人员
                                children.add(values);
                                //是否包含该部门
                                if (!includeStaffOrg.contains(org_level_code)) {
                                    includeStaffOrg.add(org_level_code);
                                }
                            }
                        }
                    }
                }
                //排序
                includeStaffOrg.sort(String::compareTo);
                //逆循环 (从最后往前)
                for (int i = includeStaffOrg.size() - 1; i > -1; i--) {
                    orgLevelParent(result, orgLevel.get(includeStaffOrg.get(i)), orgLevel);
                }
            }
        }
        return result;
    }
 
    private JSONObject orgLevelTotalRow(JSONObject current){
        JSONObject totalRow = new JSONObject();
        current.put("totalRow", true);
        totalRow.put("name", current.getString(CmnConst.NAME) + "的合计行");
        totalRow.put(CmnConst.ORG_LEVEL_UUID,current.getString(CmnConst.UUID));
        totalRow.put(CmnConst.ORG_LEVEL_CODE,current.getString(CmnConst.ORG_LEVEL_CODE));
        totalRow.put("~type~",3);
        return totalRow;
    }
 
    private void orgLevelParent(List<JSONObject> result, JSONObject current, Map<String, JSONObject> orgLevel) {
        String org_level_code_parent = current.getString(CmnConst.ORG_LEVEL_CODE_PARENT);
 
        //没有父级了
        if (StringUtils.isEmpty(org_level_code_parent)) {
 
            result.add(current);
            result.add(orgLevelTotalRow(current));
        } else {
            JSONObject parent_org = orgLevel.get(org_level_code_parent);
            JSONArray children = parent_org.getJSONArray(CmnConst.CHILDREN);
            if (children == null) {
                orgLevelParent(result, parent_org, orgLevel);
            }
            if (current != null) {
                JSONArray array = new JSONArray();
                array.add(current);
                array.add(orgLevelTotalRow(current));
                if (children != null) {
                    array.addAll(children);
                }
                parent_org.put(CmnConst.CHILDREN, array);
            }
        }
 
    }
 
 
    /**
     * 将dt 转为 org_level_code 作为键的map
     *
     * @param dt
     * @return
     * @throws BaseException
     */
    private Map<String, JSONObject> getOrgLevel(DataTableEntity dt) throws BaseException {
        Map<String, JSONObject> map = Maps.newHashMap();
        if (!BaseUtil.dataTableIsEmpty(dt)) {
            JSONArray array = BaseUtil.dataTableEntityToJson(dt);
            for (int i = 0; i < array.size(); i++) {
                String code = dt.getString(i, CmnConst.ORG_LEVEL_CODE);
                array.getJSONObject(i).put("~type~", dt.getInt(i, CmnConst.ORG_LEVEL_TYPE));
                array.getJSONObject(i).put("name", dt.getString(i, CmnConst.ORG_LEVEL_NAME));
                map.put(code, array.getJSONObject(i));
            }
        }
        return map;
    }
 
 
    /**
     * 查询功能字段 field_type = datetime,userid
     *
     * @param table_uuid
     * @return
     * @throws BaseException
     */
    public Map<String, Object> getFieldsByFunction(String table_uuid) throws BaseException {
        DataTableEntity dt = DataPoolCacheImpl.getInstance().getCacheData("所有字段信息并按表分组", new String[]{table_uuid});
        if (!BaseUtil.dataTableIsEmpty(dt)) {
            List<Object> user = Lists.newArrayList();
            List<Object> datetime = Lists.newArrayList();
            for (int i = 0; i < dt.getRows(); i++) {
                String field_type = dt.getString(i, CmnConst.FIELD_TYPE);
                HashMap<Object, Object> values = (HashMap<Object, Object>) dt.getFieldSetEntity(i).getValues();
                values = (HashMap<Object, Object>) values.clone();
                if ("datetime".equals(field_type)) {
                    datetime.add(values);
                } else if ("userid".equals(field_type)) {
                    user.add(values);
                } else {
                    values.clear();
                }
            }
            Map<String, Object> m = Maps.newHashMap();
            m.put("user", user);
            m.put("datetime", datetime);
            return m;
        }
        return null;
    }
 
    /**
     * 获取指标列表
     *
     * @param fse
     * @return
     * @throws BaseException
     */
    public DataTableEntity getList(FieldSetEntity fse) throws BaseException {
        return listTable(fse, true, 1, null, null);
    }
 
    public DataTableEntity getFucntionByRole() throws BaseException {
        String roles = SpringMVCContextHolder.getCurrentUser().getRoles();
        if (!StringUtils.isEmpty(roles)) {
            StringBuilder sql = new StringBuilder();
            sql.append(" SELECT uuid,function_name,function_description,table_uuid FROM product_sys_functions ");
            sql.append(" where uuid in (select function_uuid FROM product_sys_function_permission where ");
            sql.append(BaseUtil.buildQuestionMarkFilter("role_uuid", roles.split(CmnConst.COMMA).length, true));
            sql.append(") ");
            return getBaseDao().listTable(sql.toString(), roles.split(CmnConst.COMMA));
        }
        return null;
    }
 
 
}