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;
|
}
|
|
|
|
}
|