package com.sunda.spmswms.service.impl;

import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmswms.entity.WhsStorageInventoryLastMonth;
import com.sunda.spmswms.mapper.WhsStorageInventoryLastMonthMapper;
import com.sunda.spmswms.service.IWhsStorageInventoryLastMonthService;

import lombok.extern.log4j.Log4j;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 上月底库存结余数量表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2023-01-18
 */
@Log4j
@Service
public class WhsStorageInventoryLastMonthServiceImpl extends ServiceImpl<WhsStorageInventoryLastMonthMapper, WhsStorageInventoryLastMonth> implements IWhsStorageInventoryLastMonthService {

	@Override
	public ResponseResult getWhsInventoryLastMonth() {
		try {
			// 查询CN01工厂下的物料库存数量、备件的库存数量（备件数量以交货单号进行汇总）
			List<WhsStorageInventoryLastMonth> materialList = this.baseMapper.selectMaterialDeliveryInventory();
			// 删除上次的数据
			this.baseMapper.deleteBatch();
			if (CollectionUtils.isNotEmpty(materialList)) {
				this.saveBatch(materialList);
			}
			return ResponseResult.success().add("list", materialList);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("每月底汇总库存数量任务失败："+e.getMessage());
			return ResponseResult.error("每月底汇总库存数量任务失败："+e.getMessage());
		}
	}

}
