package com.engine.htsc.tripInfo.cmd;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.cus.HTUtil;

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

/**
 * Author:DTY-PC
 * Date:2020/10/29 10:59
 * Description:
 * Copyright:上海泛微网络科技股份有限公司
 */
public class TripInfoCmd extends AbstractCommonCommand<Map<String,Object>> {
    private Log log = LogFactory.getLog(this.getClass());
    public TripInfoCmd(User user,Map<String,Object> params) {
        this.user = user;
        this.params = params;
    }


    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String,Object> apidatas  = new HashMap<>();
        try {
            HTUtil ht = new HTUtil();
            DepartmentComInfo dc = new DepartmentComInfo();
            String reqids = Util.null2String(params.get("reqids"));//请求id的拼接字符串
            List<String> reqList = Util.splitString2List(reqids,",");
            //联合出差流程的主表名
            String tableName = Util.null2String(ht.getTableNameByRequestid(reqList.get(0),"0"));
            String tableDt2Name = tableName+"_dt2";//明细2
            String tableDt3Name = tableName+"_dt3";//明细3
            String tableDt4Name = tableName+"_dt4";//数据取值 明细4
            String sql = "select m.szjg,m.sfhwcc,m.cclx,m.cclxzxx,d.xm,d.bm,d.fycdbm,m.xmjc,m.custname,m.xmjc from "+tableName+" m,"
                    +tableDt2Name+" d where m.id = d.mainid and m.requestid = ?";
            String sql_dt4 = "select h.lastname,d3.cfsj,'9:00' begintime,d3.fhsj,'18:00' endtime,d3.cfdgn,d3.mddgn,d3.cfdhw,d3.mddhw,d2.xm \n" +
            " from "+tableName+" m\n" +
                    " left join "+tableDt2Name+" d2\n" +
                    " on m.id = d2.mainid\n" +
                    " left join hrmresource h\n" +
                    " on d2.xm = h.id\n" +
                    " left join "+tableDt4Name+" d3\n" +
                    " on m.id = d3.mainid\n" +
                    " where d3.CFSJ IS NOT NULL AND m.requestid = ?";
            RecordSet rs = new RecordSet();
            RecordSet rs_dt3 = new RecordSet();
            ResourceComInfo rc = new ResourceComInfo();
            JSONArray arrayFee = new JSONArray();//差旅费用
            JSONArray arrayTrip = new JSONArray();//行程信息
            Map<String,String> mutilHrm = new HashMap<>();//提供给主表的多人的【报销人员】
            for(String reqid : reqList){
                rs.executeQuery(sql,new Object[]{reqid});
                while(rs.next()){
                    JSONObject obj = new JSONObject();
                    String szjg = Util.null2String(rs.getString("szjg"));//所在机构
                    String sfhwcc = Util.null2String(rs.getString("sfhwcc"));//是否海外出差
                    String cclx = Util.null2String(rs.getString("cclx"));//出差类型
                    String cclxzxx = Util.null2String(rs.getString("cclxzxx"));//出差类型子选项
                    String hrmid = Util.null2String(rs.getString("xm"));//出差人员ID
                    String bm = Util.null2String(rs.getString("bm"));//部门
                    String fycdbm = Util.null2String(rs.getString("fycdbm"));//费用承担部门
                    String lastname = Util.null2String(rc.getLastname(hrmid));//出差人员姓名
                    String thxmbm = Util.null2String(rs.getString("xmjc"));//投行项目编码
                    String custname = Util.null2String(rs.getString("custname"));//CRM客户
                    String xmjc = Util.null2String(rs.getString("xmjc"));//投行项目

                    obj.put("sfhwcc",sfhwcc);
                    obj.put("cclx",cclx);
                    obj.put("cclxzxx",cclxzxx);
                    obj.put("hrmid",hrmid);
                    obj.put("lastname",lastname);
                    obj.put("reqid",reqid);
                    obj.put("bm",bm);
                    obj.put("bmname",dc.getDepartmentName(bm));
                    obj.put("fycdbm",fycdbm);
                    obj.put("fycdbmname",dc.getDepartmentName(fycdbm));
                    obj.put("szjg",szjg);
                    obj.put("szjgname",dc.getDepartmentName(szjg));
                    obj.put("thxmbm",thxmbm);
                    obj.put("cust",custname);
                    obj.put("custname",dc.getDepartmentName(custname));
                    obj.put("xmjc",xmjc);
                    obj.put("xmjcname",dc.getDepartmentName(xmjc));
                    arrayFee.add(obj);
                }
                rs_dt3.executeQuery(sql_dt4,new Object[]{reqid});

                while(rs_dt3.next()){
                    JSONObject obj = new JSONObject();
                    String lastname = Util.null2String(rs_dt3.getString("lastname"));
                    String cfsj = Util.null2String(rs_dt3.getString("cfsj"));
                    String begintime = Util.null2String(rs_dt3.getString("begintime"));
                    String fhsj = Util.null2String(rs_dt3.getString("fhsj"));
                    String endtime = Util.null2String(rs_dt3.getString("endtime"));
                    String cfdgn = Util.null2String(rs_dt3.getString("cfdgn"));
                    String mddgn = Util.null2String(rs_dt3.getString("mddgn"));
                    String cfdhw = Util.null2String(rs_dt3.getString("cfdhw"));
                    String mddhw = Util.null2String(rs_dt3.getString("mddhw"));
                    String hrmid = Util.null2String(rs_dt3.getString("xm"));
                    obj.put("lastname",lastname);
                    obj.put("hrmid",hrmid);
                    mutilHrm.put(hrmid,lastname);
                    obj.put("cfsj",cfsj);
                    obj.put("begintime",begintime);
                    obj.put("fhsj",fhsj);
                    obj.put("endtime",endtime);
                    obj.put("cfdgn",cfdgn);
                    obj.put("mddgn",mddgn);
                    obj.put("cfdhw",cfdhw);
                    obj.put("mddhw",mddhw);
                    obj.put("reqid",reqid);
                    arrayTrip.add(obj);
                }

                if (CollectionUtils.isEmpty(arrayTrip)) {
                  //明细4里没有数据,取明细3; 适配历史数据
                  String sql_dt3 = "select h.lastname,d3.cfsj,'9:00' begintime,d3.fhsj,'18:00' endtime,d3.cfdgn,d3.mddgn,d3.cfdhw,d3.mddhw,d2.xm \n" +
                          " from "+tableName+" m\n" +
                          " left join "+tableDt2Name+" d2\n" +
                          " on m.id = d2.mainid\n" +
                          " left join hrmresource h\n" +
                          " on d2.xm = h.id\n" +
                          " left join "+tableDt3Name+" d3\n" +
                          " on m.id = d3.mainid\n" +
                          " where m.requestid = ?";
                  rs_dt3.executeQuery(sql_dt3,new Object[]{reqid});

                  while(rs_dt3.next()){
                    JSONObject obj = new JSONObject();
                    String lastname = Util.null2String(rs_dt3.getString("lastname"));
                    String cfsj = Util.null2String(rs_dt3.getString("cfsj"));
                    String begintime = Util.null2String(rs_dt3.getString("begintime"));
                    String fhsj = Util.null2String(rs_dt3.getString("fhsj"));
                    String endtime = Util.null2String(rs_dt3.getString("endtime"));
                    String cfdgn = Util.null2String(rs_dt3.getString("cfdgn"));
                    String mddgn = Util.null2String(rs_dt3.getString("mddgn"));
                    String cfdhw = Util.null2String(rs_dt3.getString("cfdhw"));
                    String mddhw = Util.null2String(rs_dt3.getString("mddhw"));
                    String hrmid = Util.null2String(rs_dt3.getString("xm"));
                    obj.put("lastname",lastname);
                    obj.put("hrmid",hrmid);
                    mutilHrm.put(hrmid,lastname);
                    obj.put("cfsj",cfsj);
                    obj.put("begintime",begintime);
                    obj.put("fhsj",fhsj);
                    obj.put("endtime",endtime);
                    obj.put("cfdgn",cfdgn);
                    obj.put("mddgn",mddgn);
                    obj.put("cfdhw",cfdhw);
                    obj.put("mddhw",mddhw);
                    obj.put("reqid",reqid);
                    arrayTrip.add(obj);
                  }
                }
            }
            JSONObject mutiObj = new JSONObject();
            JSONArray specArr = new JSONArray();
            List<String> keys = new ArrayList<>();
            for(Map.Entry<String, String> entry : mutilHrm.entrySet()){
                keys.add(entry.getKey());
                JSONObject specObj = new JSONObject();
                specObj.put("id",entry.getKey());
                specObj.put("name",entry.getValue());
                specArr.add(specObj);
            }
            mutiObj.put("value",StringUtils.join(keys.toArray(),","));
            mutiObj.put("specialobj",specArr);
            JSONObject info = new JSONObject();

            info.put("travel",arrayFee);
            info.put("mutiObj",mutiObj);
            info.put("trip",arrayTrip);
            apidatas.put("api_status",true);
            apidatas.put("info",info);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("api_status",false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return apidatas;
    }



}
