package com.engine.htsc.seal.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;

import java.util.*;

public class SealChangeService extends Service {

    /**
     * 根据流程ID获取相关文件
     * @param gllc
     * @return
     */
    public Map<String,Object> getSealInfoByRequestid(String gllc){
        String sql = "select * from uf_yyxx where gllc in ("+gllc+") and sfzz = '0'";

        SealInformationService sealInformationService = ServiceUtil.getService(SealInformationService.class);
        Map<String, Object> resultMap = sealInformationService.getSealInfo(sql);

        return resultMap;
    }

    /**
     * 根据id获取用印信息
     * @param infoids
     * @return
     */
    public Map<String, Object> getSealInfoByInfoids(String infoids){
        String sql = "select * from uf_yyxx where id in ("+infoids+")";

        SealInformationService sealInformationService = ServiceUtil.getService(SealInformationService.class);
        Map<String, Object> resultMap = sealInformationService.getSealInfo(sql);

        return resultMap;
    }

    /**
     * 根据变更类别类型触发用印变更
     * @param user
     * @param requestid
     * @param docids
     * @param bglb
     * @param bglx
     * @return
     */
    public String sendSealChange(User user, String requestid, String docids, String bglb, String bglx){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        JSONObject resobject=new JSONObject();

        SendManualSignService sendManualSignService = ServiceUtil.getService(SendManualSignService.class);
        SendApplyPrintService sendApplyPrintService = ServiceUtil.getService(SendApplyPrintService.class);

        if("3".equals(bglb)||"5".equals(bglx)){//变更用印内容或物理-电子，没送过的，直接送
            return sendManualSignService.sendFromChangeFile(user,requestid,docids);
        }else{
            String tableName = JAVATools.getMaintable(requestid);
            String mainid = "";
            String sql = "select id,gllc from "+tableName+" where requestid = '"+requestid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainid = Util.null2String(rs.getString("id"));
            }

            List<String> oldDocList = new ArrayList<>();
            List<String> newDocList = new ArrayList<>();
            List<String> docidList = Arrays.asList(docids.split(","));
            for(String docid:docidList){
                sql = "select yyid from "+tableName+"_dt1 where mainid = '"+mainid+"' and wjmc = '"+docid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    if(StringUtils.isNotEmpty(Util.null2String(rs.getString("yyid")))){
                        oldDocList.add(docid);
                    }else {
                        newDocList.add(docid);
                    }
                }
            }

            if(oldDocList.size()>0){
                if("0".equals(bglx)||"1".equals(bglx)||"3".equals(bglx)||"4".equals(bglx)){//变更用印信息
                    String oldDocids = String.join(",",oldDocList);
                    String response = sendApplyPrintService.SendApplyPrint(user,requestid,oldDocids,bglx);
                    JSONObject res = JSONObject.parseObject(response);
                    String status = Util.null2String(res.get("status"));
                    if(!"200".equals(status)){
                        return response;
                    }
                }else if("2".equals(bglx)){//单双面打印，取原文件送
                    List<String> ywjList = new ArrayList<>();
                    for(String docid:oldDocList){
                        sql = "select ywj from "+tableName+"_dt1 where mainid = '"+mainid+"' and wjmc = '"+docid+"'";
                        bs.writeLog(sql);
                        rs.executeQuery(sql);
                        if(rs.next()){
                            ywjList.add(Util.null2String(rs.getString("ywj")));
                        }
                    }
                    String ywjDocids = String.join(",",ywjList);
                    String response = sendManualSignService.sendFromChangeFile(user,requestid,ywjDocids);
                    JSONObject res = JSONObject.parseObject(response);
                    String status = Util.null2String(res.get("status"));
                    if(!"200".equals(status)){
                        return response;
                    }
                }
            }

            if(newDocList.size()>0){//没送过的，直接送
                String newDocids = String.join(",",newDocList);
                String response = sendManualSignService.sendFromChangeFile(user,requestid,newDocids);
                JSONObject res = JSONObject.parseObject(response);
                String status = Util.null2String(res.get("status"));
                if(!"200".equals(status)){
                    return response;
                }
            }
        }

        resobject.put("status","200");
        resobject.put("data","用印请求发送成功");
        return resobject.toJSONString();
    }

    /**
     * 用印变更办结后，更新原流程的关联流程
     * @param requestid
     * @param gllc
     */
    public void updateAssociatedProcess(String requestid, String gllc){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();

        for(String reqid:gllc.split(",")){
            String tableName = JAVATools.getMaintable(reqid);
            String oldGllc = "";
            String sql = "select gllc from "+tableName+" where requestid = '"+reqid+"'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                oldGllc = Util.null2String(rs.getString("gllc"));
            }

            List<String> gllcList = new ArrayList<>();
            if(StringUtils.isNotEmpty(oldGllc)){
                gllcList.addAll(Arrays.asList(oldGllc.split(",")));
            }
            gllcList.add(requestid);
            String newGllc = String.join(",",gllcList);

            String update_sql = "update "+tableName+" set gllc = '"+newGllc+"' where requestid = '"+reqid+"'";
            bs.writeLog(update_sql);
            rs.executeUpdate(update_sql);
        }
    }
}
