package com.rzkj.bigdata.service.impl;
import com.rzkj.bigdata.domain.*;
import com.rzkj.bigdata.mapper.*;
import com.rzkj.bigdata.service.IOaCostShareFormService;
import com.rzkj.common.constant.CostShareConstants;
import com.rzkj.common.constant.ItemConstans;
import com.rzkj.common.utils.DateUtils;
import com.rzkj.common.utils.StringUtils;
import com.rzkj.system.domain.SysDept;
import com.rzkj.system.mapper.SysDeptMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
*
* OA费用报销单
* @Author:hyl
*
*/
public class OaCostShareFormServiceImpl implements IOaCostShareFormService {
private static Logger logger= LoggerFactory.getLogger(OaCostShareFormServiceImpl.class);
@Autowired
private OaCostShareTempMapper oaCostShareTempMapper;
@Autowired
private BaseItemDeptMapper baseItemDeptMapper;
@Autowired
private SysDeptMapper sysDeptMapper;
@Autowired
private BaseObjectInfoMapper baseObjectInfoMapper;
@Autowired
private OaCostShareMapper oaCostShareMapper;
@Autowired
private OaOrgUnitMapper oaOrgUnitMapper;
@Autowired
private BaseCostInfoMapper baseCostInfoMapper;
@Autowired
private IOaCostShareFormService oaCostShareFormService;
/**
* OA机构报销单同步到分摊数据
* :成本中心匹配不上:则后续科目/项目一律置为空
* @param dayTime 日期(yyyy-MM-dd)
*/
public void createCostShareByShareForm(String dayTime){
//对应日期的机构报销单信息
OaCostShare shareQuery=new OaCostShare();
shareQuery.setAudTimeQuery(dayTime);
shareQuery.setDocType(CostShareConstants.ORG_SHARE);
List<OaCostShare> shareList=oaCostShareMapper.selectOaCostShareList(shareQuery);
//OA部门配置表
OaOrgUnit orgQuery=new OaOrgUnit();
List<OaOrgUnit> oaCenterList= oaOrgUnitMapper.selectOaOrgUnitList(orgQuery);
//部门成本中心
List<SysDept> deptList= sysDeptMapper.selectAllCenterDeptList();
//部门成本中心全部项目信息
BaseItemDept itemDeptQuery=new BaseItemDept();
itemDeptQuery.setStatus(ItemConstans.NOT_DELETE_STATUS);
List<BaseItemDept> itemList= baseItemDeptMapper.selectBaseItemDeptDetailList(itemDeptQuery);
//全部科目信息
BaseObjectInfo objectInfoQuery=new BaseObjectInfo();
objectInfoQuery.setStatus(ItemConstans.NOT_DELETE_STATUS);
List<BaseObjectInfo> objectInfoList= baseObjectInfoMapper.selectBaseObjectInfoList(objectInfoQuery);
//报销单信息
List<OaCostShareTemp> temps= oaCostShareTempMapper.selectOrgShareForm(dayTime);
Map<Long,List<OaCostShareTemp>> groupTempsList=temps.stream().collect(Collectors.groupingBy(OaCostShareTemp::getFatherId));
int successNum=0;
for (Long fatherId:groupTempsList.keySet()) {
List<OaCostShareTemp> oneGroupList= groupTempsList.get(fatherId);
// BigDecimal totalMoney=oneGroupList.get(0).getDocMoney();
// BigDecimal waitAllMoney= oneGroupList.stream().map(e->e.getWaitShareMoney()==null?new BigDecimal(0):e.getWaitShareMoney()).reduce(BigDecimal.ZERO, BigDecimal::add);
//
// if (totalMoney.compareTo(waitAllMoney) !=compareWaitResult){
// logger.info("报销单ID:"+fatherId+"====》》报销金额和分摊金额对应不上:"+oneGroupList);
// logger.info("报销单ID:"+fatherId+"====》》报销金额:"+totalMoney +" 分摊金额:"+waitAllMoney);
// failureNum++;
// }
for (OaCostShareTemp shareTemp:oneGroupList){
List<OaCostShare> matchShareList=shareList.stream().filter(x->x.getSourceId().intValue()==shareTemp.getSonId().intValue()).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(matchShareList)){
OaCostShare costShare=matchShareList.get(0);
if (costShare.getSendStatus().equals(CostShareConstants.SHARE_STATUS)){
continue;
}else{
//更新信息
int num= oaCostShareFormService.updateCostShareByShareForm(costShare,shareTemp,deptList,objectInfoList,itemList,oaCenterList);
successNum=successNum+num;
}
}else {
//新增信息
int num= oaCostShareFormService.addCostShareByShareForm(shareTemp,deptList,objectInfoList,itemList,oaCenterList);
successNum=successNum+num;
}
}
}
int errorNum=temps.size()-successNum;
logger.info("OA机构报销单类型的分摊信息同步共计:"+temps.size()+"条;其中同步成功共计:"+successNum+"条;同步失败共计:"+errorNum+"条");
}
/**
* OA报销单临时信息更新到分摊信息
* @param costShare 分摊信息
* @param shareTemp oa临时组装信息
* @param deptList 部门成本集合
* @param objectInfoList 科目集合
* @param itemList 项目集合
* @param oaCenterList oa部门配置集合
* @return
*/
@Override
@Transactional
public int updateCostShareByShareForm(OaCostShare costShare,OaCostShareTemp shareTemp, List<SysDept> deptList,List<BaseObjectInfo> objectInfoList,List<BaseItemDept> itemList, List<OaOrgUnit> oaCenterList) {
BaseCostInfo costInfo = new BaseCostInfo();
int returnNum=0;
try {
costShare.setUpdateTime(DateUtils.getNowDate());
costShare.setUpdateBy("1");
String result=matchCostShareInfo(costInfo, costShare,shareTemp,deptList,objectInfoList,itemList,oaCenterList);
returnNum= oaCostShareMapper.updateOaCostShare(costShare);
// if (shareTemp.getSonId().toString().equals("-1705836187364470843")){
// throw new Exception();
// }
if (result.equals(CostShareConstants.MATCH_YES)){
costInfo.setId(costShare.getId());
returnNum= baseCostInfoMapper.insertBaseCostInfo(costInfo);
}
} catch (Exception e) {
logger.error("更新OA机构报销单类型的分摊信息异常:{}","OA机构报销单信息:"+shareTemp+"【====>>>>>待更新信息"+costShare);
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
e.printStackTrace();
return returnNum;
}
if (returnNum>0){
logger.info("更新OA机构报销单类型的分摊信息成功:{}","OA机构报销单信息:"+shareTemp+"====>>>>>对应分摊信息"+costShare);
}else{
logger.info("更新OA机构报销单类型的分摊信息失败:{}","OA机构报销单信息:"+shareTemp+"====>>>>>对应分摊信息"+costShare);
}
return returnNum;
}
/**
* OA报销单临时信息插入分摊信息
* @param shareTemp oa临时组装信息
* @param deptList 部门成本集合
* @param objectInfoList 科目集合
* @param itemList 项目集合
* @param oaCenterList oa部门配置集合
* @return
*/
@Override
@Transactional
public int addCostShareByShareForm(OaCostShareTemp shareTemp, List<SysDept> deptList, List<BaseObjectInfo> objectInfoList, List<BaseItemDept> itemList, List<OaOrgUnit> oaCent