package com.engine.htsc.announcement.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.announcement.service.HeadquartersAnncService;
import com.engine.htsc.announcement.util.WeaToolsUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.formmode.virtualform.VirtualFormHandler;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.workflow.workflow.WorkflowBillComInfo;

import java.util.*;

public class HeadquartersAnncServiceImpl extends Service implements HeadquartersAnncService {

    private Log log = LogFactory.getLog(this.getClass());
    BaseBean bb = new BaseBean();

    @Override
    public Map<String, Object> isPostion(String userid) {
        Map<String,Object> datas = new HashMap<>();
        try {
            RecordSet rs = new RecordSet();
            String field12 = "";
            rs.executeQuery("select field12 from cus_fielddata where id = ?",userid);
            if (rs.next()){
                field12 = Util.null2String(rs.getString("field12"));
            }
            if ("营业部正职".equals(field12)){
                datas.put("isPostion",true);
            }else {
                datas.put("isPostion",false);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return datas;
    }

    @Override
    public Map<String, Object> afterLeadSign(String jobs,Integer requestid) {
        Map<String,Object> datas = new HashMap<>();
        try {
            boolean sendOut = true;//是否全部送完领导会签 true已送完
            log.info("岗位信息==="+jobs);
            PropBean pb = new PropBean();
            RecordSet rs = new RecordSet();
            //1.根据岗位获取对应人员
            if (!"".equals(jobs)) {
                String[] jobsarray = jobs.split(",");
                for (String job : jobsarray) {
                    String nodeunnumber = "";
                    //2.根据岗位类型不同获取nodeunnumber值
                    if ("1".equals(job)) {//合规总监
                        nodeunnumber = "zbggfb_hgzj";
                    } else if ("2".equals(job)) { //首席风险官
                        nodeunnumber = "zbggfb_sxfxg";
                    } else if ("3".equals(job)) { //工会主席
                        nodeunnumber = "zbggfb_gwhz";
                    } else if ("4".equals(job)) { //首席财务官
                        nodeunnumber = "zbggfb_sxcwg";
                    }
                    String nodeid = pb.getActionNodeIdByUnNumber(nodeunnumber);
                    log.info("nodeid=="+nodeid+"=="+nodeunnumber);
                    //判断是否已经经过领导会签节点
                    rs.executeQuery("select DISTINCT userid from workflow_currentoperator " +
                            " where requestid = ? and  nodeid = ? and isremark <> 0 ", requestid, nodeid);
                    if (rs.next()) {
                        log.info(nodeunnumber + "该节点已经经过");
                    } else {
                        log.info(nodeunnumber+"该节点未经过");
                        sendOut = false;
                        break;//结束循环
                    }
                }
            }
            datas.put("sendAllOut",sendOut);

        }catch (Exception e){
            e.printStackTrace();
        }
        return datas;
    }

    @Override
    public Map<String, Object> signDepartment(String formid, Integer requestid, String workflowid) {
        Map<String,Object> datas = new HashMap<>();
        RecordSet rs = new RecordSet();
        try {
            log.info("===signDepartment===执行开始");
            WorkflowBillComInfo workflowBillComInfo = new WorkflowBillComInfo();
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            String tableName = VirtualFormHandler.getRealFromName(Util.null2String(workflowBillComInfo.getTablename(formid)));
            //1.获取明细表2的值
            List<Map> dataList = new ArrayList<>();
            rs.executeQuery("select b.hqbm,b.bmzz from "+tableName+" a, "+tableName+"_dt2 b " +
                    " where a.id = b.mainid and a.requestid = ? and b.yhq = 1",requestid);
            while (rs.next()){
                Map<String,Object> specialMap = new HashMap<>();
                String hqbm = Util.null2String(rs.getString("hqbm"));
                String departmentName = departmentComInfo.getDepartmentName(hqbm);
                Integer bmzz = Util.getIntValue(rs.getString("bmzz"));
                specialMap.put("departmentId",hqbm);
                specialMap.put("departmentName",departmentName);
                specialMap.put("bmzz",bmzz);
                dataList.add(specialMap);
            }
            log.info("==明细表2数据=="+dataList.toString());
            datas.put("datas",dataList);

        }catch (Exception e){
            e.printStackTrace();
        }
        return datas;
    }

    @Override
    public Map<String, Object> createHeadAnnouncementWorkflow(Map<String, Object> params) {
        Map<String,Object> datas = new HashMap<>();
        RecordSet rs = new RecordSet();
        try{
            String ecologyContentPath = GCONST.getContextPath();
            String userid = (String)params.get("ngr");//拟稿人工号
            String workflowid = (String)params.get("workflowid");
            String requestName = (String)params.get("requestName");
            String IsNextFlow = (String)params.get("IsNextFlow");//1提交到下一节点，0不提交
            Map<String,String> requestMainMap = new HashMap<>();
            requestMainMap.put("lcbt",requestName);
            requestMainMap.put("ngr",userid);
            int requestid = Util.getIntValue(new WeaToolsUtil().workflowCreateByRequestMap(userid, workflowid, requestName,
                    IsNextFlow, requestMainMap));
            bb.infoLog("requestid:",requestid);
            long timestamp = System.currentTimeMillis();
            if (requestid > 0){
                datas.put("url",ecologyContentPath+"/spa/workflow/static4form/index.html?" +
                        "_rdm="+timestamp+"#/main/workflow/req?requestid="+requestid+"&_workflowid="+workflowid+"&_workflowtype=&isovertime=0");
                datas.put("info","create workflow success");
            }else {
                datas.put("url","");
                datas.put("info","create workflow fail");

            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return datas;
    }
}
