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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package com.changhong.epc.tenant.service.system.impl;
 
import com.alibaba.fastjson.TypeReference;
import com.changhong.epc.bean.admin.CostFromTemplate;
import com.changhong.epc.bean.admin.InitCompany;
import com.changhong.epc.bean.admin.TenantInfo;
import com.changhong.epc.bean.admin.extend.CostGroupTypeExtend;
import com.changhong.epc.bean.admin.extend.CostTypeTemplateExtend;
import com.changhong.epc.bean.count.system.SystemMathExtend;
import com.changhong.epc.bean.tenant.cost.CostFromMapping;
import com.changhong.epc.bean.tenant.system.EpcShowColumn;
import com.changhong.epc.bean.tenant.system.SystemForm;
import com.changhong.epc.bean.tenant.system.SystemFormInfo;
import com.changhong.epc.config.tool.MybatisTool;
import com.changhong.epc.constter.base.BaseConst;
import com.changhong.epc.constter.base.Context;
import com.changhong.epc.constter.system.prop.ChangHongProperties;
import com.changhong.epc.rely.api.bean.CscResult;
import com.changhong.epc.rely.api.epc.admin.CostFromTemplateApi;
import com.changhong.epc.rely.api.epc.admin.InitCompanyApi;
import com.changhong.epc.rely.api.service.CscApiService;
import com.changhong.epc.rely.api.tool.EpcApiHttp;
import com.changhong.epc.tenant.mapper.tenant.cost.CostFromMappingMapper;
import com.changhong.epc.tenant.mapper.tenant.system.EpcShowColumnMapper;
import com.changhong.epc.tenant.mapper.tenant.system.TenantInfoMapper;
import com.changhong.epc.tenant.service.cost.CostTypeService;
import com.changhong.epc.tenant.service.norm.SuppStanderService;
import com.changhong.epc.tenant.service.system.TenantInfoService;
import com.changhong.epc.tenant.service.system.budget.BudgetConfigService;
import com.changhong.epc.tenant.service.system.math.SystemMathService;
import com.iemsoft.framework.cloud.core.thread.Keys;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import com.iemsoft.framework.cloud.core.tools.http.Http;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
 
@Slf4j
@Service("tenantInfoServiceImpl")
public class TenantInfoServiceImpl implements TenantInfoService, BaseConst {
 
    @Value("${form.temp.tenantId:100000023}")
    private String formTempTenantId;
 
    @Resource
    private TenantInfoMapper tenantInfoMapper;
 
    @Resource(name="cscApiServiceImpl")
    private CscApiService cscApiService;
 
    @Resource
    private BudgetConfigService budgetConfigService;
 
    @Resource
    private CostTypeService costTypeService;
 
    @Resource
    private CostFromTemplateApi costFromTemplateApi;
 
    @Resource
    private CostFromMappingMapper costFromMappingMapper;
 
    @Resource
    private ChangHongProperties changHongProperties;
 
    @Resource
    private SystemMathService systemMathService;
 
    @Resource
    private SuppStanderService suppStanderService;
 
    @Resource
    private InitCompanyApi initCompanyApi;
 
    @Resource
    private EpcShowColumnMapper epcShowColumnMapper;
 
    @Value("${temp.tenantId:100000023}")
    private Integer tempTenantId;
 
    @Value("${temp.companyId:17}")
    private Integer tempCompanyId;
 
 
 
    @Override
    public TenantInfo getTenantInfo(TenantInfo tenantInfo) {
        return tenantInfoMapper.selectTenant(tenantInfo);
    }
 
    @Override
    public int updateTenantInfo(TenantInfo tenantInfo) {
        int i = cscApiService.updateUserName(new ModelMap("realName",tenantInfo.getUserName()));
        if(tenantInfoMapper.selectTenant(tenantInfo)!=null){
            //tenantInfo.initParam();
            tenantInfo.setUpdP(ThreadData.get(Keys.USER_ID));
            tenantInfo.setUpdT(new Date());
            i+=tenantInfoMapper.updateTenanName(tenantInfo);
        }else{
            tenantInfo.initParam();
            i+=tenantInfoMapper.insert(tenantInfo);
        }
        return i;
    }
 
    @Override
//    @Transactional
    // 需要切换数据源,不能用事务
    public int initTenantInfo(InitCompany initCompany) {
        ThreadData.set(COMPANY_MASTER_ID, initCompany.getCompanyMasterId());
        ThreadData.set(COMPANY_ID, initCompany.getCompanyId());
        ThreadData.set(TENANT_ID, String.valueOf(initCompany.getTenantId()));
 
        // 初始化表结构
        initTable(initCompany);
 
        // 初始化组织机构
        initOrg(initCompany);
 
        // 初始化系统变量
        initSystemMath(initCompany);
 
        // 初始化费用类型
        initCost(initCompany);
 
        return 1;
    }
    @Override
    public Integer updateShowColumnById(EpcShowColumn epc, String openId){
        if(epc != null){
            this.deleteShowColumnById(epc,openId);
            return epcShowColumnMapper.updateShowColumnById(epc,openId);
        }
        return null;
    }
    @Override
    public EpcShowColumn selectShowColumnById(EpcShowColumn epc, String openId){
       return epcShowColumnMapper.selectShowColumnById(epc,openId);
    }
    @Override
    public void deleteShowColumnById(EpcShowColumn epc, String openId){
       epcShowColumnMapper.deleteShowColumnById(epc,openId);
    }
 
    @Override
    public void insertShowColumn(EpcShowColumn epc, String openId){
       epcShowColumnMapper.insertShowColumn(epc,openId);
    }
 
 
 
 
 
