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

import cn.com.countrygarden.bi.main.dao.phoenix.PhoenixSalesBuildMapper;
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.service.phoenix.PhoenixSalesBuildService;
import cn.com.countrygarden.bi.main.service.phoenix.PhoenixSalesHttpClientService;
import cn.com.countrygarden.bi.main.service.phoenix.PhoenixSalesProjectService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageRowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class PhoenixSalesBuildServiceImpl implements PhoenixSalesBuildService {

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

    @Autowired
    private PhoenixSalesHttpClientService httpClientService;

    @Autowired
    private PhoenixSalesBuildMapper mapper;

    @Autowired
    private PhoenixSalesProjectService projectService;


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

    @Override
    public List<PhoenixSalesBuild> persistence(String areaId) {
        PhoenixSalesProject project = projectService.findOne(areaId);
        if (project == null) {
            return null;
        }

        List<PhoenixSalesBuild> builds = sync(areaId);

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

        mapper.persistence(builds, areaId, project.getAreaname());

        return builds;
    }

    @Override
    public List<PhoenixSalesBuild> persistence(String areaId, String areaName) {
        List<PhoenixSalesBuild> builds = sync(areaId);

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

        mapper.persistence(builds, areaId, areaName);

        return builds;
    }

    @Override
    public void persistenceBatch() {
        List<PhoenixSalesProject> projects = projectService.findAll();

        for (PhoenixSalesProject project : projects) {
            persistence(project.getAreaid(), project.getAreaname());
        }
    }

    @Override
    public PageData search(String areaId, int page, int pageSize) {
        PageRowBounds pageRowBounds = new PageRowBounds(page, pageSize);
        List<PhoenixSalesBuild> builds = mapper.search(areaId, pageRowBounds);

        PageData pageData = new PageData(builds, pageRowBounds);

        return pageData;
    }
}
