package weaver.interfaces.htsc.cus;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * Author:DTY-PC
 * Date:2020/11/6 9:55
 * Description: 联合差旅报销流程在创建节点提交后，需要手动调用接口关联发票信息，
 * 只有是发票的需要手动，非发票的在上传验真后会自动关联
 * Copyright:上海泛微网络科技股份有限公司
 */
public class ConnectInvAndBill4SubAction extends BaseBean implements Action {
    private Log log = LogFactory.getLog(this.getClass());
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = Util.null2String(requestInfo.getRequestid());
        log.info("关联发票信息------START---->"+requestid);
        try {
            HTUtil ht = new HTUtil();
            int uid = requestInfo.getRequestManager().getUser().getUID();
            ResourceComInfo rc = new ResourceComInfo();
            String workcode = rc.getWorkcode(uid+"");
            String tableName = ht.getTableNameByRequestid(requestid,"0");//主表
            String tableDt4Name = tableName + "_dt4";//明细4
            String tableDt5Name = tableName + "_dt5";//明细5
            String tableDt6Name = tableName + "_dt6";//明细6
            Map<String, Object> map4 = new HashMap<>(linked2Invoice(workcode, requestid, tableDt4Name));
            if(!map4.isEmpty()){
                requestInfo.getRequestManager().setMessageid("11111111");
                requestInfo.getRequestManager().setMessagecontent("关联明细4发票失败，请联系管理员处理！"+map4.get("msg"));
                return Action.FAILURE_AND_CONTINUE;
            }

            Map<String, Object> map5 = new HashMap<>(linked2Invoice(workcode, requestid, tableDt5Name));
            if(!map5.isEmpty()){
                requestInfo.getRequestManager().setMessageid("11111111");
                requestInfo.getRequestManager().setMessagecontent("关联明细5发票失败，请联系管理员处理！"+map5.get("msg"));
                return Action.FAILURE_AND_CONTINUE;
            }

            Map<String, Object> map6 = new HashMap<>(linked2Invoice(workcode, requestid, tableDt6Name));
            if(!map6.isEmpty()){
                requestInfo.getRequestManager().setMessageid("11111111");
                requestInfo.getRequestManager().setMessagecontent("关联明细6发票失败，请联系管理员处理！"+map6.get("msg"));
                return Action.FAILURE_AND_CONTINUE;
            }

        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("11111111");
            requestInfo.getRequestManager().setMessagecontent("关联发票接口捕获异常，请联系管理员处理！");
            return Action.FAILURE_AND_CONTINUE;
        }
        log.info("关联发票信息------END---->"+requestid);
        return Action.SUCCESS;
    }

    /**
     * 关联发票
     * @param workcode 工号
     * @param requestid 请求ID
     * @param tableDtName 明细表名
     * @return 集合  不为空表示出错了，为空则正确的
     */
    private Map<String,Object> linked2Invoice(String workcode,String requestid,String tableDtName){
        Map<String,Object> resMap = new HashMap<>();
        try{
            HTUtil ht = new HTUtil();
            String tableName = Util.null2String(ht.getTableNameByRequestid(requestid,"0"));
            String sql = "select id,lcbh from " + tableName + " where requestid = " + requestid;
            RecordSet rs = new RecordSet();
            rs.execute(sql);
            rs.next();
            int mainid = Util.getIntValue(rs.getString(1));//主表id
            String lcbh = Util.null2String(rs.getString("lcbh"));//流程编号
            sql = "select fphm,fpdm,fplxid from " + tableDtName + " where mainid = " + mainid;
            rs.execute(sql);
            JSONArray InvoiceInfoArr = new JSONArray();//发票信息集合
            while(rs.next()){
                JSONObject obj = new JSONObject();
                String fphm = Util.null2String(rs.getString("fphm"));//发票号码
                String fpdm = Util.null2String(rs.getString("fpdm"));//发票代码
                String fplxbm = Util.null2String(rs.getString("fplxid"));//发票类型编码
                obj.put("InvoiceCode",fpdm);
                obj.put("InvoiceNum",fphm);
                obj.put("InvoiceType",fplxbm);
                InvoiceInfoArr.add(obj);
            }
            if(InvoiceInfoArr.size() > 0){//只有当有发票的时候才会去调用关联接口
                Map<String,Object> params = new HashMap<>();
                params.put("UserCode",workcode);
                params.put("BillID",lcbh);
                params.put("InvoiceInfo",InvoiceInfoArr);
                String res = ht.connectInvAndBill(params);
                JSONObject returnObj = JSONObject.parseObject(res);
                String code = Util.null2String(returnObj.get("ReCode"));// 1:成功
                String msg = Util.null2String(returnObj.get("ReMsg"));// 返回信息
                if(!"1".equals(code)){
                    resMap.put("isSucc",false);
                    resMap.put("msg",msg);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            log.info(tableDtName+"---单独关联发票捕获异常："+e.getMessage());
            resMap.put("isSucc",false);
            resMap.put("msg",e.getMessage());
        }
        return resMap;
    }
}
