|
|
@ -1,20 +1,43 @@ |
|
|
|
package com.qs.serve.modules.third.service; |
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.qs.serve.common.model.consts.ResultFlag; |
|
|
|
import com.qs.serve.common.util.Assert; |
|
|
|
import com.qs.serve.common.util.CollectionUtil; |
|
|
|
import com.qs.serve.common.util.StringUtils; |
|
|
|
import com.qs.serve.modules.bms.entity.BmsSubject; |
|
|
|
import com.qs.serve.modules.bms.entity.BmsSupplier; |
|
|
|
import com.qs.serve.modules.bms.service.BmsSubjectService; |
|
|
|
import com.qs.serve.modules.bms.service.BmsSupplierService; |
|
|
|
import com.qs.serve.modules.goods.entity.GoodsCategory; |
|
|
|
import com.qs.serve.modules.goods.entity.GoodsSku; |
|
|
|
import com.qs.serve.modules.goods.entity.GoodsSpu; |
|
|
|
import com.qs.serve.modules.goods.entity.dto.TbsCenterDto; |
|
|
|
import com.qs.serve.modules.goods.service.GoodsCategoryService; |
|
|
|
import com.qs.serve.modules.goods.service.GoodsSkuService; |
|
|
|
import com.qs.serve.modules.goods.service.GoodsSpuService; |
|
|
|
import com.qs.serve.modules.sys.entity.SysUser; |
|
|
|
import com.qs.serve.modules.sys.service.SysUserService; |
|
|
|
import com.qs.serve.modules.tbs.common.TbsActivityState; |
|
|
|
import com.qs.serve.modules.tbs.common.TbsCostApplyState; |
|
|
|
import com.qs.serve.modules.tbs.entity.TbsActivity; |
|
|
|
import com.qs.serve.modules.tbs.entity.TbsCostApply; |
|
|
|
import com.qs.serve.modules.tbs.service.TbsActivityService; |
|
|
|
import com.qs.serve.modules.tbs.service.TbsCenterDtoService; |
|
|
|
import com.qs.serve.modules.tbs.entity.*; |
|
|
|
import com.qs.serve.modules.tbs.entity.dto.TbsBudgetCostResult; |
|
|
|
import com.qs.serve.modules.tbs.service.*; |
|
|
|
import com.qs.serve.modules.third.entity.ProcessCreateBo; |
|
|
|
import com.qs.serve.modules.third.entity.ProcessGoodsItem; |
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.List; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.util.Random; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author YenHex |
|
|
@ -25,23 +48,227 @@ import org.springframework.stereotype.Service; |
|
|
|
@AllArgsConstructor |
|
|
|
public class PortalOfCostApplication { |
|
|
|
|
|
|
|
private TbsActivityService tbsActivityService; |
|
|
|
private TbsActivityService activityService; |
|
|
|
private TbsActivityCenterService activityCenterService; |
|
|
|
private TbsActivitySubjectService activitySubjectService; |
|
|
|
private TbsActivityGoodsService activityGoodsService; |
|
|
|
private TbsActivityCenterGoodsService activityCenterGoodsService; |
|
|
|
private BmsSupplierService supplierService; |
|
|
|
private BmsSubjectService subjectService; |
|
|
|
private SysUserService userService; |
|
|
|
private TbsCenterDtoService tbsCenterDtoService; |
|
|
|
private TbsBudgetApplicationService budgetApplicationService; |
|
|
|
private TbsCostApplyService costApplyService; |
|
|
|
private GoodsSkuService goodsSkuService; |
|
|
|
private GoodsSpuService goodsSpuService; |
|
|
|
private GoodsCategoryService goodsCategoryService; |
|
|
|
|
|
|
|
void createCostProcess(ProcessCreateBo createBo){ |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void createCostProcess(ProcessCreateBo createBo){ |
|
|
|
SysUser user = userService.getByAccount(createBo.getUserCode()); |
|
|
|
if(user==null){Assert.throwEx("用户不存在,请重新编辑");} |
|
|
|
BmsSupplier supplier = supplierService.getByCode(createBo.getCustomerCode(),createBo.getCustomerBelong()); |
|
|
|
if(supplier==null){Assert.throwEx("客户不存在,请重新编辑");} |
|
|
|
TbsCenterDto tbsCenterDto = tbsCenterDtoService.getCenterDto(createBo.getCenterType(),createBo.getCenterId()); |
|
|
|
if(tbsCenterDto==null){Assert.throwEx("部分成本中心不存在了,请重新编辑");} |
|
|
|
BmsSubject subject = subjectService.getByCode(createBo.getSubjectCode()); |
|
|
|
if(subject==null){Assert.throwEx("科目不存在,请重新编辑");} |
|
|
|
//初始化sku列表
|
|
|
|
List<GoodsSku> skuList = this.initSkuListOfProcess(createBo); |
|
|
|
//常用参数
|
|
|
|
final String GOODS_TYPE = "sku"; |
|
|
|
final String CENTER_TYPE = "customer"; |
|
|
|
final BigDecimal OneHundred = new BigDecimal("100"); |
|
|
|
LocalDateTime nowTime = LocalDateTime.now(); |
|
|
|
LocalDate nowDate = LocalDate.now(); |
|
|
|
Long supplierId = Long.parseLong(supplier.getId()); |
|
|
|
BigDecimal totalAmount = createBo.getTotalAmount(); |
|
|
|
String costCode = createBo.getCostCode(); |
|
|
|
String costTheme = createBo.getCostTheme(); |
|
|
|
|
|
|
|
// String random = String.format("%03d",new Random().nextInt(999));
|
|
|
|
// activity.setActivityCode("G"+ StringUtils.genShortId() + random);
|
|
|
|
String activityCode = createBo.getActivityCode(); |
|
|
|
String activityTitle = createBo.getActivityTitle(); |
|
|
|
//构建费用申请
|
|
|
|
TbsCostApply costApply = new TbsCostApply(); |
|
|
|
costApply.setCode(createBo.getCostCode()); |
|
|
|
costApply.setCode(createBo.getCostTheme()); |
|
|
|
costApply.setCode(costCode); |
|
|
|
costApply.setChargeTheme(costTheme); |
|
|
|
costApply.setChargeState(TbsCostApplyState.State_3_finished.getCode()); |
|
|
|
costApply.setSupplierId(supplierId); |
|
|
|
costApply.setSupplierName(supplier.getName()); |
|
|
|
costApply.setSupplierCode(supplier.getCode()); |
|
|
|
costApply.setUserId(user.getId()); |
|
|
|
costApply.setUserCode(user.getCode()); |
|
|
|
costApply.setUserName(user.getName()); |
|
|
|
costApply.setTotalActivity(1); |
|
|
|
costApply.setTotalActivityAmount(totalAmount); |
|
|
|
costApply.setTotalActivityUsedAmount(totalAmount); |
|
|
|
//构建活动
|
|
|
|
TbsActivity activity = new TbsActivity(); |
|
|
|
activity.setActivityCode(activityCode); |
|
|
|
activity.setActivityState(TbsActivityState.STATE_1_Finished); |
|
|
|
activity.setCostPassFlag(1); |
|
|
|
activity.setCostPassTime(nowTime); |
|
|
|
activity.setActTitle(activityTitle); |
|
|
|
activity.setSupplierId(supplierId); |
|
|
|
activity.setSupplierName(supplier.getName()); |
|
|
|
activity.setSupplierCode(supplier.getCode()); |
|
|
|
activity.setActStartDate(nowDate); |
|
|
|
activity.setActEndDate(nowDate); |
|
|
|
activity.setPreStartDate(nowDate); |
|
|
|
activity.setPreEndDate(nowDate); |
|
|
|
activity.setPreCheckDate(nowDate); |
|
|
|
activity.setTotalAmount(totalAmount); |
|
|
|
activity.setUsedAmount(totalAmount); |
|
|
|
activity.setFinishedFlag(ResultFlag.OK); |
|
|
|
activity.setFinishedTime(nowTime); |
|
|
|
//活动-科目
|
|
|
|
TbsActivitySubject activitySubject = new TbsActivitySubject(); |
|
|
|
activitySubject.setSubjectId(subject.getId()); |
|
|
|
activitySubject.setSubjectCode(subject.getSubjectCode()); |
|
|
|
activitySubject.setSubjectName(subject.getSubjectName()); |
|
|
|
activitySubject.setAmount(totalAmount); |
|
|
|
activitySubject.setUsedAmount(totalAmount); |
|
|
|
activitySubject.setCountSession(0); |
|
|
|
activitySubject.setCountPerson(0); |
|
|
|
|
|
|
|
//活动-成本中心
|
|
|
|
TbsActivityCenter activityCenter = new TbsActivityCenter(); |
|
|
|
activityCenter.setSubjectId(subject.getId()); |
|
|
|
activityCenter.setCenterAmount(totalAmount); |
|
|
|
activityCenter.setCenterRate(OneHundred); |
|
|
|
activityCenter.setCenterType(CENTER_TYPE); |
|
|
|
activityCenter.setCenterId(supplier.getId()); |
|
|
|
activityCenter.setCenterName(supplier.getName()); |
|
|
|
activityCenter.setCenterCode(supplier.getCode()); |
|
|
|
activityCenter.setUsedAmount(totalAmount); |
|
|
|
|
|
|
|
//活动-商品
|
|
|
|
List<TbsActivityGoods> activityGoodsList = new ArrayList<>(); |
|
|
|
for (GoodsSku sku : skuList) { |
|
|
|
GoodsSpu goodsSpu = goodsSpuService.getById(sku.getSpuId()); |
|
|
|
GoodsCategory category1 = goodsCategoryService.getById(goodsSpu.getCategoryFirst()); |
|
|
|
GoodsCategory category2 = goodsCategoryService.getById(goodsSpu.getCategorySecond()); |
|
|
|
GoodsCategory category3 = goodsCategoryService.getById(goodsSpu.getCategoryThird()); |
|
|
|
TbsActivityGoods activityGoods = new TbsActivityGoods(); |
|
|
|
activityGoods.setTargetType(GOODS_TYPE); |
|
|
|
activityGoods.setTargetId(sku.getId()); |
|
|
|
activityGoods.setTargetCode(sku.getSkuCode()); |
|
|
|
activityGoods.setTargetName(sku.getSpecInfos()); |
|
|
|
activityGoods.setTargetLevelPathIds(category1.getId()+"_"+category2.getId()+"_"+category3.getId()+"_"+goodsSpu.getId()+"_"+sku.getId()); |
|
|
|
activityGoods.setTargetLevelPathNames(category1.getName()+"_"+category2.getName()+"_"+category3.getName()+"_"+goodsSpu.getName()+"_"+sku.getSpecInfos()); |
|
|
|
activityGoodsList.add(activityGoods); |
|
|
|
} |
|
|
|
//活动-商品与成本中心记录
|
|
|
|
List<TbsActivityCenterGoods> activityCenterGoodsList = new ArrayList<>(); |
|
|
|
for (int i = 0; i < activityGoodsList.size(); i++) { |
|
|
|
TbsActivityGoods activityGoods = activityGoodsList.get(i); |
|
|
|
TbsActivityCenterGoods centerGoods = new TbsActivityCenterGoods(); |
|
|
|
centerGoods.setActivityCode(activity.getActivityCode()); |
|
|
|
centerGoods.setCenterGoodsCode(activity.getActivityCode()+"_"+(i+1)); |
|
|
|
centerGoods.setSupplierId(supplierId); |
|
|
|
centerGoods.setSupplierCode(supplier.getCode()); |
|
|
|
centerGoods.setSupplierName(supplier.getName()); |
|
|
|
//设置成本中心
|
|
|
|
centerGoods.setCenterType(CENTER_TYPE); |
|
|
|
centerGoods.setCenterId(supplier.getId()); |
|
|
|
centerGoods.setCenterCode(supplier.getCode()); |
|
|
|
centerGoods.setCenterName(supplier.getName()); |
|
|
|
centerGoods.setCenterRate(OneHundred); |
|
|
|
centerGoods.setCenterAmount(totalAmount); |
|
|
|
//设置科目
|
|
|
|
centerGoods.setSubjectId(subject.getId()); |
|
|
|
centerGoods.setSubjectCode(subject.getSubjectCode()); |
|
|
|
centerGoods.setSubjectName(subject.getSubjectName()); |
|
|
|
//TODO 分配
|
|
|
|
centerGoods.setCenterGoodsAmount(BigDecimal.ZERO); |
|
|
|
centerGoods.setCenterGoodsRate(BigDecimal.ZERO); |
|
|
|
centerGoods.setUsedAmount(BigDecimal.ZERO); |
|
|
|
//设置活动时间
|
|
|
|
centerGoods.setActEndDate(nowDate); |
|
|
|
centerGoods.setActStartDate(nowDate); |
|
|
|
centerGoods.setPreEndDate(nowDate); |
|
|
|
centerGoods.setPreStartDate(nowDate); |
|
|
|
centerGoods.setPreCheckDate(nowDate); |
|
|
|
//设置目标
|
|
|
|
centerGoods.setTargetId(activityGoods.getTargetId()); |
|
|
|
centerGoods.setTargetCode(activityGoods.getTargetCode()); |
|
|
|
centerGoods.setTargetName(activityGoods.getTargetName()); |
|
|
|
centerGoods.setTargetLevelPathIds(activityGoods.getTargetLevelPathIds()); |
|
|
|
centerGoods.setTargetLevelPathNames(activityGoods.getTargetLevelPathNames()); |
|
|
|
activityCenterGoodsList.add(centerGoods); |
|
|
|
} |
|
|
|
|
|
|
|
//保存活动有关数据
|
|
|
|
costApplyService.save(costApply); |
|
|
|
activity.setCostApplyId(costApply.getId()); |
|
|
|
activityService.save(activity); |
|
|
|
activitySubject.setCostApplyId(costApply.getId()); |
|
|
|
activitySubject.setActivityId(activity.getId()); |
|
|
|
activitySubjectService.save(activitySubject); |
|
|
|
activityCenter.setCostApplyId(costApply.getId()); |
|
|
|
activityCenter.setActivityId(activity.getId()); |
|
|
|
activityCenterService.save(activityCenter); |
|
|
|
activityGoodsList.forEach(item->{ |
|
|
|
item.setCostApplyId(costApply.getId()); |
|
|
|
item.setActivityId(activity.getId()); |
|
|
|
}); |
|
|
|
activityGoodsService.saveBatch(activityGoodsList); |
|
|
|
activityCenterGoodsList.forEach(item->{ |
|
|
|
item.setCostApplyId(costApply.getId()); |
|
|
|
item.setActivityId(activity.getId()); |
|
|
|
}); |
|
|
|
activityCenterGoodsService.saveBatch(activityCenterGoodsList); |
|
|
|
|
|
|
|
//获取匹配的预算结果
|
|
|
|
TbsBudgetCostResult budgetCostResult = budgetApplicationService.buildBudgetCostResult( |
|
|
|
costApply.getId(), |
|
|
|
Arrays.asList(activity), |
|
|
|
Arrays.asList(activitySubject), |
|
|
|
activityCenterGoodsList, |
|
|
|
Arrays.asList(activityCenter),true,false,false); |
|
|
|
List<TbsBudgetCostItem> matchBudgetItem = budgetCostResult.getBudgetMatchList(); |
|
|
|
//TODO 匹配列表生成预算日志
|
|
|
|
List<TbsBudgetCostItem> unMatchBudgetItem = budgetCostResult.getBudgetUnMatchList(); |
|
|
|
//不匹配保存到无预算标
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 创建流程中,初始化SKU列表 |
|
|
|
* @param createBo |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private List<GoodsSku> initSkuListOfProcess(ProcessCreateBo createBo) { |
|
|
|
List<GoodsSku> goodsSkus = null; |
|
|
|
if(CollectionUtil.isNotEmpty(createBo.getGoodsList())){ |
|
|
|
List<String> invCodes = createBo.getGoodsList().stream() |
|
|
|
.map(ProcessGoodsItem::getInventoryCode).distinct().collect(Collectors.toList()); |
|
|
|
LambdaQueryWrapper<GoodsSku> skuLqw = new LambdaQueryWrapper<>(); |
|
|
|
skuLqw.in(GoodsSku::getSkuCode,invCodes); |
|
|
|
goodsSkus = goodsSkuService.list(skuLqw); |
|
|
|
if(invCodes.size()!=goodsSkus.size()){ |
|
|
|
List<String> skuCodeNotExistList = new ArrayList<>(); |
|
|
|
for (String invCode : invCodes) { |
|
|
|
boolean notExist = true; |
|
|
|
for (GoodsSku sku : goodsSkus) { |
|
|
|
if(sku.getSkuCode().equals(invCode)){ |
|
|
|
notExist = false; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
if(notExist){ |
|
|
|
skuCodeNotExistList.add(invCode); |
|
|
|
} |
|
|
|
} |
|
|
|
if(skuCodeNotExistList.size()>0){ |
|
|
|
String invCodesString = skuCodeNotExistList.stream().collect(Collectors.joining(",")); |
|
|
|
Assert.throwEx("存货编码["+invCodesString+"]不存在,请重新编辑"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if(goodsSkus==null){ |
|
|
|
Assert.throwEx("存货编码未设置,请重新编辑"); |
|
|
|
} |
|
|
|
return goodsSkus; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|