package weaver.interfaces.htsc.hthy.action;

import java.util.HashMap;
import java.util.Map;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.hthy.MeetingSmsNotice;
import weaver.soa.workflow.request.RequestInfo;



/**
 * 说明
 * 修改时
 * 类名要与文件名保持一致
 * class文件存放位置与路径保持一致。
 * 请把编译后的class文件，放在对应的目录中才能生效
 * 注意 同一路径下java名不能相同。
 * @author Administrator
 *
 */
public class HthyUpdateAction extends AbstractModeExpandJavaCodeNew {

    
    /**
     * 执行模块扩展动作
     * @param param
     *  param包含(但不限于)以下数据
     *  user 当前用户
     *  importtype 导入方式(仅在批量导入的接口动作会传输) 1 追加，2覆盖,3更新，获取方式(int)param.get("importtype")
     *  导入链接中拼接的特殊参数(仅在批量导入的接口动作会传输)，比如a=1，可通过param.get("a")获取参数值
     *  页面链接拼接的参数，比如b=2,可以通过param.get("b")来获取参数
     * @return
     */
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User)param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
            if(requestInfo!=null){
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if(billid>0&&modeid>0){
                    //------请在下面编写业务逻辑代码------
                    //获得明细表1的数量
                    RecordSet sqlNum1 = new RecordSet();
                    sqlNum1.execute("SELECT count(*)num1 FROM UF_HYWH_DT1 uhd WHERE mainid = "+billid);
                    Integer num1 = 0;
                    if(sqlNum1.next()){
                        num1 = Util.getIntValue(sqlNum1.getString("num1"));
                    }
                    //获得明细表2的数量
                    RecordSet sqlNum2 = new RecordSet();
                    sqlNum2.execute("SELECT count(*)num2 FROM UF_HYWH_DT2 uhd2 WHERE mainid= "+billid);
                    Integer num2 = 0;
                    if (sqlNum2.next()){
                        num2 = Util.getIntValue(sqlNum2.getString("num2"));
                    }
                    Integer numAll = num1 + num2;
                    //获得会议议案通过和未通过数量
                    RecordSet sqlYiAn = new RecordSet();
                    sqlYiAn.execute("SELECT * FROM  (SELECT count(*)count1 FROM UF_HYWH_DT3 uhd WHERE mainid = "+billid+" AND bjjg = 1),\n" +
                            " (SELECT count(*)count2 FROM UF_HYWH_DT3 uhd WHERE mainid = "+billid+" AND bjjg = 2),\n" +
                            "  (SELECT count(*)count3 FROM UF_HYWH_DT3 uhd WHERE mainid = "+billid+")");
                    Integer countPass = 0;
                    Integer countNotPass = 0;
                    Integer countAll = 0;
                    if(sqlYiAn.next()){
                        countPass = Util.getIntValue(sqlYiAn.getString("count1"));
                        countNotPass = Util.getIntValue(sqlYiAn.getString("count2"));
                        countAll = Util.getIntValue(sqlYiAn.getString("count3"));
                    }
                    //更新主表
                    RecordSet sqlUpdate = new RecordSet();
                    sqlUpdate.execute("UPDATE uf_hywh SET chrs = "+numAll+",yasl = "+countAll+",tgyas = '"+countPass+"/"+countAll+"' WHERE id = "+billid);
                    //更新会议文件版本和时间
                    RecordSet sqlFour = new RecordSet();
                    sqlFour.execute("SELECT id,wjmc FROM UF_HYWH_DT4 uhd WHERE MAINID  = "+billid);
                    while (sqlFour.next()){
                        Integer id = Util.getIntValue(sqlFour.getString("id"));
                        String wjmc = Util.null2String(sqlFour.getString("wjmc"));
                        if (!wjmc.equals("")){
                            RecordSet sqlFourFind = new RecordSet();
                            sqlFourFind.execute("SELECT count(*)num  FROM DOCIMAGEFILE d WHERE docid = "+wjmc);
                            if (sqlFourFind.next()){
                                Integer num = Util.getIntValue(sqlFourFind.getString("num"));
                                RecordSet sqlFourDate = new RecordSet();
                                sqlFourDate.execute("SELECT OPERATEDATE FROM (SELECT * FROM DOCIMAGEFILE WHERE docid ="+wjmc+" ORDER BY OPERATEDATE desc)t WHERE rownum = 1");
                                String operatedate = "";
                                if (sqlFourDate.next()){
                                    operatedate = Util.null2String(sqlFourDate.getString("OPERATEDATE"));
                                }
                                RecordSet sqlUpdateFour = new RecordSet();
                                sqlUpdateFour.execute("UPDATE UF_HYWH_DT4 SET BBXX = 'v"+num+"',ZJXGRQ = '"+operatedate+"' WHERE id = "+id);
                            }
                        }
                    }
                    //更新会议议案版本和时间
                    RecordSet sqlYA = new RecordSet();
                    sqlYA.execute("SELECT id,ytmc FROM UF_HYWH_DT3 uhd  WHERE mainid = "+billid);
                    while (sqlYA.next()){
                        Integer id = Util.getIntValue(sqlYA.getString("id"));
                        String ytmc = Util.null2String(sqlYA.getString("ytmc"));
                        if (!ytmc.equals("")){
                            RecordSet sqlFourFind = new RecordSet();
                            sqlFourFind.execute("SELECT count(*)num  FROM DOCIMAGEFILE d WHERE docid = "+ytmc);
                            if (sqlFourFind.next()){
                                Integer num = Util.getIntValue(sqlFourFind.getString("num"));
                                RecordSet sqlFourDate = new RecordSet();
                                sqlFourDate.execute("SELECT OPERATEDATE FROM (SELECT * FROM DOCIMAGEFILE WHERE docid ="+ytmc+" ORDER BY OPERATEDATE desc)t WHERE rownum = 1");
                                String operatedate = "";
                                if (sqlFourDate.next()){
                                    operatedate = Util.null2String(sqlFourDate.getString("OPERATEDATE"));
                                }
                                RecordSet sqlUpdateFour = new RecordSet();
                                sqlUpdateFour.execute("UPDATE UF_HYWH_DT3 SET BBH = 'v"+num+"',XGRQ = '"+operatedate+"' WHERE id = "+id);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            result.put("errmsg","自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

}