杜洪波
2024-10-09 0265a4b398b7618880d5dcb6fdb50939409eb2a0
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
package com.product.administration.service;
 
import java.util.*;
 
import com.product.core.entity.DataTableEntity;
import com.product.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.product.admin.service.CodeService;
import com.product.administration.config.CmnConst;
import com.product.administration.config.SystemCode;
import com.product.administration.service.ide.ITeamService;
import com.product.common.lang.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
 
@Component
public class TeamService extends AbstractBaseService implements ITeamService {
 
    @Autowired
    BaseDao baseDao;
 
    @Autowired
    CodeService codeService;
 
    /**
     * 团队树
     *
     * @return
     */
    public DataTableEntity treeTeam() {
        String sql = "SELECT *,(SELECT GROUP_CONCAT(user_id) FROM product_sys_staffs WHERE team_code =`code`)user_ids FROM product_sys_team_config";
        DataTableEntity dt = baseDao.listTable(sql, new Object[]{});
 
        return BaseUtil.dataTableToTreeTable(dt, "code", "parent_code", null);
    }
 
    /**
     * 详情
     *
     * @return
     */
    public FieldSetEntity findTeam(String uuid) {
        String sql = "SELECT *,(SELECT GROUP_CONCAT(user_id) FROM product_sys_staffs WHERE team_code =`code`)user_ids FROM product_sys_team_config where uuid =?";
        return baseDao.getFieldSetEntityBySQL(sql, new Object[]{uuid}, false);
 
    }
 
    /**
     * 团队保存
     *
     * @param fse
     */
    @Transactional
    public void saveTeam(FieldSetEntity fse) {
 
        //获取团队名称
        String teanName = fse.getString("name");
        FieldSetEntity fseValitation = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_TEAM_CONFIG, "name=?", new Object[]{teanName}, false);
        if (fseValitation != null) {
            throw new BaseException(SystemCode.TEAM_SAVE_FAIL_DUPLICATE_NAME.getValue(), SystemCode.TEAM_SAVE_FAIL_DUPLICATE_NAME.getText());
        }
 
        //是否生成新上级
        boolean isNewParent = false;
 
        //获取上级编码
        String newParentCode = fse.getString("parent_code");
        if (newParentCode == null) {
            newParentCode = "";
        }
 
        //原编码
        String originCode = "";
 
        //判断是否修改
        if (!StringUtils.isEmpty(fse.getUUID())) {
            //查询原数据
            FieldSetEntity fseOriginTeam = this.findTeam(fse.getUUID());
            //获取原上级编码
            String originparentCode = fseOriginTeam.getString("parent_code");
            if (originparentCode == null) {
                originparentCode = "";
            }
            //判断上级是否变化
            if (!newParentCode.equals(originparentCode)) {
                isNewParent = true;
                originCode = fseOriginTeam.getString("code");
            }
        } else {    //新增
            isNewParent = true;
        }
 
        //判断是否需要重新生成编码
        if (isNewParent) {
            //生成新编码
            String newCode = codeService.createCode(CmnConst.PRODUCT_SYS_TEAM_CONFIG, "code", newParentCode);
            fse.setValue("code", newCode);
            //判断是否修改
            if (!StringUtils.isEmpty(fse.getUUID())) {
                //变更员工关联编码
                StringBuilder sqlStaff = new StringBuilder();
                sqlStaff.append(" update mrbase_sys_staffs ");
                sqlStaff.append(" set team_code=concat(replace(substring(team_code,1,locate(?,team_code)+length(?)),?,?),substring(team_code,locate(?,team_code)+length(?)+1)) ");
                sqlStaff.append(" where team_code like ? ");
                baseDao.executeUpdate(sqlStaff.toString(), new Object[]{originCode, originCode, originCode, newCode, originCode, originCode, originCode + "%"});
 
                //变更子代团队编码
                StringBuilder sqlTeam = new StringBuilder();
                sqlTeam.append(" update product_sys_team_config ");
                sqlTeam.append(" set code=concat(replace(substring(code,1,locate(?,code)+length(?)),?,?),substring(code,locate(?,code)+length(?)+1)), ");
                sqlTeam.append(" parent_code=concat(replace(substring(parent_code,1,locate(?,parent_code)+length(?)),?,?),substring(parent_code,locate(?,parent_code)+length(?)+1)) ");
                sqlTeam.append(" where code like ? ");
                baseDao.executeUpdate(sqlStaff.toString(), new Object[]{originCode, originCode, originCode, newCode, originCode, originCode, originCode, originCode, originCode, newCode, originCode, originCode, originCode + "%"});
            }
        }
        BaseUtil.createCreatorAndCreationTime(fse);
        baseDao.saveFieldSetEntity(fse);
    }
 
    /**
     * 删除团队
     *
     * @param code 编码
     * @return
     * @throws BaseException
     */
    @Transactional
    public boolean deleteTeam(String code) throws BaseException {
 
        //子团队验证
        DataTableEntity subTeam = baseDao.listTable(CmnConst.PRODUCT_SYS_TEAM_CONFIG, "parent_code like ?", new Object[]{code + "%"});
        if (!BaseUtil.dataTableIsEmpty(subTeam)) {
            throw new BaseException(SystemCode.TEAM_MANAGER_DELETE_FAIL_HAS_SUB_TEAM.getValue(), SystemCode.TEAM_MANAGER_DELETE_FAIL_HAS_SUB_TEAM.getText());
        }
 
        //员工验证
        DataTableEntity staffTeam = baseDao.listTable(CmnConst.PRODUCT_SYS_STAFFS, "team_code like ?", new Object[]{code + "%"});
        if (!BaseUtil.dataTableIsEmpty(staffTeam)) {
            throw new BaseException(SystemCode.TEAM_MANAGER_DELETE_FAIL_HAS_STAFF_REFERED.getValue(), SystemCode.TEAM_MANAGER_DELETE_FAIL_HAS_STAFF_REFERED.getText());
        }
 
 
        //删除团队及及下级团队
        return baseDao.delete(CmnConst.PRODUCT_SYS_TEAM_CONFIG, "code = ? ", new Object[]{code});
    }
}