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.GLPPGFWebsitWorkerService;
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.*;

/**
 * 格力品牌广佛网点维修工基础资料下载（销售与售后网点维修工）
 * 1: 落下载表 存在修改不存在新增
 * 新增或修改生成 worker:师傅信息表，
 * 商户网点与平台网点存在对应关系时 同步师傅数据到网点表 websit_worker:网点师傅信息表， worker_brand_account：师傅网点对应品牌网点信息表
 */
@Slf4j
@Component
public class GLPPGFWebsitWorker {
    @Value("${baseurl}")
    private String baseurl = "";
    @Value("${gfwwwc}")
    private String gfwwwc = "";
    @Value("${fsxwxgc}")
    private String fsxwxgc = "";
    @Value("${createworkerc}")
    private String createworkerc = "";


    @Resource
    TokenCenter tokenCenter;


    @Resource
    GLPPGFWebsitWorkerService glppgfWebsitWorkerService;

    // 网点维修工基础资料下载
    @Scheduled(fixedDelayString = "${fsxwxg}")
    public void process() throws Exception {
        if (fsxwxgc.equals(TaskConfigEnum.FALSE.getCode())){
            return;
        }

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

        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());
        req.add(InterFaceTypeEnum.TMSC_WEBSIT_CODE.getCode());
        req.add(InterFaceTypeEnum.TMYP_WEBSIT_CODE.getCode());

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

        Token token = tokenCenter.getCurrentToken();
        String url = baseurl + InterFaceTypeEnum.GLXSSH_WEBSIT_WORK.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())){
                        glppgfWebsitWorkerService.extractedFSXS(jsonArray);
                    } else if (bc.equals(InterFaceTypeEnum.GZXS_WEBSIT_CODE.getCode())){
                        glppgfWebsitWorkerService.extractedGZXS(jsonArray);
                    } else if (bc.equals(InterFaceTypeEnum.FSSH_WEBSIT_CODE.getCode())){
                        glppgfWebsitWorkerService.extractedFSSH(jsonArray);
                    } else if (bc.equals(InterFaceTypeEnum.GSSH_WEBSIT_CODE.getCode())){
                        glppgfWebsitWorkerService.extractedGZSH(jsonArray);
                    } else if (bc.equals(InterFaceTypeEnum.TMSC_WEBSIT_CODE.getCode())){
                        glppgfWebsitWorkerService.extractedTMSC(jsonArray);
                    } else if (bc.equals(InterFaceTypeEnum.TMYP_WEBSIT_CODE.getCode())){
                        glppgfWebsitWorkerService.extractedTMYP(jsonArray);
                    }


                    jsonArray.clear();
                }else {
                    log.info("["+url+"]无数据");
                }
            } catch (Exception ex) {
                log.info("网点维修工基础资料信息处理失败");
                ex.printStackTrace();
            }
        }

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


    /**
     * 数据同步到本地表 师傅信息
     * 师傅信息一定新增 - 多次变到后无则新增，有则修改
     */
    @Scheduled(fixedDelayString = "${createworker}")
    private void processData() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (createworkerc.equals(TaskConfigEnum.FALSE.getCode())){
            return;
        }
        try {
            log.info("开始处理网点维修工基础资料生成师傅数据: \uF0B7{}",sdf.format(new Date()));
            glppgfWebsitWorkerService.inputWorkerData();
            log.info("处理网点维修工基础资料生成师傅数据结束: \uF0B7{}",sdf.format(new Date()));
        } catch (Exception ex) {
            log.info("网点维修工基础资料生成师傅数据失败");
            ex.printStackTrace();
        }
    }


    /**
     * 添加网点师傅数据及平台与商户网点师傅数据
     *   worker_brand_account(师傅网点对应品牌网点信息表) + websit_worker(网点师傅信息表))
     *   品牌网点与平台网点有对应关系(websit_order_sign)才会同步生成生成平台网点及品牌商网点师傅数据
     */
    @Scheduled(fixedDelayString = "${gfwww}")
    private void processWebsitWorkerData() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (gfwwwc.equals(TaskConfigEnum.FALSE.getCode())){
            return;
        }
        try {
            log.info("开始处理网点维修工基础资料生成网点师傅数据: \uF0B7{}",sdf.format(new Date()));
            glppgfWebsitWorkerService.inputWebsitWorkerData();
            log.info("处理网点维修工基础资料生成网点师傅数据结束: \uF0B7{}",sdf.format(new Date()));
        } catch (Exception ex) {
            log.info("网点维修工基础资料生成网点师傅数据失败");
            ex.printStackTrace();
        }
    }


}
