package com.zfire.jiasm.syncdata.upload.order;

import com.alibaba.fastjson.JSON;
import com.zfire.jiasm.syncdata.common.AddJdueServiceImpl;
import com.zfire.jiasm.syncdata.constant.TaskConfigEnum;
import com.zfire.jiasm.syncdata.data.Token;
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.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 格力商城的工单数据下载走正常工单流程
 *
 * 天猫商城工单数据同步到格力总部  itf_tbl_tmall_fkmx_req
 */
@Slf4j
@Component
public class TMallFkmxUpload {
    @Value("${baseurl}")
    private String baseurl;
    @Value("${tmscc}")
    private String tmscc = "";
    @Autowired
    private TokenCenter tokenCenter;
    @Autowired
    private AddJdueServiceImpl service;
    @Resource
    private PublicService publicService;



    @Scheduled(fixedDelayString = "${tmsc}")
    private void process() {
        if (tmscc.equals(TaskConfigEnum.FALSE.getCode())){
            return;
        }

        try {
            Token token = tokenCenter.getCurrentToken();
            // 提取待回传的数据
            List<Map<String, Object>> result = service.getTaskData4Tmall();
            String serviceName = "tblTmallFkmx";
            String addDataUrl = baseurl + serviceName + "/add";

            for (Map<String, Object> row : result) {
                Long synReqId = (Long) row.get("syn_req_id");
                // 查看该网点数据是否需要回传，不需要回传时直接修改同步状态将不再回传
                // 工单号，名表，记录ID
                if (publicService.checkWebsitSyncFlag((String) row.get("worker_order_no"),"itf_tbl_tmall_fkmx_req",synReqId)){
                    continue;
                }

                try {
                    // 数据同步
                    JSONResult addResult = HttpUtil.httpRequestWithToken(addDataUrl, "POST",
                            JSON.toJSONStringWithDateFormat(genDataMap(row), "yyyy-MM-dd HH:mm:ss"), token);
                    if (addResult.isOK()) {
                        if (addResult.getData() != null && !addResult.getData().toString().equals("")) {
                            service.updateTmallSuccess(synReqId, new Long(addResult.getData().toString()));
                        } else {
                            service.updateSuccess(synReqId, "itf_tbl_tmall_fkmx_req");
                        }
                    } else {
                        throw new Exception("天猫商城新增数据失败[" + synReqId + "],[" + addResult.getMsg() + "]");
                    }
                } catch (Exception ex) {
                    log.error("执行天猫商城任务[" + synReqId + "]发生错误:" + ex.getMessage());
                    service.updateError("itf_tbl_tmall_fkmx_req", synReqId, ex.getMessage());
                }
            }
        } catch (Exception ex) {
            log.error("ִ执行天猫商城取任务发生错误", ex);
        }
    }

    private Map<String, Object> genDataMap(Map<String, Object> sourceData) {
        Map<String, Object> paramMap = new HashMap<String, Object>();

        paramMap.put("lcid", sourceData.get("lcid"));
        paramMap.put("dsguid", sourceData.get("dsguid"));
        paramMap.put("dqjd", sourceData.get("dqjd"));
        paramMap.put("azrenid", sourceData.get("azrenid"));
        paramMap.put("lclx", sourceData.get("lclx"));
        paramMap.put("dsclsj", sourceData.get("dsclsj"));
        paramMap.put("dsclztai", sourceData.get("dsclztai"));
        paramMap.put("fksj", sourceData.get("fksj"));
        paramMap.put("flag", sourceData.get("flag"));
        paramMap.put("tmtz", sourceData.get("tmtz"));

        paramMap.put("yysj", sourceData.get("yysj"));
        paramMap.put("beiz", sourceData.get("beiz"));
        paramMap.put("address", sourceData.get("address"));
        paramMap.put("cfclcs", sourceData.get("cfclcs"));
        paramMap.put("azrenmc", sourceData.get("azrenmc"));
        paramMap.put("azrendh", sourceData.get("azrendh"));

        paramMap.put("created_by", sourceData.get("created_by"));
        paramMap.put("created_date", sourceData.get("created_date"));
        paramMap.put("last_modified_by", sourceData.get("last_modified_by"));
        paramMap.put("last_modified_date", sourceData.get("last_modified_date"));
        paramMap.put("yyjssj", sourceData.get("yyjssj"));

        return paramMap;
    }

}