    private void initCost(InitCompany initCompany) {
        log.debug("开始初始化费用类型...");
        Map<String, String> formIdCache = new HashMap<>();
 
        // 全量拉取 业务类型
        CostTypeTemplateExtend costTypeTemplate = costFromTemplateApi.getCostTypeTemplate(initCompany.getCostTypeTempId());
 
        // 设置基础表单id,如:预算、合同、借款等
        Map<SystemForm, SystemFormInfo> systemFormInfoMap = JSONTool.toObj(costTypeTemplate.getFormIdStr(), new TypeReference<Map<SystemForm, SystemFormInfo>>(){}.getType());
        if(ObjectUtil.notEmpty(systemFormInfoMap)) {
            systemFormInfoMap.entrySet()
                    .stream()
                    .filter(entry->ObjectUtil.notEmpty(entry.getValue()))
                    .map(entry->{
                        entry.getValue().setCode(copyFrom(entry.getValue().getCode(), formIdCache, initCompany));
                        return entry;
                    })
                    .forEach(tenantInfoMapper::initSysForm);
        }
 
        log.debug("costTypeTemplate:{}", JSONTool.toJson(costTypeTemplate));
        Map<String, List<CostFromTemplate>> costFrom =
                Optional.ofNullable(costTypeTemplate.getConstGroupTypes()).orElse(new ArrayList<>())
                        .stream()
                        .collect(Collectors.toMap(CostGroupTypeExtend::getCodeId, CostGroupTypeExtend::getCostFromTemplate));
        log.debug("costFrom:{}", JSONTool.toJson(costFrom));
        costTypeService.addCostTypeList(updataBusType -> {
 
            // 同步业务类型对应表单
            List<CostFromTemplate> costFromTemplates = costFrom.get(updataBusType.getId());
            if(ObjectUtil.empty(costFromTemplates)){
                return;
            }
            costFromTemplates
                    .stream()
                    .forEach(costFromTemplate -> {
                        String fromId = copyFrom(costFromTemplate.getFId(), formIdCache, initCompany);
                        log.debug("生成新表单:{}", fromId);
                        // 复制表单
                        CostFromMapping costFromMapping = new CostFromMapping();
                        costFromMapping.setCtId(Integer.valueOf(updataBusType.getId()));
                        costFromMapping.setFId(fromId);
                        costFromMapping.setFName(costFromTemplate.getFName());
                        costFromMapping.setType(costFromTemplate.getType());
                        costFromMapping.initParam();
                        costFromMappingMapper.insertSelective(costFromMapping);
 
 
                    });
 
            // 复制补助标准
            initSupp(initCompany.getCostTypeTempId(), Integer.valueOf(updataBusType.getId()));
        });
        log.debug("初始化费用类型成功...");
    }
 
    /**
     * 复制补助标准
     * @param costTypeTempId
     * @param ctId
     */
    private void initSupp(Integer costTypeTempId, Integer ctId) {
        suppStanderService.copyAppAndSuppSave(costTypeTempId, ctId);
    }
 
    /**
     * 初始化组织机构
     * @param initCompany
     */
    private void initOrg(InitCompany initCompany) {
        log.debug("开始初始化组织机构...");
        // 全量拉取 组织机构
        budgetConfigService.synOrg();
        log.debug("初始化组织机构成功...");
    }
 
    /**
     * 初始化表结构
     * @param initCompany
     */
    private void initTable(InitCompany initCompany) {
        log.debug("开始初始化表结构...");
        // 创建租户 表结构
        tenantInfoMapper.createTable();
        // 删除租户 函数
        tenantInfoMapper.dropFunction();
        // 创建租户 函数
        tenantInfoMapper.createFunction();
        log.debug("初始化表结构成功...");
    }
 
    /**
     * 初始化系统变量
     * @param initCompany
     */
    private void initSystemMath(InitCompany initCompany) {
        log.debug("开始初始化系统变量...");
        ThreadData.set(TENANT_ID, tempTenantId+"");
        ThreadData.set(COMPANY_ID, tempCompanyId);
        String dbName = initCompanyApi.getInitCompany(tempTenantId, tempCompanyId).getDbName();
        ThreadData.set(DATA_SOURCE, dbName);
        List<SystemMathExtend> maths = systemMathService.selectAllSystemMathExtend();
 
        ThreadData.set(TENANT_ID, initCompany.getTenantId()+"");
        ThreadData.set(COMPANY_ID, initCompany.getCompanyId());
        ThreadData.set(DATA_SOURCE, initCompany.getDbName());
        maths.stream().forEach(systemMathService::insert);
        log.debug("初始化系统变量成功...");
    }
 
    private String copyFrom(String fId, Map<String, String> formIdCache, InitCompany initCompany) {
        if(formIdCache.get(fId) != null){
            return formIdCache.get(fId);
        }
        log.debug("复制表单:{}", fId);
        String tenantId = ThreadData.get(TENANT_ID);
        Integer companyId = ThreadData.get(COMPANY_ID);
        String res = Http.post("http://"+changHongProperties.getFormServiceUrl()+":"+changHongProperties.getFormServicePost()+"/autoForm/copy/form", CscResult.class)
                .addUrlParam("sourceFormId", fId)
                .addUrlParam("sourceTenantId", formTempTenantId)
                .addUrlParam("targetTenantId", tenantId)
                .addHeader(new ModelMap("Cookie"
                        , String.format("token=token; openId=openId; tenantId=%s; companyId=%s; dataSourceId=%s;", tenantId, companyId, initCompany.getDbName())))
                .execute(new TypeReference<CscResult<Map<String, Object>>>(){}.getType())
                .map(CscResult::getData)
                .map(formInfo->(Map<String, Object>)formInfo)
                .map(formInfo->formInfo.get("formid"))
                .map(Object::toString)
                .orElse("");
        formIdCache.put(fId, res);
        return res;
    }
 
 
 
}