package com.huigou.topsun.sap.nonProducePick.application.impl;

import com.huigou.data.query.model.QueryDescriptor;
import com.huigou.data.query.model.QueryModel;
import com.huigou.topsun.sap.nonProducePick.application.NonProducePickItemApplication;
import com.huigou.topsun.sap.nonProducePick.domain.NonProducePickItem;
import com.huigou.topsun.sap.nonProducePick.domain.query.NonProducePickQueryRequest;
import com.huigou.topsun.sap.nonProducePick.repository.NonProducePickItemRepository;
import com.huigou.uasp.bmp.common.application.BaseApplication;
import com.huigou.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Auther: xin.lu
 * @Date: 2024/03/13/9:45
 * @Description:
 */
@Service("nonProducePickItemApplication")
public class NonProducePickItemApplicationImpl extends BaseApplication implements NonProducePickItemApplication {
    @Autowired
    private NonProducePickItemRepository nonProducePickItemRepository;
    @Override
    public void saveNonProducePickItems(String nonProducePickId, List<NonProducePickItem> nonProducePickItems) {
        for (NonProducePickItem nonProducePickItem : nonProducePickItems) {
            nonProducePickItem.setNonProducePickId(nonProducePickId);
            nonProducePickItemRepository.save(nonProducePickItem);
        }
    }

    @Override
    public Map<String, Object> queryNonProducePickItems(NonProducePickQueryRequest queryRequest) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtil.isNotBlank(queryRequest.getNonProducePickId())){
            QueryDescriptor queryDescriptor = this.sqlExecutorDao.getQuery(QUERY_XML_FILE_PATH, "nonProducePickItems");
            QueryModel queryModel = this.sqlExecutorDao.getQueryModel(queryDescriptor, queryRequest);
            map = this.sqlExecutorDao.executeSlicedQuery(queryModel);
        }
        return map;
    }

    @Override
    public void deleteByIds(List<String> ids) {
        ids.forEach(id->{
            nonProducePickItemRepository.delete(id);
        });
    }

    @Override
    public List<NonProducePickItem> findByNonProducePickId(String nonProducePickId) {
        return nonProducePickItemRepository.findByNonProducePickId(nonProducePickId);
    }
}
