package com.zfire.jiasm.syncdata.sendmessage;

import com.alibaba.fastjson.JSONObject;
import com.zfire.jiasm.syncdata.constant.TaskConfigEnum;
import com.zfire.jiasm.syncdata.pojo.bo.DownRepairOrderGD;
import com.zfire.jiasm.syncdata.service.impl.SMSSelfGetArrivedServiceImpl;
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 java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 查看评价短信回复信息 自建短信回复查询 + 同步回复内容信息
 */
@Slf4j
@Component
public class SMSGetArrived {
    @Value("${qmsgsendc}")
    private String qmsgsendc = "";
    @Value("${syncsmsflag}")
    private String syncsmsflag = "";
    @Autowired
    private SMSSelfManager smsSelfManager;
    @Autowired
    private SMSSelfGetArrivedServiceImpl smsSelfGetArrivedServiceImpl;



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

       /* // 测试用
        smsSelfGetArrivedServiceImpl.insertDataGDtest();
        if (1==1){
            return;
        }*/

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

        try {
            log.info("开始查询短信状态报告: \uF0B7{}",sdf.format(new Date()));

            // 从工单系统中获取
            if ("TRUE".equals(syncsmsflag)){
                DownRepairOrderGD data = smsSelfManager.getArrivedGD("sync/smsorder");
                // 存在时不再同步
                if (smsSelfGetArrivedServiceImpl.getSyncSmsOrderById(data.getId())){
                    // 数据同步
                    smsSelfGetArrivedServiceImpl.insertDataGD(data);
                }

                // 与总部进行状态同步
                smsSelfGetArrivedServiceImpl.syncSmsOrderStatus();

            } else {
                JSONObject data = smsSelfManager.getArrived();
                smsSelfGetArrivedServiceImpl.insertData(data);
            }

            log.info("查询短信状态报告结束: \uF0B7{}",sdf.format(new Date()));
        } catch (Exception ex) {
            log.error("查询短信状态报告失败", ex);
        }
    }

}
