Merge remote-tracking branch 'origin/master'
commit
92750942b1
@ -1,18 +1,162 @@
|
||||
package com.glxp.api.service.inout.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.glxp.api.dao.inout.IoOrderDao;
|
||||
import com.glxp.api.dao.inout.IoStatDayDao;
|
||||
import com.glxp.api.dao.inout.IoStatMonthDao;
|
||||
import com.glxp.api.dao.inout.IoStatOrderDao;
|
||||
import com.glxp.api.entity.inout.IoStatDayEntity;
|
||||
import com.glxp.api.entity.inout.IoStatMonthEntity;
|
||||
import com.glxp.api.service.inout.IoStatMonthService;
|
||||
import com.glxp.api.util.GennerOrderUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public class IoStatMonthServiceImpl implements IoStatMonthService {
|
||||
|
||||
@Resource
|
||||
private IoStatMonthDao statMonthDao;
|
||||
@Resource
|
||||
private IoStatDayDao statDayDao;
|
||||
@Resource
|
||||
private IoOrderDao orderDao;
|
||||
@Resource
|
||||
private GennerOrderUtils gennerOrderUtils;
|
||||
@Resource
|
||||
private IoStatOrderDao statOrderDao;
|
||||
@Resource
|
||||
private SqlSessionFactory sqlSessionFactory;
|
||||
|
||||
@Override
|
||||
public void statData(Date date) {
|
||||
//汇总上一个月的数据
|
||||
DateTime lastMonth = DateUtil.lastMonth();
|
||||
//查询上个月每天的汇总数据
|
||||
List<IoStatDayEntity> statDayList = statDayDao.selectList(new QueryWrapper<IoStatDayEntity>()
|
||||
.eq("year", lastMonth.year())
|
||||
.eq("quarter", lastMonth.quarter())
|
||||
.eq("month", lastMonth.monthBaseOne())
|
||||
);
|
||||
|
||||
if (CollUtil.isNotEmpty(statDayList)) {
|
||||
//声明用于存放汇总数据的集合
|
||||
List<IoStatMonthEntity> dataList = new ArrayList<>();
|
||||
statDayList.forEach(statDayEntity -> {
|
||||
//查询上月的数据
|
||||
IoStatMonthEntity statMonthEntity = statMonthDao.selectOne(buildQueryWrapper(statDayEntity, lastMonth));
|
||||
|
||||
//获取新的汇总数据
|
||||
IoStatMonthEntity statData = getStatData(dataList, statDayEntity, lastMonth);
|
||||
|
||||
//设置入库数据
|
||||
statData.setInCount(statData.getInCount() + statDayEntity.getInCount());
|
||||
statData.setInPrice(statDayEntity.getBalancePrice());
|
||||
BigDecimal inAmount = statDayEntity.getBalancePrice().multiply(BigDecimal.valueOf(statDayEntity.getInCount()));
|
||||
statData.setInAmount(inAmount.add(statData.getInAmount()));
|
||||
|
||||
//设置出库数据
|
||||
statData.setOutCount(statData.getOutCount() + statDayEntity.getOutCount());
|
||||
statData.setOutPrice(statDayEntity.getBalancePrice());
|
||||
BigDecimal outAmount = statDayEntity.getBalancePrice().multiply(BigDecimal.valueOf(statDayEntity.getOutCount()));
|
||||
statData.setOutAmount(outAmount.add(statData.getOutAmount()));
|
||||
|
||||
//设置结余数据
|
||||
statData.setBalanceCount(statData.getBalanceCount() + statData.getInCount() - statData.getOutCount());
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取新的汇总数据
|
||||
*
|
||||
* @param dataList
|
||||
* @param statDayEntity
|
||||
* @param lastMonth
|
||||
* @return
|
||||
*/
|
||||
private IoStatMonthEntity getStatData(List<IoStatMonthEntity> dataList, IoStatDayEntity statDayEntity, DateTime lastMonth) {
|
||||
if (CollUtil.isNotEmpty(dataList)) {
|
||||
for (IoStatMonthEntity statMonthEntity : dataList) {
|
||||
if (statMonthEntity.getRelIdFk().equals(statDayEntity.getRelIdFk())) {
|
||||
if ((StrUtil.isNotBlank(statMonthEntity.getBatchNo()) && StrUtil.isNotBlank(statDayEntity.getRelIdFk())) && statMonthEntity.getBatchNo().equals(statDayEntity.getBatchNo())) {
|
||||
return statMonthEntity;
|
||||
}
|
||||
} else if (StrUtil.isBlank(statMonthEntity.getBatchNo()) && StrUtil.isBlank(statDayEntity.getBatchNo())) {
|
||||
return statMonthEntity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IoStatMonthEntity statData = new IoStatMonthEntity();
|
||||
statData.setYear(lastMonth.year());
|
||||
statData.setQuarter(lastMonth.quarter());
|
||||
statData.setMonth(lastMonth.monthBaseOne());
|
||||
statData.setRelIdFk(statDayEntity.getRelIdFk());
|
||||
statData.setNameCode(statDayEntity.getNameCode());
|
||||
statData.setProductName(statDayEntity.getProductName());
|
||||
statData.setGgxh(statDayEntity.getGgxh());
|
||||
statData.setBatchNo(statDayEntity.getBatchNo());
|
||||
//设置结余价格
|
||||
statData.setBalancePrice(statDayEntity.getBalancePrice());
|
||||
//设置初始化出入库数量和价格
|
||||
statData.setInCount(0);
|
||||
statData.setInPrice(statDayEntity.getInPrice());
|
||||
statData.setInAmount(BigDecimal.ZERO);
|
||||
|
||||
statData.setOutCount(0);
|
||||
statData.setOutPrice(statDayEntity.getOutPrice());
|
||||
statData.setOutAmount(BigDecimal.ZERO);
|
||||
|
||||
//设置期初数据
|
||||
IoStatMonthEntity statMonthEntity = statMonthDao.selectOne(buildQueryWrapper(statDayEntity, lastMonth));
|
||||
if (null == statMonthEntity) {
|
||||
//第一次汇总月度数据
|
||||
statData.setBeginCount(0);
|
||||
statData.setBeginPrice(statDayEntity.getBalancePrice());
|
||||
statData.setBeginAmount(BigDecimal.ZERO);
|
||||
} else {
|
||||
//已存在此产品汇总数据,基于旧数据生成新的汇总数据
|
||||
statData.setBeginCount(statMonthEntity.getBalanceCount());
|
||||
statData.setBeginPrice(statDayEntity.getBalancePrice());
|
||||
statData.setBeginAmount(statMonthEntity.getBalanceAmount());
|
||||
}
|
||||
|
||||
dataList.add(statData);
|
||||
return statData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造查询条件
|
||||
*
|
||||
* @param statDayEntity
|
||||
* @param lastMonth
|
||||
* @return
|
||||
*/
|
||||
private Wrapper<IoStatMonthEntity> buildQueryWrapper(IoStatDayEntity statDayEntity, DateTime lastMonth) {
|
||||
QueryWrapper<IoStatMonthEntity> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq("year", lastMonth.year())
|
||||
.eq("month", lastMonth.monthBaseOne() - 1) //取上上月的数据
|
||||
.eq("relIdFk", statDayEntity.getRelIdFk())
|
||||
.eq(StrUtil.isNotBlank(statDayEntity.getBatchNo()), "batchNo", statDayEntity.getBatchNo())
|
||||
.eq(StrUtil.isNotBlank(statDayEntity.getNameCode()), "nameCode", statDayEntity.getNameCode());
|
||||
return wrapper;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue