package com.engine.htsc.lhcd.Service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.lhcd.Service.LhcdWorkflowBaseService;
import com.weaver.general.TimeUtil;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

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

/**
 * @author xf
 * @version 1.0.0
 * @ClassName LhcdWorkflowBaseServiceimpl.java
 * @createTime 2021年12月31日 09:55:00
 */
public class LhcdWorkflowBaseServiceimpl extends Service implements LhcdWorkflowBaseService {

    @Override
    public Map<String,Object> checkCoDirectorNotice(String requestid, String nodename){
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        Map<String,Object> resultMap = new HashMap<>();

        try{
            ResourceComInfo resourceComInfo = new ResourceComInfo();

            String nodeid = "";
            if("bmfzr".equals(nodename)){
                nodeid = pb.getNodeIdsByUnNumber(pb.getPropName("lhcd.nodeunnumber.lhcd_bmfzrsh"));
            }else if("hq".equals(nodename)){
                nodeid = pb.getNodeIdsByUnNumber(pb.getPropName("lhcd.nodeunnumber.lhcd_fxgl"));
         }

            List<Map<String,String>> userMapList = new ArrayList<>();
            String sql = "select userid from workflow_currentoperator where requestid = '"+requestid+"' and nodeid = '"+nodeid+"' and isremark = '9'";
            bs.writeLog(sql);
            rs.executeQuery(sql);
            while(rs.next()){
                Map<String,String> userMap = new HashMap<>();
                String userid = Util.null2String(rs.getString("userid"));
                userMap.put("id",userid);
                userMap.put("name",resourceComInfo.getLastname(userid));
                userMapList.add(userMap);
            }

            resultMap.put("result",userMapList);
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultMap;
    }


    /**
     * 会签部门联席负责人抄送
     * @param requestid
     * @param nodeid
     * @param deptid
     */
    @Override
    public Map<String,Object> noticeSignDeptCoDirector(String requestid, String nodeid, String deptid){
        RecordSet rs = new RecordSet();
        BaseBean bs = new BaseBean();
        Map<String,Object> resultMap = new HashMap<>();

        //联席负责人
        String cohead = "";
        String sql = "select cohead from hrmdepartmentdefined where deptid = '"+deptid+"'";
        bs.writeLog(sql);
        rs.executeQuery(sql);
        if(rs.next()){
            cohead = Util.null2String(rs.getString("cohead"));
        }

        if(StringUtils.isNotEmpty(cohead)){
            for(String id:cohead.split(",")){
                String workflowid = "";
                String workflowtype = "";
                sql = "select t1.id,t1.workflowtype from workflow_base t1,workflow_requestbase t2 where t2.workflowid = t1.id and t2.requestid = '"+requestid+"'";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    workflowid = Util.null2String(rs.getString("id"));
                    workflowtype = Util.null2String(rs.getString("workflowtype"));
                }

                char flag = com.weaver.general.Util.getSeparator();
                String operatorgroup = "0";
                String creatertype = "0";
                String currentdate = TimeUtil.getCurrentDateString();
                String currenttime =  TimeUtil.getCurrentTimeString().substring(11,19);
                String isremarktemp = "9";
                String lastnodeid = ""+nodeid;
                String groupdetailid = "-1";
                String useridtemp = id;//抄送人
                String Procparatemp = "" + requestid + flag + useridtemp + flag + operatorgroup + flag + workflowid + flag
                        + workflowtype + flag + creatertype + flag + isremarktemp + flag + lastnodeid + flag + -1
                        + flag + "0" + flag + -1 + flag + groupdetailid+flag+currentdate+flag+currenttime;
                bs.writeLog("chaosong Procparatemp====="+Procparatemp);
                boolean executesuccess = rs.executeProc("workflow_CurrentOperator_I2", Procparatemp);
                resultMap.put("result",executesuccess);

                if(executesuccess){
                    bs.writeLog("CC SUCCESS:"+executesuccess);
                }else{
                    bs.writeLog("CC FAIL:"+executesuccess);
                }
            }
        }
        return resultMap;
    }

}
