package com.changhong.epc.form.service.budget.tool.impl;
|
|
import com.changhong.epc.bean.form.FormType;
|
import com.changhong.epc.bean.tenant.system.SystemForm;
|
import com.changhong.epc.form.service.budget.CheckFormDataServes;
|
import com.changhong.epc.form.service.budget.department.model.BudgetExtend;
|
import com.changhong.epc.form.service.budget.tool.UpdateBudgetFactory;
|
import com.changhong.epc.form.service.budget.tool.entity.UpdateBudgetEntity;
|
import com.codingapi.tx.annotation.TxTransaction;
|
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.JSONTool;
|
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.math.NumberUtils;
|
import org.springframework.context.annotation.Scope;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.ui.ModelMap;
|
|
import javax.annotation.Resource;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Objects;
|
|
/**
|
* 合同扣减预算
|
*/
|
@Service(UpdateBudgetFactory.CONTRACT_TYPE)
|
@Slf4j
|
@Scope("prototype")
|
public class ContractBudgetUpdate extends AbstractUpdateBudget{
|
|
private Map<String, Object> contractInfo;
|
|
private Map<String, Object> budgetChildInfo;
|
|
@Resource
|
private CheckFormDataServes checkFormDataServes;
|
|
@Override
|
public void checkBudget(UpdateBudgetEntity updateBudgetEntity) {
|
this.contractInfo = getFormInfo(updateBudgetEntity);
|
Integer budgetId = getParentId(contractInfo);
|
if(budgetId == null){
|
return;
|
}
|
this.budgetChildInfo = getFormInfo(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM, budgetId);
|
Assert.condition(budgetChildInfo == null, BUDGET_INVALID);
|
if(contractInfo.get(EXPEND_USE_MONEY) == null || getBudgetBalance(budgetChildInfo) == null){
|
return;
|
}
|
addLock(budgetId);
|
// 验证预算
|
Assert.condition(
|
toDouble(contractInfo.get(EXPEND_USE_MONEY)) > toDouble(getBudgetBalance(budgetChildInfo))
|
, BUDGET_MONEY_NOT_ENOUGH);
|
|
// 验证合同
|
forEach(updateBudgetEntity, getFormId(SystemForm.CONTRACT_FORM_ID), (contract, contractExpend)->{
|
Assert.condition(
|
toDouble(contract.get(PAYMENT_MONEY)) <
|
toDouble(contract.get(ALREADYPAID)) + toDouble(contractExpend.get(PAID_MONDY))
|
, OVERTOP_CONTRACT_MONEY);
|
});
|
}
|
|
@Override
|
@TxTransaction
|
@Transactional
|
public void updateBudget(UpdateBudgetEntity updateBudgetEntity) {
|
super.updateBudget(updateBudgetEntity);
|
checkBudget(updateBudgetEntity);
|
if(ObjectUtil.empty(this.budgetChildInfo) || contractInfo.get(EXPEND_USE_MONEY) == null){
|
account(updateBudgetEntity.getFormId(),ThreadData.get(Keys.TENANT_ID), Objects.toString(updateBudgetEntity.getDataRowNum(),""));
|
return;
|
}
|
double money = toDouble(getBudgetBalance(budgetChildInfo)) - toDouble(contractInfo.get(EXPEND_USE_MONEY));
|
checkMoney(money);
|
|
addBudgetLogMoney(
|
this.budgetChildInfo
|
, money
|
, updateBudgetEntity.getFormId()
|
, this.contractInfo
|
, toDouble(contractInfo.get(EXPEND_USE_MONEY))
|
);
|
String budgetFormId = getFormId(SystemForm.BUDGET_FORM_ID);
|
// 修改预算
|
updateData(budgetFormId, BUDGET_SUB_FROM
|
, getDataRowNum(budgetChildInfo)
|
, new ModelMap(BUDGET_ITEM_BALANCE, format(money,getFormBase(budgetFormId),BUDGET_ITEM_BALANCE)));
|
|
// 修改合同
|
forEach(updateBudgetEntity, getFormId(SystemForm.CONTRACT_FORM_ID), (contract, contractExpend)->{
|
double alreadyPaid = toDouble(contract.get(ALREADYPAID)) + toDouble(contractExpend.get(PAID_MONDY));
|
Assert.condition(
|
toDouble(contract.get(PAYMENT_MONEY)) < alreadyPaid
|
, OVERTOP_CONTRACT_MONEY);
|
checkMoney(alreadyPaid);
|
|
updateData(updateBudgetEntity.getFormId(), SUBFORM , getDataRowNum(contract) , new ModelMap(ALREADYPAID, format(alreadyPaid,getFormBase(updateBudgetEntity.getFormId()),ALREADYPAID)));
|
updateData(updateBudgetEntity.getFormId(), SUBFORM , getDataRowNum(contractExpend) , new ModelMap(ALREADYPAID, format(alreadyPaid,getFormBase(updateBudgetEntity.getFormId()),ALREADYPAID)));
|
});
|
account(updateBudgetEntity.getFormId(),ThreadData.get(Keys.TENANT_ID), Objects.toString(updateBudgetEntity.getDataRowNum(),""));
|
}
|
|
@Override
|
public void flowAfterCall(UpdateBudgetEntity updateBudgetEntity) {
|
this.contractInfo = getFormInfo(updateBudgetEntity);
|
Integer budgetId = getParentId(contractInfo);
|
if(budgetId == null){
|
return;
|
}
|
delLock(budgetId);
|
}
|
|
@Override
|
public void rollback(UpdateBudgetEntity updateBudgetEntity) {
|
super.rollback(updateBudgetEntity);
|
// this.contractInfo = getFormInfo(updateBudgetEntity);
|
// Integer budgetId = getParentId(contractInfo);
|
// if(budgetId == null){
|
// return;
|
// }
|
// this.budgetChildInfo = getFormInfo(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM, budgetId);
|
// Assert.condition(budgetChildInfo == null, BUDGET_INVALID);
|
// if(contractInfo.get(EXPEND_USE_MONEY) == null || getBudgetBalance(budgetChildInfo) == null){
|
// return;
|
// }
|
// double money = toDouble(getBudgetBalance(budgetChildInfo)) + toDouble(contractInfo.get(EXPEND_USE_MONEY));
|
// String budgetFormId = getFormId(SystemForm.BUDGET_FORM_ID);
|
// // 修改预算
|
// updateData(budgetFormId, BUDGET_SUB_FROM
|
// , getDataRowNum(budgetChildInfo)
|
// , new ModelMap(BUDGET_ITEM_BALANCE, format(money,getFormBase(budgetFormId),BUDGET_ITEM_BALANCE)));
|
//
|
// // 修改合同
|
// forEach(updateBudgetEntity, getFormId(SystemForm.CONTRACT_FORM_ID), (contract, contractExpend)->{
|
// double alreadyPaid = toDouble(contractExpend.get(PAID_MONDY)) - toDouble(contract.get(ALREADYPAID));
|
// Assert.condition(
|
// toDouble(contract.get(PAYMENT_MONEY)) < alreadyPaid
|
// , OVERTOP_CONTRACT_MONEY);
|
// checkMoney(alreadyPaid);
|
//
|
// updateData(updateBudgetEntity.getFormId(), SUBFORM , getDataRowNum(contract) , new ModelMap(ALREADYPAID, format(alreadyPaid,getFormBase(updateBudgetEntity.getFormId()),ALREADYPAID)));
|
// updateData(updateBudgetEntity.getFormId(), SUBFORM , getDataRowNum(contractExpend) , new ModelMap(ALREADYPAID, format(alreadyPaid,getFormBase(updateBudgetEntity.getFormId()),ALREADYPAID)));
|
// });
|
}
|
|
/**
|
* 循环合同报销和合同信息
|
* @param updateBudgetEntity
|
* @param budgetForEachFunction
|
*/
|
protected void forEach(UpdateBudgetEntity updateBudgetEntity, String parentFormId, AbstractOccupyUpdateBudget.BudgetForEachFunction budgetForEachFunction){
|
Integer parentId = getContractId(getFormInfo(updateBudgetEntity));
|
if(parentId == null){
|
log.debug("{}:{}没找到合同信息", updateBudgetEntity.getFormId(), updateBudgetEntity.getDataRowNum());
|
return;
|
}
|
List<Map<String, Object>> contractExpends = getFormChildInfos(updateBudgetEntity.getFormId() , SUBFORM , updateBudgetEntity.getDataRowNum());
|
List<Map<String, Object>> contracts = getFormChildInfos(parentFormId , SUBFORM , parentId);
|
//实时合同付款金额
|
List<Map<String,Object>> sunAlreadys = checkFormDataServes.getSumAlreadyPaid(new BudgetExtend(parentFormId,parentId,ThreadData.get(Keys.TENANT_ID),updateBudgetEntity.getDataRowNum()));
|
log.debug("实时付款金额:"+ JSONTool.toJson(sunAlreadys));
|
contracts.stream().forEach(o-> {
|
Map<String,Object> sunAlready = sunAlreadys
|
.stream().filter(fi -> Objects.equals(fi.get(PAYMENTSTAGE), o.get(PAYMENTSTAGE)))
|
.findFirst().orElse(null);
|
Object money = sunAlready!=null?sunAlready.get(ALREADYPAID):"0";
|
log.debug("第"+o.get(PAYMENTSTAGE)+"阶段付款金额:"+money);
|
o.put(ALREADYPAID, money);
|
}
|
);
|
log.debug("金额处理后数据:"+ JSONTool.toJson(contracts));
|
// 子表单数量不同,抛出异常
|
// Assert.condition(contracts.size() != contractExpends.size(), CONTRACT_CHILD_SIZE);
|
for (int i = 0, j = contractExpends.size(); i < j; i++) {
|
Map<String, Object> contract = new HashMap<>();
|
Map<String, Object> contractExpend = contractExpends.get(i);
|
boolean flag = true;
|
for(Map<String,Object> m:contracts){
|
if(Objects.equals(contractExpend.get(PAYMENTSTAGE),m.get(PAYMENTSTAGE))){
|
contract = m;
|
flag = false;
|
break;
|
}
|
}
|
// 没有余额,忽略
|
if(flag || contract.get(ALREADYPAID) == null
|
|| contract.get(PAYMENT_MONEY) == null
|
|| contractExpend.get(PAID_MONDY) == null){
|
continue;
|
}
|
budgetForEachFunction.each(contract, contractExpend);
|
}
|
}
|
|
/**
|
* 获得关联合同信息控件id
|
* @param formInfo
|
* @return
|
*/
|
protected Integer getContractId(Map<String, Object> formInfo){
|
Object parentId = formInfo.get(SELECT_CONTRACTS);
|
if(ObjectUtil.empty(parentId)){
|
return null;
|
}
|
return NumberUtils.createInteger(parentId.toString());
|
}
|
|
@Override
|
public FormType getFormType() {
|
return FormType.HT;
|
}
|
|
}
|