package com.zfire.jiasm.syncdata.sendmessage;

/**
 * 联通 - 自建完工评价短信发送
 * itf_messagehuifangentity
 */

import com.zfire.jiasm.syncdata.constant.TaskConfigEnum;
import com.zfire.jiasm.syncdata.service.PublicService;
import com.zfire.jiasm.syncdata.service.SMSRepairFinishService;
import com.zfire.jiasm.syncdata.utils.SMSSelfManager;
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;

@Component
@Slf4j
public class SMSMessageFinish {
    @Value("${SMSSelf_templateId}")
    private String templateId;
    @Value("${zwgmsgsendc}")
    private String zwgmsgsendc = "";
    @Autowired
    private SMSRepairFinishService smsRepairFinishService;
    @Autowired
    private SMSSelfManager smsSelfManager;
    @Resource
    PublicService publicService;



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

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if(!smsSelfManager.checkCanSend()) {
            log.info("自建工单评价短信不到发送时间段");
            return;
        }

        try {
            log.info("开始自建工单评价短信发送: \uF0B7{}", sdf.format(new Date()));
            // 从完工短信发送记录表中提取数据
            List<Map<String, Object>> result = smsRepairFinishService.getTaskData4Self();
            for (Map<String, Object> row : result) {
                String synTaskNo = (String) row.get("syn_task_no");
                String[] datas = new String[1];
                datas[0] = (String) row.get("fsnr");
                String resultData = smsSelfManager.templateSMS((String) row.get("sjid"), templateId, (String) row.get("syn_task_no"),
                        (String) row.get("fsma"), datas);

                if (resultData.length()>0){
                    smsRepairFinishService.failureUpdate(synTaskNo,resultData);
                } else {
                    smsRepairFinishService.successUpdate(synTaskNo);
                    publicService.upSmsRecordSendTime(synTaskNo,(String)row.get("sjid"),new Date());
                }
            }

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

        } catch (Exception ex) {
            log.error("评价短信发送失败", ex);
        }

    }
}
