package weaver.interfaces.htsc.payment;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.cus.HTUtil;
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: 通用付款流程
 */
public class PaymentLinkdFlowAction extends BaseBean implements Action {
    private Log log = LogFactory.getLog(this.getClass());

    public String getDetail() {
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    private  String detail;

    @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 = JAVATools.getMaintable(requestid);//主表
            String tabledetail = tableName + "_dt"+detail;//明细表
            Map<String, Object> map = new HashMap<>(linked2Invoice(workcode, requestid, tabledetail));
            if(!map.isEmpty()){
                requestInfo.getRequestManager().setMessageid("11111111");
                requestInfo.getRequestManager().setMessagecontent("关联明细表发票失败，请联系管理员处理！"+map.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();
            JAVATools.SetCmdwritelog("付款的主表sql：",sql);
            rs.execute(sql);
            rs.next();
            int mainid = Util.getIntValue(rs.getString(1));//主表id
            String lcbh = Util.null2String(rs.getString("lcbh"));//流程编号
            sql = "select fphm,fpdm,fplx from " + tableDtName + " where mainid = " + mainid;
            JAVATools.SetCmdwritelog("付款的明细sql：",sql);
            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("fplx"));//发票类型编码
                obj.put("InvoiceCode",fpdm);
                obj.put("InvoiceNum",fphm);
                obj.put("InvoiceType",fplxbm);
                if(StringUtils.isNotEmpty(fphm)&&StringUtils.isNotEmpty(fpdm)) {//仅当发票号码发票代码不为空，才调用浪潮绑定，否则浪潮报错
                    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;
    }
}