zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package com.changhong.epc.admin.service.initguide;
 
import com.changhong.epc.admin.mapper.centen.InitDataMapper;
import com.changhong.epc.admin.mapper.centen.initguide.BatchInitCompanyMapper;
import com.changhong.epc.admin.mapper.centen.initguide.InitCompanyMapper;
import com.changhong.epc.bean.PageResult;
import com.changhong.epc.bean.admin.BatchInitCompany;
import com.changhong.epc.bean.admin.CostGroupType;
import com.changhong.epc.bean.admin.InitCompany;
import com.changhong.epc.bean.admin.InitPowerInfo;
import com.changhong.epc.bean.tenant.system.permission.PowerKey;
import com.changhong.epc.constter.base.BaseConst;
import com.changhong.epc.constter.system.SystemClients;
import com.changhong.epc.rely.api.bean.Company;
import com.changhong.epc.rely.api.bean.Tenant;
import com.changhong.epc.rely.api.epc.admin.InitCompanyApi;
import com.changhong.epc.rely.api.epc.tenant.TenantInitApi;
import com.changhong.epc.rely.api.service.CscApiService;
import com.changhong.epc.rely.api.service.workFlow.WorkFlowService;
import com.changhong.epc.rely.api.tool.PowerTool;
import com.iemsoft.framework.cloud.core.thread.Keys;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
import com.iemsoft.framework.cloud.core.tools.Assert;
import com.iemsoft.framework.cloud.core.tools.StringUtil;
import com.iemsoft.framework.cloud.redis.annotation.CacheRm;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
 
import static com.changhong.epc.constter.cache.admin.InitCompanyCache.INIT_COMPANY_INFO;
import static com.changhong.epc.constter.cache.admin.InitCompanyCache.INIT_COMPANY_INFO_LIST;
 
@Service
public class InitGuideServiceImpl implements InitGuideService,SystemClients,BaseConst {
    
    @Resource
    private InitCompanyMapper initCompanyMapper;
    
    @Resource
    private InitDataMapper initDataMapper;
 
    @Resource
    private CscApiService cscApiService;
 
    @Resource
    private WorkFlowService workFlowService;
 
    @Resource
    private InitCompanyApi initCompanyApi;
 
    @Resource
    private TenantInitApi tenantInitApi;
 
    @Resource
    private BatchInitCompanyMapper batchInitCompanyMapper;
 
    public PageResult<Tenant> getTenants(Tenant tenant){
        return cscApiService.getTenants(tenant);
    }
 
    @Override
    public PageResult<Company> getCompanys(Company company) {
//        List<Company> companies = cscApiService.getCompanyOrgs(tenantId);
//        // 过滤掉已经初始化成功的公司
//        Set<Integer> initCompanyIds = Arrays.asList(
//                Optional.ofNullable(
//                        initCompanyMapper.selectInitCompanyId(tenantId)
//                ).map(ids->ids.split(","))
//                        .orElse(new String[0])
//        ).stream()
//                .map(Integer::valueOf)
//                .collect(Collectors.toSet());
//
//        return companies
//                .stream()
//                .filter(comp->!initCompanyIds.contains(comp.getId()))
//                .collect(Collectors.toList());
        return cscApiService.getCompanys(company);
    }
 
    public int doInit(List<Company> companys,int id,List<Tenant> tenants){
        try{
            int count = 0;
            List<CostGroupType> costGroupType = initCompanyMapper.getCostGroupType(id);
            for(Company c:companys){
                String tenantName = null;
                Tenant tenant = null;
                for(Tenant t:tenants){
                    if(t.getId() == c.getTenantId() || t.getId().equals(c.getTenantId())){
                        // System.out.println(t.getName());
                        tenant = t;
                        tenantName = t.getName();
                        break;
                    }
                }
                //初始化公司
                //==========================
                PowerKey powerKey = PowerTool.createChildSystem(c, tenant);
                //创建子系统 
                //==========================
                //添加初始化得公司
                count += initCompanyMapper.addInitCompany(c,powerKey);
                //添加初始化得租户
                count += initDataMapper.initTenant(c.getTenantId(), tenantName);
                // System.out.println("isok"+count);
                //通过租户id、公司id 创建相应得业务表
                count += initDataMapper.createTable(c.getTenantId(),c.getId());
                // System.out.println("isok"+count);
                //初始化租户费用类型表,从云上行同步
                count += initDataMapper.initdataToCompType(costGroupType,id);
                //初始化公司费用类型表,从云上行同步
                count += initDataMapper.initdataToComp(c.getTenantId(), c.getId(), costGroupType,id);
                //新租户,同步租户主数据
 
                //调用工作流初始化
 
                //表单初始化
 
                // System.out.println("isok"+count);
            }
            return count;
        }catch (Exception e){
            // System.out.println("发生异常---initGuideServiceImpl.doInit");
            return 0;
        }
    }
 
