package weaver.interfaces.htsc.itlxlc.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.docs.docs.DocComInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
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 java.util.HashMap;
import java.util.Map;

/**
 * It立项申请办结节点更新预算数据
 */
public class ItProjectApplyUpdateDataAction extends BaseBean implements Action {
    private Log log = LogFactory.getLog(this.getClass());

    @Override
    public String execute(RequestInfo requestInfo) {
        try{
            HTUtil htUtil = new HTUtil();
            RecordSet rs = new RecordSet();
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            String tablename = requestInfo.getRequestManager().getBillTableName();
            String requestid = requestInfo.getRequestid();
            JSONObject datas = new JSONObject();
            log.info("requestid:"+requestid+"==tablename:"+tablename);
            rs.executeQuery("select requestid,ngr,xmfzr,xmbh,lcbh,sbwj,fj,nf from " + tablename + " where requestid = ?",requestid);
            String sbwj = ""; //申报文件
            String fj = ""; //附件
            String nf = ""; //年份
            //主表
            if (rs.next()){
                String ngr = Util.null2String(rs.getString("ngr"));
                String ngrLoginID = resourceComInfo.getLoginID(ngr);
                String xmfzr = Util.null2String(rs.getString("xmfzr"));
                String fzrLoginID = resourceComInfo.getLoginID(xmfzr);
                String xmbh = Util.null2String(rs.getString("xmbh"));
                String lcbh = Util.null2String(rs.getString("lcbh"));
                sbwj = Util.null2String(rs.getString("sbwj"));
                fj = Util.null2String(rs.getString("fj"));
                nf = Util.null2String(rs.getString("nf"));
                datas.put("badge",ngrLoginID);
                datas.put("projectDirector",fzrLoginID);
                datas.put("projectCode",xmbh);
                datas.put("workFlowNum",lcbh);
            }

            log.info("申报文件："+sbwj+"==附件:"+fj);

            //正文和附件
            DocComInfo docComInfo =new DocComInfo();
            JSONArray filesArray = new JSONArray();
            JSONObject fileObject = new JSONObject();
            String docname = docComInfo.getDocname(sbwj);
            rs.executeQuery(" select * from (select b.fileid,row_number() over(partition by docid order by versionid desc) as xh from docimagefile a, imagefile b " +
                    " where a.imagefileid = b.imagefileid and docid = ? ) l where l.xh = 1",sbwj);
            if (rs.next()){
                sbwj = Util.null2String(rs.getString("fileid"));
            }
            fileObject.put("field",sbwj);
            fileObject.put("fieldname",docname);
            fileObject.put("requestid",requestid);
            fileObject.put("flag",0);
            filesArray.add(fileObject);



            if (!"".equals(fj)){
                String[] split = fj.split(",");
                for (String value : split){
                    JSONObject attachObject = new JSONObject();
                    String attachname = docComInfo.getDocname(value);
                    rs.executeQuery(" select * from ( select b.fileid,row_number() over(partition by docid order by versionid desc) as xh from docimagefile a, imagefile b " +
                            " where a.imagefileid = b.imagefileid and docid = ? ) l where l.xh = 1",value);
                    if (rs.next()){
                        value = Util.null2String(rs.getString("fileid"));
                    }
                    attachObject.put("field",value);
                    attachObject.put("fieldname",attachname);
                    attachObject.put("requestid",requestid);
                    attachObject.put("flag",1);
                    filesArray.add(attachObject);
                }
            }

            datas.put("attachments",filesArray);

            String dt6 = tablename+"_dt6";
            String dt7 = tablename+"_dt7";
            String dt8 = tablename+"_dt8";
            JSONArray dt7Array = new JSONArray();
            //明细表6
            rs.executeQuery("select b.fycdbm,b.yskmbm,b.jewyhs from "+tablename+" a, "+dt6+" b " +
                    " where a.id = b.mainid and requestid = ?",requestid);
            while (rs.next()){
                JSONObject dt7Object = new JSONObject();
                String fycdbm = Util.null2String(rs.getString("fycdbm"));
                String departmentCode = departmentComInfo.getDepartmentCode(fycdbm);
                String yskmbm = Util.null2String(rs.getString("yskmbm"));
                String jewyhs = Util.null2String(rs.getString("jewyhs"));
                String year = String.valueOf(Integer.parseInt(nf) + 1);
                dt7Object.put("departmentcode",departmentCode);
                dt7Object.put("budgetItemcode",yskmbm);
                dt7Object.put("money",jewyhs);
                dt7Object.put("year",year);
                dt7Array.add(dt7Object);
            }

            //明细表7
            rs.executeQuery("select b.fycdbm,b.yskmbm,b.jewyhs from "+tablename+" a, "+dt7+" b " +
                    " where a.id = b.mainid and requestid = ?",requestid);
            while (rs.next()){
                JSONObject dt7Object = new JSONObject();
                String fycdbm = Util.null2String(rs.getString("fycdbm"));
                String departmentCode = departmentComInfo.getDepartmentCode(fycdbm);
                String yskmbm = Util.null2String(rs.getString("yskmbm"));
                String jewyhs = Util.null2String(rs.getString("jewyhs"));
                String year = String.valueOf(Integer.parseInt(nf) + 1);
                dt7Object.put("departmentcode",departmentCode);
                dt7Object.put("budgetItemcode",yskmbm);
                dt7Object.put("money",jewyhs);
                dt7Object.put("year",year);
                dt7Array.add(dt7Object);
            }

            rs.executeQuery("select b.fycdbm,b.yskmbm,b.jewyhs from "+tablename+" a, "+dt8+" b " +
                    " where a.id = b.mainid and requestid = ?",requestid);
            while (rs.next()){
                JSONObject dt7Object = new JSONObject();
                String fycdbm = Util.null2String(rs.getString("fycdbm"));
                String departmentCode = departmentComInfo.getDepartmentCode(fycdbm);
                String yskmbm = Util.null2String(rs.getString("yskmbm"));
                String jewyhs = Util.null2String(rs.getString("jewyhs"));
                String year = String.valueOf(Integer.parseInt(nf) + 1);
                dt7Object.put("departmentcode",departmentCode);
                dt7Object.put("budgetItemcode",yskmbm);
                dt7Object.put("money",jewyhs);
                dt7Object.put("year",year);
                dt7Array.add(dt7Object);
            }

            datas.put("budgetData",dt7Array);

            log.info("参数详情"+requestid+"==="+datas.toString());
            PropBean propBean = new PropBean();
            String itProjectApply = propBean.getPropName("ItProjectApply");
            String url = itProjectApply + "/jtxt/ITProjects/updateBudgetData";
            String encode = "utf-8";
            Map<String, String> headers = new HashMap<>();
            log.info("url:"+url);
            String content = htUtil.httpPostRaw(url, datas.toString(), headers, encode);
            log.info("content结果集："+ content);

        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("更新预算数据失败");
        }
        return Action.SUCCESS;
    }


}




