package weaver.interfaces.htsc.seal;

import com.engine.common.util.ServiceUtil;
import com.engine.htsc.seal.entity.SealInfoEntity;
import com.engine.htsc.seal.service.SealInformationService;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SealInfoToModeAction  extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表

        writeLog("----SealInfoToModeAction-------" + requestid);
        try{
            SealInformationService sealInformationService = ServiceUtil.getService(SealInformationService.class);
            String mainid = "";
            String lcbh = "";
            String ngr = "";
            String yygly = "";
            String yz = "";
            String sfsdfq="";
            String sql = "select id,lcbh,ngr,yz,yzgly,sfsdfq from "+tablename+" where requestid = ?";
            writeLog(sql);
            rs.executeQuery(sql,requestid);
            if(rs.next()){
                mainid = Util.null2String(rs.getString("id"));
                lcbh = Util.null2String(rs.getString("lcbh"));
                ngr = Util.null2String(rs.getString("ngr"));
                // 用印管理员
                yygly = Util.null2String(rs.getString("yzgly"));
                yz = Util.null2String(rs.getString("yz"));
                sfsdfq= Util.null2String(rs.getString("sfsdfq"));
            }
            sql = "select * from "+tablename+"_dt1 where yyzt=3 and mainid = ?";
            writeLog(sql);
            rs.executeQuery(sql,mainid);
            while(rs.next()){
                Map<String,String> map=new HashMap<>();
                Map<String,String> yylxMap=new HashMap<>();
                map.put("dtoneid",Util.null2String(rs.getString("id")));
                map.put("mainid",Util.null2String(rs.getString("mainid")));

                String sfzw=Util.null2String(rs.getString("sfzw"));
                String yyhwj=Util.null2String(rs.getString("yyhwj"));
                String yyfs=Util.null2String(rs.getString("yyfs"));
                String yyfsvalue=yyfs;
                String dzyyfs=Util.null2String(rs.getString("dzyyfs"));
                if("3".equals(yyfs)){
                    //如果用印方式为公文用印件的情况下则
                    yylxMap=Getsealtype(yyhwj);
                    yyfsvalue="0";
                    dzyyfs="1";
                }
                List<Map<String,Object>> yyidlistmap=getyid(map,tablename);
                if(yyidlistmap!=null &&yyidlistmap.size()>0){
                    for(Map<String,Object> yyidmap:yyidlistmap){
                        String value=Util.null2String(yyidmap.get("docid"));
                         if(value.equals("")){
                            continue;
                        }
                        if("3".equals(yyfs) &&yylxMap!=null && yylxMap.size()>0){
                            String dzyylx=Util.null2String(yyidmap.get("dzyylx"));
                            //如果用印方式为公文用印件的情况下则
                            if("1".equals(dzyylx)){
                                dzyyfs="0";
                                yyhwj=Util.null2String(yylxMap.get("fwdocid"));
                            }else {
                                dzyyfs="1";
                                yyhwj=Util.null2String(yylxMap.get("dzdocid"));
                            }
                        }

                        SealInfoEntity sealInfo = new SealInfoEntity();
                        sealInfo.setGllc(requestid); //requestid
                        sealInfo.setLcbh(lcbh);  //流程编号
                        sealInfo.setNgr(ngr);  //拟稿人
                        sealInfo.setYygly(yygly); //用印管理员
                        sealInfo.setYz(yz); //印章
                        if("1".equals(sfzw)){
                            sealInfo.setSfzw(1); //是否正文  0否  1是
                        }else{
                            sealInfo.setSfzw(0);
                        }

                        if("1".equals(sfsdfq)){
                            sealInfo.setLylx(1); //来源类型 0表示其他，1表示公文
                        }else{
                            sealInfo.setLylx(0);
                        }


                        sealInfo.setYywj(yyhwj);//用印文件
                        sealInfo.setYwj(Util.null2String(rs.getString("wjmc")));//用印源文件
                        sealInfo.setYqxdyr(Util.null2String(rs.getString("yqxdyr")));//有权限打印人
                        sealInfo.setYyfs(yyfsvalue);   //用印方式
                        sealInfo.setDzyyfs(dzyyfs);   //电子用印方式
                        sealInfo.setDsmdy(Util.null2String(rs.getString("dsmdy"))); //单双面打印
                        sealInfo.setFwdyfs(Util.null2String(rs.getString("fwdyfs"))); //电子防伪打印份数
                        sealInfo.setWlyyfs(Util.null2String(rs.getString("wlyyfs")));   //物理件打印份数
                        sealInfo.setYyid(value); //传到后端的文件id
                        sealInfo.setSfzz("0");
                        String wjzt=Getwjzt(Util.null2String(rs.getString("yyzt")));
                        sealInfo.setWjzt(wjzt);
                        sealInformationService.createSealInfo(sealInfo);
                    }
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    public List getyid(Map<String,String> map,String tablename){
        String value="";
        List list=new ArrayList();
        List<Map<String,Object>> returnlist=new ArrayList<>();
        RecordSet rs=new RecordSet();
        String mainid=Util.null2String(map.get("mainid"));
        String dtoneid=Util.null2String(map.get("dtoneid"));
        if(Util.null2String(tablename)==""||mainid==""||dtoneid==""){
            return null;
        }
        String sql="select docid,dzyylx from "+tablename+"_dt2 where 1=1 and dzyylx=0  order by id desc ";
        Map<String,Object> dzyylx0= JAVATools.GetTableToMap(sql,map);
        if(dzyylx0!=null && dzyylx0.size()>0){
            returnlist.add(dzyylx0);
        }
        String sql2="select docid,dzyylx from "+tablename+"_dt2 where 1=1 and dzyylx=1  order by id desc ";
        Map<String,Object> dzyylx1= JAVATools.GetTableToMap(sql2,map);
        if(dzyylx1!=null && dzyylx1.size()>0){
            returnlist.add(dzyylx1);
        }
        return returnlist;
    }


    public String Getwjzt(String wjzt){
        String value="0";
        if("2".equals(value)){
            value="1";
        }
        return value;
    }


    public Map<String,String> Getsealtype(String docid){
        Map<String,String> returnma=new HashMap<>();
        Map<String,String> map=new HashMap<>();
        map.put("docid",docid);
      //  String sql=" select docid,imagefilename from docimagefile where docid in (?)";
        //List<Map<String,Object>> list=JAVATools.GetTableToListMap(sql,map);
        String sql=" select docid,imagefilename from docimagefile where docid in ("+docid+")";

        RecordSet rs=new RecordSet();
        rs.execute(sql);
        if(rs.getCounts()>0){
            while (rs.next()){
                String imagefilename= Util.null2String(rs.getString("imagefilename"));
                if(!"".equals(imagefilename) && imagefilename.toLowerCase().indexOf(".rxf")>=0){
                    returnma.put("fwdocid",Util.null2String(rs.getString("docid")));
                }
                if(!"".equals(imagefilename) && imagefilename.toLowerCase().indexOf(".pdf")>=0){
                    returnma.put("dzdocid",Util.null2String(rs.getString("docid")));
                }
            }


        }
        return returnma;
    }

}
