|
|
@ -1,10 +1,22 @@ |
|
|
|
package com.qs.serve.modules.tzc.service; |
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.qs.serve.common.util.Assert; |
|
|
|
import com.qs.serve.common.util.CollectionUtil; |
|
|
|
import com.qs.serve.common.model.consts.BudgetLogOptFlag; |
|
|
|
import com.qs.serve.common.model.consts.BudgetLogRollbackFlag; |
|
|
|
import com.qs.serve.common.model.dto.R; |
|
|
|
import com.qs.serve.common.util.*; |
|
|
|
import com.qs.serve.common.util.model.DateFormatString; |
|
|
|
import com.qs.serve.modules.bms.entity.BmsSupplier; |
|
|
|
import com.qs.serve.modules.bms.service.BmsSupplierService; |
|
|
|
import com.qs.serve.modules.seeyon.entity.BaseCreateCallbackBo; |
|
|
|
import com.qs.serve.modules.seeyon.entity.BaseCreateProcessBo; |
|
|
|
import com.qs.serve.modules.seeyon.service.SeeYonService; |
|
|
|
import com.qs.serve.modules.sys.entity.SysUser; |
|
|
|
import com.qs.serve.modules.sys.service.SysConfigService; |
|
|
|
import com.qs.serve.modules.sys.service.SysUserService; |
|
|
|
import com.qs.serve.modules.tbs.common.TbsCostApplyState; |
|
|
|
import com.qs.serve.modules.tbs.common.TbsGoodsType; |
|
|
|
import com.qs.serve.modules.tbs.common.TbsSeeYonConst; |
|
|
|
import com.qs.serve.modules.tbs.common.util.TbsBudgetCostUtil; |
|
|
|
import com.qs.serve.modules.tbs.entity.*; |
|
|
|
import com.qs.serve.modules.tbs.entity.dto.TbsBudgetCostResult; |
|
|
@ -12,10 +24,8 @@ import com.qs.serve.modules.tbs.entity.dto.TbsBudgetItemCostResult; |
|
|
|
import com.qs.serve.modules.tbs.entity.vo.TbsBudgetTableVo; |
|
|
|
import com.qs.serve.modules.tbs.mapper.TbsBudgetMapper; |
|
|
|
import com.qs.serve.modules.tbs.mapper.TbsScheduleItemBudgetMapper; |
|
|
|
import com.qs.serve.modules.tbs.service.TbsBudgetApplicationService; |
|
|
|
import com.qs.serve.modules.tbs.service.TbsBudgetConditionService; |
|
|
|
import com.qs.serve.modules.tbs.service.TbsBudgetCostItemService; |
|
|
|
import com.qs.serve.modules.tbs.service.TbsScheduleItemBudgetService; |
|
|
|
import com.qs.serve.modules.tbs.service.*; |
|
|
|
import com.qs.serve.modules.tzc.common.TzcPolicyStatus; |
|
|
|
import com.qs.serve.modules.tzc.entity.TzcPolicy; |
|
|
|
import com.qs.serve.modules.tzc.entity.TzcPolicyGoods; |
|
|
|
import com.qs.serve.modules.tzc.entity.TzcPolicyItem; |
|
|
@ -23,8 +33,10 @@ import lombok.AllArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.jetbrains.annotations.NotNull; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.util.*; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
@ -40,19 +52,123 @@ public class TzcPolicyApplication { |
|
|
|
private TzcPolicyService tzcPolicyService; |
|
|
|
private TzcPolicyItemService tzcPolicyItemService; |
|
|
|
private TzcPolicyGoodsService tzcPolicyGoodsService; |
|
|
|
private TbsCostUnItemPolicyService tbsCostUnItemPolicyService; |
|
|
|
private TbsBudgetCostItemPolicyService tbsBudgetCostItemPolicyService; |
|
|
|
private TbsBudgetApplicationService budgetApplicationService; |
|
|
|
private TbsScheduleItemBudgetService tbsScheduleItemBudgetService; |
|
|
|
private TbsBudgetMapper tbsBudgetMapper; |
|
|
|
private TbsScheduleItemBudgetMapper tbsScheduleItemBudgetMapper; |
|
|
|
private TbsBudgetLogService budgetLogService; |
|
|
|
private TbsBudgetConditionService tbsBudgetConditionService; |
|
|
|
private TbsBudgetCostItemService tbsBudgetCostItemService; |
|
|
|
private SysConfigService configService; |
|
|
|
private SysUserService sysUserService; |
|
|
|
private SeeYonService seeYonService; |
|
|
|
private BmsSupplierService bmsSupplierService; |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* 提交申请 |
|
|
|
* @param policyId |
|
|
|
*/ |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void commitPolicy(Long policyId){ |
|
|
|
//
|
|
|
|
seeYonService.testConnection(); |
|
|
|
TzcPolicy tzcPolicy = tzcPolicyService.getById(policyId); |
|
|
|
if(!tzcPolicy.allowEditFlag()){ |
|
|
|
Assert.throwEx("操作失败,政策状态不支持"); |
|
|
|
} |
|
|
|
BmsSupplier supplier = bmsSupplierService.getById(tzcPolicy.getSupplierId()); |
|
|
|
if(supplier.getCostFlag().equals(0)){ |
|
|
|
Assert.throwEx("["+supplier.getName()+"]无投放费用权限"); |
|
|
|
} |
|
|
|
TbsBudgetCostResult result = this.buildBudgetCostResult(policyId,true,false); |
|
|
|
SysUser sysUser = sysUserService.getById(AuthContextUtils.getSysUserId()); |
|
|
|
sysUser.checkSyAccount(); |
|
|
|
Map<String, Object> data = new HashMap<>(10); |
|
|
|
data.put("applyUserCode",sysUser.getCode()); |
|
|
|
data.put("applyUserName",sysUser.getName()); |
|
|
|
data.put("supplierName",tzcPolicy.getSupplierName()); |
|
|
|
data.put("supplierCode",tzcPolicy.getSupplierCode()); |
|
|
|
data.put("title",tzcPolicy.getTitle()); |
|
|
|
data.put("rowDate", DateUtils.format(LocalDateTime.now(), DateFormatString.DATE_TIME)); |
|
|
|
data.put("rowState","03"); |
|
|
|
data.put("orgId",tzcPolicy.getId()); |
|
|
|
data.put("targetId",tzcPolicy.getId()); |
|
|
|
data.put("targetCode",tzcPolicy.getPolicyCode()); |
|
|
|
data.put("exsp1", TbsSeeYonConst.PolicyConf.Code()); |
|
|
|
BaseCreateProcessBo createProcess = new BaseCreateProcessBo(); |
|
|
|
createProcess.setTemplateCode(TbsSeeYonConst.PolicyConf.Code()); |
|
|
|
createProcess.setMemberLoginName(sysUser.getSyAccount()); |
|
|
|
createProcess.setSubjectTitle(TbsSeeYonConst.PRE_TITLE_COST_APPLY + tzcPolicy.getTitle()); |
|
|
|
createProcess.setDataJson(JsonUtil.objectToJson(data)); |
|
|
|
R<String> flowIdResult = seeYonService.baseCreateProcess(createProcess); |
|
|
|
if(flowIdResult.getStatus()!=200){ |
|
|
|
Assert.throwEx("远程服务调用失败"); |
|
|
|
} |
|
|
|
String flowId = flowIdResult.getData(); |
|
|
|
String formId = null; |
|
|
|
R<String> formIdResult = seeYonService.getCostFormId(policyId); |
|
|
|
if(formIdResult.getStatus()==200){ |
|
|
|
formId = formIdResult.getData(); |
|
|
|
} |
|
|
|
TzcPolicy updPolicy = new TzcPolicy(); |
|
|
|
updPolicy.setId(policyId); |
|
|
|
updPolicy.setSyFlowId(flowId); |
|
|
|
updPolicy.setSyFormId(formId); |
|
|
|
updPolicy.setPolicyStatus(TzcPolicyStatus.Status_1_Checking); |
|
|
|
tzcPolicyService.updateById(updPolicy); |
|
|
|
tzcPolicy = tzcPolicyService.getById(policyId); |
|
|
|
//移除历史记录
|
|
|
|
tzcPolicyItemService.removeByPolicyId(policyId); |
|
|
|
//保存无命中条件的预算
|
|
|
|
if(CollectionUtil.isNotEmpty(result.getBudgetUnMatchList())){ |
|
|
|
List<TbsCostUnItemPolicy> costUnItems = result.getBudgetUnMatchList().stream() |
|
|
|
.map(obj-> { |
|
|
|
TbsCostUnItemPolicy itemPolicy = CopierUtil.copy(obj,new TbsCostUnItemPolicy()); |
|
|
|
itemPolicy.initSetNull(); |
|
|
|
return itemPolicy; |
|
|
|
}).collect(Collectors.toList()); |
|
|
|
tbsCostUnItemPolicyService.saveBatch(costUnItems); |
|
|
|
} |
|
|
|
//重新保存
|
|
|
|
List<TbsBudgetCostItemPolicy> allBudgetItem = result.getPolicyBudgetMatchList(); |
|
|
|
tbsBudgetCostItemPolicyService.saveBatch(allBudgetItem); |
|
|
|
//保存预算使用日志
|
|
|
|
if(tzcPolicy.getPolicyStatus().equals(TzcPolicyStatus.Status_0_UnCommit)){ |
|
|
|
List<TbsBudgetLog> budgetLogList = saveBudgetLog(result, sysUser, tzcPolicy, result.getPolicyBudgetMatchList(),result.getPolicyItemList()); |
|
|
|
if(CollectionUtil.isNotEmpty(budgetLogList)){ |
|
|
|
budgetLogService.saveBatch(budgetLogList); |
|
|
|
} |
|
|
|
}else { |
|
|
|
//排除历史记录
|
|
|
|
List<TbsBudgetLog> budgetLogList = saveBudgetLog(result, sysUser, tzcPolicy, result.getPolicyBudgetMatchList(),result.getPolicyItemList()); |
|
|
|
LambdaQueryWrapper<TbsBudgetLog> logLqw = new LambdaQueryWrapper<>(); |
|
|
|
logLqw.eq(TbsBudgetLog::getPolicyId,policyId); |
|
|
|
logLqw.eq(TbsBudgetLog::getRollbackFlag, BudgetLogRollbackFlag.State_1); |
|
|
|
List<TbsBudgetLog> oldLogList = budgetLogService.list(logLqw); |
|
|
|
List<TbsBudgetLog> oldLogList4Upd = oldLogList.stream().map(a->{ |
|
|
|
TbsBudgetLog budgetLog = new TbsBudgetLog(); |
|
|
|
budgetLog.setId(a.getId()); |
|
|
|
budgetLog.setRollbackFlag(BudgetLogRollbackFlag.State_2); |
|
|
|
return budgetLog; |
|
|
|
}).collect(Collectors.toList()); |
|
|
|
budgetLogService.updateBatchById(oldLogList4Upd); |
|
|
|
for (TbsBudgetLog budgetLog : oldLogList) { |
|
|
|
budgetLog.setCreateBy(null); |
|
|
|
budgetLog.setCreateTime(null); |
|
|
|
budgetLog.setUpdateBy(null); |
|
|
|
budgetLog.setUpdateTime(null); |
|
|
|
budgetLog.setId(null); |
|
|
|
budgetLog.setAmount(budgetLog.getAmount().negate()); |
|
|
|
budgetLog.setOptType(BudgetLogOptFlag.State_12); |
|
|
|
} |
|
|
|
budgetLogService.saveBatch(oldLogList); |
|
|
|
budgetLogList.forEach(a->a.setOptType(BudgetLogOptFlag.State_13)); |
|
|
|
budgetLogService.saveBatch(budgetLogList); |
|
|
|
} |
|
|
|
//创建流程后回调
|
|
|
|
BaseCreateCallbackBo callbackBo = new BaseCreateCallbackBo(TbsSeeYonConst.PolicyConf.Code(),policyId+""); |
|
|
|
seeYonService.createCallbackStatus(callbackBo); |
|
|
|
} |
|
|
|
|
|
|
|
public TbsBudgetCostResult buildBudgetCostResult(Long policyId, |
|
|
@ -530,4 +646,41 @@ public class TzcPolicyApplication { |
|
|
|
return scheduleItemList; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 保存预算使用日志 |
|
|
|
* @param result |
|
|
|
* @param sysUser |
|
|
|
* @param tzcPolicy |
|
|
|
* @param allBudgetItem |
|
|
|
*/ |
|
|
|
private List<TbsBudgetLog> saveBudgetLog(TbsBudgetCostResult result, SysUser sysUser, TzcPolicy tzcPolicy, List<TbsBudgetCostItemPolicy> allBudgetItem, List<TzcPolicyItem> policyItemList) { |
|
|
|
List<TbsBudgetLog> budgetLogList = new ArrayList<>(); |
|
|
|
List<TbsBudget> budgetList = result.getBudgetList(); |
|
|
|
for (TbsBudgetCostItemPolicy item : allBudgetItem) { |
|
|
|
//排除不匹配预算的项
|
|
|
|
if(item.getBudgetId().equals(0L)){ |
|
|
|
continue; |
|
|
|
} |
|
|
|
TbsBudget budget = null; |
|
|
|
for (TbsBudget tbsBudget : budgetList) { |
|
|
|
if(item.getBudgetId().equals(tbsBudget.getId())){ |
|
|
|
budget = tbsBudget; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
if(budget==null){ |
|
|
|
continue; |
|
|
|
} |
|
|
|
TzcPolicyItem currPolicyItem = null; |
|
|
|
for (TzcPolicyItem policyItem : policyItemList) { |
|
|
|
if(item.getPolicyItemId().equals(policyItem.getId())){ |
|
|
|
currPolicyItem = policyItem; |
|
|
|
} |
|
|
|
} |
|
|
|
TbsBudgetLog budgetLog = budgetLogService.buildTbsBudgetLog(BudgetLogOptFlag.State_11,sysUser, tzcPolicy, item, budget,item.getPolicyItemAmount().negate(),currPolicyItem); |
|
|
|
budgetLogList.add(budgetLog); |
|
|
|
} |
|
|
|
return budgetLogList; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|