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

import cn.com.countrygarden.bi.main.dao.phoenix.PhoenixSalesRoomMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.phoenix.PhoenixSalesBuild;
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.PhoenixSalesBuildService;
import cn.com.countrygarden.bi.main.service.phoenix.PhoenixSalesHttpClientService;
import cn.com.countrygarden.bi.main.service.phoenix.PhoenixSalesProjectService;
import cn.com.countrygarden.bi.main.service.phoenix.PhoenixSalesRoomService;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
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;
import java.util.stream.Collectors;

@Service
@Transactional
public class PhoenixSalesRoomServiceImpl implements PhoenixSalesRoomService {

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

    @Autowired
    private PhoenixSalesHttpClientService httpClientService;

    @Autowired
    private PhoenixSalesRoomMapper mapper;

    @Autowired
    private PhoenixSalesProjectService projectService;

    @Autowired
    private PhoenixSalesBuildService buildService;


    @Override
    public List<PhoenixSalesRoom> sync(String areaId, long timestamp) {
        Map<String, Object> params = new HashMap<String, Object>() {{
            put("areaid", areaId);
            put("timestamp", timestamp);
        }};
        String responsePackage = httpClientService.post("GetRoom", params);
        List<PhoenixSalesRoom> items = JSON.parseArray(responsePackage, PhoenixSalesRoom.class);
        return items;
    }

    @Override
    public List<PhoenixSalesRoom> persistence(String areaId) {
        // TODO 查询最后更新时间
        long timestamp = 0L;
        PhoenixSalesRoom latest = mapper.findLastTimestamp(areaId);
        if (latest != null) {
            timestamp = latest.getTimestamp();
        }
        List<PhoenixSalesRoom> items = sync(areaId, timestamp);

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

        mapper.persistence(items, areaId);

        return items;
    }

    @Override
    public void persistenceBatch(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 room: " + project.getAreaname());
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {

            }
            persistence(project.getAreaid());
        }
    }

    @Override
    public List<PhoenixSalesRoom> syncByBuild(String buildIds) {
        Map<String, Object> params = new HashMap<String, Object>() {{
            put("bldid", buildIds);
        }};
        String responsePackage = httpClientService.post("GetRoom", params);
        List<PhoenixSalesRoom> items = JSON.parseArray(responsePackage, PhoenixSalesRoom.class);
        return items;
    }

    @Override
    public List<PhoenixSalesRoom> persistenceByBuild(String areaId, int page, int pageSize) {
        PageData pageData = buildService.search(areaId, page, pageSize);

        List<PhoenixSalesBuild> builds = (ArrayList) pageData.getData();

        String buildIds = getBuildIdsStrongFromList(builds);
        if (StringUtils.isEmpty(buildIds)) {
            return null;
        }

        List<PhoenixSalesRoom> items = syncByBuild(buildIds);

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

        mapper.persistence(items, areaId);

        if (pageData.isEndOfPage() == true) {
            persistenceByBuild(areaId, page + 1, pageSize);
        }

        return items;
    }

    @Override
    public void persistenceBatchByBuild(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 room: " + project.getAreaname());
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {

            }
            persistenceByBuild(project.getAreaid(), 1, pageSize);
        }
    }

    private String getBuildIdsStrongFromList(List<PhoenixSalesBuild> builds) {
        String buildIds = "";

        if (!builds.isEmpty()) {
            List<String> ids = builds.stream().map(b -> b.getBldid()).collect(Collectors.toList());
            buildIds = String.join(",", ids);
        }

        return buildIds;
    }
}
