package com.lightinit.hsdatashow.impl.task;

import com.lightinit.hsdatashow.entity.AirQualityIndex;
import com.lightinit.hsdatashow.entity.AreaAirPollutionIndex;
import com.lightinit.hsdatashow.entity.HbCityAirQualityIndex;
import com.lightinit.hsdatashow.mapper.AirQualityIndexMapper;
import com.lightinit.hsdatashow.mapper.AreaAirPollutionIndexMapper;
import com.lightinit.hsdatashow.mapper.HbCityAirQualityIndexMapper;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.service.task.IGrabDataFromWebTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

/**
 * @Author: sunxiang
 * @Date: 2018/9/15 16:42
 * @Description:
 **/
@Service
public class GrabDataFromWebTaskServiceImpl implements IGrabDataFromWebTaskService {

    @Autowired
    private AirQualityIndexMapper airQualityIndexMapper;
    @Autowired
    private AreaAirPollutionIndexMapper areaAirPollutionIndexMapper;
    @Autowired
    private HbCityAirQualityIndexMapper hbCityAirQualityIndexMapper;
    @Transactional
    @Override
    public <E>ResultState insert(ArrayList<E> list) {
        ResultState resultState = new ResultState();
        try {
            for (E e : list) {
               if(e.getClass().getSimpleName().equalsIgnoreCase("AirQualityIndex")){
                   airQualityIndexMapper.insert((AirQualityIndex)e);
                }else if(e.getClass().getSimpleName().equalsIgnoreCase("AreaAirPollutionIndex")){
                   areaAirPollutionIndexMapper.insert((AreaAirPollutionIndex)e);
               }else{
                   hbCityAirQualityIndexMapper.insert((HbCityAirQualityIndex)e);
               }
            }
            return resultState;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
        resultState.setStateCode(400);
        resultState.setMsg("插入失败");
        return resultState;
    }

    @Override
    public Date getDatabaseLastDate() {
        return airQualityIndexMapper.getDatabaseLastDate();
    }

}
