#include <QTextCodec>
#include <QDir>
#include <QDebug>
#include <QDateTime>
#include <QThread>
#include "generateword.h"
#include "qword.h"

GenerateWord::GenerateWord(QObject *parent) : QObject(parent)
{

}

void setTalbeCurve(QWord &word,QList<QPointF> &points,QString picture)
{
    word.setFontSize(7);
    word.setFontBold(false);

    QAxObject *table;
    int length = points.length();
    int integer = length/10;
    int remainder = length%10;
    if(remainder > 0)
        integer += 1;
    table = word.createTableWithColor(integer, 10);
    for(int i=0;i<length; i++){
        QPointF point = points.at(i);
        QString text = QString::number(point.y());
        int lineNum = i/10+1;
        int row = i%10+1;
        word.setCellText(table, lineNum, row, text,false);
    }
    word.autoFitBehavior(table,wdAutoFitWindow);
    word.moveForEnd();
    word.insertMoveDown();
    if(table != nullptr)
        delete table;
    //添加曲线图
    word.AddPicture(QDir::currentPath()+"/"+picture);
    word.moveForEnd();
    word.insertMoveDown();
    word.insertEnter();
}

void setTalbeCurve1(QWord &word,QList<QVariant> &points,QString picture)
{
    if(points.length() == 0){

        word.insertEnter();
        return;
    }

    word.setFontSize(7);
    word.setFontBold(false);

    QAxObject *table;
    int length = points.length();
    int integer = length/10;
    int remainder = length%10;
    if(remainder > 0)
        integer += 1;
    table = word.createTableWithColor(integer, 10);
    for(int i=0;i<length; i++){
        QVariant variant = points.at(i);
        QPointF point = variant.toPointF();
        QString text = QString::number(point.y());
        int lineNum = i/10+1;
        int row = i%10+1;
        word.setCellText(table, lineNum, row, text,false);
    }
    word.autoFitBehavior(table,wdAutoFitWindow);
    word.moveForEnd();
    word.insertMoveDown();
    if(table != nullptr)
        delete table;
    //添加曲线图
    word.AddPicture(QDir::currentPath()+"/"+picture);
    word.moveForEnd();
    word.insertMoveDown();
    word.insertEnter();
}


