package com.api.customization.qc3189074.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.kq.log.KQLog;
import com.engine.workflow.constant.PAResponseCode;
import com.engine.workflow.entity.publicApi.PAResponseEntity;
import com.engine.workflow.publicApi.WorkflowRequestOperatePA;
import com.engine.workflow.publicApi.impl.WorkflowRequestOperatePAImpl;
import com.wbi.util.Util;
import weaver.general.BaseBean;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class CustomRequestUtil {
    public KQLog kqLog = new KQLog();
    public String  wfId = new BaseBean().getPropValue("qc3189074","wfid");//流程id
    String tableDetailName = new BaseBean().getPropValue("qc3189074","tableDetailName");//排班流程明细表

    /**
     * 新建流程
     * @param user
     * @param wfId
     * @param mainTable
     * @param detaiTable
     */
    public Integer createRequest(User user, String wfId, JSONArray mainTable,JSONArray detaiTable) {
        new BaseBean().writeLog("==zj==(创建流程方法进入1111)");

        int requestid = 0;     //创建流程id
        try {
            WorkflowRequestOperatePA workflowRequestOperatePA= ServiceUtil.getService(WorkflowRequestOperatePAImpl.class);
            JSONObject requestJSON = new JSONObject();
            requestJSON.put("detailData",JSON.toJSONString(detaiTable));//明细表数据(非必填)
            requestJSON.put("mainData", JSON.toJSONString(mainTable));//主表数据(必填)
            requestJSON.put("otherParams", "");//其他参数，比如messageType,isnextflow,requestSecLevel，delReqFlowFaild(非必填)
            requestJSON.put("remark", "");//签字意见，默认值流程默认意见若未设置则为空(非必填)
            requestJSON.put("requestLevel", "");//紧急程度(非必填)
            requestJSON.put("requestName", "排班审批");//流程标题(必填)
            requestJSON.put("workflowId", wfId);//流程Id(必填)

            PAResponseEntity resultEntity = workflowRequestOperatePA.doCreateRequest(user, WorkFlowUtil.parseRequestEntity(requestJSON));

            boolean isSuccess = resultEntity.getCode() == PAResponseCode.SUCCESS;
            new BaseBean().writeLog("==zj==(创建流程失败信息)" + resultEntity.getErrMsg() + " | code: " + resultEntity.getCode());

            requestid = 0;
            if (isSuccess){
                Object resultData = resultEntity.getData();
                if (resultData != null){
                    JSONObject requestidData =JSONObject.parseObject(JSON.toJSONString(resultData)) ;
                     requestid = requestidData.getInteger("requestid");
                }
            }
        } catch (Exception e) {
            new BaseBean().writeLog("==zj==(创建流程异常)" + JSON.toJSONString(e));
        }

        return requestid;
    }

    /**
     * 提交流程
     * @param user
     * @param requestId
     * @param mainTable
     */
    public static Boolean submitRequest(User user, int requestId, JSONArray mainTable,JSONArray detaiTable) {
        WorkflowRequestOperatePA workflowRequestOperatePA= ServiceUtil.getService(WorkflowRequestOperatePAImpl.class);
        JSONObject requestJSON = new JSONObject();
        requestJSON.put("mainData",mainTable);//主表数据(非必填)
        requestJSON.put("detailData",detaiTable);//明细表数据(非必填)
        requestJSON.put("otherParams", "");//其他参数，比如messageType,isnextflow,requestSecLevel，delReqFlowFaild(非必填)
        requestJSON.put("remark", "");//签字意见，默认值流程默认意见若未设置则为空(非必填)
        requestJSON.put("requestId", requestId);//流程Id(必填)
        PAResponseEntity resultEntity = workflowRequestOperatePA.submitRequest(user, WorkFlowUtil.parseRequestEntity(requestJSON));
        boolean isSuccess = resultEntity.getCode() == PAResponseCode.SUCCESS;
        new BaseBean().writeLog("==zj==(提交流程信息)" + resultEntity.getErrMsg() + " | code: " + resultEntity.getCode());
        return isSuccess;
    }

    /**
     * 设置主表数据
     * @param mainParam
     * @return
     */
    public JSONArray setMainTable(Map<String, String> mainParam,Map<String,String> otherparam){
        JSONArray mainTable = new JSONArray();
        try{
            //提交人员信息
            String operaterId = Util.null2String(mainParam.get("operaterId"));    //提交人员id
            String operaterSubCompany = Util.null2String(mainParam.get("operaterSubCompany"));    //提交人员分部
            String operaterDepartment = Util.null2String(mainParam.get("operaterDepartment"));         //提交人员部门
            String operaterJobTitle = Util.null2String(mainParam.get("operaterJobTitle"));    //提交人员岗位
            String type = Util.null2String(mainParam.get("type"));    //默认排班
            String scheduleResourceIds = Util.null2String(mainParam.get("scheduleResourceIds"));    //被排班人员
            String kqMonth = Util.null2String(mainParam.get("kqMonth"));    //排班月份
            String kqGroupId =  Util.null2String(mainParam.get("kqGroupId"));//考勤组
            //把主表数据设置一下
                    //创建人
                    JSONObject mainTablaDatas = new JSONObject();
                    mainTablaDatas.put("fieldName","cjr");
                    mainTablaDatas.put("fieldValue",operaterId);
                    mainTable.add(mainTablaDatas);
                    //创建人分部
                     mainTablaDatas = new JSONObject();
                     mainTablaDatas.put("fieldName","fb");
                     mainTablaDatas.put("fieldValue",operaterSubCompany);
                     mainTable.add(mainTablaDatas);
                    //创建人部门
                    mainTablaDatas = new JSONObject();
                    mainTablaDatas.put("fieldName","cjrbm");
                    mainTablaDatas.put("fieldValue",operaterDepartment);
                    mainTable.add(mainTablaDatas);
                    //创建人岗位
                    mainTablaDatas = new JSONObject();
                    mainTablaDatas.put("fieldName","cjrgw");
                    mainTablaDatas.put("fieldValue",operaterJobTitle);
                    mainTable.add(mainTablaDatas);
                    //申请类型，默认为排班
                    mainTablaDatas = new JSONObject();
                    mainTablaDatas.put("fieldName","sqlx");
                    mainTablaDatas.put("fieldValue",type);
                    mainTable.add(mainTablaDatas);
                    //排班月份
                    mainTablaDatas = new JSONObject();
                    mainTablaDatas.put("fieldName","pbyf");
                    mainTablaDatas.put("fieldValue",kqMonth);
                    mainTable.add(mainTablaDatas);
                    //被排班人员
                    mainTablaDatas = new JSONObject();
                    mainTablaDatas.put("fieldName","pbmd");
                    mainTablaDatas.put("fieldValue",scheduleResourceIds);
                    mainTable.add(mainTablaDatas);
                    //考勤组
                    mainTablaDatas = new JSONObject();
                    mainTablaDatas.put("fieldName","kqz");
                    mainTablaDatas.put("fieldValue",kqGroupId);
                    mainTable.add(mainTablaDatas);

        }catch (Exception e){
            kqLog.info("==zj==(设置主表数据报错)" + e);
        }
        return  mainTable;
    }

    /**
     * 设置明细表数据
     * @param detailParam
     * @return
     */
    public JSONArray setDetailTable(Map<String,List<List<Object>>> detailParam,Map<String,String> otherparam){
        JSONArray detailTable = new JSONArray();
        try {
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            int index = 0;
            JSONArray workflowRequestTableRecords = new JSONArray();
            JSONArray workflowRequestTableRecord = new JSONArray();
            JSONObject detailTableDatas = new JSONObject();
            //这里直接循环遍历每个人员
            Set keySet =detailParam.keySet(); //获取到所有值
            for(Object key : keySet ){
                //设置明细表数据
                String resourceid = Util.null2String(key);
                String subCompanyID = Util.null2String(resourceComInfo.getSubCompanyID(resourceid));
                String departmentID = Util.null2String(resourceComInfo.getDepartmentID(resourceid));
                String jobTitle = Util.null2String(resourceComInfo.getJobTitle(resourceid));//岗位id
                JSONObject workflowRequestTableRecordsData = new JSONObject();
                //2024-08-28 先改成固定值看看
                workflowRequestTableRecordsData.put("recordOrder",0);
                JSONArray workflowRequestTableFields = new JSONArray();
                List<List<Object>> Datas = detailParam.get(key);
                if (Datas.size() <= 0){
                    //人员
                    JSONObject workflowRequestTableFieldData = new JSONObject();
                    workflowRequestTableFieldData.put("fieldName","yg");
                    workflowRequestTableFieldData.put("fieldValue",resourceid);
                    workflowRequestTableFields.add(workflowRequestTableFieldData);

                    //部门
                    workflowRequestTableFieldData = new JSONObject();
                    workflowRequestTableFieldData.put("fieldName","szbm");
                    workflowRequestTableFieldData.put("fieldValue",departmentID);
                    workflowRequestTableFields.add(workflowRequestTableFieldData);

                    //岗位
                    workflowRequestTableFieldData = new JSONObject();
                    workflowRequestTableFieldData.put("fieldName","gw");
                    workflowRequestTableFieldData.put("fieldValue",jobTitle);
                    workflowRequestTableFields.add(workflowRequestTableFieldData);

                }else {
                    for (int i = 0; i < Datas.size(); i++) {
                        List<Object> detailData = Datas.get(i);
                        /*String resourceid =Util.null2String(detailData.get(0));//人员id*/
                   /* String subCompanyID = Util.null2String(detailData.get(1));//分部id
                    String departmentID = Util.null2String(detailData.get(2));//部门id*/
                        /*String jobTitle = Util.null2String(detailData.get(3));//岗位id*/
                        String kqdate = Util.null2String(detailData.get(4));//排班日期
                        String serialId = Util.null2String(detailData.get(5));//排班id
                        if ( i == 0){
                            //刚开始先设置下排班人员基本信息
                            //人员
                            JSONObject workflowRequestTableFieldData = new JSONObject();
                            workflowRequestTableFieldData.put("fieldName","yg");
                            workflowRequestTableFieldData.put("fieldValue",resourceid);
                            workflowRequestTableFields.add(workflowRequestTableFieldData);

                            //部门
                            workflowRequestTableFieldData = new JSONObject();
                            workflowRequestTableFieldData.put("fieldName","szbm");
                            workflowRequestTableFieldData.put("fieldValue",departmentID);
                            workflowRequestTableFields.add(workflowRequestTableFieldData);

                            //岗位
                            workflowRequestTableFieldData = new JSONObject();
                            workflowRequestTableFieldData.put("fieldName","gw");
                            workflowRequestTableFieldData.put("fieldValue",jobTitle);
                            workflowRequestTableFields.add(workflowRequestTableFieldData);
                        }
                        //设置该员工当天班次
                        if (kqdate.length() > 0){
                            String[] split = kqdate.split("-");
                            int num = Integer.parseInt(split[2]);

                            JSONObject workflowRequestTableFieldData = new JSONObject();
                            workflowRequestTableFieldData.put("fieldName","bc"+num);
                            workflowRequestTableFieldData.put("fieldValue",serialId);
                            workflowRequestTableFields.add(workflowRequestTableFieldData);
                        }
                    }
                }

                index +=1;
                workflowRequestTableRecordsData.put("workflowRequestTableFields",workflowRequestTableFields);
                workflowRequestTableRecords.add(workflowRequestTableRecordsData);

            }
            //设置明细表名称
            detailTableDatas.put("tableDBName",tableDetailName);
            //设置明细表数据
            detailTableDatas.put("workflowRequestTableRecords",workflowRequestTableRecords);
            //是否删除所有明细
            if ("1".equals(otherparam.get("deleteAll"))){
                detailTableDatas.put("deleteAll","1");
            }
            detailTable.add(detailTableDatas);
        } catch (Exception e) {
            kqLog.info("==zj==(设置明细表数据报错)" + e);
        }
        return detailTable;
    }
    }
