package weaver.interfaces.htsc.cus;

import org.apache.commons.collections4.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

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

/**
 * Author:DTY-PC
 * Date:2020/10/20 14:34
 * Description: 流程提交审批数据至携程ACtion
 * Copyright:上海泛微网络科技股份有限公司
 */
public class SubData2TripAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = Util.null2String(requestInfo.getRequestid());//请求ID
        writeLog("SubData2TripAction---->START"+requestid);
        HTUtil htUtil = new HTUtil();
        try {
            String tableName = Util.null2String(requestInfo.getRequestManager().getBillTableName());//主表名
            String tableDt2Name = tableName+"_dt2";//明细2表名
            String tableDt3Name = tableName+"_dt3";//明细3表名
            String tableDt4Name = tableName+"_dt4";//明细3表名
            String sql = "select id,lcbh,sfhwcc,ngr,szbm from " + tableName + " where requestid = " + requestid;
            RecordSet rs = new RecordSet();
            if(rs.execute(sql) && rs.next()){
                int mainid = Util.getIntValue(rs.getInt("id"));//主表ID
                String lcbh = Util.null2String(rs.getString("lcbh"));//流程编号
                String sfhwcc = Util.null2String(rs.getString("sfhwcc"));//是否海外出差，海外出差不调用接口
                String ngr = Util.null2String(rs.getString("ngr"));//拟稿人
//                String szbm = Util.null2String(rs.getString("szbm"));//部门ID

                ResourceComInfo rc = new ResourceComInfo();
                DepartmentComInfo dc = new DepartmentComInfo();
                String departmentCode = dc.getDepartmentCode(rc.getDepartmentID(ngr));//拟稿人部门编码
                if(sfhwcc.equals("1")){
                    boolean isReturn = false;//单程或往返  默认false
                    String cityType = "MAINLAND";//
                    String currency = "RMB";//币种
                    String status = "1";
                    RecordSet rs_mx_2 = new RecordSet();
                    sql = "select ccrgh,xm from " + tableDt2Name + " where mainid = " + mainid;
                    String employeeIdList = "";//出差人工号字符串，逗号相隔
                    rs_mx_2.execute(sql);
                    List<String> hrmList = new ArrayList<>();//出差人集合，用于处理职级
                    while(rs_mx_2.next()){
                        if(employeeIdList.equals("")){
                            employeeIdList = Util.null2String(rs_mx_2.getString("ccrgh"));
                        }else{
                            employeeIdList += "," + Util.null2String(rs_mx_2.getString("ccrgh"));
                        }
                        String xm = Util.null2String(rs_mx_2.getString("xm"));//出差人
                        hrmList.add(xm);
                    }
                    RecordSet rs_mx_3 = new RecordSet();
                    sql = "select cfsj,fhsj,cfdgn,mddgn from " + tableDt4Name + " where mainid = " + mainid;
                    rs_mx_3.execute(sql);
                    String cfdString = "";
                    String mddString = "";
                    List<String> cfsjList = new ArrayList<>();//出发时间集合
                    List<String> fhsjList = new ArrayList<>();//返回时间集合
                    while(rs_mx_3.next()){
                        if(cfdString.equals("")){
                            cfdString = Util.null2String(rs_mx_3.getString("cfdgn"));//国内出发地
                        }else{
                            cfdString += "," + Util.null2String(rs_mx_3.getString("cfdgn"));//国内出发地
                        }
                        if(mddString.equals("")){
                            mddString = Util.null2String(rs_mx_3.getString("mddgn"));//国内目的地
                        }else{
                            mddString += "," + Util.null2String(rs_mx_3.getString("mddgn"));//国内目的地
                        }
                        cfsjList.add(Util.null2String(rs_mx_3.getString("cfsj")));//出发时间
                        fhsjList.add(Util.null2String(rs_mx_3.getString("fhsj")));//返回时间
                    }
                    //明细4无数据,取明细表3
                    if (CollectionUtils.isEmpty(cfsjList)) {
                      sql = "select cfsj,fhsj,cfdgn,mddgn from " + tableDt3Name + " where mainid = " + mainid;
                      rs_mx_3.execute(sql);
                      while(rs_mx_3.next()){
                        if(cfdString.equals("")){
                          cfdString = Util.null2String(rs_mx_3.getString("cfdgn"));//国内出发地
                        }else{
                          cfdString += "," + Util.null2String(rs_mx_3.getString("cfdgn"));//国内出发地
                        }
                        if(mddString.equals("")){
                          mddString = Util.null2String(rs_mx_3.getString("mddgn"));//国内目的地
                        }else{
                          mddString += "," + Util.null2String(rs_mx_3.getString("mddgn"));//国内目的地
                        }
                        cfsjList.add(Util.null2String(rs_mx_3.getString("cfsj")));//出发时间
                        fhsjList.add(Util.null2String(rs_mx_3.getString("fhsj")));//返回时间
                      }
                    }

                    String minCFSJ = htUtil.sortDateList(cfsjList, false);
                    String maxFHSJ = htUtil.sortDateList(fhsjList, true);
                    writeLog("SubData2TripAction返回时间" + minCFSJ + "===" + maxFHSJ);
                    Map<String,Object> params = new HashMap<>();
                    if ("".equals(lcbh)) {
                        return Action.SUCCESS;
                    }
                    params.put("journeyId",lcbh);
                    params.put("employeeIdList",Util.splitString2List(employeeIdList,","));
                    params.put("status",status);
                    String expiredTime = htUtil.getPreOrLaterDay(maxFHSJ,1);//最大返回时间加一天
                    params.put("expiredTime",expiredTime+"T00:00:00.000Z");
                    List<String> rankList = htUtil.getLevList(hrmList);//职级集合
                    params.put("departCityCodes",Util.splitString2List(cfdString,","));//出发地
                    params.put("arrivalCityCodes",Util.splitString2List(mddString,","));//目的地
                    params.put("departDate",minCFSJ+"T09:00:00.000Z");//最小的出发时间
                    params.put("returnDate",maxFHSJ+"T17:00:00.000Z");//最大的返回时间
                    params.put("deptCode",departmentCode);//拟稿人的部门编码
                    params.put("isReturn",isReturn);//
                    params.put("cityType",cityType);//
                    params.put("currency",currency);//
                    writeLog("SubData2TripAction===保存行程接口参数===params===" + params);
                    for(String rank : rankList){
                        params.put("rankName",rank);//
                        boolean isSucc = htUtil.SubData2Trip(params);//调用接口
                        if(!isSucc){
                            requestInfo.getRequestManager().setMessageid("1111111");
                            requestInfo.getRequestManager().setMessagecontent("【提交预审批数据至携程】接口出错，请联系管理员！");
                            return Action.FAILURE_AND_CONTINUE;
                        }
                    }
                }

            }else{
                requestInfo.getRequestManager().setMessageid("1111111");
                requestInfo.getRequestManager().setMessagecontent("【提交预审批数据至携程】接口出错，请联系管理员！");
                return Action.FAILURE_AND_CONTINUE;
            }
        }catch (Exception e){
            e.printStackTrace();
            writeLog("【提交预审批数据至携程】接口捕获异常:requestid->"+requestid+e.getMessage());
            requestInfo.getRequestManager().setMessageid("1111111");
            requestInfo.getRequestManager().setMessagecontent("【提交预审批数据至携程】接口捕获异常，请联系管理员！");
            return Action.FAILURE_AND_CONTINUE;
        }
        writeLog("SubData2TripAction---->END"+requestid);
        return Action.SUCCESS;
    }
}