    @Override
    @Transactional
    @CacheRm(value = {INIT_COMPANY_INFO, INIT_COMPANY_INFO_LIST}, tenant = false, company = false)
    public int initTenantCompany(InitCompany initCompanyP) {
        ThreadData.set(BaseConst.OPEN_ID    , initCompanyP.getOpenId());
        initCompanyP.setDataStart(0);
        InitCompany initCompany = initCompanyMapper.selectOne(initCompanyP);
        // 必须先初始化
        if(initCompany == null){
            InitPowerInfo initPowerInfo = new InitPowerInfo();
            initPowerInfo.setTenantId(initCompanyP.getTenantId());
            initPowerInfo.setCompanyId(initCompanyP.getCompanyId());
            initCompany = addCompanyPowerInfo(initPowerInfo);
        }
        // 已经初始化
        if(Objects.equals(initCompany.getInitState(), 0)){
            return 0;
        }
 
        Integer tenantId = initCompany.getTenantId()
                , companyId = initCompany.getCompanyId();
        ThreadData.set(Keys.TENANT_ID       , Objects.toString(tenantId));
        ThreadData.set(Keys.COMPANY_ID      , Objects.toString(companyId));
        ThreadData.set(BaseConst.TENANT_ID  , Objects.toString(tenantId));
        ThreadData.set(BaseConst.COMPANY_ID , companyId);
 
        // 初始化菜单列表
        initCompanyMapper.initMenu(tenantId, companyId);
        // 初始化工作流
        workFlowService.initialization(tenantId, companyId);
        // 初始化租户数据
        tenantInitApi.init(initCompany);
        // 修改初始化状态
        initCompany.setInitState(0);
        initCompanyMapper.updateByPrimaryKeySelective(initCompany);
        return 0;
    }
 
    /**
     * 查寻初始化过的公司
     */
    @Override
    public List<InitCompany> getInitCompany(InitCompany initCompany) {
        return initCompanyMapper.selectInitCompany(initCompany);
    }
 
    @Override
    public InitCompany addCompanyPowerInfo(InitPowerInfo initPowerInfo) {
        initCompanyApi.removeInte();
        Integer tenantId = initPowerInfo.getTenantId();
        Integer companyId = initPowerInfo.getCompanyId();
        Tenant tenant = cscApiService.getTenantAndCompany(tenantId, companyId);
        Company company = Optional.ofNullable(tenant).map(Tenant::getCompanys).orElse(new ArrayList<>()).stream().findFirst().orElse(null);
        Assert.empty(tenant, "no this tenant:{0}", Objects.toString(tenantId));
        Assert.empty(company, "no this company:{0}", Objects.toString(companyId));
 
        InitCompany initCompany = new InitCompany();
        initCompany.setTenantId(initPowerInfo.getTenantId());
        initCompany.setCompanyId(initPowerInfo.getCompanyId());
        if(initPowerInfo.getAppKey() == null) {
            // 查询权限
            PowerKey powerKey = cscApiService.getSubCompanySystem(initCompany.getCompanyId());
            if(powerKey == null) {
                // 创建权限
                powerKey = PowerTool.createChildSystem(company, tenant);
            }
            initPowerInfo.setAppKey(powerKey.getAppKey());
            initPowerInfo.setSecretKey(powerKey.getSecretKey());
        }
        initCompany.setPowerAppKey(initPowerInfo.getAppKey());
        initCompany.setPowerSecretKey(initPowerInfo.getSecretKey());
        initCompany.setInitState(2);
        initCompany.setDbName(getDataSourceRef(initPowerInfo.getCompanyId()));
        initCompany.setCompanyMasterId(Optional.ofNullable(company.getMasterCompanyId()).map(NumberUtils::createInteger).orElse(0));
        initCompany.setCompanyName(company.getName());
        initCompany.setTenantName(tenant.getName());
        initCompany.initParam();
        initCompanyMapper.insertSelective(initCompany);
        return initCompany;
    }
 
    @Override
    public String initCompanys(List<BatchInitCompany> batchInitCompany, Integer costTypeTempId) {
        String groupId = StringUtil.getUUID();
        batchInitCompany
                .stream()
                .map(comp->{
                    comp.setCostTypeTempId(costTypeTempId);
                    comp.setGroupId(groupId);
                    comp.initParam();
                    return comp;
                })
                .forEach(batchInitCompanyMapper::insertSelective);
        return groupId;
    }
 
    @Override
    public List<BatchInitCompany> selectInitCompanys(String groupId) {
        BatchInitCompany param = new BatchInitCompany();
        param.setGroupId(groupId);
        return batchInitCompanyMapper.select(param);
    }
 
    @Override
    public int addInitCompany(InitCompany initCompany) {
        // 初始化菜单列表
        initCompanyMapper.initMenu(initCompany.getTenantId(), initCompany.getCompanyId());
        initCompany.initParam();
        return initCompanyMapper.insertSelective(initCompany);
    }
 
    @Override
    public List<BatchInitCompany> selectBatchCompanys() {
        BatchInitCompany batchInitCompany = new BatchInitCompany();
        batchInitCompany.setDataStart(0);
        return batchInitCompanyMapper.select(batchInitCompany);
    }
 
    @Override
    public int updateBatchCompanys(BatchInitCompany batchInitCompany) {
        return batchInitCompanyMapper.updateByPrimaryKeySelective(batchInitCompany);
    }
 
    public static final int DATA_SOURCE_SIZE = 2;
 
    public static final String[] DATA_SOURCE_INFO = {"tenant_1", "tenant_2"};
 
    static final String getDataSourceRef(Integer companyId){
        return DATA_SOURCE_INFO[(DATA_SOURCE_SIZE - 1) & companyId];
    }
 
}