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

import cn.com.countrygarden.bi.main.dao.phoenix.PhoenixSalesOnSaleRoomMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.phoenix.PhoenixSalesOnSaleRoom;
import cn.com.countrygarden.bi.main.model.phoenix.PhoenixSalesProject;
import cn.com.countrygarden.bi.main.model.phoenix.PhoenixSalesRoom;
import cn.com.countrygarden.bi.main.service.phoenix.PhoenixSalesHttpClientService;
import cn.com.countrygarden.bi.main.service.phoenix.PhoenixSalesOnSalesRoomService;
import cn.com.countrygarden.bi.main.service.phoenix.PhoenixSalesProjectService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class PhoenixSalesOnSalesRoomServiceImpl implements PhoenixSalesOnSalesRoomService {

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

    @Autowired
    private PhoenixSalesHttpClientService httpClientService;

    @Autowired
    private PhoenixSalesOnSaleRoomMapper mapper;

    @Autowired
    private PhoenixSalesProjectService projectService;


    @Override
    public List<PhoenixSalesOnSaleRoom> sync(String areaName, String start, String end, int page, int pageSize) {
        Map<String, Object> params = new HashMap<String, Object>() {{
            put("areaname", areaName);
            put("yjslDateStart", start);
            put("yjslDateEnd", end);
            put("page", page);
            put("size", pageSize);
        }};
        String responsePackage = httpClientService.post("GetSaleRoomList", params);
        List<PhoenixSalesOnSaleRoom> items = JSON.parseArray(responsePackage, PhoenixSalesOnSaleRoom.class);
        return items;
    }

    @Override
    public List<PhoenixSalesOnSaleRoom> persistence(String areaId, String start, String end, int page, int pageSize) {
        PhoenixSalesProject project = projectService.findOne(areaId);
        if (project == null) {
            return null;
        }
        List<PhoenixSalesOnSaleRoom> items = sync(project.getAreaname(), start, end, page, pageSize);

        if (items.isEmpty()) {
            return null;
        }

        mapper.persistence(items, areaId);

        return items;
    }

    @Override
    public void persistenceBatch(String start, String end, int page, int pageSize, int projectPage, int projectPageSize) {
        PageData pageData = projectService.search(projectPage, projectPageSize);
        List<PhoenixSalesProject> projects = (ArrayList) pageData.getData();

        for (PhoenixSalesProject project : projects) {
            System.out.println("PhoenixSalesProject###################### for onSale Room: " + project.getAreaname());
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {

            }
            persistence(project.getAreaid(), start, end, page, pageSize);
        }
    }
}
