|
|
|
@ -7,13 +7,11 @@ import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
|
|
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.entity.inout.IoStatOrderEntity;
|
|
|
|
|
import com.glxp.api.constant.ConstantType;
|
|
|
|
|
import com.glxp.api.dao.inout.*;
|
|
|
|
|
import com.glxp.api.entity.inout.*;
|
|
|
|
|
import com.glxp.api.req.inout.FilterStatDataDetailRequest;
|
|
|
|
|
import com.glxp.api.res.inv.DateRequest;
|
|
|
|
|
import com.glxp.api.service.inout.IoStatMonthService;
|
|
|
|
|
import com.glxp.api.util.GennerOrderUtils;
|
|
|
|
|
import com.glxp.api.util.OrderNoTypeBean;
|
|
|
|
@ -27,7 +25,10 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
@ -44,36 +45,65 @@ public class IoStatMonthServiceImpl implements IoStatMonthService {
|
|
|
|
|
private IoStatOrderDao statOrderDao;
|
|
|
|
|
@Resource
|
|
|
|
|
private SqlSessionFactory sqlSessionFactory;
|
|
|
|
|
@Resource
|
|
|
|
|
private IoOrderDao orderDao;
|
|
|
|
|
@Resource
|
|
|
|
|
private IoOrderDetailResultDao ioOrderDetailResultDao;
|
|
|
|
|
|
|
|
|
|
@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)) {
|
|
|
|
|
//汇总上一个月的数据
|
|
|
|
|
DateRequest dateRequest = getYearAndMonth(1);
|
|
|
|
|
DateRequest dateRequest1 = getfirstDayAndLastDay(dateRequest.getYear(), dateRequest.getMomth());
|
|
|
|
|
List<IoStatMonthEntity> dataList = new ArrayList<>();
|
|
|
|
|
//查询单号
|
|
|
|
|
List<String> orderIdFkList = new ArrayList<>();
|
|
|
|
|
orderIdFkList = orderDao.selectOrderfirstAndLastIdList(dateRequest1.getFirstDay(), dateRequest1.getLastDay());
|
|
|
|
|
List<IoOrderDetailResultEntity> orderDetailResultEntities = new ArrayList<>();
|
|
|
|
|
if(orderIdFkList.size()>0){
|
|
|
|
|
orderDetailResultEntities = ioOrderDetailResultDao.selectStatDataByTime(orderIdFkList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (CollUtil.isNotEmpty(orderDetailResultEntities)) {
|
|
|
|
|
//声明用于存放汇总数据的集合
|
|
|
|
|
List<IoStatMonthEntity> dataList = new ArrayList<>();
|
|
|
|
|
statDayList.forEach(statDayEntity -> {
|
|
|
|
|
orderDetailResultEntities.forEach(orderDetailResultEntity -> {
|
|
|
|
|
//获取新的汇总数据
|
|
|
|
|
IoStatMonthEntity statData = getStatData(dataList, statDayEntity, lastMonth);
|
|
|
|
|
IoStatMonthEntity statData = getStatData(dataList, orderDetailResultEntity, lastMonth);
|
|
|
|
|
|
|
|
|
|
//设置入库数据
|
|
|
|
|
statData.setInCount(statData.getInCount() + statDayEntity.getInCount());
|
|
|
|
|
statData.setInPrice(statDayEntity.getBalancePrice());
|
|
|
|
|
statData.setInAmount(statData.getInAmount().add(statDayEntity.getInAmount()));
|
|
|
|
|
|
|
|
|
|
//设置出库数据
|
|
|
|
|
statData.setOutCount(statData.getOutCount() + statDayEntity.getOutCount());
|
|
|
|
|
statData.setOutPrice(statDayEntity.getBalancePrice());
|
|
|
|
|
statData.setOutAmount(statData.getOutAmount().add(statDayEntity.getOutAmount()));
|
|
|
|
|
//查询此单是出库还是入库
|
|
|
|
|
IoOrderEntity order = orderDao.selectOne(new QueryWrapper<IoOrderEntity>().select("mainAction").eq("billNo", orderDetailResultEntity.getOrderIdFk()));
|
|
|
|
|
if (order.getMainAction().equals(ConstantType.TYPE_PUT)) {
|
|
|
|
|
//入库
|
|
|
|
|
statData.setInCount(orderDetailResultEntity.getReCount() + statData.getInCount());
|
|
|
|
|
if (null != orderDetailResultEntity.getPrice()) {
|
|
|
|
|
BigDecimal inAmount = orderDetailResultEntity.getPrice().multiply(BigDecimal.valueOf(orderDetailResultEntity.getReCount()));
|
|
|
|
|
statData.setInAmount(inAmount.add(statData.getInAmount()));
|
|
|
|
|
}
|
|
|
|
|
} else if (order.getMainAction().equals(ConstantType.TYPE_OUT)) {
|
|
|
|
|
//出库
|
|
|
|
|
statData.setOutCount(orderDetailResultEntity.getReCount() + statData.getOutCount());
|
|
|
|
|
if (null != orderDetailResultEntity.getPrice()) {
|
|
|
|
|
BigDecimal outAmount = orderDetailResultEntity.getPrice().multiply(BigDecimal.valueOf(orderDetailResultEntity.getReCount()));
|
|
|
|
|
statData.setOutAmount(outAmount.add(statData.getOutAmount()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//汇总上一天存在上上一天不存在产品的汇总数据
|
|
|
|
|
//获取上上天的数据
|
|
|
|
|
DateTime twoDay = new DateTime();
|
|
|
|
|
QueryWrapper<IoStatMonthEntity> ew = new QueryWrapper<>();
|
|
|
|
|
ew.eq("year", twoDay.year());
|
|
|
|
|
ew.eq("quarter", twoDay.quarter());
|
|
|
|
|
ew.eq("month", twoDay.monthBaseOne()-2);
|
|
|
|
|
List<IoStatMonthEntity> ioStatMonthEntityList=statMonthDao.selectList(ew);
|
|
|
|
|
List<IoStatMonthEntity> newOrOldDate=getNewOrOldDate(dataList,ioStatMonthEntityList);
|
|
|
|
|
dataList.addAll(newOrOldDate);
|
|
|
|
|
|
|
|
|
|
//插入汇总记录
|
|
|
|
|
saveData(lastMonth, dataList);
|
|
|
|
|
}
|
|
|
|
@ -137,19 +167,20 @@ public class IoStatMonthServiceImpl implements IoStatMonthService {
|
|
|
|
|
* 获取新的汇总数据
|
|
|
|
|
*
|
|
|
|
|
* @param dataList
|
|
|
|
|
* @param statDayEntity
|
|
|
|
|
* @param orderDetailResultEntity
|
|
|
|
|
* @param lastMonth
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private IoStatMonthEntity getStatData(List<IoStatMonthEntity> dataList, IoStatDayEntity statDayEntity, DateTime lastMonth) {
|
|
|
|
|
private IoStatMonthEntity getStatData(List<IoStatMonthEntity> dataList, IoOrderDetailResultEntity orderDetailResultEntity, 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;
|
|
|
|
|
for (IoStatMonthEntity statDayEntity : dataList) {
|
|
|
|
|
if (Long.valueOf(statDayEntity.getRelIdFk()).equals(orderDetailResultEntity.getBindRlFk())) {
|
|
|
|
|
if ((StrUtil.isNotBlank(statDayEntity.getBatchNo()) && StrUtil.isNotBlank(orderDetailResultEntity.getBatchNo())) && statDayEntity.getBatchNo().equals(orderDetailResultEntity.getBatchNo())) {
|
|
|
|
|
return statDayEntity;
|
|
|
|
|
} else if (StrUtil.isBlank(statDayEntity.getBatchNo()) && StrUtil.isBlank(orderDetailResultEntity.getBatchNo())) {
|
|
|
|
|
return statDayEntity;
|
|
|
|
|
}
|
|
|
|
|
} else if (StrUtil.isBlank(statMonthEntity.getBatchNo()) && StrUtil.isBlank(statDayEntity.getBatchNo())) {
|
|
|
|
|
return statMonthEntity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -158,28 +189,28 @@ public class IoStatMonthServiceImpl implements IoStatMonthService {
|
|
|
|
|
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.setRelIdFk(String.valueOf(orderDetailResultEntity.getBindRlFk()));
|
|
|
|
|
statData.setNameCode(orderDetailResultEntity.getNameCode());
|
|
|
|
|
statData.setProductName(orderDetailResultEntity.getCoName());
|
|
|
|
|
statData.setGgxh(orderDetailResultEntity.getSpec());
|
|
|
|
|
statData.setBatchNo(orderDetailResultEntity.getBatchNo());
|
|
|
|
|
//设置结余价格
|
|
|
|
|
statData.setBalancePrice(statDayEntity.getBalancePrice());
|
|
|
|
|
statData.setBalancePrice(orderDetailResultEntity.getPrice());
|
|
|
|
|
//设置初始化出入库数量和价格
|
|
|
|
|
statData.setInCount(0);
|
|
|
|
|
statData.setInPrice(statDayEntity.getInPrice());
|
|
|
|
|
statData.setInPrice(null == orderDetailResultEntity.getPrice() ? BigDecimal.ZERO : orderDetailResultEntity.getPrice());
|
|
|
|
|
statData.setInAmount(BigDecimal.ZERO);
|
|
|
|
|
|
|
|
|
|
statData.setOutCount(0);
|
|
|
|
|
statData.setOutPrice(statDayEntity.getOutPrice());
|
|
|
|
|
statData.setOutPrice(null == orderDetailResultEntity.getPrice() ? BigDecimal.ZERO : orderDetailResultEntity.getPrice());
|
|
|
|
|
statData.setOutAmount(BigDecimal.ZERO);
|
|
|
|
|
|
|
|
|
|
//设置期初数据
|
|
|
|
|
IoStatMonthEntity statMonthEntity = statMonthDao.selectOne(buildQueryWrapper(statDayEntity, lastMonth));
|
|
|
|
|
IoStatMonthEntity statMonthEntity = statMonthDao.selectOne(buildQueryWrapper(orderDetailResultEntity, lastMonth));
|
|
|
|
|
if (null == statMonthEntity) {
|
|
|
|
|
//第一次汇总月度数据
|
|
|
|
|
statData.setBeginCount(0);
|
|
|
|
|
statData.setBeginPrice(statDayEntity.getBalancePrice());
|
|
|
|
|
statData.setBeginPrice(null == orderDetailResultEntity.getPrice() ? BigDecimal.ZERO : orderDetailResultEntity.getPrice());
|
|
|
|
|
statData.setBeginAmount(BigDecimal.ZERO);
|
|
|
|
|
|
|
|
|
|
statData.setBalanceCount(0);
|
|
|
|
@ -190,12 +221,12 @@ public class IoStatMonthServiceImpl implements IoStatMonthService {
|
|
|
|
|
statData.setBeginPrice(statMonthEntity.getBalancePrice());
|
|
|
|
|
statData.setBeginAmount(statMonthEntity.getBalanceAmount());
|
|
|
|
|
|
|
|
|
|
statData.setBalanceCount(statDayEntity.getBalanceCount());
|
|
|
|
|
statData.setBalanceCount(statMonthEntity.getBalanceCount());
|
|
|
|
|
statData.setBalanceAmount(statMonthEntity.getBalanceAmount());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//设置结余价格
|
|
|
|
|
statData.setBalancePrice(statDayEntity.getBalancePrice());
|
|
|
|
|
statData.setBalancePrice(statData.getBeginPrice());
|
|
|
|
|
|
|
|
|
|
dataList.add(statData);
|
|
|
|
|
return statData;
|
|
|
|
@ -204,11 +235,11 @@ public class IoStatMonthServiceImpl implements IoStatMonthService {
|
|
|
|
|
/**
|
|
|
|
|
* 构造查询条件
|
|
|
|
|
*
|
|
|
|
|
* @param statDayEntity
|
|
|
|
|
* @param ioOrderDetailResultEntity
|
|
|
|
|
* @param lastMonth
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Wrapper<IoStatMonthEntity> buildQueryWrapper(IoStatDayEntity statDayEntity, DateTime lastMonth) {
|
|
|
|
|
private Wrapper<IoStatMonthEntity> buildQueryWrapper(IoOrderDetailResultEntity ioOrderDetailResultEntity, DateTime lastMonth) {
|
|
|
|
|
QueryWrapper<IoStatMonthEntity> wrapper = new QueryWrapper<>();
|
|
|
|
|
if (lastMonth.monthBaseOne() == 1) {
|
|
|
|
|
//当前汇总的记录为1月,上一月份的时间取前一年的12月
|
|
|
|
@ -219,9 +250,68 @@ public class IoStatMonthServiceImpl implements IoStatMonthService {
|
|
|
|
|
.eq("month", lastMonth.monthBaseOne() - 1);//取上上月的数据
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wrapper.eq("relIdFk", statDayEntity.getRelIdFk())
|
|
|
|
|
.eq(StrUtil.isNotBlank(statDayEntity.getBatchNo()), "batchNo", statDayEntity.getBatchNo())
|
|
|
|
|
.eq(StrUtil.isNotBlank(statDayEntity.getNameCode()), "nameCode", statDayEntity.getNameCode());
|
|
|
|
|
wrapper.eq("relIdFk", ioOrderDetailResultEntity.getBindRlFk());
|
|
|
|
|
wrapper.eq(StrUtil.isNotBlank(ioOrderDetailResultEntity.getBatchNo()), "batchNo", ioOrderDetailResultEntity.getBatchNo());
|
|
|
|
|
wrapper.eq(StrUtil.isNotBlank(ioOrderDetailResultEntity.getNameCode()), "nameCode", ioOrderDetailResultEntity.getNameCode());
|
|
|
|
|
return wrapper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<IoStatMonthEntity> getNewOrOldDate(List<IoStatMonthEntity> newIoStatDayEntity, List<IoStatMonthEntity> oldIoStatDayEntity) {
|
|
|
|
|
|
|
|
|
|
// 计算出两个集合之间产品批次号组成的差异
|
|
|
|
|
// List<IoStatMonthEntity> ioStatMonthEntityList = oldIoStatDayEntity.stream().filter(notComment -> {
|
|
|
|
|
// List<IoStatMonthEntity> filtered = newIoStatDayEntity.stream().filter(all -> all.getBatchNo().equals(notComment.getBatchNo()) && all.getRelIdFk().equals(notComment.getRelIdFk())).collect(Collectors.toList());
|
|
|
|
|
// return filtered.isEmpty();
|
|
|
|
|
// }).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
List<IoStatMonthEntity> ioStatMonthEntityList = oldIoStatDayEntity.stream()
|
|
|
|
|
.filter(notComment -> {
|
|
|
|
|
List<IoStatMonthEntity> filtered = newIoStatDayEntity.stream().filter(all ->
|
|
|
|
|
Objects.equals(all.getBatchNo(), notComment.getBatchNo()) // 使用 Objects.equals 来进行非空判断
|
|
|
|
|
&& Objects.equals(all.getRelIdFk(), notComment.getRelIdFk()))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
return filtered.isEmpty();
|
|
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
for (IoStatMonthEntity ioStatDayEntity : ioStatMonthEntityList) {
|
|
|
|
|
ioStatDayEntity.setId(null);
|
|
|
|
|
ioStatDayEntity.setBeginCount(ioStatDayEntity.getBalanceCount());
|
|
|
|
|
ioStatDayEntity.setBalanceAmount(ioStatDayEntity.getBalanceAmount());
|
|
|
|
|
ioStatDayEntity.setBeginPrice(ioStatDayEntity.getBalancePrice());
|
|
|
|
|
ioStatDayEntity.setInCount(0);
|
|
|
|
|
ioStatDayEntity.setOutCount(0);
|
|
|
|
|
}
|
|
|
|
|
return ioStatMonthEntityList;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取目标月份的起始和结束时间
|
|
|
|
|
private DateRequest getfirstDayAndLastDay(int year, int month) {
|
|
|
|
|
|
|
|
|
|
LocalDate firstDayOfMonth = LocalDate.of(year, month, 1);
|
|
|
|
|
LocalDate lastDayOfMonth = firstDayOfMonth.withDayOfMonth(firstDayOfMonth.lengthOfMonth());
|
|
|
|
|
|
|
|
|
|
String pattern = "yyyy-MM-dd";
|
|
|
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
|
|
|
|
|
DateRequest dateRequest = new DateRequest();
|
|
|
|
|
dateRequest.setLastDay(firstDayOfMonth.format(formatter));
|
|
|
|
|
dateRequest.setFirstDay(lastDayOfMonth.format(formatter));
|
|
|
|
|
return dateRequest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取指定的月份和年份
|
|
|
|
|
private DateRequest getYearAndMonth(int lastNum) {
|
|
|
|
|
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
|
int year = calendar.get(Calendar.YEAR); // 获取当前年份
|
|
|
|
|
int month = calendar.get(Calendar.MONTH) + 1 - lastNum;
|
|
|
|
|
if (month < 1) { // 如果当前是一月份,则上一个月份是去年 12 月
|
|
|
|
|
year--;
|
|
|
|
|
month = 12 - month;
|
|
|
|
|
}
|
|
|
|
|
DateRequest dateRequest = new DateRequest();
|
|
|
|
|
dateRequest.setYear(year);
|
|
|
|
|
dateRequest.setMomth(month);
|
|
|
|
|
return dateRequest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|