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.service.PublicService;
import com.zfire.jiasm.syncdata.service.SMSService;
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;

/**
 * 发送给顾客的预约改约短信
 * fa_gree_sms   greesmsc=FALSE
 */
@Slf4j
@Component
public class SendGreeSms {
    @Value("${greesmsc}")
    private String greesmsc = "";
    @Value("${installTopicName}")
    private String installTopicName;
    @Value("${repaireMessageApointmentTopicName}")
    private String repaireMessageApointmentTopicName;

    @Autowired
    private TokenCenter tokenCenter;
    @Autowired
    private SMSService smsService;
    @Autowired
    private OLDSystemUtil oldSystemTask;
    @Resource
    PublicService publicService;



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

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("开始预约改约短信发送: \uF0B7{}", sdf.format(new Date()));

        try{
            List<Map<String, Object>> result = smsService.getTaskData("fa_gree_sms");
            Token token = tokenCenter.getCurrentToken();

            for (Map<String, Object> row : result) {
                String synTaskNo = (String) row.get("syn_task_no");
                Map<String, Object> messageTableData = row;
                boolean isInstall = (((Integer) row.get("azorwx")) == 1);
                StringBuffer messageJson = new StringBuffer();
                if (row.get("yhlb") != null && ((Long) row.get("yhlb")) == 1) {
                    row.put("yhlb", 2);
                }

                String format;
                if (isInstall) {
                    messageJson.append("{\"messageHuifangEntity\":");
                    format = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
                    if (row.get("dxguid") != null) {
                        row.remove("dxguid");
                    }
                } else {
                    format = "yyyy-MM-dd HH:mm:ss";
                    messageTableData.put("fsdt", row.get("crdt"));
                    if (row.get("dxguid") == null || ((String) row.get("dxguid")).trim().length() == 0) {
                        row.put("dxguid", synTaskNo.toUpperCase());
                    }
                }

                messageJson.append(JSON
                        .toJSONStringWithDateFormat(oldSystemTask.filterDataMap(messageTableData, isInstall), format));

                if (isInstall) {
                    messageJson.append("}");
                }

                String key = (String) row.get("fjid");
                String json = messageJson.toString();
                log.info("预约改约短信" + json);
                oldSystemTask.sendMessage2Server(
                        (isInstall ? this.installTopicName : this.repaireMessageApointmentTopicName), json, token, key);

                smsService.successUpdate("fa_gree_sms", synTaskNo);
                publicService.upSmsRecordSendTime(synTaskNo,(String)row.get("sjid"),new Date());
            }

        } catch (Exception ex) {
            log.error("预约改约短信发送失败", ex);
        }
    }






}
