package com.zfire.jiasm.syncdata.download.basedata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zfire.jiasm.syncdata.constant.InterFaceTypeEnum;
import com.zfire.jiasm.syncdata.constant.TaskConfigEnum;
import com.zfire.jiasm.syncdata.data.Token;
import com.zfire.jiasm.syncdata.plus.entity.Brand;
import com.zfire.jiasm.syncdata.plus.service.BrandService;
import com.zfire.jiasm.syncdata.service.GLPPXuQiuLeiBieService;
import com.zfire.jiasm.syncdata.utils.HttpUtil;
import com.zfire.jiasm.syncdata.utils.JSONResult;
import com.zfire.jiasm.syncdata.utils.TokenCenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 格力品牌 需求类别 - 需求类别基础资料下载（需求大类、需求小类）
 */
@Slf4j
@Component
public class GLPPXuQiuLeiBie {
    @Value("${baseurl}")
    private String baseurl = "";
    @Value("${xqdlc}")
    private String xqdlc = "";
    @Value("${xqxlc}")
    private String xqxlc = "";

    @Resource
    TokenCenter tokenCenter;
    @Resource
    BrandService brandService;
    @Resource
    GLPPXuQiuLeiBieService glppXuQiuLeiBieService;

    // 格力品牌需求大类基础资料信息同步
    @Scheduled(fixedDelayString = "${xqdl}")
    public void process() throws Exception {
        if (xqdlc.equals(TaskConfigEnum.FALSE.getCode())){
            return;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, String> paramMap = new HashMap<String, String>();
        List<String> req = new ArrayList<>();

        req.add(InterFaceTypeEnum.GELI_XQDL.getCode());
        req.add(InterFaceTypeEnum.GELI_XQXL.getCode());


        log.info("开始下载格力品牌需求类别信息: \uF0B7{}",sdf.format(new Date()));

        // 提取品牌信息
        Brand brandRec = brandService.lambdaQuery().eq(Brand::getBrand,"格力").one();
        if (brandRec == null){
            log.info("格力品牌信息不存在【brand】");
            return;
        }

        Token token = tokenCenter.getCurrentToken();

        for (String bc :req){
            try{
                String url = baseurl + bc + "/querylist";
                String paramJson= JSON.toJSONString(paramMap);

                JSONResult jsonResult = HttpUtil.httpRequestWithToken(url, "POST", paramJson, token);
                if (jsonResult.getStatus() == null || !jsonResult.getStatus().toString().equals("200")) {
                    log.info("下载格力品牌需求类别信息失败！: {}",jsonResult.getMessage());
                    continue;
                }
                JSONArray jsonArray = (JSONArray) jsonResult.getData();

                if (jsonArray != null) {
                    log.info("下载了{}条数据",jsonArray.size());

                    if (bc.equals(InterFaceTypeEnum.GELI_XQDL.getCode())){
                        glppXuQiuLeiBieService.extractedXQDL(brandRec,jsonArray);
                    } else if (bc.equals(InterFaceTypeEnum.GELI_XQXL.getCode())){
                        glppXuQiuLeiBieService.extractedXQXL(brandRec,jsonArray);
                    }
                    jsonArray.clear();
                }else {
                    log.info("["+url+"]无数据");
                }
            } catch (Exception ex) {
                log.info("格力品牌需求类别信息处理失败");
                ex.printStackTrace();
            }
        }

        log.info("格力品牌需求类别处理完成: \uF0B7{}",sdf.format(new Date()));
    }
}
