package com.changhong.epc.form.execute.impl;
|
|
import java.util.*;
|
|
import javax.annotation.Resource;
|
|
import com.changhong.autoform.entity.ProcessBudget;
|
import com.changhong.epc.bean.form.FormType;
|
import com.changhong.epc.form.execute.BudgetRecord;
|
import com.changhong.epc.form.execute.IFormOthersOperation;
|
import com.changhong.epc.form.execute.IProcessBudgetBack;
|
import com.iemsoft.framework.cloud.core.exception.IEMRuntimeException;
|
import com.iemsoft.framework.cloud.core.tools.JSONTool;
|
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
|
|
import lombok.Data;
|
|
@Data
|
public class MainLogic {
|
|
@Resource
|
private IFormOthersOperation formOthersOperation = SpringUtil.getBean(IFormOthersOperation.class);
|
|
// @Resource
|
private IProcessBudgetBack processBudgetBack;
|
|
private ProcessBudget processBudget;
|
|
private Map<FormType, List<Integer>> pbs;
|
|
private List<Double> pzMoney;
|
private List<Double> pzMoneynew;
|
private List<BudgetRecord> pdrs;
|
|
private Double xyMoney = 0D;
|
|
private Boolean flog = true;
|
|
private List<BudgetRecord> ySs;
|
private List<BudgetRecord> budgetRecords;
|
|
private FormType billType;
|
|
|
|
public void initData(ProcessBudget processBudget){
|
pbs = new HashMap<>();
|
for(FormType type : FormType.values()){
|
pbs.put(type, new ArrayList<>());
|
}
|
pdrs = new ArrayList<>();
|
pzMoney = new ArrayList<>();
|
this.processBudget = processBudget;
|
}
|
|
public boolean addOfType(FormType formType, List<Integer> pds){
|
if (pbs.get(formType).size() > 0){
|
return false;
|
}
|
pbs.get(formType).addAll(pds);
|
|
return true;
|
}
|
|
/**
|
* 查找
|
* @param formType
|
* @return
|
*/
|
public Integer findOfTypeEques(FormType formType){
|
if(FormType.YS == formType){
|
if(pbs.get(formType).size() > 0){
|
Integer processBudget = pbs.get(formType).get(0);
|
pbs.get(FormType.YS).remove(processBudget);
|
return processBudget;
|
}
|
}
|
return null;
|
}
|
|
public List<Integer> getYsDataRowNumbers(){
|
return Optional.ofNullable(pbs.get(FormType.YS)).orElse(Collections.EMPTY_LIST);
|
}
|
|
public void exeLogic(){
|
// 1. 预运行一次
|
try {
|
executePrimary(processBudget);
|
} catch (Exception e) {
|
}
|
double sum = 0D;
|
pzMoneynew = new ArrayList<>(pzMoney);
|
for(double s : pzMoney){
|
sum = sum + s;
|
}
|
// if(xyMoney != sum){
|
// throw new IEMRuntimeException("E001", "配置错误,配置金额与报销金额不符合!");
|
// }
|
|
splittingData();
|
|
|
// 3. 没有一次出现异常,执行 真正的回写预算
|
for (BudgetRecord budgetRecord : pdrs) {
|
updateForm(budgetRecord);
|
}
|
|
}
|
|
|
|
|
|
/**
|
*
|
*/
|
private void splittingData() {
|
// budgetRecords
|
int i = 0;
|
for(BudgetRecord brys : ySs){
|
budgetRecords.remove(budgetRecords.size()-1);
|
Double money = pzMoneynew.get(i);
|
BudgetRecord budgetRecord = budgetRecords.get(budgetRecords.size()-1);
|
budgetRecord.getMoney().money = money;
|
budgetRecords.add(brys);
|
|
executeProcessBudget(budgetRecords);
|
|
i ++;
|
}
|
}
|
|
public void setXyMoney(Double money) {
|
if(flog){
|
xyMoney = money;
|
}
|
}
|
|
public void setPzMoney(List<String> obs){
|
if(flog){
|
obs.forEach(s->pzMoney.add(Double.parseDouble(s)));
|
}
|
flog = false;
|
}
|
|
public Integer loopNumber(){
|
return pbs.get(FormType.YS).size();
|
}
|
|
public Double getEachMoney(){
|
if(pzMoneynew == null || pzMoneynew.size() == 0){
|
return 0D;
|
}
|
Double double1 = pzMoneynew.get(0);
|
pzMoneynew.remove(0);
|
return double1;
|
}
|
|
/**
|
* 预运行一次
|
* @param processBudget
|
*/
|
public void executePrimary(ProcessBudget processBudget) {
|
// formOthersOperation.getMoneyCpnMdCode();
|
|
budgetRecords = new ArrayList<>();
|
processBudgetBack = new ProcessBudgetBack(this);
|
|
/**
|
* FormType.BX
|
* formOthersOperation.getBillType(processBudget)
|
*/
|
billType = formOthersOperation.getBillType(processBudget);
|
processBudget.setCostType(billType.toString());
|
switch (billType) {
|
// A.预算
|
case YS:
|
processBudgetBack.exeBudget(budgetRecords, processBudget);
|
|
// System.out.println(budgetRecords);
|
break;
|
|
// B.申请
|
case SQ:
|
processBudgetBack.exeApply(budgetRecords, processBudget);
|
break;
|
|
// 报销
|
case BX:
|
processBudgetBack.exeExpense(budgetRecords, processBudget);
|
break;
|
|
default:
|
break;
|
}
|
ySs = new ArrayList<>();
|
ProcessBudget atProcessBudget = budgetRecords.get(budgetRecords.size() -1).getAtProcessBudget();
|
|
for(Integer dataRowNumber : getYsDataRowNumbers()){
|
ProcessBudget obj = JSONTool.toObj(JSONTool.toJson(atProcessBudget),ProcessBudget.class );
|
obj.setDataRowNUm(dataRowNumber);
|
processBudgetBack.exeBasics(ySs, obj);
|
}
|
|
}
|
|
|
|
/**
|
* 真正开始执行业务逻辑
|
* @param budgetRecords2
|
*/
|
public void executeProcessBudget(List<BudgetRecord> bRs) {
|
List<BudgetRecord> budgetRecords = JSONTool.toList(JSONTool.toJson(bRs), BudgetRecord.class);
|
switch (billType) {
|
|
// A.预算
|
case YS:
|
|
if(budgetRecords.size() == 1){
|
throw new IEMRuntimeException("C001", "没有上级预算!");
|
}
|
BudgetRecord budgetRecord = budgetRecords.get(0);
|
// System.out.println("预算A的金额:" + budgetRecord.getMoney().money);
|
BudgetRecord budgetRecord2 = budgetRecords.get(1);
|
// System.out.println("预算B的金额:" + budgetRecord2.getMoney().money);
|
budgetRecord2.getMoney().money = budgetRecord2.getMoney().money - budgetRecord.getMoney().money;
|
if(budgetRecord2.getMoney().money < 0){
|
throw new IEMRuntimeException("C001", "上级预算余额不足!");
|
}
|
setUpdateForm(budgetRecord2,budgetRecord2.getMoney().money );
|
break;
|
|
// B.申请
|
case SQ:
|
if(budgetRecords.size() == 1){
|
// 没有上级报销
|
}
|
BudgetRecord b1 = budgetRecords.get(0);
|
BudgetRecord b2 = budgetRecords.get(1);
|
b2.getMoney().money = b2.getMoney().money - b1.getMoney().money;
|
if(b2.getMoney().money < 0){
|
throw new IEMRuntimeException("C001", "申请金额失败, 预算余额不足!");
|
}
|
// 正常
|
setUpdateForm(b2, b2.getMoney().money);
|
break;
|
|
// 报销
|
case BX:
|
Double money;
|
if(budgetRecords.size() == 1){
|
// 没有上级报销
|
throw new IEMRuntimeException("C001", "没有上级预算!");
|
}
|
BudgetRecord b01 = budgetRecords.get(0);
|
BudgetRecord b02 = budgetRecords.get(1);
|
BudgetRecord b03 = null;
|
try {
|
b03 = budgetRecords.get(2);
|
if(b03 == null){
|
throw new IEMRuntimeException("C002", "其他异常请找程序员询问。");
|
}
|
money = (b03.getMoney().money + b02.getMoney().money)- b01.getMoney().money;
|
if(money < 0){
|
throw new IEMRuntimeException("C001", "预算余额不足!");
|
}
|
setUpdateForm(b03,money);
|
break;
|
} catch (Exception e) {
|
}
|
|
// if(b02.getType() == FormType.SQ){
|
|
// 正常
|
|
// }
|
|
money = b02.getMoney().money - b01.getMoney().money;
|
if(money < 0){
|
throw new IEMRuntimeException("C001", "预算余额不足!");
|
}
|
// 正常
|
setUpdateForm(b02,money);
|
break;
|
|
default:
|
break;
|
}
|
}
|
|
|
|
public void setUpdateForm(BudgetRecord budgetRecord, Double money){
|
//budgetRecord.getAtProcessBudget().setFormID("80b0627f825ef169x9da7646eb0847c0");
|
// if(budgetRecord.getMoney().alias == null){
|
// budgetRecord.getMoney().alias = "预算金额";
|
// }
|
budgetRecord.getMoney().money = money;
|
pdrs.add(budgetRecord);
|
}
|
|
public void updateForm(BudgetRecord budgetRecord){
|
// System.out.println(budgetRecord.getAtProcessBudget().getDataRowNUm());
|
formOthersOperation.updateFormFieldProperty(budgetRecord.getAtProcessBudget(), budgetRecord.getMoney());
|
}
|
|
|
}
|