You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-wms-java/src/main/java/com/glxp/api/service/inout/IoStatDetailService.java

236 lines
12 KiB
Java

package com.glxp.api.service.inout;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.thread.ThreadUtil;
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.common.res.BaseResponse;
import com.glxp.api.common.util.ResultVOUtils;
import com.glxp.api.constant.ConstantType;
import com.glxp.api.dao.inout.*;
import com.glxp.api.entity.inout.IoStatDayEntity;
import com.glxp.api.entity.inout.IoStatDetailEntity;
import com.glxp.api.entity.inout.IoStatMonthEntity;
import com.glxp.api.entity.inout.IoStatOrderEntity;
import com.glxp.api.req.inout.FilterStatDataDetailRequest;
import com.glxp.api.res.inv.IoOrderDetailStatRsponse;
import com.glxp.api.util.GennerOrderUtils;
import com.glxp.api.util.IntUtil;
import com.glxp.api.util.MsDateUtil;
import com.glxp.api.util.OrderNoTypeBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class IoStatDetailService extends ServiceImpl<IoStatDetailMapper, IoStatDetailEntity> {
@Resource
IoStatDetailMapper statDetailMapper;
@Resource
private IoStatOrderDao statOrderDao;
@Resource
private GennerOrderUtils gennerOrderUtils;
public List<IoStatDetailEntity> filterList(FilterStatDataDetailRequest statDataDetailRequest) {
if (null == statDataDetailRequest) {
return Collections.emptyList();
}
if (null != statDataDetailRequest.getPage() && null != statDataDetailRequest.getLimit()) {
PageHelper.offsetPage((statDataDetailRequest.getPage() - 1) * statDataDetailRequest.getLimit(), statDataDetailRequest.getLimit());
}
return statDetailMapper.filterList(statDataDetailRequest);
}
public List<IoStatDetailEntity> filterListByRecordKey(String recordKey) {
return statDetailMapper.filterListByRecordKey(recordKey);
}
@Resource
private IoOrderDao orderDao;
@Resource
private IoOrderDetailResultDao ioOrderDetailResultDao;
@Resource
private SqlSessionFactory sqlSessionFactory;
/**
*
*/
public BaseResponse<String> createCustomStat(FilterStatDataDetailRequest detailRequest) {
//1.判断是否存在相同汇总
Boolean b = statOrderDao.exists(new QueryWrapper<IoStatOrderEntity>()
.eq(StrUtil.isNotEmpty(detailRequest.getInvCode()), "invCode", detailRequest.getInvCode())
.eq("startDate", detailRequest.getStartDate())
.eq("endDate", detailRequest.getEndDate())
.eq("statType", detailRequest.getStatType())
);
if (b) {
return ResultVOUtils.error(500, "已存在相同出入库明细汇总!");
}
IoStatOrderEntity statOrderEntity = new IoStatOrderEntity();
OrderNoTypeBean orderNoTypeBean = new OrderNoTypeBean("STATC", "yyyyMMdd");
String orderNo = gennerOrderUtils.createStatOrderNo(orderNoTypeBean);
statOrderEntity.setRecordKey(orderNo);
statOrderEntity.setStartDate(MsDateUtil.parseDate(detailRequest.getStartDate()));
statOrderEntity.setEndDate(MsDateUtil.parseDate(detailRequest.getEndDate()));
statOrderEntity.setUpdateTime(new Date());
statOrderEntity.setStatus(1);
statOrderEntity.setType(4);
statOrderEntity.setStatType(detailRequest.getStatType());
statOrderEntity.setInvCode(detailRequest.getInvCode());
statOrderEntity.setRemark(detailRequest.getRemark());
statOrderDao.insert(statOrderEntity);
ThreadUtil.execAsync(() -> {
try {
List<IoStatDetailEntity> dataList = new ArrayList<>();
List<String> orderIdFkList = orderDao.selectOrderfirstAndLastIdList(
detailRequest.getStartDate(),
detailRequest.getEndDate());
List<IoOrderDetailStatRsponse> orderDetailResultEntities = new ArrayList<>();
if (orderIdFkList.size() > 0) {
orderDetailResultEntities = ioOrderDetailResultDao.selectStatDataByTime(orderIdFkList);
if (CollUtil.isNotEmpty(orderDetailResultEntities)) {
orderDetailResultEntities.forEach(orderDetailResultEntity -> {
IoStatDetailEntity statData = getStatData(detailRequest.getStatType(), dataList, orderDetailResultEntity);
if (orderDetailResultEntity.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 (orderDetailResultEntity.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()));
}
}
statData.setBalanceCount(IntUtil.value(statData.getBalanceCount()) + statData.getInCount() - statData.getOutCount());
if (statData.getBalanceAmount() == null) {
statData.setBalanceAmount(new BigDecimal(0));
}
statData.setBalanceAmount(statData.getBalanceAmount().add(statData.getInAmount()).subtract(statData.getOutAmount()));
statData.setBalancePrice(statData.getOutPrice());
}
);
}
}
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, TransactionIsolationLevel.READ_COMMITTED);
IoStatDetailMapper mapper = sqlSession.getMapper(IoStatDetailMapper.class);
dataList.forEach(statDetailEntity -> {
statDetailEntity.setUpdateTime(new Date());
statDetailEntity.setRecordKeyFk(orderNo);
//计算结余数据
statDetailEntity.setBalanceCount(statDetailEntity.getInCount() - statDetailEntity.getOutCount());
statDetailEntity.setBalanceAmount(statDetailEntity.getInAmount().subtract(statDetailEntity.getOutAmount()));
mapper.insert(statDetailEntity);
});
sqlSession.commit();
sqlSession.close();
statOrderEntity.setStatus(2);
statOrderDao.updateById(statOrderEntity);
} catch (Exception e) {
log.error(ExceptionUtils.getStackTrace(e));
statOrderEntity.setStatus(3);
statOrderDao.updateById(statOrderEntity);
}
});
return ResultVOUtils.success("后台正在生成,请稍后刷新!");
}
private IoStatDetailEntity getStatData(Integer statType, List<IoStatDetailEntity> dataList, IoOrderDetailStatRsponse orderDetailResultEntity) {
if (CollUtil.isNotEmpty(dataList)) {
for (IoStatDetailEntity statDayEntity : dataList) {
if (IntUtil.value(statType) == 1) {
if (statDayEntity.getRelIdFk().equals(orderDetailResultEntity.getBindRlFk()) && statDayEntity.getInvCode().equals(orderDetailResultEntity.getInvCode())) {
return statDayEntity;
}
} else {
if (statDayEntity.getRelIdFk().equals(orderDetailResultEntity.getBindRlFk()) && statDayEntity.getInvCode().equals(orderDetailResultEntity.getInvCode())) {
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;
}
}
}
}
}
IoStatDetailEntity statData = new IoStatDetailEntity();
statData.setInvCode(orderDetailResultEntity.getInvCode());
statData.setDeptCode(orderDetailResultEntity.getDeptCode());
statData.setRelIdFk(String.valueOf(orderDetailResultEntity.getBindRlFk()));
statData.setNameCode(orderDetailResultEntity.getNameCode());
statData.setProductName(orderDetailResultEntity.getCoName());
statData.setGgxh(orderDetailResultEntity.getSpec());
statData.setBatchNo(orderDetailResultEntity.getBatchNo());
//设置结余价格
statData.setBalancePrice(orderDetailResultEntity.getPrice());
//设置初始化出入库数量和价格
statData.setInCount(0);
statData.setInPrice(null == orderDetailResultEntity.getPrice() ? BigDecimal.ZERO : orderDetailResultEntity.getPrice());
statData.setInAmount(BigDecimal.ZERO);
statData.setOutCount(0);
statData.setOutPrice(null == orderDetailResultEntity.getPrice() ? BigDecimal.ZERO : orderDetailResultEntity.getPrice());
statData.setOutAmount(BigDecimal.ZERO);
dataList.add(statData);
return statData;
}
/**
*
*
* @param ioOrderDetailResultEntity
* @param lastMonth
* @return
*/
private Wrapper<IoStatMonthEntity> buildQueryWrapper(IoOrderDetailStatRsponse ioOrderDetailResultEntity, DateTime lastMonth) {
QueryWrapper<IoStatMonthEntity> wrapper = new QueryWrapper<>();
if (lastMonth.monthBaseOne() == 1) {
//当前汇总的记录为1月上一月份的时间取前一年的12月
wrapper.eq("year", lastMonth.year() - 1)
.eq("month", 12);
} else {
wrapper.eq("year", lastMonth.year())
.eq("month", lastMonth.monthBaseOne() - 1);//取上上月的数据
}
wrapper.eq("relIdFk", ioOrderDetailResultEntity.getBindRlFk());
wrapper.eq(StrUtil.isNotBlank(ioOrderDetailResultEntity.getDeptCode()), "deptCode", ioOrderDetailResultEntity.getDeptCode());
wrapper.eq(StrUtil.isNotBlank(ioOrderDetailResultEntity.getBatchNo()), "batchNo", ioOrderDetailResultEntity.getBatchNo());
wrapper.eq(StrUtil.isNotBlank(ioOrderDetailResultEntity.getNameCode()), "nameCode", ioOrderDetailResultEntity.getNameCode())
.eq(StrUtil.isNotBlank(ioOrderDetailResultEntity.getNameCode()), "invCode", ioOrderDetailResultEntity.getInvCode());
return wrapper;
}
}