package com.changhong.epc.form.service.budget.department.impl;
|
|
import com.changhong.autoform.entity.ProcessBudget;
|
import com.changhong.epc.bean.form.BugetData;
|
import com.changhong.epc.bean.form.BugetSubForm;
|
import com.changhong.epc.bean.form.FormType;
|
import com.changhong.epc.bean.form.budget.BudgetUserDefined;
|
import com.changhong.epc.bean.form.budget.BudgetUserDefined.Dimension;
|
import com.changhong.epc.bean.form.budget.filtrate.BudgetclassifyKey;
|
import com.changhong.epc.bean.form.budget.filtrate.BudgetclassifyValue;
|
import com.changhong.epc.bean.tenant.cost.budgetlog.MoneyBudgetLog;
|
import com.changhong.epc.form.execute.IFormOthersOperation;
|
import com.changhong.epc.form.service.budget.department.SuperBudgt;
|
import com.changhong.epc.form.service.budget.department.model.BudgetRow;
|
import com.changhong.epc.form.service.budget.department.model.BudgetRowUpdateRecord;
|
import com.changhong.epc.form.service.budget.impl.GetConstant;
|
import com.iemsoft.framework.cloud.core.exception.IEMRestException;
|
import com.iemsoft.framework.cloud.core.tools.JSONTool;
|
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
|
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
|
import com.iemsoft.framework.cloud.core.tools.StringUtil;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.math.BigDecimal;
|
import java.util.*;
|
|
/**
|
* 上级预算部门
|
*/
|
@SuppressWarnings("unchecked")
|
@Slf4j
|
public class SuperBudgtDepartments extends SuperBudgt{
|
|
private IFormOthersOperation formOthersOperation = SpringUtil.getBean(IFormOthersOperation.class);
|
|
//当前预算
|
private static final String NOWBUDGET = "nowBudget";
|
//上级预算
|
private static final String SUPERBUDGET = "superBudget";
|
|
|
|
public static void main(String[] args) {
|
String str = "{\"formID\":\"a60c5a8c444c34f1aab44a2fd8c8f076\",\"data\":{\"budget_itemCostType\":\"2017\",\"数字2\":\"0\",\"下拉框3\":[\"aaa\"],\"多行文本6\":\"fdsafasdfsdafdsfasd\",\"supperBudgetRows\":[{\"DATAROWNUM\":\"123\",\"日历7\":\"2017\"},{\"DATAROWNUM\":\"\",\"日历7\":\"2017\"},{\"DATAROWNUM\":\"\",\"日历7\":\"2018\"},{\"DATAROWNUM\":\"\",\"日历7\":\"2019\"}]},\"tenantID\":\"001\",\"userName\":\"WangYX\"}";
|
// System.out.println(str);
|
Map<String,Object> obj = (Map<String, Object>) JSONTool.toObj(str, Map.class).get("data");
|
BudgetRow budgetRow = new BudgetRow(obj);
|
// System.out.println(budgetRow);
|
}
|
|
/**
|
* 获得可用预算
|
*/
|
@Override
|
public List<Map<String,Object>> getUsableBudgetList() {
|
|
List<Map<String,Object>> datas = new ArrayList<>();
|
Collection<BudgetclassifyValue> values = data.values();
|
values.forEach(o->datas.addAll(o.dispose()));
|
return datas;
|
}
|
|
private MoneyBudgetLog moneyBudgetLog = new MoneyBudgetLog();
|
|
@Override
|
public boolean executeWriteBack(Map<String, Object> fromInfo, ProcessBudget processBudget,String formID) {
|
List<Map<String,Object>> subData = (List<Map<String, Object>>) fromInfo.get(BUDGET_SUB_FROM);
|
|
this.moneyBudgetLog.setAlterAlias(BUDGET_ITEM_BALANCE);
|
this.moneyBudgetLog.setFormId(processBudget.getFormId());
|
this.moneyBudgetLog.setQFormId(processBudget.getFormId());
|
|
log.debug("subData: "+JSONTool.toJson(subData));
|
List<BudgetRow> budgetRows = new ArrayList<>();
|
subData.forEach(o->budgetRows.add(new BudgetRow(o)));
|
budgetItemBalanceType = Integer.parseInt(fromInfo.get("budgetItemBalanceType").toString());
|
log.debug("List<BudgetRow>: "+JSONTool.toJson(budgetRows));
|
|
// 验证每一条费用够不够扣
|
List<BudgetRowUpdateRecord> verify = verify(budgetRows);
|
|
|
log.debug("List<BudgetRowUpdateRecord>: "+JSONTool.toJson(verify));
|
|
verify.forEach(o->{
|
formOthersOperation.updateFormFieldProperty(o.getProcessBudget(), o.getData() ,o.getFormType(),o.getIBillCostLog());
|
});
|
|
return true;
|
}
|
|
|
private List<MoneyBudgetLog> moneyBudgetLogs = new LinkedList<>();
|
|
public List<BudgetRowUpdateRecord> verify(List<BudgetRow> budgetRows){
|
|
log.debug("开始算钱了");
|
|
List<BudgetRowUpdateRecord> budgetRowRecord = new ArrayList<>();
|
if(ObjectUtil.empty(budgetRows)){
|
return budgetRowRecord;
|
}
|
for (BudgetRow budgetRow : budgetRows) {
|
//序列化复制一个对象
|
MoneyBudgetLog budgetlog = JSONTool.toObj(JSONTool.toJson(this.moneyBudgetLog), MoneyBudgetLog.class);
|
budgetlog.setQRowId(budgetRow.getDataRowNumber());
|
BigDecimal applyMoney = new BigDecimal(budgetRow.getMoney());
|
for(BudgetRow supperbudgetRow : budgetRow.getSupperBudgetRows()){
|
BigDecimal balance =new BigDecimal(supperbudgetRow.getBalance());
|
BigDecimal money = balance.subtract(applyMoney);
|
|
log.debug("上级预算" + balance);
|
log.debug("当前预算" + applyMoney);
|
log.debug("计算后" + money);
|
|
//记录本地log中
|
budgetlog.setRowId(supperbudgetRow.getDataRowNumber());
|
budgetlog.setOperatorMsg("预算占用上级预算---存子表单dataRowNum");
|
budgetlog.setAlterContentOld(balance.toEngineeringString());
|
budgetlog.setAlterContentNew(money.toEngineeringString());
|
budgetlog.initParam();
|
// this.moneyBudgetLogs.add(budgetlog);
|
|
if(money.compareTo(BigDecimal.ZERO) == -1){
|
//当上级预算余额不足时,一条也不让扣
|
throw new IEMRestException("预算不足");
|
//12.7 yuezhw 注释
|
// applyMoney = applyMoney.subtract(balance);
|
// budgetRowRecord.add(new BudgetRowUpdateRecord(
|
// new ProcessBudget(
|
// GetConstant.getFormIdGetConstant(YS_FORMID)
|
// , supperbudgetRow.getDataRowNumber(), BUDGET_SUB_FROM)
|
// , BUDGET_ITEM_BALANCE, "0"));
|
}else{
|
BudgetRowUpdateRecord param= new BudgetRowUpdateRecord(
|
new ProcessBudget(GetConstant.getFormIdGetConstant(YS_FORMID)
|
, supperbudgetRow.getDataRowNumber(), BUDGET_SUB_FROM)
|
, BUDGET_ITEM_BALANCE, getValueByBalanceType(money) + "",budgetRow.getTenantId());
|
|
param.setFormType(FormType.YS);
|
param.setIBillCostLog(budgetlog);
|
budgetRowRecord.add(param);
|
applyMoney = BigDecimal.ZERO;
|
}
|
}
|
if(applyMoney.compareTo(BigDecimal.ZERO) == -1){
|
throw new IEMRestException("预算不足");
|
}
|
|
}
|
log.debug("算完了");
|
return budgetRowRecord;
|
}
|
|
public List<BudgetRowUpdateRecord> refunds(Map<String, List<BugetSubForm>> buget){
|
List<BudgetRowUpdateRecord> budgetRowRecord = new ArrayList<>();
|
buget.get(NOWBUDGET).stream().forEach(now ->{
|
buget.get(SUPERBUDGET).stream().forEach(suPer ->{
|
if(suPer.getC_Type().equals(now.getC_Type())){
|
//将金额相加
|
budgetRowRecord.add(new BudgetRowUpdateRecord(
|
new ProcessBudget(superFormId
|
, Integer.parseInt(suPer.getDATAROWNUM()), BUDGET_SUB_FROM)
|
, BUDGET_ITEM_BALANCE
|
, getValueByBalanceType(new BigDecimal(suPer.getBudget_itemBalance()).add(new BigDecimal(now.getBudget_itemBalance()))) + ""
|
,"13"));
|
}
|
});
|
});
|
return budgetRowRecord;
|
}
|
@Override
|
public void setDataSource(BudgetUserDefined budgetUserDefined,
|
Map<BudgetclassifyKey,BudgetclassifyValue> data) {
|
this.budgetUserDefined = budgetUserDefined;
|
this.data = data;
|
}
|
|
|
@Override
|
public boolean isMereg() {
|
return false;
|
}
|
|
@Override
|
public boolean isFilter(Map<String, Object> data) {
|
return false;
|
}
|
|
@Override
|
public BudgetUserDefined getBudgetUserDefined() {
|
BudgetUserDefined budgetUserDefined = new BudgetUserDefined();
|
budgetUserDefined.setFilterSubForm(false);
|
budgetUserDefined.setMergeSubForm(false);
|
budgetUserDefined.setKey("SuperBudgtDepartments");
|
budgetUserDefined.setVal("上级部门");
|
List<Dimension> mainDimension = new ArrayList<>();
|
List<Dimension> subDimension = new ArrayList<>();
|
|
mainDimension.add(new Dimension(BUDGET_START_DATE));
|
mainDimension.add(new Dimension(BUDGET_END_DATE));
|
mainDimension.add(new Dimension(CURRENCY));
|
|
subDimension.add(new Dimension(BUDGET_ITEM_COST_TYPE));
|
subDimension.add(new Dimension(BUDGET_ITEM_DEPARTMENT));
|
|
budgetUserDefined.setMainDimension(mainDimension);
|
budgetUserDefined.setSubDimension(subDimension);
|
|
return budgetUserDefined;
|
}
|
|
@Override
|
public Boolean excuteRefunds(Map<String, Object> formInfo) {
|
budgetItemBalanceType = Integer.parseInt(StringUtil.toString(formInfo.get(BUDGETITEMBALANCETYPE)));
|
BugetData bugetData = JSONTool.toObj(JSONTool.toJson(formInfo),BugetData.class);
|
HashSet<String> costType = new HashSet<>();
|
//获得业务类型
|
superFormId = formInfo.get(YS_FORMID).toString();
|
bugetData.getBudget_subFrom().stream().forEach(sub ->{
|
costType.add(sub.getC_Type());
|
});
|
|
List<BugetSubForm> supersubBudget = formOthersOperation
|
.queryBudgetByCostTypeAndParentId(new ProcessBudget(formInfo.get("formID").toString()
|
,Integer.parseInt(bugetData.getToWriteBudget())
|
,BUDGET_SUB_FROM), costType);
|
if(supersubBudget.isEmpty())
|
return false;
|
Map<String, List<BugetSubForm>> nowAndSuper = new HashMap<>();
|
nowAndSuper.put(NOWBUDGET, bugetData.getBudget_subFrom());
|
nowAndSuper.put(SUPERBUDGET, supersubBudget);
|
// System.err.println(JSONTool.toJson(bugetData));
|
refunds(nowAndSuper).stream().forEach( o ->{
|
formOthersOperation.updateFormFieldProperty(o.getProcessBudget(), o.getData() ,o.getFormType(),o.getIBillCostLog());
|
});
|
return true;
|
}
|
|
public BudgetUserDefined getBudgetUserDefined(String fun) {
|
BudgetUserDefined budgetUserDefined = new BudgetUserDefined();
|
budgetUserDefined.setFilterSubForm(false);
|
budgetUserDefined.setMergeSubForm(false);
|
budgetUserDefined.setKey("SuperBudgtDepartments");
|
budgetUserDefined.setVal("上级部门");
|
List<Dimension> mainDimension = new ArrayList<>();
|
List<Dimension> subDimension = new ArrayList<>();
|
|
mainDimension.add(new Dimension(BUDGET_START_DATE));
|
mainDimension.add(new Dimension(BUDGET_END_DATE));
|
mainDimension.add(new Dimension(CURRENCY));
|
|
subDimension.add(new Dimension(BUDGET_ITEM_COST_TYPE));
|
subDimension.add(new Dimension(BUDGET_ITEM_DEPARTMENT));
|
|
budgetUserDefined.setMainDimension(mainDimension);
|
budgetUserDefined.setSubDimension(subDimension);
|
|
return budgetUserDefined;
|
}
|
|
|
}
|