package com.changhong.epc.form.service.budget.department.impl; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Objects; import javax.annotation.Resource; import com.alibaba.fastjson.annotation.JSONField; import com.changhong.autoform.entity.BudgetData; import com.changhong.autoform.entity.ProcessBudget; import com.changhong.autoform.mapper.associate.Associate; 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.MoneyExpendLog; 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.SpringUtil; import com.iemsoft.framework.cloud.core.tools.StringUtil; import lombok.extern.slf4j.Slf4j; /** * 报销使用申请 * @author jksk * */ @Slf4j public class ExpendUseApplyFun extends SuperBudgt{ //@Resource private IFormOthersOperation formOthersOperation = SpringUtil.getBean(IFormOthersOperation.class); @Override @JSONField(serialize=false) public List> getUsableBudgetList() { List> datas = new ArrayList<>(); Collection values = data.values(); values.forEach(o->datas.addAll(o.dispose())); return datas; } // @Override // public boolean executeWriteBack(Map fromInfo, ProcessBudget processBudget) { // log.debug("ApplyUseBudgtFun.executeWriteBack > frominfo: "+JSONTool.toJson(fromInfo)); // processBudget.setAlias(BUDGET_SUB_FROM); // processBudget.setFormId(GetConstant.getFormIdGetConstant(YS_FORMID)); // if(fromInfo.get(TO_WRITE_BUDGET).toString().contains(",")){ // return false; // } // processBudget.setDataRowNUm(Integer.parseInt(fromInfo.get(TO_WRITE_BUDGET).toString())); // Map subData = formOthersOperation.getFromInfo(processBudget); // BudgetRow budgetRow = new BudgetRow(); // budgetRow.setDataRowNumber(Integer.parseInt(fromInfo.get(TO_WRITE_BUDGET).toString())); // budgetRow.setBalance(subData.get(BUDGET_ITEM_BALANCE).toString()); // budgetRow.setMoney(money(fromInfo)); // log.debug("ApplyUseBudgtFun.executeWriteBack > budgetRows:" + JSONTool.toJson(budgetRow)); // verify(budgetRow).forEach(o->{ // formOthersOperation.updateFormFieldProperty(o.getProcessBudget(), o.getData()); // }); // // return true; // } private MoneyExpendLog moneyExpendLog = new MoneyExpendLog(); @Override public boolean executeWriteBack(Map fromInfo, ProcessBudget processBudget,String formID) { log.debug("ApplyUseBudgtFun.executeWriteBack > frominfo: "+JSONTool.toJson(fromInfo)); // System.out.println("formInfo:"+fromInfo); // System.out.println("表单Id:"+formID); // System.out.println("processBudget:"+processBudget); budgetItemBalanceType = Integer.parseInt(Objects.toString(fromInfo.get("budgetItemBalanceType"),"0")); processBudget.setAlias(BUDGET_SUB_FROM); String ysFormid = GetConstant.getFormIdGetConstant(YS_FORMID); processBudget.setFormId(ysFormid); if(fromInfo.get(TO_WRITE_BUDGET).toString().contains(",")){ return false; } //记录log参数 你能优化就优化了吧.. this.moneyExpendLog.setAlterAlias(BUDGET_ITEM_BALANCE); this.moneyExpendLog.setFormId(processBudget.getFormId()); this.moneyExpendLog.setQFormId(ysFormid); this.moneyExpendLog.setQRowId(Integer.parseInt(fromInfo.get(TO_WRITE_BUDGET).toString())); this.moneyExpendLog.setRowId(processBudget.getDataRowNUm()); this.moneyExpendLog.setAlterAlias(BUDGET_ITEM_MONEY); this.moneyExpendLog.setOperatorMsg("报销使用申请--存主表单dataRowNum"); this.moneyExpendLog.initParam(); processBudget.setDataRowNUm(Integer.parseInt(fromInfo.get(TO_WRITE_BUDGET).toString())); Map subData = formOthersOperation.getFromInfo(processBudget); BudgetRow budgetRow = new BudgetRow(); budgetRow.setTenantId(processBudget.getTenantID()); budgetRow.setDataRowNumber(Integer.parseInt(fromInfo.get(TO_WRITE_BUDGET).toString())); budgetRow.setBalance(Objects.toString(subData.get(BUDGET_ITEM_BALANCE),"0")); budgetRow.setMoney(money(fromInfo)); verify(budgetRow).forEach(o->{ formOthersOperation.updateFormFieldProperty(o.getProcessBudget(), o.getData() ,o.getFormType(),o.getIBillCostLog()); }); return true; } private static String money(Map map){ Object ysm = map.get(BUDGET_ITEM_MONEY); if(ysm != null) return Objects.toString(ysm,"0"); ysm = map.get(APPLY_USE_MONEY); if(ysm != null) return Objects.toString(ysm,"0"); ysm = map.get(EXPEND_USE_MONEY); if(ysm != null) return Objects.toString(ysm,"0"); return "0"; } public List verify(BudgetRow budgetRow){ List budgetRowRecord = new ArrayList<>(); BigDecimal applyMoney = new BigDecimal(budgetRow.getMoney()); BigDecimal balance = new BigDecimal(budgetRow.getBalance()); BigDecimal money = balance.add(applyMoney); log.debug("---[ 报销使用申请 ]---"); log.debug("---申请(减去)报销金额 : " + applyMoney + "---"); log.debug("---计算前预算金额 : " + applyMoney + "---"); log.debug("---计算后预算金额 : " + applyMoney + "---"); if(money.compareTo(BigDecimal.ZERO) == -1){ throw new IEMRestException("预算不足"); } BudgetRowUpdateRecord param = new BudgetRowUpdateRecord( new ProcessBudget(GetConstant.getFormIdGetConstant(YS_FORMID) , budgetRow.getDataRowNumber(), BUDGET_SUB_FROM) , BUDGET_ITEM_BALANCE, getValueByBalanceType(money) + "" ,budgetRow.getTenantId()); param.setFormType(FormType.BX); param.setIBillCostLog(this.moneyExpendLog); budgetRowRecord.add(param); return budgetRowRecord; } @Override public void setDataSource(BudgetUserDefined budgetUserDefined, Map data) { this.budgetUserDefined = budgetUserDefined; this.data = data; } @Override public boolean isMereg() { return false; } @Override public boolean isFilter(Map data) { return true; } @Override public BudgetUserDefined getBudgetUserDefined() { BudgetUserDefined budgetUserDefined = new BudgetUserDefined(); budgetUserDefined.setFilterSubForm(false); budgetUserDefined.setMergeSubForm(false); budgetUserDefined.setKey("ExpendUseApplyFun"); budgetUserDefined.setVal("报销使用申请"); List mainDimension = new ArrayList<>(); List subDimension = new ArrayList<>(); mainDimension.add(new Dimension(BUDGET_START_DATE)); mainDimension.add(new Dimension(CURRENCY)); mainDimension.add(new Dimension(EXPEND_COST_TYPE)); mainDimension.add(new Dimension(EXPEND_DEPARTMENT, APPLY_DEPARTMENT)); budgetUserDefined.setMainDimension(mainDimension); budgetUserDefined.setSubDimension(subDimension); return budgetUserDefined; } @Override public Boolean excuteRefunds(Map formInfo) { budgetItemBalanceType = Integer.parseInt(StringUtil.toString(formInfo.get(BUDGETITEMBALANCETYPE))); if(formInfo.get(TO_WRITE_BUDGET).toString().contains(",")){ return false; } /**查出申请单信息*/ superFormId = formInfo.get(YS_FORMID).toString(); ProcessBudget applyBudget = new ProcessBudget(); applyBudget.setFormId(superFormId); applyBudget.setDataRowNUm(Integer.parseInt(formInfo.get(TO_WRITE_BUDGET).toString())); Map applyData = formOthersOperation.selectFormDataInfo(applyBudget); if(applyData.isEmpty()) return false; /**查出预算单信息*/ ProcessBudget processBudget = new ProcessBudget(); processBudget.setAlias(BUDGET_SUB_FROM); processBudget.setFormId(superFormId); processBudget.setDataRowNUm(Integer.parseInt(applyData.get(TO_WRITE_BUDGET).toString())); Map budgetData = formOthersOperation.getFromInfo(processBudget); if(budgetData.isEmpty()) return false; /**为退回准备参数*/ BudgetRow budgetRow = new BudgetRow(); budgetRow.setDataRowNumber(Integer.parseInt(formInfo.get(TO_WRITE_BUDGET).toString())); budgetRow.setBalance(applyData.get(BUDGET_ITEM_BALANCE).toString()); budgetRow.setMoney(new BigDecimal(money(budgetData)) .subtract(new BigDecimal(money(formInfo))).toString()); refunds(budgetRow).stream().forEach( o ->{ formOthersOperation.updateFormFieldProperty(o.getProcessBudget(), o.getData() ,o.getFormType(),o.getIBillCostLog()); }); return true; } /** * 退回费用 * @param budgetRow * @return */ private List refunds(BudgetRow budgetRow){ List budgetRowRecord = new ArrayList<>(); BigDecimal applyMoney = new BigDecimal(budgetRow.getMoney()); BigDecimal balance =new BigDecimal(budgetRow.getBalance()); BigDecimal money = balance.add(applyMoney); budgetRowRecord.add(new BudgetRowUpdateRecord( new ProcessBudget(GetConstant.getFormIdGetConstant(YS_FORMID) , budgetRow.getDataRowNumber(), BUDGET_SUB_FROM) , BUDGET_ITEM_BALANCE, getValueByBalanceType(money) + "" ,budgetRow.getTenantId())); return budgetRowRecord; } public BudgetUserDefined getBudgetUserDefined(String fun) { BudgetUserDefined budgetUserDefined = new BudgetUserDefined(); budgetUserDefined.setFilterSubForm(false); budgetUserDefined.setMergeSubForm(false); budgetUserDefined.setKey("ExpendUseApplyFun"); budgetUserDefined.setVal("报销使用申请"); List mainDimension = new ArrayList<>(); List subDimension = new ArrayList<>(); mainDimension.add(new Dimension(BUDGET_START_DATE)); mainDimension.add(new Dimension(CURRENCY)); mainDimension.add(new Dimension(EXPEND_COST_TYPE)); mainDimension.add(new Dimension(EXPEND_DEPARTMENT, APPLY_DEPARTMENT)); budgetUserDefined.setMainDimension(mainDimension); budgetUserDefined.setSubDimension(subDimension); return budgetUserDefined; } }