package com.zfire.jiasm.syncdata.sendmessage;

import com.alibaba.fastjson.JSON;
import com.zfire.jiasm.syncdata.constant.TaskConfigEnum;
import com.zfire.jiasm.syncdata.data.Token;
import com.zfire.jiasm.syncdata.plus.entity.ItfMessagehuifangentity;
import com.zfire.jiasm.syncdata.service.PublicService;
import com.zfire.jiasm.syncdata.service.SMSRepairFinishService;
import com.zfire.jiasm.syncdata.utils.OLDSystemUtil;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 完工评价短信发送，发给格力总部，由格力总部发送
 * itf_messagehuifangentity 表中字段全部发送
 */
@Slf4j
@Component
public class SendGreeMessage {
    @Value("${installTopicName}")
    private String installTopicName;
    @Value("${repaireMessageFinishTopicName}")
    private String repaireMessageFinishTopicName;
    @Value("${wgmsgsendc}")
    private String wgmsgsendc = "";
    @Autowired
    private TokenCenter tokenCenter;
    @Autowired
    private OLDSystemUtil oldSystemTask;
    @Autowired
    private SMSRepairFinishService smsRepairFinishService;
    @Resource
    PublicService publicService;



    @Scheduled(fixedDelayString = "${wgmsgsend}")
    public void process() throws Exception {
        if (wgmsgsendc.equals(TaskConfigEnum.FALSE.getCode())){
            return;
        }


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 提取数据
        List<Map<String, Object>> result = smsRepairFinishService.getTaskData();
        log.info("开始评价短信发送到总部: \uF0B7{}", sdf.format(new Date()));

        Token token = tokenCenter.getCurrentToken();
        for (Map<String, Object> row : result) {
            String synTaskNo = (String) row.get("syn_task_no");
            Map<String, Object> messageTableData = row;
            String name;
            String topic;
            String format;
            StringBuffer messageJson = new StringBuffer();
            Integer azorwx=(Integer)row.get("azorwx");
            if(azorwx==1) {
                name="安装";
                topic=this.installTopicName;
                format="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
                if(row.get("dxguid")!=null) {
                    row.remove("dxguid");
                }
                messageJson.append("{\"messageHuifangEntity\":"+JSON.toJSONStringWithDateFormat(oldSystemTask.filterDataMap(messageTableData,true),
                        format)+"}");
            }else {
                name="维修";
                topic=this.repaireMessageFinishTopicName;
                format="yyyy-MM-dd HH:mm:ss";
                messageTableData.put("fsdt", row.get("crdt"));
                messageJson.append(JSON.toJSONStringWithDateFormat(oldSystemTask.filterDataMap(messageTableData,false),
                        format));
            }

            String key = (String)row.get("fjid");
            String json = messageJson.toString();
            // log.info(name+"评价短信" + json);
            oldSystemTask.sendMessage2Server(topic, json, token, key);
            smsRepairFinishService.successUpdate(synTaskNo);
            publicService.upSmsRecordSendTime(synTaskNo,(String)row.get("sjid"),new Date());
        }

        log.info("评价短信发送到总部结束: \uF0B7{}", sdf.format(new Date()));

    }

    private void getMessageJson(ItfMessagehuifangentity item, Map<String, Object> messageTableData) {
        messageTableData.put("synTaskNo", item.getSynTaskNo());
        messageTableData.put("orderBaseId", item.getOrderBaseId());
        messageTableData.put("sendtype", item.getSendtype());
        messageTableData.put("plansendtime", item.getPlansendtime());
        messageTableData.put("sendtime", item.getSendtime());
        messageTableData.put("azorwx", item.getAzorwx());
        messageTableData.put("crdt", item.getCrdt());
        messageTableData.put("fjid", item.getFjid());
        messageTableData.put("fjwd", item.getFjwd());
        messageTableData.put("fscs", item.getFscs());
        messageTableData.put("fsma", item.getFsma());
        messageTableData.put("fsnr", item.getFsnr());
        messageTableData.put("sjid", item.getSjid());
        messageTableData.put("sjly", item.getSjly());
        messageTableData.put("stat", item.getStat());
        messageTableData.put("stat2", item.getStat2());
        messageTableData.put("yhlb", item.getYhlb());
        messageTableData.put("fsStatus", item.getFsStatus());
        if (item.getDxguid() != null) {
            messageTableData.put("dxguid", item.getDxguid());
        }
        messageTableData.put("exeCount", item.getExeCount());
        messageTableData.put("errorMsg", item.getErrorMsg());
    }

    private void getOneMessageJson(ItfMessagehuifangentity item, Map<String, Object> messageTableData) {
        messageTableData.put("synTaskNo", item.getSynTaskNo());
        messageTableData.put("orderBaseId", item.getOrderBaseId());
        messageTableData.put("sendtype", item.getSendtype());
        messageTableData.put("plansendtime", item.getPlansendtime());
        messageTableData.put("sendtime", item.getSendtime());
        messageTableData.put("azorwx", item.getAzorwx());
        messageTableData.put("crdt", item.getCrdt());
        messageTableData.put("fjid", item.getFjid());
        messageTableData.put("fjwd", item.getFjwd());
        messageTableData.put("fscs", item.getFscs());
        messageTableData.put("fsma", item.getFsma());
        messageTableData.put("fsnr", item.getFsnr());
        messageTableData.put("sjid", item.getSjid());
        messageTableData.put("sjly", item.getSjly());
        messageTableData.put("stat", item.getStat());
        messageTableData.put("stat2", item.getStat2());
        messageTableData.put("yhlb", item.getYhlb());
        messageTableData.put("fsStatus", item.getFsStatus());
        messageTableData.put("dxguid", item.getDxguid());
        messageTableData.put("exeCount", item.getExeCount());
        messageTableData.put("errorMsg", item.getErrorMsg());
        messageTableData.put("fsdt", item.getCrdt());
    }
}
