package com.sw.mes.store.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.store.entity.WarehouseInventory;
import com.sw.mes.store.entity.WarehouseStocktaking;
import com.sw.mes.store.mapper.WarehouseStocktakingMapper;
import com.sw.mes.store.service.WarehouseInventoryService;
import com.sw.mes.store.service.WarehouseStocktakingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 线边仓盘点服务
 *
 * @Author：wanjia
 * @Date：2024/3/17 21:50
 */
@Service
@Transactional
public class WarehouseStocktakingServiceImpl extends ServiceImpl<WarehouseStocktakingMapper, WarehouseStocktaking> implements WarehouseStocktakingService {

    @Autowired
    private WarehouseStocktakingMapper mapper;

    @Autowired
    private WarehouseInventoryService inventoryService;

    @Override
    public Result<List<WarehouseStocktaking>> queryStocktaking(String batchNo, String serialNo, String traceId, String uniqueCode) {
        List<String> batchNoList = new ArrayList<>();
        //通过箱号和托号
        if (StringUtils.isNotBlank(serialNo) || StringUtils.isNotBlank(traceId)) {
            List<String> batchNos = inventoryService.findBatchNoBySerialNoOrTraceId(serialNo, traceId);
            if (!CollectionUtils.isEmpty(batchNos)) {
                batchNoList.addAll(batchNos);
            }
        }
        //通过批次号查询
        if (StringUtils.isNotEmpty(batchNo)) {
            batchNoList.add(batchNo);
        }
        //查库存情况
        List<WarehouseInventory> inventoryList = new ArrayList<>();
        for (String bn : batchNoList) {
            List<WarehouseInventory> tmpList = inventoryService.getByBatchNo(bn);
            if (CollectionUtils.isEmpty(tmpList)) {
                continue;
            }
            inventoryList.addAll(tmpList);
        }

        //构建结果
        List<WarehouseStocktaking> stocktakingList = new ArrayList<>(inventoryList.size());
        for (WarehouseInventory inventory : inventoryList) {
            WarehouseStocktaking stocktaking = new WarehouseStocktaking();
            stocktaking.setWarehouseId(inventory.getWarehouseId());
            stocktaking.setCount(inventory.getQuantity());
            stocktaking.setUnit(inventory.getUnitName());
            stocktaking.setBatchNo(inventory.getBatchNo());
            stocktaking.setGmtCreate(new Date());
            stocktaking.setUsername(BaseContextHandler.getUsername());
            stocktaking.setWarehouseCode(inventory.getWarehouseCode());
            stocktaking.setWarehouseName(inventory.getWarehouseName());
            stocktaking.setTraceId(inventory.getTraceid());
            stocktaking.setSerialNo(inventory.getSerialNo());
            stocktaking.setUniqueCode(inventory.getUniqueCode());
            stocktaking.setMaterialModelCode(inventory.getMaterialModelCode());
            stocktaking.setMaterialModelName(inventory.getMaterialModelName());
            stocktakingList.add(stocktaking);
        }

        return ResultGenerator.success(stocktakingList);
    }

    @Override
    public int batchInsert(List<WarehouseStocktaking> stocktakingList) {
        if (CollectionUtils.isEmpty(stocktakingList)) {
            return 0;
        }
        int count = 0;
        for (WarehouseStocktaking stocktaking : stocktakingList) {
            stocktaking.setId(null);
            stocktaking.setGmtCreate(new Date());
            stocktaking.setUsername(BaseContextHandler.getUsername());
            count += mapper.insert(stocktaking);
        }
        return count;
    }
}
