package weaver.interfaces.htsc.zbgg.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ApproveBulletinAction extends BaseBean implements Action {
    private Log log = LogFactory.getLog(this.getClass());
    @Override
    public String execute(RequestInfo requestInfo) {
        HTUtil htUtil = new HTUtil();
        String requestid = Util.null2String(requestInfo.getRequestid());
        RequestManager requestManager = requestInfo.getRequestManager();
        PropBean propBean = new PropBean();
        String ApproveBulletin = propBean.getPropName("getInfoToIbull"); //测试环境ip地址
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取主表名
        String tableName = requestManager.getBillTableName();
        log.info("tableName: "+tableName+"==requestid "+requestid);

        //主表
        Long id;
        String ggbh = "";
        String oaId = "";
        Short status = 3;
        String zw = "";
        String fj = "";
        String badge = "";
        String name = "";
        JSONObject datas = new JSONObject();
        RecordSet rs = new RecordSet();
        try {
            String mainsql = "select a.ggbh as ggbh,a.lcbh as lcbh,a.zw as zw,a.fj as fj,b.loginid as badge,b.lastname as name from " + tableName + " a,hrmresource b where a.ngr = b.id and requestid = " + requestid;
            rs.execute(mainsql);
            log.info("查询主表sql: " + mainsql);
            if(rs.next()){
                ggbh = Util.null2String(rs.getString("ggbh"));
                oaId = Util.null2String(rs.getString("lcbh"));
                zw = Util.null2String(rs.getString("zw"));
                fj = Util.null2String(rs.getString("fj"));
                badge = Util.null2String(rs.getString("badge"));
                name = Util.null2String(rs.getString("name"));
            }
            if(!"".equals(ggbh)){
                id = Long.parseLong(ggbh);
                datas.put("id",id);
                datas.put("oaId",oaId);
                datas.put("status",status);
                //正文
                JSONArray bulletinAnnexArray = new JSONArray();
                JSONObject fileObject = new JSONObject();
                String zwName = "";
                String zwfileId = "";
                String zwuploadTime = "";
                String zwsql = "select a.imagefilename as filename,a.operatedate as operatedate,a.operatetime as operatetime,b.fileid as fileid from docimagefile a, imagefile b where a.imagefileid = b.imagefileid and docid =" + zw;
                rs.execute(zwsql);
                if(rs.next()){
                    zwName = Util.null2String(rs.getString("filename"));
                    zwfileId = Util.null2String(rs.getString("fileid"));
                    zwuploadTime = Util.null2String(rs.getString("operatedate")) + " " + Util.null2String(rs.getString("operatetime"));
                }

                Date uploadTime1 = new Date();
                try {
                    uploadTime1 = sdf.parse(zwuploadTime);
                }catch (Exception e) {
                    log.error("正文上传时间转换date格式异常");
                    e.printStackTrace();
                }
                fileObject.put("annexName",zwName);
                fileObject.put("annexCeName",zwName);
                fileObject.put("annexCeId",zwfileId);
                fileObject.put("annexType",1);
                fileObject.put("uploadPname",name);
                fileObject.put("uploadWorkno",badge);
                fileObject.put("uploadTime",uploadTime1);
                bulletinAnnexArray.add(fileObject);

                //附件
                if(!"".equals(fj)){
                    String[] split = fj.split(",");
                    for(String value : split){
                        JSONObject attachObject = new JSONObject();
                        String fjname = "";
                        String fjfileid = "";
                        String fjuploadTime = "";
                        String fjsql = "select a.imagefilename as filename,a.operatedate as operatedate,a.operatetime as operatetime，b.fileid as fileid from docimagefile a, imagefile b where a.imagefileid = b.imagefileid and docid =" + value;;
                        rs.execute(fjsql);
                        if(rs.next()){
                            fjname = Util.null2String(rs.getString("filename"));
                            fjfileid = Util.null2String(rs.getString("fileid"));
                            fjuploadTime = Util.null2String(rs.getString("operatedate")) + " " + Util.null2String(rs.getString("operatetime"));
                        }
                        Date uploadTime2 = new Date();
                        try {
                            uploadTime2 = sdf.parse(fjuploadTime);
                        }catch (Exception e) {
                            log.error("附件上传时间转换date格式异常");
                            e.printStackTrace();
                        }
                        attachObject.put("annexName",fjname);
                        attachObject.put("annexCeName",fjname);
                        attachObject.put("annexCeId",fjfileid);
                        attachObject.put("annexType",2);
                        attachObject.put("uploadPname",name);
                        attachObject.put("uploadWorkno",badge);
                        attachObject.put("uploadTime",uploadTime2);
                        bulletinAnnexArray.add(attachObject);
                    }
                }

                datas.put("bulletinAnnex",bulletinAnnexArray);
                log.info("参数详情"+requestid+"==="+datas.toString());


                String encode = "utf-8";
                Map<String, String> headers = new HashMap<>();
                log.info("url:"+ApproveBulletin);
                String content = htUtil.httpPostRaw(ApproveBulletin, datas.toString(), headers, encode);
                log.info("content结果集："+ content);
            }else{
                log.info("公告编号为空，该流程非外部平台触发，无需回调接口");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return Action.FAILURE_AND_CONTINUE;
        }

        return Action.SUCCESS;
    }
}
