package com.gic.haoban.manage.web.utils.statistics;

import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.manage.api.dto.statistics.StatisticsDTO;
import com.gic.haoban.manage.api.dto.statistics.StatisticsPage;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 数据接口参数拼接工具
 * @Author guojx
 * @Date 2022/7/13 10:49
 */
public class DataStatisticsParamUtils {
    private static final Logger LOGGER = LogManager.getLogger(DataStatisticsParamUtils.class);



    public static JSONObject getParam(BasePageInfo basePageInfo, JSONObject objParam) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pageNo", basePageInfo.getPageNum());
        jsonObject.put("pageSize", basePageInfo.getPageSize());

        if (objParam != null) {
            if (objParam.containsKey("storeId")) {
                Object storeId = objParam.get("storeId");
                if (storeId == null || StringUtils.isBlank(storeId.toString())) {
                    objParam.remove("storeId");
                }
            }
        }
        jsonObject.put("inFields", objParam);
        return jsonObject;
    }

    public static JSONObject getParamNoPage(JSONObject objParam) {
        JSONObject jsonObject = new JSONObject();
        if (objParam != null) {
            if (objParam.containsKey("storeId")) {
                Object storeId = objParam.get("storeId");
                if (storeId == null || StringUtils.isBlank(storeId.toString())) {
                    objParam.remove("storeId");
                }
            }
        }
        jsonObject.put("inFields", objParam);
        return jsonObject;
    }

    public static RestResponse response(StatisticsDTO statisticsDTO) {
        if (statisticsDTO == null) {
            return RestResponse.successResult();
        }
        return RestResponse.successResult(statisticsDTO.getData());
    }

    public static RestResponse responseOne(StatisticsDTO statisticsDTO) {
        if (statisticsDTO == null) {
            return RestResponse.successResult();
        }
        if (CollectionUtils.isNotEmpty(statisticsDTO.getData())) {
            return RestResponse.successResult(statisticsDTO.getData().get(0));
        }
        return RestResponse.successResult();
    }

    public static RestResponse responsePage(StatisticsDTO statisticsDTO) {
        if (statisticsDTO == null) {
            return RestResponse.successResult();
        }
        Page page = new Page();
        page.setResult(statisticsDTO.getData());
        StatisticsPage statisticsPage = statisticsDTO.getPage();
        if (statisticsPage != null) {
            page.setPageSize(statisticsPage.getPageSize());
            page.setCurrentPage(statisticsPage.getCurrentPage());
            page.setTotalPage(statisticsPage.getTotalPage());
            page.setTotalCount(statisticsPage.getTotalCount());
        }
        return RestResponse.successResult(page);
    }

    public static Page getPage(StatisticsDTO statisticsDTO) {
        RestResponse response = responsePage(statisticsDTO);
        if ("0".equals(response.getCode())) {
            Page page = (Page) response.getResult();
            return page;
        }
        return null;
    }

}