void GenerateWord::breatheValveRecordWrite(QWord &word,QString testType, QVariant &obj)
{
    QString text;
    QString picture;
    QAxObject *table;
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QVariantMap map = obj.toMap();
    QList<QVariant> points0 = map["pointList0"].toList();
    QList<QVariant> points1 = map["pointList1"].toList();
    QList<QVariant> points2 = map["pointList2"].toList();
    QList<QVariant> points3 = map["pointList3"].toList();
    QList<QVariant> points4 = map["pointList4"].toList();
    QList<QVariant> points5 = map["pointList5"].toList();


    QString resultSetPressure1 = map["resultSetPressure1"].toString();
    QString resultSetPressure2 = map["resultSetPressure2"].toString();
    QString resultSetPressure3 = map["resultSetPressure3"].toString();

    QString resultSealPressure1 = map["resultSealPressure1"].toString();
    QString resultSealPressure2 = map["resultSealPressure2"].toString();
    QString resultSealPressure3 = map["resultSealPressure3"].toString();

    //第一次 开启压力试验
    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);

    if(testType == "negative"){
        text=codec->toUnicode("3.1、第一次负压开启压力试验记录表(kPa)");
        //text="3.1、第一次负压开启压力试验记录表(kPa)";
    }
    else if(testType == "positive"){
        text=codec->toUnicode("2.1、第一次正压开启压力试验记录表(kPa)");
        //text="2.1、第一次正压开启压力试验记录表(kPa)";
    }
    else if(testType == "capsize"){
        text=codec->toUnicode("4.1、90°颠覆性密封试验记录表(kPa)");
        //text="4.1、90°颠覆性密封试验记录表(kPa)";
    }

    word.insertText(text);
    word.insertMoveDown();
    if(testType == "positive"){
        picture= "2.1.jpg";
    }
    else if(testType == "negative"){
        picture= "3.1.jpg";
    }
    else if(testType == "capsize"){
        picture= "4.1.jpg";
    }
    setTalbeCurve1(word,points0,picture);
    //第二次 开启压力试验
    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    if(testType == "positive"){
        text=codec->toUnicode("2.2、第二次正压开启压力试验记录表(kPa)");
    }
    else if(testType == "negative"){
        text=codec->toUnicode("3.2、第二次负压开启压力试验记录表(kPa)");
    }
    else if(testType == "capsize"){
        text=codec->toUnicode("4.2、180°颠覆性密封试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    if(testType == "positive"){
        picture= "2.2.jpg";
    }
    else if(testType == "negative"){
        picture= "3.2.jpg";
    }
    else if(testType == "capsize"){
        picture= "4.2.jpg";
    }
    setTalbeCurve1(word,points1,picture);
    //第三次 开启压力试验
    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    if(testType == "positive"){
        text=codec->toUnicode("2.3、第三次正压开启压力试验记录表(kPa)");
    }
    else if(testType == "negative"){
        text=codec->toUnicode("3.3、第三次负压开启压力试验记录表(kPa)");
    }
    else if(testType == "capsize"){
        text=codec->toUnicode("4.3、270°颠覆性密封试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    if(testType == "positive"){
        picture= "2.3.jpg";
    }
    else if(testType == "negative"){
        picture= "3.3.jpg";
    }
    else if(testType == "capsize"){
        picture= "4.3.jpg";
    }
    setTalbeCurve1(word,points2,picture);

    if(testType == "capsize"){
        goto result;
    }

    //第一次 密封压力试验
    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    if(testType == "positive"){
        text=codec->toUnicode("2.4、第一次正压密封压力试验记录表(kPa)");
    }
    else if(testType == "negative"){
        text=codec->toUnicode("3.4、第一次负压密封压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    if(testType == "positive"){
        picture= "2.4.jpg";
    }
    else if(testType == "negative"){
        picture= "3.4.jpg";
    }
    setTalbeCurve1(word,points3,picture);
    //第二次 密封压力试验
    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    if(testType == "positive"){
        text=codec->toUnicode("2.5、第二次正压密封压力试验记录表(kPa)");
    }
    else if(testType == "negative"){
        text=codec->toUnicode("3.5、第二次负压密封压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    if(testType == "positive"){
        picture= "2.5.jpg";
    }
    else if(testType == "negative"){
        picture= "3.5.jpg";
    }
    setTalbeCurve1(word,points4,picture);
    //第三次 密封压力试验
    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    if(testType == "positive"){
        text=codec->toUnicode("2.6、第三次正压密封压力试验记录表(kPa)");
    }
    else if(testType == "negative"){
        text=codec->toUnicode("3.6、第三次负压密封压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    if(testType == "positive"){
        picture= "2.6.jpg";
    }
    else if(testType == "negative"){
        picture= "3.6.jpg";
    }
    setTalbeCurve1(word,points5,picture);

    //试验结果
result:
    if(testType == "positive"){
        text=codec->toUnicode("2.7、正压开启压力和密封试验结果");
    }
    else if(testType == "negative"){
        text=codec->toUnicode("3.7、负压开启压力和密封试验结果");
    }
    else if(testType == "capsize"){
        text=codec->toUnicode("4.4、颠覆性密封试验结果");
    }
    word.setFontBold(true);
    word.setFontSize(9);
    word.insertText(text);
    word.insertEnter();
    word.moveForEnd();

    if(testType == "capsize"){
        word.setFontSize(7);
        word.setFontBold(false);
        table = word.createTableWithColor(3, 2);

        text=codec->toUnicode("90°颠覆性密封试验结果");
        word.setCellText(table, 1, 1, text,true);
        word.setCellText(table, 1, 2, resultSealPressure1.toUtf8(),false);

        text=codec->toUnicode("180°颠覆性密封试验结果");
        word.setCellText(table, 2, 1, text,true);
        word.setCellText(table, 2, 2, resultSealPressure2.toUtf8(),false);

        text=codec->toUnicode("270°颠覆性密封试验结果");
        word.setCellText(table, 3, 1, text,true);
        word.setCellText(table, 3, 2, resultSealPressure3.toUtf8(),false);

    }
    else{
        word.setFontSize(7);
        word.setFontBold(false);
        table = word.createTableWithColor(3, 4);

        if(testType == "positive")
            text=codec->toUnicode("第一次正压开启压力（kPa）");
        else if(testType == "negative")
            text=codec->toUnicode("第一次负压开启压力（kPa）");
        word.setCellText(table, 1, 1, text,true);
        word.setCellText(table, 1, 2, resultSetPressure1.toUtf8(),false);
        if(testType == "positive")
            text=codec->toUnicode("第一次正压密封压力（kPa）");
        else if(testType == "negative")
            text=codec->toUnicode("第一次负压密封压力（kPa）");
        word.setCellText(table, 1, 3, text,true);
        word.setCellText(table, 1, 4, resultSealPressure1.toUtf8(),false);

        if(testType == "positive")
            text=codec->toUnicode("第二次正压开启压力（kPa）");
        else if(testType == "negative")
            text=codec->toUnicode("第二次负压开启压力（kPa）");
        word.setCellText(table, 2, 1, text,true);
        word.setCellText(table, 2, 2, resultSetPressure2.toUtf8(),false);
        if(testType == "positive")
            text=codec->toUnicode("第二次正压密封压力（kPa）");
        else if(testType == "negative")
            text=codec->toUnicode("第二次负压密封压力（kPa）");
        word.setCellText(table, 2, 3, text,true);
        word.setCellText(table, 2, 4, resultSealPressure2.toUtf8(),false);

        if(testType == "positive")
            text=codec->toUnicode("第三次正压开启压力（kPa）");
        else if(testType == "negative")
            text=codec->toUnicode("第三次负压开启压力（kPa）");
        word.setCellText(table, 3, 1, text,true);
        word.setCellText(table, 3, 2, resultSetPressure3.toUtf8(),false);
        if(testType == "positive")
            text=codec->toUnicode("第三次正压密封压力（kPa）");
        else if(testType == "negative")
            text=codec->toUnicode("第三次负压密封压力（kPa）");
        word.setCellText(table, 3, 3, text,true);
        word.setCellText(table, 3, 4, resultSealPressure3.toUtf8(),false);

    }

    word.autoFitBehavior(table,wdAutoFitWindow);
    word.moveForEnd();
    word.insertMoveDown();
    delete table;
}


void GenerateWord::receiveGenerateBreatheValveWord(QString savePath, QVariant para,QVariant obj1, QVariant obj2, QVariant obj3)
{
    qDebug()<<"receiveGenerateBreatheValveWord";



    QVariantMap map = para.toMap();
    QString manufacture = map["manufacture"].toString();
    QString type = map["type"].toString();
    QString serialNumber = map["serialNumber"].toString();
    QString user = map["user"].toString();
    QString carplate = map["carplate"].toString();
    QString valve = map["valve"].toString();
    QString setPressure = map["setPressure"].toString();
    QString state = map["state"].toString();

    QString text;
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QString text1 = codec->toUnicode("正在生成报告,请耐心等待");
    QString text2 = codec->toUnicode("正在保存中...");
    QString text3 = codec->toUnicode("报告已完成");
    QString text4 = codec->toUnicode("打开word失败");
    QString title = manufacture+"_"+type+"_"+serialNumber;

    sendWordReportProgress(text1);
    QThread::sleep(1);

    QWord word;
    if( !word.createNewWord(savePath) )
    {
        QString error = tr("Failed to export report,") + word.getStrErrorInfo();
        qDebug()<<error;
        sendWordReportProgress(text4);
        QThread::msleep(500);
        sendWordReportProgress("close");
        return;
    }



    word.setPageOrientation(0);			//页面方向
    word.setWordPageView(3);			//页面视图
    //字体选择
    QString font;
    font=codec->toUnicode("宋体");
    word.setFontName(font);
    //标题
    word.setParagraphAlignment(ALIGNMENT_MIDDLE);		//下面文字位置
    word.setFontSize(10);                               //字体大小
    word.setFontBold(true);                             //字体加粗
    word.insertText(title);
    word.insertMoveDown();
    //1、基本信息 题目
    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    text=codec->toUnicode("1、基本信息");
    word.insertText(text);
    word.insertMoveDown();
    //基本信息表格
    word.setFontSize(7);
    word.setFontBold(false);
    QAxObject *table;
    table = word.createTableWithColor(4, 4);
    text = codec->toUnicode("制造单位");
    word.setCellText(table, 1, 1, text,true);
    word.setCellText(table, 1, 2, manufacture.toUtf8(),false);
    text = codec->toUnicode("阀门型号");
    word.setCellText(table, 1, 3, text,true);
    word.setCellText(table, 1, 4, type.toUtf8(),false);
    text = codec->toUnicode("使用单位");
    word.setCellText(table, 2, 1, text,true);
    word.setCellText(table, 2, 2, user.toUtf8(),false);
    text = codec->toUnicode("出厂编号");
    word.setCellText(table, 2, 3, text,true);
    word.setCellText(table, 2, 4, serialNumber.toUtf8(),false);
    text = codec->toUnicode("车牌号");
    word.setCellText(table, 3, 1, text,true);
    word.setCellText(table, 3, 2, carplate.toUtf8(),false);
    text = codec->toUnicode("阀门类型");
    word.setCellText(table, 3, 3, text,true);
    word.setCellText(table, 3, 4, valve.toUtf8(),false);
    text = codec->toUnicode("阀门状态");
    word.setCellText(table, 4, 1, text,true);
    word.setCellText(table, 4, 2, state.toUtf8(),false);
    //自动对齐
    word.autoFitBehavior(table,wdAutoFitWindow);
    word.moveForEnd();
    word.insertMoveDown();
    delete table;

    breatheValveRecordWrite(word,"positive", obj1);
    breatheValveRecordWrite(word,"negative", obj2);
    breatheValveRecordWrite(word,"capsize", obj3);

    word.insertEnter();
    word.insertEnter();
    word.insertEnter();
    //报告人员:                                   报告日期:
    QString current_Time = QDateTime::currentDateTime().toString("yyyy-MM-dd");
    word.setFontSize(10);
    word.setFontBold(true);
    text = codec->toUnicode("    检测人员:                                   报告日期: ");
    word.insertText(text+current_Time);


    word.saveAs();
    word.close();

    sendWordReportProgress(text3);
    QThread::msleep(500);
    sendWordReportProgress("close");

}




void GenerateWord::receiveGenerateWord(QString savePath, QVariant obj)
{
    qDebug()<<"receiveMsg";

    QVariantMap map = obj.toMap();
    QList<QVariant> points0 = map["pointList0"].toList();
    QList<QVariant> points1 = map["pointList1"].toList();
    QList<QVariant> points2 = map["pointList2"].toList();
    QList<QVariant> points3 = map["pointList3"].toList();
    QList<QVariant> points4 = map["pointList4"].toList();
    QList<QVariant> points5 = map["pointList5"].toList();

    QString manufacture = map["manufacture"].toString();
    QString type = map["type"].toString();
    QString serialNumber = map["serialNumber"].toString();
    QString user = map["user"].toString();
    QString carplate = map["carplate"].toString();
    QString valve = map["valve"].toString();
    QString setPressure = map["setPressure"].toString();
    QString state = map["state"].toString();
    QString resultSetPressure1 = map["resultSetPressure1"].toString();
    QString resultSetPressure2 = map["resultSetPressure2"].toString();
    QString resultSetPressure3 = map["resultSetPressure3"].toString();

    QString resultSealPressure1 = map["resultSealPressure1"].toString();
    QString resultSealPressure2 = map["resultSealPressure2"].toString();
    QString resultSealPressure3 = map["resultSealPressure3"].toString();

    QString text;
    QTextCodec *codec = QTextCodec::codecForName("GBK");
    QString text1 = codec->toUnicode("正在生成报告,请耐心等待");
    QString text2 = codec->toUnicode("正在保存中...");
    QString text3 = codec->toUnicode("报告已完成");
    QString text4 = codec->toUnicode("打开word失败");
    QString title = manufacture+"_"+type+"_"+serialNumber;
    sendWordReportProgress(text1);

    QWord word;
    if( !word.createNewWord(savePath) )
    {
        QString error = tr("Failed to export report,") + word.getStrErrorInfo();
        qDebug()<<error;
        sendWordReportProgress(text4);
        QThread::msleep(500);
        sendWordReportProgress("close");
        return;
    }

    word.setPageOrientation(0);			//页面方向
    word.setWordPageView(3);			//页面视图


    QString font;
    font=codec->toUnicode("宋体");
    word.setFontName(font);

    //标题
    word.setParagraphAlignment(ALIGNMENT_MIDDLE);		//下面文字位置
    word.setFontSize(10);                               //字体大小
    word.setFontBold(true);                             //字体加粗
    word.insertText(title);
    word.insertMoveDown();

    //1、基本信息 题目
    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    text=codec->toUnicode("1、基本信息");
    word.insertText(text);
    word.insertMoveDown();
    //基本信息表格
    word.setFontSize(7);
    word.setFontBold(false);
    QAxObject *table;
    table = word.createTableWithColor(4, 4);
    text = codec->toUnicode("制造单位");
    word.setCellText(table, 1, 1, text,true);
    word.setCellText(table, 1, 2, manufacture.toUtf8(),false);
    text = codec->toUnicode("阀门型号");
    word.setCellText(table, 1, 3, text,true);
    word.setCellText(table, 1, 4, type.toUtf8(),false);
    text = codec->toUnicode("使用单位");
    word.setCellText(table, 2, 1, text,true);
    word.setCellText(table, 2, 2, user.toUtf8(),false);
    text = codec->toUnicode("出厂编号");
    word.setCellText(table, 2, 3, text,true);
    word.setCellText(table, 2, 4, serialNumber.toUtf8(),false);
    text = codec->toUnicode("车牌号");
    word.setCellText(table, 3, 1, text,true);
    word.setCellText(table, 3, 2, carplate.toUtf8(),false);
    text = codec->toUnicode("阀门类型");
    word.setCellText(table, 3, 3, text,true);
    word.setCellText(table, 3, 4, valve.toUtf8(),false);

    text=codec->toUnicode("安全阀");
    if(valve.toUtf8() == text){
        text = codec->toUnicode("整定压力要求");
        word.setCellText(table, 4, 3, text,true);
        word.setCellText(table, 4, 4, setPressure.toUtf8(),false);
    }
    text=codec->toUnicode("真空阀");
    if(valve.toUtf8() == text){
        text = codec->toUnicode("设计外压");
        word.setCellText(table, 4, 3, text,true);
        word.setCellText(table, 4, 4, setPressure.toUtf8(),false);
    }


    text = codec->toUnicode("阀门状态");
    word.setCellText(table, 4, 1, text,true);
    word.setCellText(table, 4, 2, state.toUtf8(),false);

    word.autoFitBehavior(table,wdAutoFitWindow);
    word.moveForEnd();
    word.insertMoveDown();
    delete table;


    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    if(valve == codec->toUnicode("安全阀")){
        text=codec->toUnicode("2、第一次整定压力试验记录表(kPa)");
    }
    else if(valve == codec->toUnicode("真空阀")){
        text=codec->toUnicode("2、第一次开启压力试验记录表(kPa)");
    }
    else if(valve == codec->toUnicode("人孔盖")){
        text=codec->toUnicode("2、第一次泄放压力试验记录表(kPa)");
    }

    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve1(word,points0,"0.jpg");

    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    if(valve == codec->toUnicode("安全阀")){
        text=codec->toUnicode("3、第二次整定压力试验记录表(kPa)");
    }
    else if(valve == codec->toUnicode("真空阀")){
        text=codec->toUnicode("3、第二次开启压力试验记录表(kPa)");
    }
    else if(valve == codec->toUnicode("人孔盖")){
        text=codec->toUnicode("3、第二次泄放压力试验记录表(kPa)");
    }


    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve1(word,points1,"1.jpg");

    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);

    if(valve == codec->toUnicode("安全阀")){
        text=codec->toUnicode("4、第三次整定压力试验记录表(kPa)");
    }
    else if(valve == codec->toUnicode("真空阀")){
        text=codec->toUnicode("4、第三次开启压力试验记录表(kPa)");
    }
    else if(valve == codec->toUnicode("人孔盖")){
        text=codec->toUnicode("4、第三次泄放压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve1(word,points2,"2.jpg");

    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    if(valve == codec->toUnicode("安全阀") || valve == codec->toUnicode("真空阀")){
        text=codec->toUnicode("5、第一次密封压力试验记录表(kPa)");
    }
    else if(valve == codec->toUnicode("人孔盖")){
        text=codec->toUnicode("5、第一次密封压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve1(word,points3,"3.jpg");

    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    if(valve == codec->toUnicode("安全阀") || valve == codec->toUnicode("真空阀")){
        text=codec->toUnicode("6、第二次密封压力试验记录表(kPa)");
    }
    else if(valve == codec->toUnicode("人孔盖")){
        text=codec->toUnicode("6、第二次密封压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve1(word,points4,"4.jpg");

    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    if(valve == codec->toUnicode("安全阀") || valve == codec->toUnicode("真空阀")){
        text=codec->toUnicode("7、第三次密封压力试验记录表(kPa)");
    }
    else if(valve == codec->toUnicode("人孔盖")){
        text=codec->toUnicode("7、第三次密封压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve1(word,points5,"5.jpg");

    //3、处理建议
    text=codec->toUnicode("8、试验结果");
    word.setFontBold(true);
    word.setFontSize(9);
    word.insertText(text);
    word.insertEnter();
    word.moveForEnd();

    if(valve != codec->toUnicode("人孔盖"))
    {
        word.setFontSize(7);
        word.setFontBold(false);
        table = word.createTableWithColor(3, 4);
        if(valve == codec->toUnicode("安全阀")  ){
            text = codec->toUnicode("第一次整定压力（kPa）");
        }
        else if(valve == codec->toUnicode("真空阀"))
        {
            text = codec->toUnicode("第一次开启压力（kPa）");
        }
        word.setCellText(table, 1, 1, text,true);
        word.setCellText(table, 1, 2, resultSetPressure1.toUtf8(),false);

        if(valve == codec->toUnicode("安全阀") || valve == codec->toUnicode("真空阀")){
            text = codec->toUnicode("第一次密封压力（kPa）");
        }
        word.setCellText(table, 1, 3, text,true);
        word.setCellText(table, 1, 4, resultSealPressure1.toUtf8(),false);

        if(valve == codec->toUnicode("安全阀")  ){
            text = codec->toUnicode("第二次整定压力（kPa）");
        }
        else if(valve == codec->toUnicode("真空阀"))
        {
            text = codec->toUnicode("第二次开启压力（kPa）");
        }
        word.setCellText(table, 2, 1, text,true);
        word.setCellText(table, 2, 2, resultSetPressure2.toUtf8(),false);
        if(valve == codec->toUnicode("安全阀") || valve == codec->toUnicode("真空阀")){
            text = codec->toUnicode("第二次密封压力（kPa）");
        }
        word.setCellText(table, 2, 3, text,true);
        word.setCellText(table, 2, 4, resultSealPressure2.toUtf8(),false);


        if(valve == codec->toUnicode("安全阀")  ){
            text = codec->toUnicode("第三次整定压力（kPa）");
        }
        else if(valve == codec->toUnicode("真空阀"))
        {
            text = codec->toUnicode("第三次开启压力（kPa）");
        }
        word.setCellText(table, 3, 1, text,true);
        word.setCellText(table, 3, 2, resultSetPressure3.toUtf8(),false);
        if(valve == codec->toUnicode("安全阀") || valve == codec->toUnicode("真空阀")){
            text = codec->toUnicode("第三次密封压力（kPa）");
        }
        word.setCellText(table, 3, 3, text,true);
        word.setCellText(table, 3, 4, resultSealPressure3.toUtf8(),false);

        word.autoFitBehavior(table,wdAutoFitWindow);
        word.moveForEnd();
        word.insertMoveDown();
        delete table;
    }


    word.insertEnter();
    word.insertEnter();
    word.insertEnter();
    //报告人员:                                   报告日期:
    QString current_Time = QDateTime::currentDateTime().toString("yyyy-MM-dd");
    word.setFontSize(10);
    word.setFontBold(true);
    text = codec->toUnicode("    检测人员:                                   报告日期: ");
    word.insertText(text+current_Time);


    word.saveAs();
    word.close();


    sendWordReportProgress(text3);
    QThread::msleep(500);
    sendWordReportProgress("close");


}


//放弃接口
void GenerateWord::receiveMsg(QString savePath, QString valve, QString manufacture, QString type, QString serialNumber, QString user, QString carplate, QString state, QString setPressure, QList<QPointF> points0, QList<QPointF> points1, QList<QPointF> points2, QList<QPointF> points3, QList<QPointF> points4, QList<QPointF> points5)
{
    qDebug()<<"receiveMsg";

    QString text;
    QTextCodec *codec = QTextCodec::codecForName("GBK");
    QString text1 = codec->toUnicode("正在生成报告");
    QString text2 = codec->toUnicode("正在保存中...");
    QString text3 = codec->toUnicode("报告已完成");
    QString text4 = codec->toUnicode("打开word失败");
    QString title = manufacture+"_"+type+"_"+serialNumber;

    QWord word;
    if( !word.createNewWord(savePath) )
    {
        QString error = tr("Failed to export report,") + word.getStrErrorInfo();
        qDebug()<<error;
        sendWordReportProgress(text4);
        return;
    }


    word.setPageOrientation(0);			//页面方向
    word.setWordPageView(3);			//页面视图


    QString font;
    font=codec->toUnicode("宋体");
    word.setFontName(font);

    //标题
    word.setParagraphAlignment(ALIGNMENT_MIDDLE);		//下面文字位置
    word.setFontSize(10);                               //字体大小
    word.setFontBold(true);                             //字体加粗
    word.insertText(title);
    word.insertMoveDown();

    //1、基本信息 题目
    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    text=codec->toUnicode("1、基本信息");
    word.insertText(text);
    word.insertMoveDown();
    //基本信息表格
    word.setFontSize(7);
    word.setFontBold(false);
    QAxObject *table;
    table = word.createTableWithColor(4, 4);
    text = codec->toUnicode("制造单位");
    word.setCellText(table, 1, 1, text,true);
    word.setCellText(table, 1, 2, manufacture.toUtf8(),false);
    text = codec->toUnicode("阀门型号");
    word.setCellText(table, 1, 3, text,true);
    word.setCellText(table, 1, 4, type.toUtf8(),false);
    text = codec->toUnicode("使用单位");
    word.setCellText(table, 2, 1, text,true);
    word.setCellText(table, 2, 2, user.toUtf8(),false);
    text = codec->toUnicode("出厂编号");
    word.setCellText(table, 2, 3, text,true);
    word.setCellText(table, 2, 4, serialNumber.toUtf8(),false);
    text = codec->toUnicode("车牌号");
    word.setCellText(table, 3, 1, text,true);
    word.setCellText(table, 3, 2, carplate.toUtf8(),false);
    text = codec->toUnicode("阀门类型");
    word.setCellText(table, 3, 3, text,true);
    word.setCellText(table, 3, 4, valve.toUtf8(),false);

    if(valve.toUtf8() == "安全阀"){
        text = codec->toUnicode("整定压力要求");
        word.setCellText(table, 4, 1, text,true);
        word.setCellText(table, 4, 2, setPressure.toUtf8(),false);
    }

    text = codec->toUnicode("阀门状态");
    word.setCellText(table, 4, 3, text,true);
    word.setCellText(table, 4, 4, state.toUtf8(),false);

    word.autoFitBehavior(table,wdAutoFitWindow);
    word.moveForEnd();
    word.insertMoveDown();
    delete table;


    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    text = codec->toUnicode("安全阀");
    if(valve == text){
        text=codec->toUnicode("2、第一次整定压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve(word,points0,"0.jpg");

    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    text = codec->toUnicode("安全阀");
    if(valve == text){
        text=codec->toUnicode("3、第二次整定压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve(word,points1,"1.jpg");

    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    text = codec->toUnicode("安全阀");
    if(valve == text){
        text=codec->toUnicode("4、第三次整定压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve(word,points2,"2.jpg");

    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    text = codec->toUnicode("安全阀");
    if(valve == text){
        text=codec->toUnicode("5、第一次密封压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve(word,points3,"3.jpg");

    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    text = codec->toUnicode("安全阀");
    if(valve == text){
        text=codec->toUnicode("6、第二次密封压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve(word,points4,"4.jpg");

    word.setFontSize(9);
    word.setParagraphAlignment(ALIGNMENT_LEFT);
    word.setFontBold(true);
    text = codec->toUnicode("安全阀");
    if(valve == text){
        text=codec->toUnicode("7、第三次密封压力试验记录表(kPa)");
    }
    word.insertText(text);
    word.insertMoveDown();
    setTalbeCurve(word,points5,"5.jpg");






    //3、处理建议
    text=codec->toUnicode("3、试验结果");
    word.setFontBold(true);
    word.setFontSize(9);
    word.insertText(text);
    word.insertEnter();
    word.moveForEnd();
    table = word.createTable(1, 1);
    word.setCellText(table, 1, 1, "\r\r",false);
    word.autoFitBehavior(table,wdAutoFitWindow);
    word.moveForEnd();
    word.insertMoveDown();
    delete table;


    word.insertEnter();
    word.insertEnter();
    word.insertEnter();
    //报告人员:                                   报告日期:
    QString current_Time = QDateTime::currentDateTime().toString("yyyy-MM-dd");
    word.setFontSize(10);
    word.setFontBold(true);
    text = codec->toUnicode("    报告人员:                                   报告日期: ");
    word.insertText(text+current_Time);


    word.saveAs();
    word.close();
}

//void GenerateWord::receiveMsg(QString savePath,QString valve,QString manufacture, QString type, QString serialNumber, QString user, QString carplate, QList<QPointF> points)
//{
//    //qDebug()<<"receiveMsg";

//    QString text;
//    QTextCodec *codec = QTextCodec::codecForName("GBK");
//    QString text1 = codec->toUnicode("正在生成报告");
//    QString text2 = codec->toUnicode("正在保存中...");
//    QString text3 = codec->toUnicode("报告已完成");
//    QString text4 = codec->toUnicode("打开word失败");
//    QString title = manufacture+"-"+type+"-"+serialNumber;

//    QWord word;
//    if( !word.createNewWord(savePath) )
//    {
//        QString error = tr("Failed to export report,") + word.getStrErrorInfo();
//        qDebug()<<error;
//        sendWordReportProgress(text4);
//        return;
//    }

//    word.setPageOrientation(0);			//页面方向
//    word.setWordPageView(3);			//页面视图


//    QString font;
//    font=codec->toUnicode("宋体");
//    word.setFontName(font);

//    //标题
//    word.setParagraphAlignment(ALIGNMENT_MIDDLE);		//下面文字位置
//    word.setFontSize(10);                               //字体大小
//    word.setFontBold(true);                             //字体加粗
//    word.insertText(title);
//    word.insertMoveDown();

//    //1、基本信息 题目
//    word.setFontSize(9);
//    word.setParagraphAlignment(ALIGNMENT_LEFT);
//    word.setFontBold(true);
//    text=codec->toUnicode("1、基本信息");
//    word.insertText(text);
//    word.insertMoveDown();
//    //基本信息表格
//    word.setFontSize(7);
//    word.setFontBold(false);
//    QAxObject *table;
//    table = word.createTableWithColor(3, 4);
//    text = codec->toUnicode("制造单位");
//    word.setCellText(table, 1, 1, text,true);
//    word.setCellText(table, 1, 2, manufacture.toUtf8(),false);
//    text = codec->toUnicode("阀门型号");
//    word.setCellText(table, 1, 3, text,true);
//    word.setCellText(table, 1, 4, type.toUtf8(),false);
//    text = codec->toUnicode("使用单位");
//    word.setCellText(table, 2, 1, text,true);
//    word.setCellText(table, 2, 2, user.toUtf8(),false);
//    text = codec->toUnicode("阀门编号");
//    word.setCellText(table, 2, 3, text,true);
//    word.setCellText(table, 2, 4, serialNumber.toUtf8(),false);
//    text = codec->toUnicode("车牌号");
//    word.setCellText(table, 3, 1, text,true);
//    word.setCellText(table, 3, 2, carplate.toUtf8(),false);
//    text = codec->toUnicode("阀门类型");
//    word.setCellText(table, 3, 3, text,true);
//    word.setCellText(table, 3, 4, valve.toUtf8(),false);

//    word.autoFitBehavior(table,wdAutoFitWindow);
//    word.moveForEnd();
//    word.insertMoveDown();
//    delete table;

//    //2、压力表格 题目
//    word.setFontSize(9);
//    word.setParagraphAlignment(ALIGNMENT_LEFT);
//    word.setFontBold(true);
//    text=codec->toUnicode("2、压力记录表");
//    word.insertText(text);
//    word.insertMoveDown();
//    //记录表格
//    word.setFontSize(7);
//    word.setFontBold(false);

//    int length = points.length();
//    int integer = length/10;
//    int remainder = length%10;
//    if(remainder > 0)
//        integer += 1;
//    table = word.createTableWithColor(integer, 10);
//    for(int i=0;i<length; i++){
//        QPointF point = points.at(i);
//        QString text = QString::number(point.y());
//        int lineNum = i/10+1;
//        int row = i%10+1;
//        word.setCellText(table, lineNum, row, text,false);
//    }
//    word.autoFitBehavior(table,wdAutoFitWindow);
//    word.moveForEnd();
//    word.insertMoveDown();
//    delete table;
//    //添加曲线图
//    word.AddPicture(QDir::currentPath()+"/1.jpg");
//    word.moveForEnd();
//    word.insertMoveDown();
//    word.insertEnter();

//    //3、处理建议
//    text=codec->toUnicode("3、试验结果");
//    word.setFontBold(true);
//    word.setFontSize(9);
//    word.insertText(text);
//    word.insertEnter();
//    word.moveForEnd();
//    table = word.createTable(1, 1);
//    word.setCellText(table, 1, 1, "\r\r",false);
//    word.autoFitBehavior(table,wdAutoFitWindow);
//    word.moveForEnd();
//    word.insertMoveDown();
//    delete table;


//    word.insertEnter();
//    word.insertEnter();
//    word.insertEnter();
//    //报告人员:                                   报告日期:
//    QString current_Time = QDateTime::currentDateTime().toString("yyyy-MM-dd");
//    word.setFontSize(10);
//    word.setFontBold(true);
//    text = codec->toUnicode("    报告人员:                                   报告日期: ");
//    word.insertText(text+current_Time);


//    word.saveAs();
//    word.close();

//}
