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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gic.commons.util.DataApiUtils;
import com.gic.commons.util.DateUtil;
import com.gic.haoban.manage.api.service.StaffApiService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Author guojx
 * @Date 2023/1/17 10:59
 */
@Component
public class StoreAuthUtils {
    private static final Logger log = LoggerFactory.getLogger(StoreAuthUtils.class);

    @Autowired
    private StaffApiService staffApiService;

    public JSONObject getTotalJson(JSONObject param, String apolloKey) {
        Map<String, Object> totalRes = DataApiUtils.http(param.toJSONString(), apolloKey);
        List<JSONObject> totalList = DataApiUtils.getPageList(totalRes);
        if (CollectionUtils.isEmpty(totalList)) {
            return null;
        }
        JSONObject total = totalList.get(0);
        return total;
    }

    /**
     * 查询区经角色下管辖的门店权限
     * @param clerkId
     * @param wxEnterpriseId
     * @return
     */
    public List<String> queryClerkStoreIds(String clerkId, String wxEnterpriseId){
        List<String> storeIds = staffApiService.getHaoBanStoreIdsRolesByClerkId(clerkId, wxEnterpriseId);
        if (CollectionUtils.isEmpty(storeIds)){
            log.info("根据区经id查询管辖的门店为空 {} {}", clerkId, wxEnterpriseId);
            return Collections.singletonList("-100");
        }
        if (storeIds.size() == 1 && StringUtils.equals(storeIds.get(0), "-1")){
            // 全部门店权限
            log.info("区经有全部门店权限 {}", clerkId);
            return Collections.emptyList();
        }
        return storeIds;
    }

    public <T> T getPageOverview(JSONObject jsonParam, String apolloKey, Class clazz) {
        List<T> list = getPageTrend(jsonParam, apolloKey, clazz, null);
        if (list != null) {
            return list.get(0);
        }
        return null;
    }

    public <T> List<T> getPageTrend(JSONObject jsonParam, String apolloKey, Class clazz, Integer pageSize) {
        if (jsonParam == null) {
            jsonParam = new JSONObject();
        }
        jsonParam.put("pageNum", 1);
        jsonParam.put("orderByFields", 1);
        jsonParam.put("orderByType", 1);
        jsonParam.put("pageSize", pageSize == null ? 1 : pageSize);
        if (!jsonParam.containsKey("storeGroup")) {
            jsonParam.put("storeGroup", 8);
        }
        Map<String, Object> res = DataApiUtils.http(jsonParam.toJSONString(), apolloKey);
        List<JSONObject> list = DataApiUtils.getPageList(res);
        if (CollectionUtils.isNotEmpty(list)) {
            return (List<T>) JSONArray.parseArray(JSON.toJSONString(list), clazz);
        }
        return null;
    }

    public Integer getDays(String startDate, String endDate) {
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return DateUtil.daysBetween(sdf.parse(startDate), sdf.parse(endDate)) + 1;
            } catch (Exception e) {

            }
        }
        return null;
    }
}
