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

import com.huigou.data.query.model.QueryDescriptor;
import com.huigou.data.query.model.QueryModel;
import com.huigou.topsun.sap.clientMaterials.application.ClientMaterialsItemApplication;
import com.huigou.topsun.sap.clientMaterials.domain.ClientMaterialsItem;
import com.huigou.topsun.sap.clientMaterials.domain.query.ClientMaterialsQueryRequest;
import com.huigou.topsun.sap.clientMaterials.repository.ClientMaterialsItemRepository;
import com.huigou.topsun.sap.componentSupply.application.ComponentSupplyItemApplication;
import com.huigou.topsun.sap.componentSupply.domain.ComponentSupplyItem;
import com.huigou.topsun.sap.componentSupply.domain.query.ComponentSupplyQueryRequest;
import com.huigou.topsun.sap.componentSupply.repository.ComponentSupplyItemRepository;
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: LXH
 * @Date: 2024/07/25/10:16
 * @Description:
 */
@Service("clientMaterialsItemApplication")
public class ClientMaterialsItemApplicationImpl extends BaseApplication implements ClientMaterialsItemApplication {
    @Autowired
    private ClientMaterialsItemRepository clientMaterialsItemRepository;
    @Override
    public void saveClientMaterialsItems(String clientMaterialsId, List<ClientMaterialsItem> items){
        for (ClientMaterialsItem item : items) {
            item.setClientMaterialsId(clientMaterialsId);
            clientMaterialsItemRepository.save(item);
        }
    }

    @Override
    public Map<String, Object> queryClientMaterialsItems(ClientMaterialsQueryRequest queryRequest){
        Map<String, Object> map = new HashMap<>();
        if (StringUtil.isNotBlank(queryRequest.getClientMaterialsId())){
            QueryDescriptor queryDescriptor = this.sqlExecutorDao.getQuery(QUERY_XML_FILE_PATH, "clientMaterialsItems");

            QueryModel queryModel = this.sqlExecutorDao.getQueryModel(queryDescriptor, queryRequest);
            if (queryRequest.getClientMaterialsId() != null) {
                queryModel.addCriteria(" and t.client_materials_id =:clientMaterialsId");
                queryModel.putParam("clientMaterialsId", queryRequest.getClientMaterialsId());
            }
           // queryModel.putDictionary("unit", DictUtil.getDictionary("ComponentSupplyUnit"));
            map = this.sqlExecutorDao.executeSlicedQuery(queryModel);
        }
        return map;
    }

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

    @Override
    public List<ClientMaterialsItem> findByClientMaterialsId(String clientMaterialsId) {
        return clientMaterialsItemRepository.findByClientMaterialsId(clientMaterialsId);
    }
}
