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.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.ObjectUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.ModelMap; import java.util.Map; import java.util.Objects; /** * 申请扣减预算 */ @Service(UpdateBudgetFactory.SQ_TYPE) @Slf4j @Scope("prototype") public class ApplyBudgetUpdate extends AbstractUpdateBudget{ private Map applyInfo; private Map budgetChildInfo; private Integer budgetId; @Override public void checkBudget(UpdateBudgetEntity updateBudgetEntity) { this.applyInfo = getFormInfo(updateBudgetEntity); this.budgetId = getParentId(applyInfo); if(budgetId == null){ return; } addLock(this.budgetId); this.budgetChildInfo = getFormInfo(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM, budgetId); Assert.condition(budgetChildInfo == null, BUDGET_INVALID); if(applyInfo.get(APPLY_USE_MONEY) == null || getBudgetBalance(budgetChildInfo) == null){ return; } Assert.condition( toDouble(applyInfo.get(APPLY_USE_MONEY)) > toDouble(getBudgetBalance(budgetChildInfo)) , BUDGET_MONEY_NOT_ENOUGH); } @Override @TxTransaction @Transactional public void updateBudget(UpdateBudgetEntity updateBudgetEntity) { super.updateBudget(updateBudgetEntity); checkBudget(updateBudgetEntity); if(ObjectUtil.empty(this.budgetChildInfo) || applyInfo.get(APPLY_USE_MONEY) == null){ // account(updateBudgetEntity.getFormId(), ThreadData.get(Keys.TENANT_ID), Objects.toString(updateBudgetEntity.getDataRowNum(), "")); return; } double money = toDouble(getBudgetBalance(budgetChildInfo)) - toDouble(applyInfo.get(APPLY_USE_MONEY)); checkMoney(money); addBudgetLogMoney( this.budgetChildInfo , money , updateBudgetEntity.getFormId() , this.applyInfo , toDouble(applyInfo.get(APPLY_USE_MONEY)) ); updateData(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM , getDataRowNum(budgetChildInfo) , new ModelMap(BUDGET_ITEM_BALANCE, format(money,getFormBase(getFormId(SystemForm.BUDGET_FORM_ID)),BUDGET_ITEM_BALANCE))); } @Override public void flowAfterCall(UpdateBudgetEntity updateBudgetEntity) { this.applyInfo = getFormInfo(updateBudgetEntity); this.budgetId = getParentId(applyInfo); if(budgetId == null){ return; } delLock(this.budgetId); //记账 account(updateBudgetEntity.getFormId(),ThreadData.get(Keys.TENANT_ID), Objects.toString(updateBudgetEntity.getDataRowNum(),"")); } @Override public void rollback(UpdateBudgetEntity updateBudgetEntity) { super.rollback(updateBudgetEntity); this.applyInfo = getFormInfo(updateBudgetEntity); this.budgetId = getParentId(applyInfo); if(budgetId == null){ return; } addLock(this.budgetId); this.budgetChildInfo = getFormInfo(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM, budgetId); Assert.condition(budgetChildInfo == null, BUDGET_INVALID); if(applyInfo.get(APPLY_USE_MONEY) == null || getBudgetBalance(budgetChildInfo) == null){ return; } // 把申请金额回滚到预算里 double money = toDouble(getBudgetBalance(budgetChildInfo)) + toDouble(applyInfo.get(APPLY_USE_MONEY)); addBudgetLogMoney( this.budgetChildInfo , money , updateBudgetEntity.getFormId() , this.applyInfo , toDouble(applyInfo.get(APPLY_USE_MONEY)) ); updateData(getFormId(SystemForm.BUDGET_FORM_ID), BUDGET_SUB_FROM , getDataRowNum(budgetChildInfo) , new ModelMap(BUDGET_ITEM_BALANCE, format(money,getFormBase(getFormId(SystemForm.BUDGET_FORM_ID)),BUDGET_ITEM_BALANCE))); } @Override public FormType getFormType() { return FormType.SQ; } }