package com.engine.htsc.seal.web;

import com.alibaba.fastjson.JSON;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.seal.entity.SealInfoEntity;
import com.engine.htsc.seal.service.SealInformationService;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.cus.HTUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WorkflowSealInfoImportAction {

    @POST
    @Path("/importSealInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String importSealInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        Map<String, Object> apidatas = new HashMap<String, Object>();

        try{
            String workflowid = request.getParameter("workflowid");

            SealInformationService sealInformationService = ServiceUtil.getService(SealInformationService.class);
            HTUtil ht = new HTUtil();
            String tablename = ht.getTableNameByWfid(Integer.parseInt(workflowid));

            List<String> requestidList = new ArrayList<>();
            List<String> importRequestidList = new ArrayList<>();
            String sql = "select requestid from workflow_requestbase where workflowid = '"+workflowid+"' and currentnodetype = '3'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while (rs.next()){
                requestidList.add(Util.null2String(rs.getString("requestid")));
            }

            for(String requestid:requestidList){
                int count = 0;
                sql = "select count(*) count from uf_yyxx where gllc = '"+requestid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    count = Util.getIntValue(rs.getString("count"));
                }

                if(count==0){
                    importRequestidList.add(requestid);
                    String mainid = "";
                    String lcbh = "";
                    String ngr = "";
                    String yygly = "";
                    String yz = "";
                    sql = "select id,lcbh,ngr,yzgly,yz from "+tablename+" where requestid = '"+requestid+"'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    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"));
                    }

                    sql = "select * from "+tablename+"_dt7 where mainid = '"+mainid+"'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    while(rs.next()){
                        SealInfoEntity sealInfo = new SealInfoEntity();
                        sealInfo.setGllc(requestid);
                        sealInfo.setLcbh(lcbh);
                        sealInfo.setNgr(ngr);
                        sealInfo.setYygly(yygly);
                        sealInfo.setYz(yz);
                        sealInfo.setYywj(Util.null2String(rs.getString("yyhwj")));
                        sealInfo.setYwj(Util.null2String(rs.getString("wjmc")));
                        sealInfo.setYqxdyr(Util.null2String(rs.getString("yqxdyr")));
                        sealInfo.setYyfs(Util.null2String(rs.getString("yyfs")));
                        sealInfo.setDzyyfs(Util.null2String(rs.getString("dzyyfs")));
                        sealInfo.setDsmdy(Util.null2String(rs.getString("dsmdy")));
                        sealInfo.setFwdyfs(Util.null2String(rs.getString("fwdyfs")));
                        sealInfo.setWlyyfs(Util.null2String(rs.getString("wlyyfs")));
                        sealInfo.setYyid(Util.null2String(rs.getString("yyid")));
                        sealInfo.setSfzz("0");
                        sealInfo.setWjzt(checkoutWjzt(Util.null2String(rs.getString("yyzt"))));
                        sealInformationService.createSealInfo(sealInfo);
                    }

                    sql = "select * from "+tablename+"_dt8 where mainid = '"+mainid+"'";
                    bs.writeLog(sql);
                    rs.executeQuery(sql);
                    while(rs.next()){
                        SealInfoEntity sealInfo = new SealInfoEntity();
                        sealInfo.setGllc(requestid);
                        sealInfo.setLcbh(lcbh);
                        sealInfo.setNgr(ngr);
                        sealInfo.setYygly(yygly);
                        sealInfo.setYz(yz);
                        sealInfo.setYywj(Util.null2String(rs.getString("yyhwj")));
                        sealInfo.setYwj(Util.null2String(rs.getString("wjmc")));
                        sealInfo.setYqxdyr(Util.null2String(rs.getString("yqxdyr")));
                        sealInfo.setYyfs(Util.null2String(rs.getString("yyfs")));
                        sealInfo.setDzyyfs(Util.null2String(rs.getString("dzyyfs")));
                        sealInfo.setDsmdy(Util.null2String(rs.getString("dsmdy")));
                        sealInfo.setFwdyfs(Util.null2String(rs.getString("fwdyfs")));
                        sealInfo.setWlyyfs(Util.null2String(rs.getString("wlyyfs")));
                        sealInfo.setYyid(Util.null2String(rs.getString("yyid")));
                        sealInfo.setSfzz("1");
                        sealInfo.setWjzt(checkoutWjzt(Util.null2String(rs.getString("yyzt"))));
                        sealInformationService.createSealInfo(sealInfo);
                    }
                }

            }
            apidatas.put("requestidList", JSON.toJSONString(requestidList));
            apidatas.put("importRequestidList", JSON.toJSONString(importRequestidList));
            apidatas.put("api_status", true);
        }catch(Exception e){
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return JSON.toJSONString(apidatas);
    }

    private String checkoutWjzt(String yyzt){
        String wjzt = "0";
        if("2".equals(yyzt)){
            wjzt = "1";
        }
        return wjzt;
    }

}
