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.service.GLPPLeiBieService;
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 GLPPLeiBie {
    @Value("${baseurl}")
    private String baseurl = "";
    @Value("${lbdlc}")
    private String lbdlc = "";
    @Value("${lbxlc}")
    private String lbxlc = "";
    @Value("${lbxltc}")
    private String lbxltc = "";
    @Value("${lbjxc}")
    private String lbjxc = "";

    @Resource
    TokenCenter tokenCenter;
    @Resource
    GLPPLeiBieService glppLeiBieService;


    //格力品牌类别基础资料信息同步
    @Scheduled(fixedDelayString = "${lbdl}")
    public void process() throws Exception {
        if (lbdlc.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<>();
        String msg ="";

        req.add(InterFaceTypeEnum.GELI_DL.getCode());
        req.add(InterFaceTypeEnum.GELI_XL.getCode());
        req.add(InterFaceTypeEnum.GELI_PPXL.getCode());
        req.add(InterFaceTypeEnum.GELI_PPJX.getCode());

        log.info("开始下载格力产品类别基础资料信息: \uF0B7{}",sdf.format(new Date()));

        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) {
                    if (bc.equals(InterFaceTypeEnum.GELI_DL.getCode())){
                        log.info("格力产品大类下载了{}条数据",jsonArray.size());
                        glppLeiBieService.extractedDL(jsonArray);
                    } else if (bc.equals(InterFaceTypeEnum.GELI_XL.getCode())){
                        log.info("格力产品小类下载了{}条数据",jsonArray.size());
                        glppLeiBieService.extractedXL(jsonArray);
                    } else if (bc.equals(InterFaceTypeEnum.GELI_PPXL.getCode())){
                        log.info("格力产品系列下载了{}条数据",jsonArray.size());
                        glppLeiBieService.extractedXiLie(jsonArray);
                    } else if (bc.equals(InterFaceTypeEnum.GELI_PPJX.getCode())){
                        log.info("格力产品机型下载了{}条数据",jsonArray.size());
                        glppLeiBieService.extractedJX(jsonArray);
                    }
                    jsonArray.clear();
                }else {
                    log.info("["+url+"]无数据");
                }
            } catch (Exception ex) {
                if (bc.equals(InterFaceTypeEnum.GELI_DL.getCode())){
                    log.info("格力产品大类基础资料信息处理失败");
                } else if (bc.equals(InterFaceTypeEnum.GELI_XL.getCode())){
                    log.info("格力产品小类基础资料信息处理失败");
                } else if (bc.equals(InterFaceTypeEnum.GELI_PPXL.getCode())){
                    log.info("格力产品系列基础资料信息处理失败");
                } else if (bc.equals(InterFaceTypeEnum.GELI_PPJX.getCode())){
                    log.info("格力产品机型基础资料信息处理失败");
                }
                ex.printStackTrace();
            }
        }

        log.info("格力产品类别基础资料信息处理完成: \uF0B7{}",sdf.format(new Date()));
    }

}
