package cn.com.countrygarden.bi.main.service.phoenix.impl;

import cn.com.countrygarden.bi.main.dao.phoenix.PhoenixSalesOnSaleProjectMapper;
import cn.com.countrygarden.bi.main.model.phoenix.PhoenixSalesOnSaleProject;
import cn.com.countrygarden.bi.main.service.phoenix.PhoenixSalesHttpClientService;
import cn.com.countrygarden.bi.main.service.phoenix.PhoenixSalesOnSaleProjectService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;

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

@Service
public class PhoenixSalesOnSaleProjectServiceImpl implements PhoenixSalesOnSaleProjectService {

    private final Logger logger = LoggerFactory.getLogger(PhoenixSalesOnSaleProjectServiceImpl.class);

    @Autowired
    private PhoenixSalesHttpClientService httpClientService;

    @Autowired
    private PhoenixSalesOnSaleProjectMapper onSaleProjectMapper;

    @Autowired

    private DataSourceTransactionManager txManager;


    @Override
    public List<PhoenixSalesOnSaleProject> sync(String start, String end, String roomSort) {
        Map<String, Object> params = new HashMap<String, Object>() {{
            put("yszDateStart", start);
            put("yszDateEnd", end);
            put("roomSort", roomSort);
        }};
        String responsePackage = httpClientService.post("GetSaleAreaList", params);
        List<PhoenixSalesOnSaleProject> onSaleProjects = JSON.parseArray(responsePackage, PhoenixSalesOnSaleProject.class);
        return onSaleProjects;
    }

    @Override
    public List<PhoenixSalesOnSaleProject> persistence(String start, String end, String roomSort) {
        List<PhoenixSalesOnSaleProject> onSaleProjects = sync(start, end, roomSort);

        onSaleProjectMapper.truncate();
        onSaleProjectMapper.persistence(onSaleProjects);

        return onSaleProjects;
    }
}
