package weaver.interfaces.htsc.itxqlc.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.general.Util;
import weaver.interfaces.htsc.util.HT_Util;
import weaver.interfaces.htsc.util.HT_WorkflowInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-09-24
 * @Description: 申请节点提交后删除明细表数据
 * @ModifyLog:
 *
 * 总部重要事项 拟稿节点提交执行
 **/

public class ItxqlcCreateJiraAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSetDataSource rsd = new RecordSetDataSource("ALM");
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String requestid = requestInfo.getRequestid();
        bb.writeLog("----ItxqlcCreateJiraAction-------" + requestid );
        System.out.println( "----ItxqlcCreateJiraAction-------" + requestid );
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表

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

        try {
//            我们创建IT需求到JIRA的功能，
//            1、用到了t_itapplyrequirement_data 的哪些字段
//            ID–主键id
//            FUNCTIONDESCRIPTION–描述                                             功能需求描述
//            PERFORMANCEDESCRIPTION–描述                                      性能需求描述
//            NOTE–描述                                                                            其他说明事项
//            TITLE–主题                                                                             流程标题
//            ITREQUIREMENTACCEPT–经办人、报告人、需求提出人      需求接收人  ---
//            DRAFTERDEPARTMENT–需求提出部门                                  拟稿人部门
//            PRACTICEBADGE–需求提出人                                               拟稿人工号
//            DRAFTERNAME--需求提出人                                                拟稿人姓名
//            WORKFLOWNUM–需求编号                                                  流程编号
//            STATUS–状态                                                                          未办结/已办结[中文]
//            DOCLIST–附件列表
//            WORKFLOWNUMBER–

            String FUNCTIONDESCRIPTION = "" ;
            String PERFORMANCEDESCRIPTION = "" ;
            String NOTE = "" ;
            String TITLE = "";
            String ITREQUIREMENTACCEPT = "" ;
            String DRAFTERDEPARTMENT = "" ;
            String PRACTICEBADGE = "" ;
            String DRAFTERNAME = "" ;
            String WORKFLOWNUM = "" ;
            String STATUS = "未办结" ;
            String DOCLIST = "" ;
            String DOCID = "" ;
            String WORKFLOWNUMBER = "" ;
            String xqjsr = "" ;

            String sql ="select l.requestid,\n" +
                        "       l.gnxqms,\n" +
                        "       l.xnxqms,\n" +
                        "       l.qtsmsx,\n" +
                        "       h.requestname,\n" +
                        "       l.xqjsr,\n" +
                        "       l.ngr,\n" +
                        "       k.lastname,\n" +
                        "       k.workcode,\n" +
                        "       d.departmentname,\n" +
                        "       l.lcbh,l.fj\n" +
                        "  from "+tablename+" l\n" +
                        " inner join workflow_requestbase h on h.requestid = l.requestid\n" +
                        " left join hrmresource k on k.id = l.ngr\n" +
                        " left join hrmdepartment d on d.id = k.departmentid" +
                        " where h.requestid="+requestid;

            bb.writeLog(sql);
            rs.execute(sql);
            if (rs.next()){
                FUNCTIONDESCRIPTION = Util.null2String(rs.getString("gnxqms"));
                PERFORMANCEDESCRIPTION = Util.null2String(rs.getString("xnxqms"));
                NOTE = Util.null2String(rs.getString("qtsmsx"));
                TITLE = Util.null2String(rs.getString("requestname"));
                xqjsr = Util.null2String(rs.getString("xqjsr"));
                DRAFTERDEPARTMENT = Util.null2String(rs.getString("departmentname"));
                PRACTICEBADGE = Util.null2String(rs.getString("workcode"));
                DRAFTERNAME = Util.null2String(rs.getString("lastname"));
                WORKFLOWNUM = requestid;
                WORKFLOWNUMBER =  Util.null2String(rs.getString("lcbh"));
                DOCID = Util.null2String(rs.getString("fj"));
            }

            if(!"".equals(xqjsr)){
                ItxqlcJiraUtil ItxqlcJiraUtil = new ItxqlcJiraUtil();
                JSONObject jsonObject = ItxqlcJiraUtil.getXqjsrInfo(xqjsr);
                ITREQUIREMENTACCEPT = jsonObject.toJSONString();
            }

            JSONObject jsonObject = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            if(!"".equals(DOCID)){
                sql = " select k.imagefilename,k.fileid,t.id from docdetail t\n" +
                        "inner join docimagefile h on h.docid = t.id\n" +
                        "inner join imagefile k on k.imagefileid = h.imagefileid\n" +
                        "where t.id in("+DOCID+")" ;
                rs.execute(sql);
                while (rs.next()){
                    String imagefilename = Util.null2String(rs.getString("imagefilename"));
                    String fileid = Util.null2String(rs.getString("fileid"));
                    String id = Util.null2String(rs.getString("id"));
                    JSONObject dataObject = new JSONObject();
                    dataObject.put("attachName",imagefilename);
                    dataObject.put("fileId",fileid);
                    dataObject.put("docId",id);
                    jsonArray.add(dataObject);
                }
            }

            bb.writeLog("WORKFLOWNUMBER:"+WORKFLOWNUMBER);
            jsonObject.put("attachmentList",jsonArray);
//            "attachmentList": [
//            {
//                "attachName": "总部付款流程问题整理.docx",
//                    "fileId": "1628666678806_000_000_56E6614280D9B070FA0CCE090600C162C4A51B74A8F23267B3774D3AB40015C2",
//                    "docId": "29289"
//            },
//            {
//                "attachName": "总部付款流程问题整理__郭宁宁__004922反馈.docx",
//                    "fileId": "1628666679903_000_000_86A03B2C87E7C9815B480BAC649B64B459D4E51173FFEC37948EDD8F2817171D",
//                    "docId": "29290"
//            }


            String nowdate = sdf.format(new Date());

            sql = " insert into t_itapplyrequirement_data(FUNCTIONDESCRIPTION,PERFORMANCEDESCRIPTION,NOTE,TITLE,ITREQUIREMENTACCEPT,DRAFTERDEPARTMENT,PRACTICEBADGE,DRAFTERNAME,WORKFLOWNUM,WORKFLOWNUMBER,STATUS,createTime,concludeTime,DOCLIST) " +
                  " values('"+FUNCTIONDESCRIPTION+"','"+PERFORMANCEDESCRIPTION+"','"+NOTE+"','"+TITLE+"','"+ITREQUIREMENTACCEPT+"','"+DRAFTERDEPARTMENT+"','"+PRACTICEBADGE+"','"+DRAFTERNAME+"','"+WORKFLOWNUM+"','"+WORKFLOWNUMBER+"','"+STATUS+"','"+nowdate+"','"+nowdate+"','"+jsonObject.toJSONString()+"')";
            bb.writeLog("----sql-------" + sql );
            rsd.execute(sql);
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
            return Action.FAILURE_AND_CONTINUE;
        }

        return Action.SUCCESS;
    }


}
