package weaver.interfaces.htsc.tgpt;

import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810039
 * @version: 1.0
 * @CreateDate: 2022-08-19
 * @Description: 这是一个更新E互动的定时任务 （老的接口）
 * @ModifyLog:
 **/
public class EhdUpdateDataCorn extends BaseCronJob {
    @Override
    public void execute() {
        RecordSetDataSource source = new RecordSetDataSource("");
        RecordSet rs = new RecordSet();
        RecordSet upRs = new RecordSet();
        String  eSql = "select * from  uf_ehd order by id desc ";
        rs.execute(eSql);
        /**
         * 第一次E互动应该没有数据，全部插入同步过来的值
         */
        if(!rs.next()){
            String selectSql = "select * from ZX_TEXTEINTERACTNEW ";
            source.execute(selectSql);
            while(source.next()){
                String id = source.getString("id");
                String reqdate = source.getString("REQDATE");//提问时间
                String rspdate = source.getString("RSPDATE");//回答时间
                String answer = source.getString("ANSWER");//答案
                String question = source.getString("QUESTION");//问题
                String questioner = source.getString("QUESTIONER");//提问者
                String hfzt = StringUtils.isBlank(answer)?"0":"1";
                String ly = StringUtils.isBlank(answer)?"1":"0";
                String insertSql="insert into uf_ehd (wtbt,wtda,twr,twsj,hfsj,hfzt,ly,zxid) values ('"+question+"','"+answer+"','"+questioner+"','"+reqdate+"','"+rspdate+"','"+hfzt+"','"+ly+"','"+id+"')";
                rs.execute(insertSql);
            }
        }else{
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime now = LocalDateTime.now();
            String nowStr = now.format(formatter);
            LocalDateTime beforeTime = now.plusHours(-24);
            String beforeTimeStr = beforeTime.format(formatter);
            /**
             * 回答时间在24小时之内的更新
             */
            String sql = "select * from ZX_TEXTEINTERACTNEW where rspdate between('"+beforeTimeStr+"','"+nowStr+"')";
            source.execute(sql);
            while(source.next()){
                String id = source.getString("id");
                String reqdate = source.getString("REQDATE");//提问时间
                String rspdate = source.getString("RSPDATE");//回答时间
                String answer = source.getString("ANSWER");//答案
                String question = source.getString("QUESTION");//问题
                String questioner = source.getString("QUESTIONER");//提问者
                String hfzt = StringUtils.isBlank(answer)?"0":"1";
                String ly = StringUtils.isBlank(answer)?"1":"0";
//                String insertSql="insert into uf_ehd (wtbt,wtda,twr,twsj,hfsj,hfzt,ly,zxid) values ('"+question+"','"+answer+"','"+questioner+"','"+reqdate+"','"+rspdate+"','"+hfzt+"','"+ly+"','"+id+"')";
//                rs.execute(insertSql);
                String ehdSql  ="select * from  uf_ehd";
                rs.execute(ehdSql);
                while(rs.next()){
                    if(Objects.equals(id,rs.getString("zxid"))){
                       String upSql = "update uf_ehd set wtda ='"+answer+"',hfsj='"+rspdate+"',hfzt=1,ly=0  where mainid ="+id;
                       upRs.execute(upSql);
                    }
                }

            }
            /**
             * 提问时间在24小时之内的新增
             */
            sql = "select * from ZX_TEXTEINTERACTNEW where reqdate between('"+beforeTimeStr+"','"+nowStr+"')";
            source.execute(sql);
            while(source.next()){
                String id = source.getString("id");
                String reqdate = source.getString("REQDATE");//提问时间
                String rspdate = source.getString("RSPDATE");//回答时间
                String answer = source.getString("ANSWER");//答案
                String question = source.getString("QUESTION");//问题
                String questioner = source.getString("QUESTIONER");//提问者
                String hfzt = StringUtils.isBlank(answer)?"0":"1";
                String ly = StringUtils.isBlank(answer)?"1":"0";
                String insertSql="insert into uf_ehd (wtbt,wtda,twr,twsj,hfsj,hfzt,ly,zxid) values ('"+question+"','"+answer+"','"+questioner+"','"+reqdate+"','"+rspdate+"','"+hfzt+"','"+ly+"','"+id+"')";
                rs.execute(insertSql);
            }
        }
//        String mainid ="";
//        if(rs.next()){
//            mainid = rs.getString("mainid");
//        }else{
//            mainid = "0";
//        }

    }

}