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.GLPPGFWebsitService;
import com.zfire.jiasm.syncdata.service.PublicService;
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 GLPPGFWebsit {
    @Value("${baseurl}")
    private String baseurl = "";
    @Value("${netfsxc}")
    private String netfsxc = "";
    @Value("${netgzsc}")
    private String netgzsc = "";
    @Value("${netgzxc}")
    private String netgzxc = "";
    @Value("${netfssc}")
    private String netfssc = "";

    @Resource
    TokenCenter tokenCenter;
    @Resource
    GLPPGFWebsitService glppgfWebsitService;
    @Resource
    BrandService brandService;
    @Resource
    private PublicService publicService;



    @Scheduled(fixedDelayString = "${netfsx}")
    public void process() throws Exception {
        if (netfsxc.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<>();

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

        req.add(InterFaceTypeEnum.FSXS_WEBSIT_CODE.getCode());
        req.add(InterFaceTypeEnum.GZXS_WEBSIT_CODE.getCode());
        req.add(InterFaceTypeEnum.FSSH_WEBSIT_CODE.getCode());
        req.add(InterFaceTypeEnum.GSSH_WEBSIT_CODE.getCode());
        String synTaskNo = publicService.getUUID();

        log.info("开始下载网点基础资料信息: \uF0B7{}",sdf.format(new Date()));

        Token token = tokenCenter.getCurrentToken();
        String url = baseurl + InterFaceTypeEnum.GLXSSH_WEBSIT.getCode() + "/querylist";
        for (String bc :req){
            try{
                paramMap.put("belongCompany", bc);
                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.FSXS_WEBSIT_CODE.getCode())){
                        glppgfWebsitService.extractedFSXS(jsonArray, record, synTaskNo);
                    } else if (bc.equals(InterFaceTypeEnum.GZXS_WEBSIT_CODE.getCode())){
                        glppgfWebsitService.extractedGZXS(jsonArray, record, synTaskNo);
                    } else if (bc.equals(InterFaceTypeEnum.FSSH_WEBSIT_CODE.getCode())){
                        glppgfWebsitService.extractedFSSH(jsonArray, record, synTaskNo);
                    } else if (bc.equals(InterFaceTypeEnum.GSSH_WEBSIT_CODE.getCode())){
                        glppgfWebsitService.extractedGZSH(jsonArray, record, synTaskNo);
                    }
                    jsonArray.clear();
                }else {
                    log.info("["+url+"]无数据");
                }
            } catch (Exception ex) {
                log.info("网点基础资料信息处理失败");
                ex.printStackTrace();
            }
        }

        log.info("网点基础资料信息处理完成: \uF0B7{}",sdf.format(new Date()));
    }
}
