package com.zg.project.Inventory.AutoInventory.service.impl;

import com.zg.framework.web.page.TableDataInfo;
import com.zg.project.Inventory.Task.mapper.InventoryTaskMapper;
import com.zg.project.Inventory.domain.dto.MatchScanPageDTO;
import com.zg.project.Inventory.domain.entity.InventoryMatchScan;
import com.zg.project.Inventory.AutoInventory.mapper.InventoryMatchScanMapper;
import com.zg.project.Inventory.AutoInventory.service.InventoryMatchScanService;
import com.zg.project.Inventory.domain.vo.InventoryMatchScanSimpleVO;
import com.zg.project.Inventory.domain.vo.InventoryMatchScanVO;
import com.zg.project.Inventory.domain.vo.RkInfoMatchVO;
import com.zg.project.wisdom.mapper.RkInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InventoryMatchScanServiceImpl implements InventoryMatchScanService {

    @Autowired
    private InventoryMatchScanMapper mapper;

    @Autowired
    private InventoryTaskMapper taskMapper;

    @Autowired
    private RkInfoMapper rkInfoMapper;

    @Override
    public void saveBatch(List<InventoryMatchScan> list) {
        mapper.insertBatch(list);
    }

    @Override
    public Map<String, Object> getAll() {

        List<InventoryMatchScan> list = mapper.getAll();


        Map<String, Object> resultMap = new HashMap<>();

        resultMap.put("list", list);


        return resultMap;
    }

    @Override
    public List<InventoryMatchScanVO> getByTaskName(MatchScanPageDTO dto) {
        return mapper.getByTaskName(dto);
    }

    @Override
    public List<InventoryMatchScan> selectInventoryMatchScanList(InventoryMatchScan matchScan) {
        return mapper.selectInventoryMatchScanList(matchScan);
    }

    @Override
    public List<RkInfoMatchVO> selectMatchScanCountList(InventoryMatchScan param) {
        // 状态=2：只查扫描表中的数据
        if ("2".equals(param.getStatus())) {
            return mapper.selectOnlyFromMatchScan(param);
        }
        // 状态=1：查询指定仓库下未被扫描的库位
        else if ("1".equals(param.getStatus())) {
            // ✅ 根据任务ID取 sceneId，再按 scene 查询“未被扫描”的库位
            String sceneId = taskMapper.getSceneByTaskId(param.getTaskId());
            return rkInfoMapper.getUnscannedPcodeByScene(sceneId, param.getTaskId());
        }
        // 其它情况：返回扫描表和库存表的关联数据
        else {
            return mapper.selectJoinRkInfo(param);
        }
    }

}
