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.entity.ItfGreeToFaDownloadPcgzdmTask;
import com.zfire.jiasm.syncdata.plus.service.BrandService;
import com.zfire.jiasm.syncdata.plus.service.ItfGreeToFaDownloadPcgzdmTaskService;
import com.zfire.jiasm.syncdata.service.GLPPGuZhandDaiMaService;
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.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 格力品牌固障代码基础资料下载  同步一次不再同步，PC端使用
 */
@Slf4j
@Component
public class GLPPGuZhandDaiMa {
    @Value("${baseurl}")
    private String baseurl = "";

    @Resource
    TokenCenter tokenCenter;
    @Resource
    BrandService brandService;
    @Resource
    GLPPGuZhandDaiMaService glppGuZhandDaiMaService;
    @Resource
    ItfGreeToFaDownloadPcgzdmTaskService itfGreeToFaDownloadPcgzdmTaskService;





    /*
     * 格力品牌固障代码资料信息同步 同步一次不再使用
     * @return
     */
    @Scheduled(fixedDelayString = "${guzdm}")
    public void processGZDMTask() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, String> paramMap = new HashMap<String, String>();

        ItfGreeToFaDownloadPcgzdmTask itfpct =  itfGreeToFaDownloadPcgzdmTaskService.lambdaQuery()
                .eq(ItfGreeToFaDownloadPcgzdmTask::getNeedDownload,1)
                .eq(ItfGreeToFaDownloadPcgzdmTask::getSrcTabName,"tbl_assign_gzdm").one();
        if (itfpct == null){
            return;
        }

        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();
        String url = baseurl + InterFaceTypeEnum.GELI_GZDM.getCode() + "/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());
            return;
        }

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

            glppGuZhandDaiMaService.extractedGZDMPC(brandRec,jsonArray,itfpct);

            jsonArray.clear();
            log.info("格力品牌固障代码处理完成: \uF0B7{}",sdf.format(new Date()));
        }else {
            log.info("["+url+"]无数据");
        }
    }

}
