package com.hs.api.service.Impl;

import com.hs.api.mapper.BlockValuesMapper;
import com.hs.api.model.respmodel.BlockValue;
import com.hs.api.service.AsynBlockValuesService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Service
public class AsynBlockValuesServiceImpl implements AsynBlockValuesService {

    private final static Object obj = new Object();

    @Resource
    private BlockValuesMapper blockValuesMapper;

    @Async
    @Override
    public void getBlockValues(List<BlockValue> list,Long blockId,String tableName,
                               String whereClause,CountDownLatch latch) {
        try{
            System.out.println("开始" + tableName);

            List<Map<String,Object>> dataList = blockValuesMapper.selectByWhere(tableName,whereClause);
            BlockValue blockValue = new BlockValue(blockId,dataList);
            synchronized(obj){
                list.add(blockValue);
            }
            System.out.println("结束" + tableName);
        }catch(Exception e){
            log.error(e.getMessage(),e);
        }finally{
            latch.countDown();
        }


    }

    @Async
    @Override
    public void getBlockValuesNew(List<BlockValue> list,Long blockId,Map<String,String> whereClause,CountDownLatch latch) {
        try{
            List<Map<String,Object>> dataList = blockValuesMapper.selectByWhereNew(whereClause);
            log.info("开始查询" + whereClause.get("tableName") + ",数据条数：" + dataList.size() + ",查询条件：" + whereClause);
            BlockValue blockValue = new BlockValue(blockId,dataList);
            synchronized(obj){
                list.add(blockValue);
            }
        }catch(Exception e){
            log.error(e.getMessage(),e);
        }finally{
            latch.countDown();

        }


    }
}
